From: <res...@us...> - 2010-09-27 23:50:49
|
Revision: 3645 http://bigdata.svn.sourceforge.net/bigdata/?rev=3645&view=rev Author: resendes Date: 2010-09-27 23:50:43 +0000 (Mon, 27 Sep 2010) Log Message: ----------- Next batch of updates for com.bigdata.util Modified Paths: -------------- branches/bbb_cleanup/bigdata-core/src/main/java/com/bigdata/util/NT.java branches/bbb_cleanup/bigdata-core/src/main/java/com/bigdata/util/NV.java branches/bbb_cleanup/bigdata-core/src/test/java/com/bigdata/util/TestAll.java branches/bbb_cleanup/bigdata-core/src/test/java/com/bigdata/util/TestEntryUtil.java Added Paths: ----------- branches/bbb_cleanup/bigdata-core/src/test/java/com/bigdata/util/TestNT.java branches/bbb_cleanup/bigdata-core/src/test/java/com/bigdata/util/TestNV.java Modified: branches/bbb_cleanup/bigdata-core/src/main/java/com/bigdata/util/NT.java =================================================================== --- branches/bbb_cleanup/bigdata-core/src/main/java/com/bigdata/util/NT.java 2010-09-27 23:38:47 UTC (rev 3644) +++ branches/bbb_cleanup/bigdata-core/src/main/java/com/bigdata/util/NT.java 2010-09-27 23:50:43 UTC (rev 3645) @@ -60,22 +60,18 @@ } + @Override public int hashCode() { return hashCode; } + @Override public boolean equals(Object o) { - return equals((NT) o); + if (!(o instanceof NT)) { - } - - public boolean equals(NT o) { - - if (o == null) { - /* * Note: This handles a case where the other instance was a key in a * WeakHashMap and the reference for the key was cleared. This @@ -85,14 +81,15 @@ return false; } + NT nt = (NT)o; - if (this == o) + if (this == nt) return true; - if (!this.name.equals(o.name)) + if (!this.name.equals(nt.name)) return false; - if (this.timestamp != o.timestamp) + if (this.timestamp != nt.timestamp) return false; return true; Modified: branches/bbb_cleanup/bigdata-core/src/main/java/com/bigdata/util/NV.java =================================================================== --- branches/bbb_cleanup/bigdata-core/src/main/java/com/bigdata/util/NV.java 2010-09-27 23:38:47 UTC (rev 3644) +++ branches/bbb_cleanup/bigdata-core/src/main/java/com/bigdata/util/NV.java 2010-09-27 23:50:43 UTC (rev 3645) @@ -5,6 +5,8 @@ /** * A name-value pair. * + * Note: this class has a natural ordering that is inconsistent with equals. + * * @author <a href="mailto:tho...@us...">Bryan Thompson</a> * @version $Id$ */ @@ -56,16 +58,20 @@ } - public int hashCode() { - - return name.hashCode(); - + @Override + public int hashCode() { + return name.hashCode(); } - public boolean equals(NV o) { - - return name.equals(o.name) && value.equals(o.value); + @Override + public boolean equals(Object o) { + if (!(o instanceof NV)) { + return false; + } + NV nv = (NV)o; + return name.equals(nv.name) && value.equals(nv.value); + } /** @@ -79,34 +85,34 @@ } - /** - * Combines the two arrays, appending the contents of the 2nd array to the - * contents of the first array. - * - * @param a - * @param b - * @return - */ - public static NV[] concat(final NV[] a, final NV[] b) { +// /** +// * Combines the two arrays, appending the contents of the 2nd array to the +// * contents of the first array. +// * +// * @param a +// * @param b +// * @return +// */ +// public static NV[] concat(final NV[] a, final NV[] b) { +// +// if (a == null && b == null) +// return a; +// +// if (a == null) +// return b; +// +// if (b == null) +// return a; +// +// final NV[] c = (NV[]) java.lang.reflect.Array.newInstance(a.getClass() +// .getComponentType(), a.length + b.length); +// +// System.arraycopy(a, 0, c, 0, a.length); +// +// System.arraycopy(b, 0, c, a.length, b.length); +// +// return c; +// +// } - if (a == null && b == null) - return a; - - if (a == null) - return b; - - if (b == null) - return a; - - final NV[] c = (NV[]) java.lang.reflect.Array.newInstance(a.getClass() - .getComponentType(), a.length + b.length); - - System.arraycopy(a, 0, c, 0, a.length); - - System.arraycopy(b, 0, c, a.length, b.length); - - return c; - - } - } Modified: branches/bbb_cleanup/bigdata-core/src/test/java/com/bigdata/util/TestAll.java =================================================================== --- branches/bbb_cleanup/bigdata-core/src/test/java/com/bigdata/util/TestAll.java 2010-09-27 23:38:47 UTC (rev 3644) +++ branches/bbb_cleanup/bigdata-core/src/test/java/com/bigdata/util/TestAll.java 2010-09-27 23:50:43 UTC (rev 3645) @@ -71,6 +71,8 @@ suite.addTestSuite( TestEntryUtil.class ); suite.addTestSuite( TestFormat.class ); suite.addTestSuite( TestHTMLUtility.class ); + suite.addTestSuite( TestNT.class ); + suite.addTestSuite( TestNV.class ); return suite; } Modified: branches/bbb_cleanup/bigdata-core/src/test/java/com/bigdata/util/TestEntryUtil.java =================================================================== --- branches/bbb_cleanup/bigdata-core/src/test/java/com/bigdata/util/TestEntryUtil.java 2010-09-27 23:38:47 UTC (rev 3644) +++ branches/bbb_cleanup/bigdata-core/src/test/java/com/bigdata/util/TestEntryUtil.java 2010-09-27 23:50:43 UTC (rev 3645) @@ -13,7 +13,6 @@ import net.jini.lookup.entry.Comment; import net.jini.lookup.entry.Location; import net.jini.lookup.entry.Name; -import net.jini.lookup.entry.ServiceType; import junit.framework.TestCase; public class TestEntryUtil extends TestCase { @@ -225,66 +224,6 @@ assertNotEquivalentSets(entries2, entries1); } - public static class MyServiceType extends ServiceType { - private static final long serialVersionUID = 1L; - //Only public, non-transient/final/static are used for Entry objs - public String name = null; - public String desc = null; - - public MyServiceType() {} //default cons per spec - - public MyServiceType(String name, String desc) { - super(); - this.name = name; - this.desc = desc; - } - - @Override - public String getDisplayName() { - return name; - } - - @Override - public String getShortDescription() { - return desc; - } - } - - public void testCompareEntrySets_equiv_serviceType() { - Entry[] entries1 = new Entry[] {new MyServiceType("A", "B")}; - Entry[] entries2 = new Entry[] {new MyServiceType("A", "B")}; - assertEquivalentSets(entries1, entries2); - assertEquivalentSets(entries2, entries1); - } - - public void testCompareEntrySets_unequiv_serviceType() { - Entry[] entries1 = new Entry[] {new MyServiceType("A", "B")}; - Entry[] entries2 = new Entry[] {new MyServiceType("A", "C")}; - assertNotEquivalentSets(entries1, entries2); - assertNotEquivalentSets(entries2, entries1); - } - - public void testCompareEntrySets_unequiv_serviceType2() { - Entry[] entries1 = new Entry[] {new MyServiceType("A", "B")}; - Entry[] entries2 = new Entry[] {new MyServiceType("D", "B")}; - assertNotEquivalentSets(entries1, entries2); - assertNotEquivalentSets(entries2, entries1); - } - - public void testCompareEntrySets_unequiv_serviceType_null1() { - Entry[] entries1 = new Entry[] {new MyServiceType("A", "B")}; - Entry[] entries2 = new Entry[] {new MyServiceType(null, "B")}; - assertNotEquivalentSets(entries1, entries2); - assertNotEquivalentSets(entries2, entries1); - } - - public void testCompareEntrySets_unequiv_serviceType_null2() { - Entry[] entries1 = new Entry[] {new MyServiceType("A", "B")}; - Entry[] entries2 = new Entry[] {new MyServiceType("A", null)}; - assertNotEquivalentSets(entries1, entries2); - assertNotEquivalentSets(entries2, entries1); - } - private static class MyEntryWithUnusableFields extends AbstractEntry { private static final long serialVersionUID = 1L; // final, static, excluded public final String finalString = "finalString"; // final, excluded Added: branches/bbb_cleanup/bigdata-core/src/test/java/com/bigdata/util/TestNT.java =================================================================== --- branches/bbb_cleanup/bigdata-core/src/test/java/com/bigdata/util/TestNT.java (rev 0) +++ branches/bbb_cleanup/bigdata-core/src/test/java/com/bigdata/util/TestNT.java 2010-09-27 23:50:43 UTC (rev 3645) @@ -0,0 +1,76 @@ +package com.bigdata.util; + +import com.bigdata.util.CSVReader.Header; + +import junit.framework.TestCase; + +public class TestNT extends TestCase { + + + public void testGetName() { + String name = "ABC"; + NT nt = new NT(name, 1L); + assertEquals(name, nt.getName()); + } + + public void testGetTimestamp() { + long time = 1L; + NT nt = new NT("ABC", time); + assertEquals(time, nt.getTimestamp()); + } + + public void testNT_null() { + try { + NT nt = new NT(null, 1L); + fail("Sucessfully called NT with a null name argument."); + } catch (IllegalArgumentException e) { + // ignore -- expected + } + } + + public void testEqualsObject() { + String name = "abc"; + long time = 1L; + NT h = new NT(name, time); + NT h_dup = new NT(name, time); + NT h_dup2 = new NT(name, time); + NT h_diff = new NT(name + "diff", time); + NT h_diff2 = new NT(name, (time + 1L)); + + // Test reflexive property + assertTrue(h.equals(h)); + + // Test symmetric property + assertTrue(h.equals(h_dup) && h_dup.equals(h)); + + //Test transitive property + assertTrue(h.equals(h_dup) && h_dup.equals(h_dup2) && h.equals(h_dup2)); + + // consistency property already tested + + // Test negative cases + assertFalse(h.equals(null)); + + assertFalse(h.equals(name)); + + assertFalse(h.equals(h_diff)); + assertFalse(h.equals(h_diff2)); + } + + public void test_hashcode() { + String name = "abc"; + long time = 1L; + NT h = new NT(name, time); + NT h_dup = new NT(name, time); + assertTrue(h.hashCode()==h_dup.hashCode()); + } + + public void test_toString() { + String name = "abc"; + long time = 1L; + NT h = new NT(name, time); + NT h_dup = new NT(name, time); + assertTrue(h.toString().equals("NT{name=" + "abc" + ",timestamp=" + 1L + "}")); + } + +} Added: branches/bbb_cleanup/bigdata-core/src/test/java/com/bigdata/util/TestNV.java =================================================================== --- branches/bbb_cleanup/bigdata-core/src/test/java/com/bigdata/util/TestNV.java (rev 0) +++ branches/bbb_cleanup/bigdata-core/src/test/java/com/bigdata/util/TestNV.java 2010-09-27 23:50:43 UTC (rev 3645) @@ -0,0 +1,102 @@ +package com.bigdata.util; + +import junit.framework.TestCase; + +public class TestNV extends TestCase { + + + public void testGetName() { + String name = "ABC"; + NV nt = new NV(name, name); + assertEquals(name, nt.getName()); + } + + public void testGetValue() { + String name = "ABC"; + NV nt = new NV(name, name); + assertEquals(name, nt.getValue()); + } + + public void testNV_null() { + try { + new NV(null, "abc"); + fail("Successfully called NV() with null name argument."); + } catch (IllegalArgumentException e) { + //ignore -- expected + } + } + + public void testToString() { + String name = "Name"; + String value = "Value"; + NV nv = new NV(name, value); + assertTrue((name+"="+value).equals(nv.toString())); + } + + public void testEqualsNV() { + + String name = "abc"; + String value = "xyz"; + NV h = new NV(name, value); + NV h_dup = new NV(name, value); + NV h_dup2 = new NV(name, value); + NV h_diff = new NV(name + "diff", value); + NV h_diff2 = new NV(name, (value + "diff")); + + // Test reflexive property + assertTrue(h.equals(h)); + + // Test symmetric property + assertTrue(h.equals(h_dup) && h_dup.equals(h)); + + //Test transitive property + assertTrue(h.equals(h_dup) && h_dup.equals(h_dup2) && h.equals(h_dup2)); + + // consistency property already tested + + // Test negative cases + assertFalse(h.equals(null)); + + assertFalse(h.equals(name)); + + assertFalse(h.equals(h_diff)); + assertFalse(h.equals(h_diff2)); + } + + public void testHashCode() { + String name = "abc"; + String value = "xyz"; + NV h = new NV(name, value); + NV h_dup = new NV(name, value); + assertTrue(h.hashCode()==h_dup.hashCode()); + } + + public void testCompareTo() { + String name = "bcd"; + String value = "xyz"; + String lessThanName = "abc"; + String greaterThanName = "cde"; + NV nv = new NV(name, value); + NV ltnv = new NV(lessThanName, value); + NV gtnv = new NV(greaterThanName, value); + assertTrue(nv.compareTo(nv) == 0); + assertTrue(ltnv.compareTo(nv) < 0); + assertTrue(gtnv.compareTo(nv) > 0); + } + + public void testCompareContract() { + /* + * The NV class has a natural ordering that is inconsistent with equals. + * When equals and compare align, need to switch this test and the note, above, + * in the NV source code. + */ + String name = "bcd"; + String value1 = "xyz"; + String value2 = "lmn"; + NV nv = new NV(name, value1); + NV nv_diff_val = new NV(name, value2); + assertFalse(nv.equals(nv_diff_val)); + assertTrue(nv.compareTo(nv_diff_val)==0); + } + +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |