equality based on the ids of the contents of SingleRouterConfig.
[xmlrouter] / impl / src / test / java / org / wamblee / xmlrouter / impl / ConfigImplTest.java
index 5e3ad92059625e3b4b5e1a5152da4ed0e9feefd1..8ebdb9f4aa16c2246dc5ff50b494239fab9dc1c8 100644 (file)
@@ -28,14 +28,18 @@ import org.wamblee.xmlrouter.config.Identifiable;
 
 public class ConfigImplTest {
 
-    private static interface MyType extends Identifiable {
+    private static final String CONFIG_TYPE = "transformation";
+
+    private static interface MyType extends Identifiable<MyType> {
 
     }
 
     private static class MyTypeWrapper implements MyType {
+        private String prefix;
         private MyType type;
 
-        public MyTypeWrapper(MyType aType) {
+        public MyTypeWrapper(String aPrefix, MyType aType) {
+            prefix = aPrefix;
             type = aType;
         }
 
@@ -45,7 +49,7 @@ public class ConfigImplTest {
 
         @Override
         public Id getId() {
-            return type.getId();
+            return new Id(prefix + type.getId().getId());
         }
     }
 
@@ -55,10 +59,10 @@ public class ConfigImplTest {
     @Before
     public void setUp() {
         sequence = new AtomicLong(1L);
-        config = new ConfigImpl<MyType>(new Id<Config>("mytype")) {
+        config = new ConfigImpl<MyType>(new Id<Config>(CONFIG_TYPE)) {
             @Override
-            public MyType wrap(MyType aT) {
-                return new MyTypeWrapper(aT);
+            public MyType wrap(String aPrefix, MyType aT) {
+                return new MyTypeWrapper(aPrefix, aT);
             }
         };
     }
@@ -71,8 +75,12 @@ public class ConfigImplTest {
         config.add(type1);
 
         assertEquals(1, config.values().size());
-        assertTrue(config.values().get(0) instanceof MyTypeWrapper);
-        assertSame(type1, ((MyTypeWrapper) config.values().get(0)).getType());
+        MyType firstValue = config.values().iterator().next();
+
+        assertTrue(firstValue instanceof MyTypeWrapper);
+        assertSame(type1, ((MyTypeWrapper) firstValue).getType());
+        assertEquals(CONFIG_TYPE + "." + type1.getId().getId(), firstValue
+            .getId().getId());
 
         // add another one.
         MyType type2 = mock(MyType.class);
@@ -99,4 +107,43 @@ public class ConfigImplTest {
     public void testUnmodifiable() {
         config.values().add(mock(MyType.class));
     }
+
+    @Test
+    public void testEquals() {
+
+        Config<MyType> config1 = new ConfigImpl<MyType>(new Id<Config>(
+            CONFIG_TYPE)) {
+            @Override
+            public MyType wrap(String aPrefix, MyType aT) {
+                return new MyTypeWrapper(aPrefix, aT);
+            }
+        };
+        assertFalse(config1.equals(null));
+        assertFalse(config1.equals("hello"));
+        Config<MyType> config2 = new ConfigImpl<MyType>(new Id<Config>(
+            CONFIG_TYPE)) {
+            @Override
+            public MyType wrap(String aPrefix, MyType aT) {
+                return new MyTypeWrapper(aPrefix, aT);
+            }
+        };
+        assertEquals(config1, config2);
+        assertEquals(config1.hashCode(), config2.hashCode());
+
+        MyType type1 = mock(MyType.class);
+        when(type1.getId()).thenReturn(new Id("type1"));
+
+        config1.add(type1);
+        assertFalse(config1.equals(config2));
+
+        MyType type2 = mock(MyType.class);
+        when(type2.getId()).thenReturn(new Id("type1"));
+
+        config2.add(type2);
+        assertEquals(config1, config2);
+        assertEquals(config1.hashCode(), config2.hashCode());
+
+        assertTrue(config2.remove(type2.getId()));
+        assertFalse(config1.equals(config2));
+    }
 }