Author: byrne Date: Thu Apr 5 22:33:50 2007 New Revision: 526062 URL: http://svn.apache.org/viewvc?view=rev&rev=526062 Log: Changed HashCode in both Key.java and Value.java to be modeled after the String.hashCode().
Added test cases for both methods in their respective test classes. build.xml's clean target now deletes the temporary directory "tests" and other created when Xindice's junit tests get ran. Fixed up FilterTestBase to not throw NPE's when tests fail prematurely. Todd Byrne Modified: xml/xindice/trunk/build.properties xml/xindice/trunk/build.xml xml/xindice/trunk/java/src/org/apache/xindice/core/data/Key.java xml/xindice/trunk/java/src/org/apache/xindice/core/data/Value.java xml/xindice/trunk/java/tests/src/org/apache/xindice/core/data/KeyTest.java xml/xindice/trunk/java/tests/src/org/apache/xindice/core/data/ValueTest.java xml/xindice/trunk/java/tests/src/org/apache/xindice/core/filer/FilerTestBase.java Modified: xml/xindice/trunk/build.properties URL: http://svn.apache.org/viewvc/xml/xindice/trunk/build.properties?view=diff&rev=526062&r1=526061&r2=526062 ============================================================================== --- xml/xindice/trunk/build.properties (original) +++ xml/xindice/trunk/build.properties Thu Apr 5 22:33:50 2007 @@ -113,6 +113,7 @@ test.build.dir=${build.dir}/classes-tests test.result.dir=${build.dir}/test-results test.report.dir=${build.dir}/test-report +test.temp.dir=tests api.dir=${build.dir}/api eclipse.temp=${build.dir}/temp release.dir=${build.dir}/dist Modified: xml/xindice/trunk/build.xml URL: http://svn.apache.org/viewvc/xml/xindice/trunk/build.xml?view=diff&rev=526062&r1=526061&r2=526062 ============================================================================== --- xml/xindice/trunk/build.xml (original) +++ xml/xindice/trunk/build.xml Thu Apr 5 22:33:50 2007 @@ -99,7 +99,7 @@ <target name="build" depends="compile, jar, war" description="* Builds Xindice jar and war files in the current directory"> </target> - <target name="clean" depends="jar-clean, war-clean" description="Cleans out all generates files"> + <target name="clean" depends="jar-clean, war-clean, test-clean" description="Cleans out all generates files"> <delete dir="${build.dir}"/> </target> @@ -256,6 +256,7 @@ </target> <target name="test-clean"> <delete dir="${test.build.dir}"/> + <delete dir="${test.temp.dir}"/> </target> Modified: xml/xindice/trunk/java/src/org/apache/xindice/core/data/Key.java URL: http://svn.apache.org/viewvc/xml/xindice/trunk/java/src/org/apache/xindice/core/data/Key.java?view=diff&rev=526062&r1=526061&r2=526062 ============================================================================== --- xml/xindice/trunk/java/src/org/apache/xindice/core/data/Key.java (original) +++ xml/xindice/trunk/java/src/org/apache/xindice/core/data/Key.java Thu Apr 5 22:33:50 2007 @@ -19,6 +19,7 @@ package org.apache.xindice.core.data; + /** * Key extends Value by providing a hash value for the Key. * @@ -46,15 +47,13 @@ } public int getHash() { - int hash = this.hash; + // modeled after String.hashCode() if (hash == 0) { - // TODO: This has to be revisited - int end = pos + len; - for (int i = pos; i < end; i++) { - hash = (hash << 5) ^ data[i]; - hash = hash % 1234567891; + int tempHash = 0; + for(int i = 0 ; i < len; i++) { + tempHash = 31 * tempHash + data[pos + i]; } - this.hash = hash = Math.abs(hash); + this.hash = Math.abs(tempHash); } return hash; } Modified: xml/xindice/trunk/java/src/org/apache/xindice/core/data/Value.java URL: http://svn.apache.org/viewvc/xml/xindice/trunk/java/src/org/apache/xindice/core/data/Value.java?view=diff&rev=526062&r1=526061&r2=526062 ============================================================================== --- xml/xindice/trunk/java/src/org/apache/xindice/core/data/Value.java (original) +++ xml/xindice/trunk/java/src/org/apache/xindice/core/data/Value.java Thu Apr 5 22:33:50 2007 @@ -39,7 +39,7 @@ protected byte[] data; protected int pos; protected int len; - + private int hash; public Value(Value value) { this.data = value.data; @@ -229,8 +229,15 @@ } public int hashCode() { - // TODO Ain't best way to go about it, but can't change on a whim either - return toString().hashCode(); + // modeled after String.hashCode() + if(hash == 0) { + int tempHash = 0; + for(int i = 0 ; i < len; i++) { + tempHash = 31 * tempHash + data[pos + i]; + } + this.hash = Math.abs(tempHash); + } + return hash; } public final String toString() { Modified: xml/xindice/trunk/java/tests/src/org/apache/xindice/core/data/KeyTest.java URL: http://svn.apache.org/viewvc/xml/xindice/trunk/java/tests/src/org/apache/xindice/core/data/KeyTest.java?view=diff&rev=526062&r1=526061&r2=526062 ============================================================================== --- xml/xindice/trunk/java/tests/src/org/apache/xindice/core/data/KeyTest.java (original) +++ xml/xindice/trunk/java/tests/src/org/apache/xindice/core/data/KeyTest.java Thu Apr 5 22:33:50 2007 @@ -31,4 +31,8 @@ assertTrue(new Key("toto").equals(new Value("toto"))); assertTrue(new Value("toto").equals(new Key("toto"))); } + public void testHashCode() { + assertTrue(new Key("blahblah").hashCode() == new Key("blahblah").hashCode()); + assertFalse(new Key("blah3").hashCode() == new Key("blah4").hashCode()); + } } Modified: xml/xindice/trunk/java/tests/src/org/apache/xindice/core/data/ValueTest.java URL: http://svn.apache.org/viewvc/xml/xindice/trunk/java/tests/src/org/apache/xindice/core/data/ValueTest.java?view=diff&rev=526062&r1=526061&r2=526062 ============================================================================== --- xml/xindice/trunk/java/tests/src/org/apache/xindice/core/data/ValueTest.java (original) +++ xml/xindice/trunk/java/tests/src/org/apache/xindice/core/data/ValueTest.java Thu Apr 5 22:33:50 2007 @@ -30,4 +30,9 @@ public void testSuccessEquals() { assertEquals(new Value("value"), new Value("value")); } + public void testHashCode() { + assertTrue(new Value("fireworks").hashCode() == new Value("fireworks").hashCode()); + assertFalse(new Value("firecracker").hashCode() == new Value("fireworks").hashCode()); + } + } Modified: xml/xindice/trunk/java/tests/src/org/apache/xindice/core/filer/FilerTestBase.java URL: http://svn.apache.org/viewvc/xml/xindice/trunk/java/tests/src/org/apache/xindice/core/filer/FilerTestBase.java?view=diff&rev=526062&r1=526061&r2=526062 ============================================================================== --- xml/xindice/trunk/java/tests/src/org/apache/xindice/core/filer/FilerTestBase.java (original) +++ xml/xindice/trunk/java/tests/src/org/apache/xindice/core/filer/FilerTestBase.java Thu Apr 5 22:33:50 2007 @@ -57,11 +57,13 @@ protected int LARGE_KEY_SIZE = 8192 + 32; protected Filer filer; - + private String testName; public FilerTestBase(String name, Filer filer) { super(name); this.filer = filer; + // After teardown filer is null and getName still can be called + testName = this.filer.getName() + " " + super.getName(); } public void setUp() throws Exception { @@ -99,7 +101,7 @@ } public String getName() { - return this.filer.getName() + " " + super.getName(); + return testName; } public void testSuccessWriteRecord() throws Exception {