From: Erik Brakkee Date: Sun, 7 Aug 2011 20:08:17 +0000 (+0200) Subject: CompositeConfig tested. X-Git-Tag: PAX_EXAM_MOCKITO_ISSUE~20 X-Git-Url: http://wamblee.org/gitweb/?a=commitdiff_plain;h=03a6b404471945aed9d48fc1e5b8447b4a9d9413;p=xmlrouter CompositeConfig tested. --- 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 b12210b..e825f9f 100644 --- a/config/src/main/java/org/wamblee/xmlrouter/config/Config.java +++ b/config/src/main/java/org/wamblee/xmlrouter/config/Config.java @@ -28,15 +28,14 @@ import org.wamblee.xmlrouter.common.Id; * @param * Type for which ids are generated. */ -public interface Config extends Identifiable { - - // TODO define what happens when there is a duplicate item. - +public interface Config extends Identifiable { /** * Adds an item. No item with the same id may exist. * * @param aT * item + * @throws DuplicateException + * 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/DuplicateException.java new file mode 100644 index 0000000..635f6c6 --- /dev/null +++ b/config/src/main/java/org/wamblee/xmlrouter/config/DuplicateException.java @@ -0,0 +1,32 @@ +/* + * 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.config; + +/** + * Exception thrown in case of duplicates. + * + * @author Erik Brakkee + */ +public class DuplicateException extends RuntimeException { + + public DuplicateException(String aMsg) { + super(aMsg); + } + + public DuplicateException(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 56e3e8b..2205210 100644 --- a/impl/src/main/java/org/wamblee/xmlrouter/impl/CompositeConfig.java +++ b/impl/src/main/java/org/wamblee/xmlrouter/impl/CompositeConfig.java @@ -15,11 +15,17 @@ */ package org.wamblee.xmlrouter.impl; +import static org.wamblee.xmlrouter.impl.MessageUtil.*; + import java.util.ArrayList; +import java.util.HashSet; import java.util.List; +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.Identifiable; /** * Composite config. The composite config @@ -28,46 +34,57 @@ import org.wamblee.xmlrouter.config.Config; * * @param */ -public class CompositeConfig implements ExtendedConfig { +public class CompositeConfig> implements + ExtendedConfig { + private static final String READ_ONLY_INSTANCE = "read only instance"; + private Set> ids; private Id id; - private List configs; + private List> valueIds; + private List values; public CompositeConfig(Id aId) { + ids = new HashSet>(); id = aId; - configs = new ArrayList(); + valueIds = new ArrayList>(); + values = new ArrayList(); } @Override public Id getId() { - // TODO test id. return id; } - public void add(Config aConfig) { - // TODO check duplicate config. + public void addConfig(Config aConfig) { + if (ids.contains(aConfig.getId())) { + throw new DuplicateException(aConfig.getId().toString()); + } for (T item : aConfig.values()) { - configs.add(item); + if (valueIds.contains(item.getId())) { + throw new DuplicateException(item.getId().toString()); + } + } + + ids.add(aConfig.getId()); + for (T item : aConfig.values()) { + valueIds.add(item.getId()); + values.add(item); } } @Override public List values() { - return configs; + return values; } @Override public boolean remove(Id aId) { - notSupported(); + notSupported(READ_ONLY_INSTANCE); return false; } @Override public void add(T aT) { - notSupported(); - } - - private void notSupported() { - throw new RuntimeException("readonly instance"); + notSupported(READ_ONLY_INSTANCE); } } diff --git a/impl/src/main/java/org/wamblee/xmlrouter/impl/CompositeRouterConfig.java b/impl/src/main/java/org/wamblee/xmlrouter/impl/CompositeRouterConfig.java index 2b40e58..08a95de 100644 --- a/impl/src/main/java/org/wamblee/xmlrouter/impl/CompositeRouterConfig.java +++ b/impl/src/main/java/org/wamblee/xmlrouter/impl/CompositeRouterConfig.java @@ -41,9 +41,9 @@ public class CompositeRouterConfig implements ExtendedRouterConfig { "transformations")); filters = new CompositeConfig(new Id("filters")); for (RouterConfig config : aConfigs) { - documentTypes.add(config.documentTypeConfig()); - transformations.add(config.transformationConfig()); - filters.add(config.filterConfig()); + documentTypes.addConfig(config.documentTypeConfig()); + transformations.addConfig(config.transformationConfig()); + filters.addConfig(config.filterConfig()); } } 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 9442db6..0324e78 100644 --- a/impl/src/main/java/org/wamblee/xmlrouter/impl/ConfigImpl.java +++ b/impl/src/main/java/org/wamblee/xmlrouter/impl/ConfigImpl.java @@ -15,6 +15,8 @@ */ package org.wamblee.xmlrouter.impl; +import static org.wamblee.xmlrouter.impl.MessageUtil.*; + import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; @@ -36,7 +38,7 @@ import org.wamblee.xmlrouter.config.Identifiable; */ // TODO make sure that each item inside this config is prefixed with the id of // the config. -public abstract class ConfigImpl implements +public abstract class ConfigImpl> implements ExtendedConfig { private Id id; @@ -63,8 +65,7 @@ public abstract class ConfigImpl implements */ @Override public synchronized void add(T aT) { - // TODO test duplicate ids. - notNull(aT); + notNull("aT", aT); registered.add(wrap(id.getId() + ".", aT)); } @@ -86,7 +87,7 @@ public abstract class ConfigImpl implements */ @Override public synchronized boolean remove(Id aId) { - notNull(aId); + notNull("aId", aId); Iterator i = registered.iterator(); while (i.hasNext()) { T t = i.next(); @@ -102,16 +103,4 @@ public abstract class ConfigImpl implements public List values() { return Collections.unmodifiableList(registered); } - - private void notNull(T aT) { - if (aT == null) { - throw new NullPointerException("Object is null"); - } - } - - private void notNull(Id aId) { - if (aId == null) { - throw new NullPointerException("Id is null"); - } - } } diff --git a/impl/src/main/java/org/wamblee/xmlrouter/impl/ExtendedConfig.java b/impl/src/main/java/org/wamblee/xmlrouter/impl/ExtendedConfig.java index 232e54d..2c1b781 100644 --- a/impl/src/main/java/org/wamblee/xmlrouter/impl/ExtendedConfig.java +++ b/impl/src/main/java/org/wamblee/xmlrouter/impl/ExtendedConfig.java @@ -16,7 +16,8 @@ package org.wamblee.xmlrouter.impl; import org.wamblee.xmlrouter.config.Config; +import org.wamblee.xmlrouter.config.Identifiable; -public interface ExtendedConfig extends Config { +public interface ExtendedConfig> extends Config { } diff --git a/impl/src/main/java/org/wamblee/xmlrouter/impl/MessageUtil.java b/impl/src/main/java/org/wamblee/xmlrouter/impl/MessageUtil.java new file mode 100644 index 0000000..37a2f40 --- /dev/null +++ b/impl/src/main/java/org/wamblee/xmlrouter/impl/MessageUtil.java @@ -0,0 +1,35 @@ +/* + * 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; + +/** + * Some simple utilities for checking values and guarding unsupported methods. + * + * @author Erik Brakkee + */ +public class MessageUtil { + + public static void notSupported(String aMessage) { + throw new RuntimeException(aMessage); + } + + public static void notNull(String aName, Object aValue) { + if (aValue == null) { + throw new IllegalArgumentException("Parameter '" + aName + + "' may not be null"); + } + } +} 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 cc778fe..1e9dd86 100644 --- a/impl/src/main/java/org/wamblee/xmlrouter/impl/SingleRouterConfig.java +++ b/impl/src/main/java/org/wamblee/xmlrouter/impl/SingleRouterConfig.java @@ -23,7 +23,6 @@ import org.wamblee.xmlrouter.config.RouterConfig; import org.wamblee.xmlrouter.config.Transformation; // TODO implement equality based on ids for the single routerconfig. -// TODO add clear method to the routerconfig // TODO implement copying of routerconfig. /** diff --git a/impl/src/test/java/org/wamblee/xmlrouter/impl/CompositeConfigTest.java b/impl/src/test/java/org/wamblee/xmlrouter/impl/CompositeConfigTest.java new file mode 100644 index 0000000..1875d4b --- /dev/null +++ b/impl/src/test/java/org/wamblee/xmlrouter/impl/CompositeConfigTest.java @@ -0,0 +1,176 @@ +/* + * 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 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.Identifiable; + +public class CompositeConfigTest { + + public static class IntClass implements Identifiable { + + private int value; + + public IntClass(int aValue) { + value = aValue; + } + + @Override + public Id getId() { + return new Id(value + ""); + } + + @Override + public int hashCode() { + return ((Integer) value).hashCode(); + } + + @Override + public boolean equals(Object aObj) { + if (aObj == null) { + return false; + } + if (!(aObj instanceof IntClass)) { + return false; + } + IntClass obj = (IntClass) aObj; + return value == obj.value; + } + } + + @Test + public void testEmptyConfig() { + Config composite = composite("c"); + assertEquals(id("c"), composite.getId()); + assertTrue(composite.values().isEmpty()); + } + + @Test(expected = RuntimeException.class) + public void testAddNotAllowed() { + composite("c").add(new IntClass(10)); + } + + @Test(expected = RuntimeException.class) + public void testRemoveNotAllowed() { + composite("c").remove(new Id("xxx")); + } + + @Test + public void testAddConfig() { + CompositeConfig composite = composite("c"); + Config c1 = new ConfigImpl(id("c1")) { + @Override + public Identifiable wrap(String aPrefix, Identifiable aT) { + return aT; + } + }; + Config c2 = new ConfigImpl(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); + + c1.add(i1); + c1.add(i2); + c2.add(i3); + c2.add(i4); + + composite.addConfig(c1); + List values = composite.values(); + assertEquals(2, values.size()); + assertTrue(values.contains(i1)); + assertTrue(values.contains(i2)); + + 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)); + } + + @Test(expected = DuplicateException.class) + public void testDuplicatesNotAllowed() { + CompositeConfig composite = composite("c"); + Config c1 = new ConfigImpl(id("c1")) { + @Override + public Identifiable wrap(String aPrefix, Identifiable aT) { + return aT; + } + }; + Config c2 = new ConfigImpl(id("c1")) { + @Override + public Identifiable wrap(String aPrefix, Identifiable aT) { + return aT; + } + }; + composite.addConfig(c1); + composite.addConfig(c2); + } + + @Test + public void testDuplicateItem() { + CompositeConfig composite = composite("c"); + Config c1 = new ConfigImpl(id("c1")) { + @Override + public Identifiable wrap(String aPrefix, Identifiable aT) { + return aT; + } + }; + Config c2 = new ConfigImpl(id("c2")) { + @Override + public Identifiable wrap(String aPrefix, Identifiable aT) { + return aT; + } + }; + + IntClass i1 = new IntClass(10); + IntClass i2 = new IntClass(10); + c1.add(i1); + c2.add(i2); + composite.addConfig(c1); + try { + composite.addConfig(c2); + fail(); + } catch (DuplicateException e) { + // ok. + } + assertEquals(1, composite.values().size()); + assertTrue(composite.values().contains(i1)); + } + + private CompositeConfig composite(String aId) { + return new CompositeConfig(id(aId)); + } + + private Id id(String aId) { + return new Id(aId); + } +} 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 781a53a..044cb1f 100644 --- a/impl/src/test/java/org/wamblee/xmlrouter/impl/ConfigImplTest.java +++ b/impl/src/test/java/org/wamblee/xmlrouter/impl/ConfigImplTest.java @@ -30,7 +30,7 @@ public class ConfigImplTest { private static final String CONFIG_TYPE = "transformation"; - private static interface MyType extends Identifiable { + private static interface MyType extends Identifiable { }