From: Erik Brakkee Date: Tue, 9 Aug 2011 14:19:15 +0000 (+0200) Subject: added copying of SingleRouterConfig. X-Git-Tag: PAX_EXAM_MOCKITO_ISSUE~9 X-Git-Url: http://wamblee.org/gitweb/?a=commitdiff_plain;h=d7938dd34e22ba8d34b4a89840c6efbb5d866380;p=xmlrouter added copying of SingleRouterConfig. --- diff --git a/impl/src/main/java/org/wamblee/xmlrouter/impl/ConfigImpl.java b/impl/src/main/java/org/wamblee/xmlrouter/impl/ConfigImpl.java index a1733ab..8088be3 100644 --- a/impl/src/main/java/org/wamblee/xmlrouter/impl/ConfigImpl.java +++ b/impl/src/main/java/org/wamblee/xmlrouter/impl/ConfigImpl.java @@ -49,6 +49,21 @@ public abstract class ConfigImpl> implements registered = new HashMap, T>(); } + /** + * Copies the config object. + * + * @param aConfig + * Config to copy. + */ + public ConfigImpl(ConfigImpl aConfig) { + notNull("config", aConfig); + id = aConfig.id; + registered = new HashMap, T>(); + for (Map.Entry, T> entry : aConfig.registered.entrySet()) { + registered.put(entry.getKey(), entry.getValue()); + } + } + @Override public Id getId() { return id; diff --git a/impl/src/main/java/org/wamblee/xmlrouter/impl/SingleRouterConfig.java b/impl/src/main/java/org/wamblee/xmlrouter/impl/SingleRouterConfig.java index 8eab34b..deb38f6 100644 --- a/impl/src/main/java/org/wamblee/xmlrouter/impl/SingleRouterConfig.java +++ b/impl/src/main/java/org/wamblee/xmlrouter/impl/SingleRouterConfig.java @@ -31,11 +31,59 @@ import org.wamblee.xmlrouter.config.Transformation; * @author Erik Brakkee */ public class SingleRouterConfig implements ExtendedRouterConfig { + + public static final class DocumentConfig extends ConfigImpl { + public DocumentConfig(Id aId) { + super(aId); + } + + public DocumentConfig(DocumentConfig aConfig) { + super(aConfig); + } + + @Override + public DocumentType wrap(String aPrefix, DocumentType aT) { + return new RobustDocumentType(aPrefix, aT); + } + } + + public static final class TransformationConfig extends + ConfigImpl { + public TransformationConfig(Id aId) { + super(aId); + } + + public TransformationConfig(TransformationConfig aConfig) { + super(aConfig); + } + + @Override + public Transformation wrap(String aPrefix, + Transformation aTransformation) { + return new RobustTransformation(aPrefix, aTransformation); + } + } + + public static final class FilterConfig extends ConfigImpl { + public FilterConfig(Id aId) { + super(aId); + } + + public FilterConfig(FilterConfig aConfig) { + super(aConfig); + } + + @Override + public Filter wrap(String aPrefix, Filter aFilter) { + return new RobustFilter(aPrefix, aFilter); + } + } + private Id id; - private ExtendedConfig documentTypes; - private ExtendedConfig transformations; - private ExtendedConfig filters; + private DocumentConfig documentTypes; + private TransformationConfig transformations; + private FilterConfig filters; /** * Constructs a router configuration. @@ -45,28 +93,18 @@ public class SingleRouterConfig implements ExtendedRouterConfig { */ public SingleRouterConfig(Id aId) { id = aId; - documentTypes = new ConfigImpl(new Id( - aId.getId() + ".documenttypes")) { - @Override - public DocumentType wrap(String aPrefix, DocumentType aT) { - return new RobustDocumentType(aPrefix, aT); - } - }; - transformations = new ConfigImpl(new Id( - aId.getId() + ".transformations")) { - @Override - public Transformation wrap(String aPrefix, - Transformation aTransformation) { - return new RobustTransformation(aPrefix, aTransformation); - } - }; - filters = new ConfigImpl(new Id(aId.getId() + - ".filters")) { - @Override - public Filter wrap(String aPrefix, Filter aFilter) { - return new RobustFilter(aPrefix, aFilter); - } - }; + documentTypes = new DocumentConfig(new Id(aId.getId() + + ".documenttypes")); + transformations = new TransformationConfig(new Id(aId.getId() + + ".transformations")); + filters = new FilterConfig(new Id(aId.getId() + ".filters")); + } + + public SingleRouterConfig(SingleRouterConfig aConfig) { + id = aConfig.id; + documentTypes = new DocumentConfig(aConfig.documentTypes); + transformations = new TransformationConfig(aConfig.transformations); + filters = new FilterConfig(aConfig.filters); } @Override 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 8ebdb9f..aa197ea 100644 --- a/impl/src/test/java/org/wamblee/xmlrouter/impl/ConfigImplTest.java +++ b/impl/src/test/java/org/wamblee/xmlrouter/impl/ConfigImplTest.java @@ -53,18 +53,28 @@ public class ConfigImplTest { } } + public static final class MyTypeConfig extends ConfigImpl { + public MyTypeConfig(Id aId) { + super(aId); + } + + public MyTypeConfig(MyTypeConfig aConfig) { + super(aConfig); + } + + @Override + public MyType wrap(String aPrefix, MyType aT) { + return new MyTypeWrapper(aPrefix, 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 @@ -146,4 +156,18 @@ public class ConfigImplTest { 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)); + } } diff --git a/impl/src/test/java/org/wamblee/xmlrouter/impl/SingleRouterConfigTest.java b/impl/src/test/java/org/wamblee/xmlrouter/impl/SingleRouterConfigTest.java index 3df4293..5f151d2 100644 --- a/impl/src/test/java/org/wamblee/xmlrouter/impl/SingleRouterConfigTest.java +++ b/impl/src/test/java/org/wamblee/xmlrouter/impl/SingleRouterConfigTest.java @@ -28,7 +28,7 @@ import org.wamblee.xmlrouter.config.Transformation; public class SingleRouterConfigTest { - private ExtendedRouterConfig config; + private SingleRouterConfig config; @Before public void setUp() { @@ -136,4 +136,25 @@ public class SingleRouterConfigTest { assertEquals(config1, config2); assertEquals(config1.hashCode(), config2.hashCode()); } + + @Test + public void testCopy() { + testDocumentType(); + testFilter(); + testTransformation(); + + SingleRouterConfig copy = new SingleRouterConfig(config); + assertEquals(config.getId(), copy.getId()); + assertEquals(config, copy); + + // verify the copy is not shallow. + + config.documentTypeConfig().remove(new Id("type1")); + config.transformationConfig().remove(new Id("t1")); + config.filterConfig().remove(new Id("f1")); + assertEquals(1, config.documentTypeConfig().values().size()); + assertEquals(1, config.transformationConfig().values().size()); + assertEquals(1, config.filterConfig().values().size()); + assertFalse(config.equals(copy)); + } }