RobustIdentifiable implemented and tested + test impacts.
authorErik Brakkee <erik@brakkee.org>
Sun, 7 Aug 2011 20:40:48 +0000 (22:40 +0200)
committerErik Brakkee <erik@brakkee.org>
Sun, 7 Aug 2011 20:40:48 +0000 (22:40 +0200)
12 files changed:
config/src/main/java/org/wamblee/xmlrouter/config/Config.java
config/src/main/java/org/wamblee/xmlrouter/config/ConfigException.java [moved from config/src/main/java/org/wamblee/xmlrouter/config/DuplicateException.java with 82% similarity]
impl/src/main/java/org/wamblee/xmlrouter/impl/CompositeConfig.java
impl/src/main/java/org/wamblee/xmlrouter/impl/RobustIdentifiable.java
impl/src/test/java/org/wamblee/xmlrouter/impl/CompositeConfigTest.java
impl/src/test/java/org/wamblee/xmlrouter/impl/RobustDocumentTypeTest.java
impl/src/test/java/org/wamblee/xmlrouter/impl/RobustFilterTest.java
impl/src/test/java/org/wamblee/xmlrouter/impl/RobustIdentifiableTest.java [new file with mode: 0644]
impl/src/test/java/org/wamblee/xmlrouter/impl/RobustTransformationTest.java
impl/src/test/java/org/wamblee/xmlrouter/impl/SingleRouterConfigTest.java
impl/src/test/java/org/wamblee/xmlrouter/impl/XMLRouterFunctionTest.java
impl/src/test/java/org/wamblee/xmlrouter/impl/XMLRouterTest.java

