From 75f42f00e16ceee9ea333e598c9287de20ede1c3 Mon Sep 17 00:00:00 2001 From: Erik Brakkee Date: Sun, 7 Aug 2011 22:40:48 +0200 Subject: [PATCH] RobustIdentifiable implemented and tested + test impacts. --- .../org/wamblee/xmlrouter/config/Config.java | 2 +- ...ateException.java => ConfigException.java} | 6 +- .../xmlrouter/impl/CompositeConfig.java | 8 ++- .../xmlrouter/impl/RobustIdentifiable.java | 20 +++--- .../xmlrouter/impl/CompositeConfigTest.java | 6 +- .../impl/RobustDocumentTypeTest.java | 2 + .../xmlrouter/impl/RobustFilterTest.java | 5 ++ .../impl/RobustIdentifiableTest.java | 61 +++++++++++++++++++ .../impl/RobustTransformationTest.java | 2 + .../impl/SingleRouterConfigTest.java | 6 +- .../xmlrouter/impl/XMLRouterFunctionTest.java | 1 + .../wamblee/xmlrouter/impl/XMLRouterTest.java | 20 +++++- 12 files changed, 114 insertions(+), 25 deletions(-) rename config/src/main/java/org/wamblee/xmlrouter/config/{DuplicateException.java => ConfigException.java} (82%) create mode 100644 impl/src/test/java/org/wamblee/xmlrouter/impl/RobustIdentifiableTest.java diff --git a/config/src/main/java/org/wamblee/xmlrouter/config/Config.java b/config/src/main/java/org/wamblee/xmlrouter/config/Config.java index e825f9f..17c0c41 100644 --- a/config/src/main/java/org/wamblee/xmlrouter/config/Config.java +++ b/config/src/main/java/org/wamblee/xmlrouter/config/Config.java @@ -34,7 +34,7 @@ public interface Config extends Identifiable { * * @param aT * item - * @throws DuplicateException + * @throws ConfigException * In case an object with the same id already exists. */ void add(T aT); diff --git a/config/src/main/java/org/wamblee/xmlrouter/config/DuplicateException.java b/config/src/main/java/org/wamblee/xmlrouter/config/ConfigException.java 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 635f6c6..5944bfe 100644 --- a/config/src/main/java/org/wamblee/xmlrouter/config/DuplicateException.java +++ b/config/src/main/java/org/wamblee/xmlrouter/config/ConfigException.java @@ -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); } } diff --git a/impl/src/main/java/org/wamblee/xmlrouter/impl/CompositeConfig.java b/impl/src/main/java/org/wamblee/xmlrouter/impl/CompositeConfig.java index 2523ef8..d837f17 100644 --- a/impl/src/main/java/org/wamblee/xmlrouter/impl/CompositeConfig.java +++ b/impl/src/main/java/org/wamblee/xmlrouter/impl/CompositeConfig.java @@ -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> implements public void addConfig(Config 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() + "'"); } } diff --git a/impl/src/main/java/org/wamblee/xmlrouter/impl/RobustIdentifiable.java b/impl/src/main/java/org/wamblee/xmlrouter/impl/RobustIdentifiable.java index 640e37e..6622e32 100644 --- a/impl/src/main/java/org/wamblee/xmlrouter/impl/RobustIdentifiable.java +++ b/impl/src/main/java/org/wamblee/xmlrouter/impl/RobustIdentifiable.java @@ -15,10 +15,13 @@ */ 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 implements Identifiable { // TODO test that id is constant even though delegated changes its id. public RobustIdentifiable(String aPrefix, Identifiable 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(Constants.UNKNOWN_ID.toString()); - temporarilyThrowException(); - } else { - id = new Id(aPrefix + id.getId()); + throwConfigException("identifiable.getId() returned null", null); } + id = new Id(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 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..1f17b68 100644 --- a/impl/src/test/java/org/wamblee/xmlrouter/impl/CompositeConfigTest.java +++ b/impl/src/test/java/org/wamblee/xmlrouter/impl/CompositeConfigTest.java @@ -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 composite = composite("c"); Config 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()); diff --git a/impl/src/test/java/org/wamblee/xmlrouter/impl/RobustDocumentTypeTest.java b/impl/src/test/java/org/wamblee/xmlrouter/impl/RobustDocumentTypeTest.java index aa724e2..2c295b5 100644 --- a/impl/src/test/java/org/wamblee/xmlrouter/impl/RobustDocumentTypeTest.java +++ b/impl/src/test/java/org/wamblee/xmlrouter/impl/RobustDocumentTypeTest.java @@ -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("docid")); robust = new RobustDocumentType("app1", documentType); source = mock(DOMSource.class); } diff --git a/impl/src/test/java/org/wamblee/xmlrouter/impl/RobustFilterTest.java b/impl/src/test/java/org/wamblee/xmlrouter/impl/RobustFilterTest.java index 4c8218d..9d434bb 100644 --- a/impl/src/test/java/org/wamblee/xmlrouter/impl/RobustFilterTest.java +++ b/impl/src/test/java/org/wamblee/xmlrouter/impl/RobustFilterTest.java @@ -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(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 index 0000000..f8fcd5c --- /dev/null +++ b/impl/src/test/java/org/wamblee/xmlrouter/impl/RobustIdentifiableTest.java @@ -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 ident; + + @Before + public void setUp() { + ident = mock(Identifiable.class); + } + + @Test(expected = ConfigException.class) + public void testIdIsNull() { + when(ident.getId()).thenReturn(null); + RobustIdentifiable robust = new RobustIdentifiable( + "prefix", ident); + } + + @Test(expected = ConfigException.class) + public void testIdThrowsException() { + doThrow(new RuntimeException("xxx")).when(ident).getId(); + RobustIdentifiable robust = new RobustIdentifiable( + "prefix", ident); + } + + @Test + public void testNormalCase() { + when(ident.getId()).thenReturn(new Id("myid")); + RobustIdentifiable robust = new RobustIdentifiable( + "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("myid2")); + assertEquals("prefix.myid", robust.getId().toString()); + } +} diff --git a/impl/src/test/java/org/wamblee/xmlrouter/impl/RobustTransformationTest.java b/impl/src/test/java/org/wamblee/xmlrouter/impl/RobustTransformationTest.java index e3a0d47..5e73d6f 100644 --- a/impl/src/test/java/org/wamblee/xmlrouter/impl/RobustTransformationTest.java +++ b/impl/src/test/java/org/wamblee/xmlrouter/impl/RobustTransformationTest.java @@ -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("t1")); robust = new RobustTransformation("transformation", transformation); source = mock(DOMSource.class); resSource = mock(DOMSource.class); 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 280e6da..a344f72 100644 --- a/impl/src/test/java/org/wamblee/xmlrouter/impl/SingleRouterConfigTest.java +++ b/impl/src/test/java/org/wamblee/xmlrouter/impl/SingleRouterConfigTest.java @@ -40,7 +40,7 @@ public class SingleRouterConfigTest { DocumentType type1 = mock(DocumentType.class); when(type1.getId()).thenReturn(new Id("type1")); DocumentType type2 = mock(DocumentType.class); - when(type1.getId()).thenReturn(new Id("type2")); + when(type2.getId()).thenReturn(new Id("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("t1")); Transformation transformation2 = mock(Transformation.class); - when(transformation1.getId()).thenReturn(new Id("t2")); + when(transformation2.getId()).thenReturn(new Id("t2")); config.transformationConfig().add(transformation1); @@ -69,7 +69,7 @@ public class SingleRouterConfigTest { Filter filter1 = mock(Filter.class); when(filter1.getId()).thenReturn(new Id("f1")); Filter filter2 = mock(Filter.class); - when(filter1.getId()).thenReturn(new Id("f2")); + when(filter2.getId()).thenReturn(new Id("f2")); config.filterConfig().add(filter1); diff --git a/impl/src/test/java/org/wamblee/xmlrouter/impl/XMLRouterFunctionTest.java b/impl/src/test/java/org/wamblee/xmlrouter/impl/XMLRouterFunctionTest.java index e2e08b4..bfb2b41 100644 --- a/impl/src/test/java/org/wamblee/xmlrouter/impl/XMLRouterFunctionTest.java +++ b/impl/src/test/java/org/wamblee/xmlrouter/impl/XMLRouterFunctionTest.java @@ -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(aType)); RouterConfig routerConfig = configService.emptyConfig("app"); routerConfig.documentTypeConfig().add(type); return routerConfig; diff --git a/impl/src/test/java/org/wamblee/xmlrouter/impl/XMLRouterTest.java b/impl/src/test/java/org/wamblee/xmlrouter/impl/XMLRouterTest.java index 2746253..ae40327 100644 --- a/impl/src/test/java/org/wamblee/xmlrouter/impl/XMLRouterTest.java +++ b/impl/src/test/java/org/wamblee/xmlrouter/impl/XMLRouterTest.java @@ -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(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(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(aType)); routerConfig.documentTypeConfig().add(type); } -- 2.31.1