X-Git-Url: http://wamblee.org/gitweb/?a=blobdiff_plain;f=impl%2Fsrc%2Ftest%2Fjava%2Forg%2Fwamblee%2Fxmlrouter%2Fimpl%2FConfigImplTest.java;h=9925c0b74a7637aa6065cca0cd7a49a7f597474f;hb=2e9a88b0478d074974818dc384ca45c2bf4fdaa5;hp=044cb1f32f9240a00da2a2347d7e518ae13015df;hpb=03a6b404471945aed9d48fc1e5b8447b4a9d9413;p=xmlrouter diff --git a/impl/src/test/java/org/wamblee/xmlrouter/impl/ConfigImplTest.java b/impl/src/test/java/org/wamblee/xmlrouter/impl/ConfigImplTest.java index 044cb1f..9925c0b 100644 --- a/impl/src/test/java/org/wamblee/xmlrouter/impl/ConfigImplTest.java +++ b/impl/src/test/java/org/wamblee/xmlrouter/impl/ConfigImplTest.java @@ -35,11 +35,9 @@ public class ConfigImplTest { } private static class MyTypeWrapper implements MyType { - private String prefix; private MyType type; - public MyTypeWrapper(String aPrefix, MyType aType) { - prefix = aPrefix; + public MyTypeWrapper(MyType aType) { type = aType; } @@ -49,22 +47,32 @@ public class ConfigImplTest { @Override public Id getId() { - return new Id(prefix + type.getId().getId()); + return type.getId(); + } + } + + public static final class MyTypeConfig extends ConfigImpl { + public MyTypeConfig(Id aId) { + super(MyType.class, aId); + } + + public MyTypeConfig(MyTypeConfig aConfig) { + super(aConfig); + } + + @Override + public MyType wrap(MyType aT) { + return new MyTypeWrapper(aT); } } private AtomicLong sequence; - private ExtendedConfig config; + private MyTypeConfig config; @Before public void setUp() { sequence = new AtomicLong(1L); - config = new ConfigImpl(new Id(CONFIG_TYPE)) { - @Override - public MyType wrap(String aPrefix, MyType aT) { - return new MyTypeWrapper(aPrefix, aT); - } - }; + config = new MyTypeConfig(new Id(CONFIG_TYPE)); } @Test @@ -75,10 +83,11 @@ 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()); - assertEquals(CONFIG_TYPE + "." + type1.getId().getId(), config.values() - .get(0).getId().getId()); + MyType firstValue = config.values().iterator().next(); + + assertTrue(firstValue instanceof MyTypeWrapper); + assertSame(type1, ((MyTypeWrapper) firstValue).getType()); + assertEquals(type1.getId().getId(), firstValue.getId().getId()); // add another one. MyType type2 = mock(MyType.class); @@ -97,7 +106,7 @@ public class ConfigImplTest { assertEquals(1, config.values().size()); - assertTrue(config.remove(new Id(CONFIG_TYPE + "." + "type1"))); + assertTrue(config.remove(new Id("type1"))); assertTrue(config.values().isEmpty()); } @@ -105,4 +114,46 @@ public class ConfigImplTest { public void testUnmodifiable() { config.values().add(mock(MyType.class)); } + + @Test + public void testEquals() { + + Config config1 = new MyTypeConfig(new Id(CONFIG_TYPE)); + assertFalse(config1.equals(null)); + assertFalse(config1.equals("hello")); + Config config2 = new MyTypeConfig(new Id(CONFIG_TYPE)); + + 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)); + } + + @Test + public void testCopy() { + testAdd(); + assertEquals(2, config.values().size()); + MyTypeConfig copy = new MyTypeConfig(config); + assertEquals(config.getId(), config.getId()); + assertEquals(config, copy); + + // verify the copy is not shallow + assertTrue(config.remove(new Id("type1"))); + assertEquals(1, config.values().size()); + assertFalse(config.equals(copy)); + } }