added copying of SingleRouterConfig.
authorErik Brakkee <erik@brakkee.org>
Tue, 9 Aug 2011 14:19:15 +0000 (16:19 +0200)
committerErik Brakkee <erik@brakkee.org>
Tue, 9 Aug 2011 14:19:15 +0000 (16:19 +0200)
impl/src/main/java/org/wamblee/xmlrouter/impl/ConfigImpl.java
impl/src/main/java/org/wamblee/xmlrouter/impl/SingleRouterConfig.java
impl/src/test/java/org/wamblee/xmlrouter/impl/ConfigImplTest.java
impl/src/test/java/org/wamblee/xmlrouter/impl/SingleRouterConfigTest.java

index a1733ab41bf84a9b84cb3986f460586871bd763e..8088be30e31df69d8bf87280f68d7bfef94bbf42 100644 (file)
@@ -49,6 +49,21 @@ public abstract class ConfigImpl<T extends Identifiable<T>> implements
         registered = new HashMap<Id<T>, T>();
     }
 
+    /**
+     * Copies the config object.
+     * 
+     * @param aConfig
+     *            Config to copy.
+     */
+    public ConfigImpl(ConfigImpl<T> aConfig) {
+        notNull("config", aConfig);
+        id = aConfig.id;
+        registered = new HashMap<Id<T>, T>();
+        for (Map.Entry<Id<T>, T> entry : aConfig.registered.entrySet()) {
+            registered.put(entry.getKey(), entry.getValue());
+        }
+    }
+
     @Override
     public Id<Config> getId() {
         return id;
index 8eab34b708996198b73c4cbfd271462937ff1119..deb38f6656fcd39ea9f3bf37e4768b23a94431cb 100644 (file)
@@ -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<DocumentType> {
+        public DocumentConfig(Id<Config> 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<Transformation> {
+        public TransformationConfig(Id<Config> 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<Filter> {
+        public FilterConfig(Id<Config> aId) {
+            super(aId);
+        }
+
+        public FilterConfig(FilterConfig aConfig) {
+            super(aConfig);
+        }
+
+        @Override
+        public Filter wrap(String aPrefix, Filter aFilter) {
+            return new RobustFilter(aPrefix, aFilter);
+        }
+    }
+
     private Id<RouterConfig> id;
 
-    private ExtendedConfig<DocumentType> documentTypes;
-    private ExtendedConfig<Transformation> transformations;
-    private ExtendedConfig<Filter> 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<RouterConfig> aId) {
         id = aId;
-        documentTypes = new ConfigImpl<DocumentType>(new Id<Config>(
-            aId.getId() + ".documenttypes")) {
-            @Override
-            public DocumentType wrap(String aPrefix, DocumentType aT) {
-                return new RobustDocumentType(aPrefix, aT);
-            }
-        };
-        transformations = new ConfigImpl<Transformation>(new Id<Config>(
-            aId.getId() + ".transformations")) {
-            @Override
-            public Transformation wrap(String aPrefix,
-                Transformation aTransformation) {
-                return new RobustTransformation(aPrefix, aTransformation);
-            }
-        };
-        filters = new ConfigImpl<Filter>(new Id<Config>(aId.getId() +
-            ".filters")) {
-            @Override
-            public Filter wrap(String aPrefix, Filter aFilter) {
-                return new RobustFilter(aPrefix, aFilter);
-            }
-        };
+        documentTypes = new DocumentConfig(new Id<Config>(aId.getId() +
+            ".documenttypes"));
+        transformations = new TransformationConfig(new Id<Config>(aId.getId() +
+            ".transformations"));
+        filters = new FilterConfig(new Id<Config>(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
index 8ebdb9f4aa16c2246dc5ff50b494239fab9dc1c8..aa197eaf54274e44fee6d9ba87555d0381833735 100644 (file)
@@ -53,18 +53,28 @@ public class ConfigImplTest {
         }
     }
 
+    public static final class MyTypeConfig extends ConfigImpl<MyType> {
+        public MyTypeConfig(Id<Config> 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<MyType> config;
+    private MyTypeConfig config;
 
     @Before
     public void setUp() {
         sequence = new AtomicLong(1L);
-        config = new ConfigImpl<MyType>(new Id<Config>(CONFIG_TYPE)) {
-            @Override
-            public MyType wrap(String aPrefix, MyType aT) {
-                return new MyTypeWrapper(aPrefix, aT);
-            }
-        };
+        config = new MyTypeConfig(new Id<Config>(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<MyType>("type1")));
+        assertEquals(1, config.values().size());
+        assertFalse(config.equals(copy));
+    }
 }
index 3df429315aeed8c33bf39eb6ab35078013126807..5f151d29536fd0a4a89874403d99d11be466161a 100644 (file)
@@ -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<DocumentType>("type1"));
+        config.transformationConfig().remove(new Id<Transformation>("t1"));
+        config.filterConfig().remove(new Id<Filter>("f1"));
+        assertEquals(1, config.documentTypeConfig().values().size());
+        assertEquals(1, config.transformationConfig().values().size());
+        assertEquals(1, config.filterConfig().values().size());
+        assertFalse(config.equals(copy));
+    }
 }