First version after introduction of meaningful ids and Identifiable interface.
[xmlrouter] / impl / src / test / java / org / wamblee / xmlrouter / impl / ConfigImplTest.java
index 1e8cfc3b35c1ef5ce9a0631f9ce8dee0d0525570..5e3ad92059625e3b4b5e1a5152da4ed0e9feefd1 100644 (file)
@@ -23,25 +23,30 @@ import java.util.concurrent.atomic.AtomicLong;
 import org.junit.Before;
 import org.junit.Test;
 import org.wamblee.xmlrouter.common.Id;
+import org.wamblee.xmlrouter.config.Config;
+import org.wamblee.xmlrouter.config.Identifiable;
 
 public class ConfigImplTest {
 
-    private static interface MyType {
+    private static interface MyType extends Identifiable {
 
     }
 
     private static class MyTypeWrapper implements MyType {
-        private Id<MyType> id;
         private MyType type;
 
-        public MyTypeWrapper(Id<MyType> aId, MyType aType) {
-            id = aId;
+        public MyTypeWrapper(MyType aType) {
             type = aType;
         }
 
         public MyType getType() {
             return type;
         }
+
+        @Override
+        public Id getId() {
+            return type.getId();
+        }
     }
 
     private AtomicLong sequence;
@@ -50,10 +55,10 @@ public class ConfigImplTest {
     @Before
     public void setUp() {
         sequence = new AtomicLong(1L);
-        config = new ConfigImpl<MyType>(sequence) {
+        config = new ConfigImpl<MyType>(new Id<Config>("mytype")) {
             @Override
-            public MyType wrap(Id<MyType> aId, MyType aT) {
-                return new MyTypeWrapper(aId, aT);
+            public MyType wrap(MyType aT) {
+                return new MyTypeWrapper(aT);
             }
         };
     }
@@ -61,36 +66,37 @@ public class ConfigImplTest {
     @Test
     public void testAdd() {
         MyType type1 = mock(MyType.class);
+        when(type1.getId()).thenReturn(new Id("type1"));
 
-        Id<MyType> id1 = config.add(type1);
+        config.add(type1);
 
-        assertNotNull(id1);
-        assertEquals(1, config.map().size());
-        assertTrue(config.map().get(id1) instanceof MyTypeWrapper);
-        assertSame(type1, ((MyTypeWrapper) config.map().get(id1)).getType());
+        assertEquals(1, config.values().size());
+        assertTrue(config.values().get(0) instanceof MyTypeWrapper);
+        assertSame(type1, ((MyTypeWrapper) config.values().get(0)).getType());
 
         // add another one.
         MyType type2 = mock(MyType.class);
-        Id<MyType> id2 = config.add(type2);
-        assertNotNull(id2);
-        assertEquals(2, config.map().size());
-        assertFalse(id1.equals(id2));
+        when(type2.getId()).thenReturn(new Id("type2"));
+        config.add(type2);
+
+        assertEquals(2, config.values().size());
     }
 
     @Test
     public void testRemove() {
         MyType type1 = mock(MyType.class);
-        Id<MyType> id1 = config.add(type1);
+        when(type1.getId()).thenReturn(new Id("type1"));
+
+        config.add(type1);
 
-        assertNotNull(id1);
-        assertEquals(1, config.map().size());
+        assertEquals(1, config.values().size());
 
-        config.remove(id1);
-        assertTrue(config.map().isEmpty());
+        assertTrue(config.remove(new Id("type1")));
+        assertTrue(config.values().isEmpty());
     }
 
     @Test(expected = UnsupportedOperationException.class)
     public void testUnmodifiable() {
-        config.map().put(new Id<MyType>(100L), mock(MyType.class));
+        config.values().add(mock(MyType.class));
     }
 }