X-Git-Url: http://wamblee.org/gitweb/?a=blobdiff_plain;f=impl%2Fsrc%2Ftest%2Fjava%2Forg%2Fwamblee%2Fxmlrouter%2Fimpl%2FCompositeConfigTest.java;h=cd0879d59cbcc3b98cb737bbda2c53b1ab1d8fb8;hb=5582d07a1ba9821cc789ea63b71f246e89d13cae;hp=1875d4b96d6ab65238eb6b1629a77f22242acaae;hpb=03a6b404471945aed9d48fc1e5b8447b4a9d9413;p=xmlrouter diff --git a/impl/src/test/java/org/wamblee/xmlrouter/impl/CompositeConfigTest.java b/impl/src/test/java/org/wamblee/xmlrouter/impl/CompositeConfigTest.java index 1875d4b..cd0879d 100644 --- a/impl/src/test/java/org/wamblee/xmlrouter/impl/CompositeConfigTest.java +++ b/impl/src/test/java/org/wamblee/xmlrouter/impl/CompositeConfigTest.java @@ -17,32 +17,42 @@ package org.wamblee.xmlrouter.impl; import static junit.framework.Assert.*; +import java.util.ArrayList; import java.util.List; import org.junit.Test; import org.wamblee.xmlrouter.common.Id; import org.wamblee.xmlrouter.config.Config; -import org.wamblee.xmlrouter.config.DuplicateException; +import org.wamblee.xmlrouter.config.ConfigException; import org.wamblee.xmlrouter.config.Identifiable; public class CompositeConfigTest { - public static class IntClass implements Identifiable { + public static interface StringClassInterface extends + Identifiable { - private int value; + } + + public static class StringClass implements StringClassInterface { - public IntClass(int aValue) { + private String value; + + public StringClass(String aValue) { value = aValue; } + public StringClass(int aValue) { + this(aValue + ""); + } + @Override - public Id getId() { - return new Id(value + ""); + public Id getId() { + return new Id(value + ""); } @Override public int hashCode() { - return ((Integer) value).hashCode(); + return value.hashCode(); } @Override @@ -50,51 +60,52 @@ public class CompositeConfigTest { if (aObj == null) { return false; } - if (!(aObj instanceof IntClass)) { + if (!(aObj instanceof StringClass)) { return false; } - IntClass obj = (IntClass) aObj; - return value == obj.value; + StringClass obj = (StringClass) aObj; + return value.equals(obj.value); } } @Test public void testEmptyConfig() { - Config composite = composite("c"); - assertEquals(id("c"), composite.getId()); + Config composite = composite(); assertTrue(composite.values().isEmpty()); } @Test(expected = RuntimeException.class) public void testAddNotAllowed() { - composite("c").add(new IntClass(10)); + composite().add(new StringClass(10)); } @Test(expected = RuntimeException.class) public void testRemoveNotAllowed() { - composite("c").remove(new Id("xxx")); + composite().remove(new Id("xxx")); } @Test public void testAddConfig() { - CompositeConfig composite = composite("c"); - Config c1 = new ConfigImpl(id("c1")) { + CompositeConfig composite = composite(); + Config c1 = new ConfigImpl( + StringClassInterface.class, id("c1")) { @Override public Identifiable wrap(String aPrefix, Identifiable aT) { return aT; } }; - Config c2 = new ConfigImpl(id("c2")) { + Config c2 = new ConfigImpl( + StringClassInterface.class, id("c2")) { @Override public Identifiable wrap(String aPrefix, Identifiable aT) { return aT; } }; - IntClass i1 = new IntClass(10); - IntClass i2 = new IntClass(20); - IntClass i3 = new IntClass(30); - IntClass i4 = new IntClass(40); + StringClass i1 = new StringClass(10); + StringClass i2 = new StringClass(20); + StringClass i3 = new StringClass(30); + StringClass i4 = new StringClass(40); c1.add(i1); c1.add(i2); @@ -102,30 +113,40 @@ public class CompositeConfigTest { c2.add(i4); composite.addConfig(c1); - List values = composite.values(); - assertEquals(2, values.size()); - assertTrue(values.contains(i1)); - assertTrue(values.contains(i2)); + List values = composite.values(); + List ids = new ArrayList(); + for (StringClassInterface intf : values) { + ids.add(intf.getId().getId()); + } + assertTrue(ids.contains("c1.10")); + assertTrue(ids.contains("c1.20")); composite.addConfig(c2); values = composite.values(); assertEquals(4, values.size()); - assertTrue(values.contains(i1)); - assertTrue(values.contains(i2)); - assertTrue(values.contains(i3)); - assertTrue(values.contains(i4)); + + ids = new ArrayList(); + for (StringClassInterface intf : values) { + ids.add(intf.getId().getId()); + } + assertTrue(ids.contains("c1.10")); + assertTrue(ids.contains("c1.20")); + assertTrue(ids.contains("c2.30")); + assertTrue(ids.contains("c2.40")); } - @Test(expected = DuplicateException.class) + @Test(expected = ConfigException.class) public void testDuplicatesNotAllowed() { - CompositeConfig composite = composite("c"); - Config c1 = new ConfigImpl(id("c1")) { + CompositeConfig composite = composite(); + Config c1 = new ConfigImpl( + StringClassInterface.class, id("c1")) { @Override public Identifiable wrap(String aPrefix, Identifiable aT) { return aT; } }; - Config c2 = new ConfigImpl(id("c1")) { + Config c2 = new ConfigImpl( + StringClassInterface.class, id("c1")) { @Override public Identifiable wrap(String aPrefix, Identifiable aT) { return aT; @@ -137,37 +158,41 @@ public class CompositeConfigTest { @Test public void testDuplicateItem() { - CompositeConfig composite = composite("c"); - Config c1 = new ConfigImpl(id("c1")) { + CompositeConfig composite = composite(); + Config c1 = new ConfigImpl( + StringClassInterface.class, id("c.x")) { @Override public Identifiable wrap(String aPrefix, Identifiable aT) { return aT; } }; - Config c2 = new ConfigImpl(id("c2")) { + Config c2 = new ConfigImpl( + StringClassInterface.class, id("c")) { @Override public Identifiable wrap(String aPrefix, Identifiable aT) { return aT; } }; - IntClass i1 = new IntClass(10); - IntClass i2 = new IntClass(10); + StringClass i1 = new StringClass("y"); + StringClass i2 = new StringClass("x.y"); c1.add(i1); c2.add(i2); composite.addConfig(c1); try { composite.addConfig(c2); fail(); - } catch (DuplicateException e) { + } catch (ConfigException e) { // ok. } assertEquals(1, composite.values().size()); - assertTrue(composite.values().contains(i1)); + assertEquals("c.x.y", composite.values().iterator().next().getId() + .getId()); } - private CompositeConfig composite(String aId) { - return new CompositeConfig(id(aId)); + private CompositeConfig composite() { + return new CompositeConfig( + StringClassInterface.class); } private Id id(String aId) {