equals and hashcode for robust identifiable.
authorErik Brakkee <erik@brakkee.org>
Sun, 7 Aug 2011 20:46:54 +0000 (22:46 +0200)
committerErik Brakkee <erik@brakkee.org>
Sun, 7 Aug 2011 20:46:54 +0000 (22:46 +0200)
impl/src/main/java/org/wamblee/xmlrouter/impl/RobustIdentifiable.java
impl/src/test/java/org/wamblee/xmlrouter/impl/RobustIdentifiableTest.java

index 6622e3277a85e35ae2dbf527bac761ae07f1d3e9..d5ba9f95bf18a2691103e6336d20a4c97e76c12a 100644 (file)
@@ -25,7 +25,8 @@ import org.wamblee.xmlrouter.config.ConfigException;
 import org.wamblee.xmlrouter.config.Identifiable;
 
 /**
- * Robust identifiable provides robustness for identifiable objects.
+ * Robust identifiable provides robustness for identifiable objects. It adds
+ * equality based on the id.
  * 
  * @author Erik Brakkee
  * 
@@ -37,14 +38,9 @@ public class RobustIdentifiable<T> implements Identifiable<T> {
 
     private Id<T> id;
 
-    // TODO test this class.
-    // TODO test that id is constant even though delegated changes its id.
-
     public RobustIdentifiable(String aPrefix, Identifiable<T> aIdentifiable) {
         notNull("prefix", aPrefix);
         notNull("identifiable", aIdentifiable);
-        // TODO test id is null
-        // TODO getId() throws exception
         try {
             id = aIdentifiable.getId();
             if (id == null) {
index f8fcd5cbbfb7d28e955481ca2f83fa4150fde3cc..257ea93a171d93c99ded1fa7ff9e27e4e027ac18 100644 (file)
@@ -58,4 +58,29 @@ public class RobustIdentifiableTest {
         when(ident.getId()).thenReturn(new Id<Integer>("myid2"));
         assertEquals("prefix.myid", robust.getId().toString());
     }
+
+    @Test
+    public void testEqualsHashCode() {
+        Identifiable<Integer> ident1 = mock(Identifiable.class);
+        Identifiable<Integer> ident2 = mock(Identifiable.class);
+        Identifiable<Integer> ident3 = mock(Identifiable.class);
+
+        when(ident1.getId()).thenReturn(new Id<Integer>("x"));
+        when(ident2.getId()).thenReturn(new Id<Integer>("y"));
+        when(ident3.getId()).thenReturn(new Id<Integer>("x"));
+
+        RobustIdentifiable<Integer> robust1 = new RobustIdentifiable<Integer>(
+            "prefix.", ident1);
+        RobustIdentifiable<Integer> robust2 = new RobustIdentifiable<Integer>(
+            "prefix.", ident2);
+        RobustIdentifiable<Integer> robust3 = new RobustIdentifiable<Integer>(
+            "prefix.", ident3);
+
+        assertEquals(robust1, robust1);
+        assertEquals(robust1, robust3);
+        assertFalse(robust1.equals(robust2));
+        assertFalse(robust1.equals("x"));
+
+        assertEquals(robust1.hashCode(), robust3.hashCode());
+    }
 }