index e825f9fc1d29b3bf77c9e673e5ea6c91f992eb2c..17c0c4123255acb198018268dcaee089d4378c46 100644 (file)
@@ -34,7 +34,7 @@ public interface Config<T extends Identifiable> extends Identifiable<Config> {
      * 
      * @param aT
      *            item
-     * @throws DuplicateException
+     * @throws ConfigException
      *             In case an object with the same id already exists.
      */
     void add(T aT);
similarity index 82%
rename from config/src/main/java/org/wamblee/xmlrouter/config/DuplicateException.java
rename to config/src/main/java/org/wamblee/xmlrouter/config/ConfigException.java
index 635f6c6c67d88e5d39d12ccbe6d94c2356a8edca..5944bfec57ee122be5a67611920044f02bedf566 100644 (file)
@@ -20,13 +20,13 @@ package org.wamblee.xmlrouter.config;
  * 
  * @author Erik Brakkee
  */
-public class DuplicateException extends RuntimeException {
+public class ConfigException extends RuntimeException {
 
-    public DuplicateException(String aMsg) {
+    public ConfigException(String aMsg) {
         super(aMsg);
     }
 
-    public DuplicateException(String aMsg, Throwable aCause) {
+    public ConfigException(String aMsg, Throwable aCause) {
         super(aMsg, aCause);
     }
 }
index 2523ef8d652b76752251f340c05e60b88ceca7a4..d837f1776aec4039cfe587874a05a0cece8d4694 100644 (file)
@@ -24,7 +24,7 @@ import java.util.Set;
 
 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;
 
 /**
@@ -59,11 +59,13 @@ public class CompositeConfig<T extends Identifiable<T>> implements
     public void addConfig(Config<T> aConfig) {
         notNull("aConfig", aConfig);
         if (ids.contains(aConfig.getId())) {
-            throw new DuplicateException(aConfig.getId().toString());
+            throw new ConfigException("duplicate id '" +
+                aConfig.getId().toString() + "'");
         }
         for (T item : aConfig.values()) {
             if (valueIds.contains(item.getId())) {
-                throw new DuplicateException(item.getId().toString());
+                throw new ConfigException("duplicate id '" +
+                    item.getId().toString() + "'");
             }
         }
 
index 640e37e13385402e9414c6beb2719bb3d6e2d378..6622e3277a85e35ae2dbf527bac761ae07f1d3e9 100644 (file)
  */
 package org.wamblee.xmlrouter.impl;
 
+import static org.wamblee.xmlrouter.impl.MessageUtil.*;
+
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
 import org.wamblee.xmlrouter.common.Id;
+import org.wamblee.xmlrouter.config.ConfigException;
 import org.wamblee.xmlrouter.config.Identifiable;
 
 /**
@@ -38,26 +41,25 @@ public class RobustIdentifiable<T> implements Identifiable<T> {
     // TODO test that id is constant even though delegated changes its id.
 
     public RobustIdentifiable(String aPrefix, Identifiable<T> aIdentifiable) {
+        notNull("prefix", aPrefix);
+        notNull("identifiable", aIdentifiable);
         // TODO test id is null
         // TODO getId() throws exception
         try {
             id = aIdentifiable.getId();
             if (id == null) {
-                id = new Id<T>(Constants.UNKNOWN_ID.toString());
-                temporarilyThrowException();
-            } else {
-                id = new Id<T>(aPrefix + id.getId());
+                throwConfigException("identifiable.getId() returned null", null);
             }
+            id = new Id<T>(aPrefix + id.getId());
         } catch (Exception e) {
-            LOGGER
-                .log(Level.WARNING, "Identifiable getId() threw exception", e);
+            throwConfigException("identifiable.getId() threw exception", e);
         }
 
     }
 
-    private void temporarilyThrowException() {
-        throw new RuntimeException(
-            "Temporary to catch nulls during refactoring");
+    private void throwConfigException(String aMsg, Exception aException) {
+        LOGGER.log(Level.WARNING, aMsg, aException);
+        throw new ConfigException("id is null");
     }
 
     @Override
index 1875d4b96d6ab65238eb6b1629a77f22242acaae..1f17b68e7c85ce81717305a01844910bc7525d96 100644 (file)
@@ -22,7 +22,7 @@ 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 {
@@ -116,7 +116,7 @@ public class CompositeConfigTest {
         assertTrue(values.contains(i4));
     }
 
-    @Test(expected = DuplicateException.class)
+    @Test(expected = ConfigException.class)
     public void testDuplicatesNotAllowed() {
         CompositeConfig<IntClass> composite = composite("c");
         Config<IntClass> c1 = new ConfigImpl(id("c1")) {
@@ -159,7 +159,7 @@ public class CompositeConfigTest {
         try {
             composite.addConfig(c2);
             fail();
-        } catch (DuplicateException e) {
+        } catch (ConfigException e) {
             // ok.
         }
         assertEquals(1, composite.values().size());
index aa724e26f3d10ca0f46fb3f976ad410ea0a470cf..2c295b59623fd071cb3fe0627eaa67a5af106d30 100644 (file)
@@ -23,6 +23,7 @@ import javax.xml.transform.dom.DOMSource;
 
 import org.junit.Before;
 import org.junit.Test;
+import org.wamblee.xmlrouter.common.Id;
 import org.wamblee.xmlrouter.config.DocumentType;
 
 public class RobustDocumentTypeTest {
@@ -34,6 +35,7 @@ public class RobustDocumentTypeTest {
     @Before
     public void setUp() {
         documentType = mock(DocumentType.class);
+        when(documentType.getId()).thenReturn(new Id<DocumentType>("docid"));
         robust = new RobustDocumentType("app1", documentType);
         source = mock(DOMSource.class);
     }
index 4c8218d50c796e82ba7258a15e9586658c58524d..9d434bb4ce4cc4fea09d0a6ac06a02f6a3a99bf3 100644 (file)
@@ -19,10 +19,13 @@ import static junit.framework.Assert.*;
 import static org.mockito.Matchers.*;
 import static org.mockito.Mockito.*;
 
+import java.util.UUID;
+
 import javax.xml.transform.dom.DOMSource;
 
 import org.junit.Before;
 import org.junit.Test;
+import org.wamblee.xmlrouter.common.Id;
 import org.wamblee.xmlrouter.config.Filter;
 
 public class RobustFilterTest {
@@ -34,6 +37,8 @@ public class RobustFilterTest {
     @Before
     public void setUp() {
         filter = mock(Filter.class);
+        when(filter.getId()).thenReturn(
+            new Id<Filter>(UUID.randomUUID().toString()));
         robust = new RobustFilter("filter", filter);
         source = mock(DOMSource.class);
     }
diff --git a/impl/src/test/java/org/wamblee/xmlrouter/impl/RobustIdentifiableTest.java b/impl/src/test/java/org/wamblee/xmlrouter/impl/RobustIdentifiableTest.java
new file mode 100644 (file)
index 0000000..f8fcd5c
--- /dev/null
@@ -0,0 +1,61 @@
+/*
+ * Copyright 2005-2011 the original author or authors.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.wamblee.xmlrouter.impl;
+
+import static junit.framework.Assert.*;
+import static org.mockito.Mockito.*;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.wamblee.xmlrouter.common.Id;
+import org.wamblee.xmlrouter.config.ConfigException;
+import org.wamblee.xmlrouter.config.Identifiable;
+
+public class RobustIdentifiableTest {
+
+    private Identifiable<Integer> ident;
+
+    @Before
+    public void setUp() {
+        ident = mock(Identifiable.class);
+    }
+
+    @Test(expected = ConfigException.class)
+    public void testIdIsNull() {
+        when(ident.getId()).thenReturn(null);
+        RobustIdentifiable<Integer> robust = new RobustIdentifiable<Integer>(
+            "prefix", ident);
+    }
+
+    @Test(expected = ConfigException.class)
+    public void testIdThrowsException() {
+        doThrow(new RuntimeException("xxx")).when(ident).getId();
+        RobustIdentifiable<Integer> robust = new RobustIdentifiable<Integer>(
+            "prefix", ident);
+    }
+
+    @Test
+    public void testNormalCase() {
+        when(ident.getId()).thenReturn(new Id<Integer>("myid"));
+        RobustIdentifiable<Integer> robust = new RobustIdentifiable<Integer>(
+            "prefix.", ident);
+        assertEquals("prefix.myid", robust.getId().toString());
+
+        // changes later do not have any effect, the id should be immutable.
+        when(ident.getId()).thenReturn(new Id<Integer>("myid2"));
+        assertEquals("prefix.myid", robust.getId().toString());
+    }
+}
index e3a0d470cf1368082ab972e99036c586b547fada..5e73d6fe239ab0557f12c9d701a63e1ae958433d 100644 (file)
@@ -23,6 +23,7 @@ import javax.xml.transform.dom.DOMSource;
 
 import org.junit.Before;
 import org.junit.Test;
+import org.wamblee.xmlrouter.common.Id;
 import org.wamblee.xmlrouter.config.Transformation;
 
 public class RobustTransformationTest {
@@ -34,6 +35,7 @@ public class RobustTransformationTest {
     @Before
     public void setUp() {
         transformation = mock(Transformation.class);
+        when(transformation.getId()).thenReturn(new Id<Transformation>("t1"));
         robust = new RobustTransformation("transformation", transformation);
         source = mock(DOMSource.class);
         resSource = mock(DOMSource.class);
index 280e6dac420fb78b05b89a8c2f0ce6c1051bd0c3..a344f7201b34bd48a88e37ef2078ffd7db9c5f89 100644 (file)
@@ -40,7 +40,7 @@ public class SingleRouterConfigTest {
         DocumentType type1 = mock(DocumentType.class);
         when(type1.getId()).thenReturn(new Id<DocumentType>("type1"));
         DocumentType type2 = mock(DocumentType.class);
-        when(type1.getId()).thenReturn(new Id<DocumentType>("type2"));
+        when(type2.getId()).thenReturn(new Id<DocumentType>("type2"));
 
         config.documentTypeConfig().add(type1);
         config.documentTypeConfig().add(type2);
@@ -54,7 +54,7 @@ public class SingleRouterConfigTest {
         Transformation transformation1 = mock(Transformation.class);
         when(transformation1.getId()).thenReturn(new Id<Transformation>("t1"));
         Transformation transformation2 = mock(Transformation.class);
-        when(transformation1.getId()).thenReturn(new Id<Transformation>("t2"));
+        when(transformation2.getId()).thenReturn(new Id<Transformation>("t2"));
 
         config.transformationConfig().add(transformation1);
 
@@ -69,7 +69,7 @@ public class SingleRouterConfigTest {
         Filter filter1 = mock(Filter.class);
         when(filter1.getId()).thenReturn(new Id<Filter>("f1"));
         Filter filter2 = mock(Filter.class);
-        when(filter1.getId()).thenReturn(new Id<Filter>("f2"));
+        when(filter2.getId()).thenReturn(new Id<Filter>("f2"));
 
         config.filterConfig().add(filter1);
 
index e2e08b4a0e7b43c941d0711d81d1e8c7385de639..bfb2b41129da4d901fb9ecd8cae3c57d079fb9db 100644 (file)
@@ -63,6 +63,7 @@ public class XMLRouterFunctionTest {
         DocumentType type = mock(DocumentType.class);
         when(type.isInstance(any(DOMSource.class))).thenReturn(true);
         when(type.getName()).thenReturn(aType);
+        when(type.getId()).thenReturn(new Id<DocumentType>(aType));
         RouterConfig routerConfig = configService.emptyConfig("app");
         routerConfig.documentTypeConfig().add(type);
         return routerConfig;
index 2746253de58653f31b04a21d63ef990f89680b44..ae40327050f34eae08805923a65397fc5096d0a1 100644 (file)
@@ -20,6 +20,7 @@ import static org.mockito.Mockito.*;
 
 import java.util.Arrays;
 import java.util.Collection;
+import java.util.UUID;
 import java.util.logging.Level;
 
 import javax.xml.transform.dom.DOMSource;
@@ -102,7 +103,7 @@ public class XMLRouterTest {
 
     @Test
     public void testMisBehavingDocumentType() {
-        DocumentType type = mock(DocumentType.class);
+        DocumentType type = mockDocument("docid");
         doThrow(new RuntimeException("x")).when(type).isInstance(
             any(DOMSource.class));
         routerConfig.documentTypeConfig().add(type);
@@ -111,10 +112,16 @@ public class XMLRouterTest {
         // no exception should occur.
     }
 
+    private DocumentType mockDocument(String docid) {
+        DocumentType type = mock(DocumentType.class);
+        when(type.getId()).thenReturn(new Id<DocumentType>(docid));
+        return type;
+    }
+
     @Test
     public void testMisBehavingFilter() {
         registerDocumentType("any");
-        Filter filter = mock(Filter.class);
+        Filter filter = mockFilter("filterid");
         doThrow(new RuntimeException("x")).when(filter).isAllowed(anyString(),
             any(DOMSource.class));
         routerConfig.filterConfig().add(filter);
@@ -123,6 +130,12 @@ public class XMLRouterTest {
         // no exception should occur.
     }
 
+    private Filter mockFilter(String filterId) {
+        Filter filter = mock(Filter.class);
+        when(filter.getId()).thenReturn(new Id<Filter>(filterId));
+        return filter;
+    }
+
     @Test
     public void testOneDestinationNoTransformationSuccess() {
         destinationSpy = registerDestination(true, "any");
@@ -147,7 +160,7 @@ public class XMLRouterTest {
     }
 
     private void registerDocumentType(String aType) {
-        DocumentType type = mock(DocumentType.class);
+        DocumentType type = mockDocument(UUID.randomUUID().toString());
         when(type.isInstance(any(DOMSource.class))).thenReturn(true);
         when(type.getName()).thenReturn(aType);
         routerConfig.documentTypeConfig().add(type);
@@ -157,6 +170,7 @@ public class XMLRouterTest {
         DocumentType type = mock(DocumentType.class);
         when(type.isInstance(same(aSource))).thenReturn(true);
         when(type.getName()).thenReturn(aType);
+        when(type.getId()).thenReturn(new Id<DocumentType>(aType));
         routerConfig.documentTypeConfig().add(type);
     }