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=1e8cfc3b35c1ef5ce9a0631f9ce8dee0d0525570;hpb=f70baadfd579f4d3aa2e8c9ee7d758fb37d7872f;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 1e8cfc3..9925c0b 100644 --- a/impl/src/test/java/org/wamblee/xmlrouter/impl/ConfigImplTest.java +++ b/impl/src/test/java/org/wamblee/xmlrouter/impl/ConfigImplTest.java @@ -23,74 +23,137 @@ 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 final String CONFIG_TYPE = "transformation"; + + private static interface MyType extends Identifiable { } private static class MyTypeWrapper implements MyType { - private Id id; private MyType type; - public MyTypeWrapper(Id aId, MyType aType) { - id = aId; + public MyTypeWrapper(MyType aType) { type = aType; } public MyType getType() { return type; } + + @Override + public Id 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(sequence) { - @Override - public MyType wrap(Id aId, MyType aT) { - return new MyTypeWrapper(aId, aT); - } - }; + config = new MyTypeConfig(new Id(CONFIG_TYPE)); } @Test public void testAdd() { MyType type1 = mock(MyType.class); + when(type1.getId()).thenReturn(new Id("type1")); + + config.add(type1); - Id id1 = config.add(type1); + assertEquals(1, config.values().size()); + MyType firstValue = config.values().iterator().next(); - assertNotNull(id1); - assertEquals(1, config.map().size()); - assertTrue(config.map().get(id1) instanceof MyTypeWrapper); - assertSame(type1, ((MyTypeWrapper) config.map().get(id1)).getType()); + assertTrue(firstValue instanceof MyTypeWrapper); + assertSame(type1, ((MyTypeWrapper) firstValue).getType()); + assertEquals(type1.getId().getId(), firstValue.getId().getId()); // add another one. MyType type2 = mock(MyType.class); - Id 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 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(100L), mock(MyType.class)); + 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)); } }