From f8027d76e1c3e517a8b80a3476f51adee845fc5b Mon Sep 17 00:00:00 2001 From: Erik Brakkee Date: Tue, 9 Aug 2011 00:09:24 +0200 Subject: [PATCH] equality based on the ids of the contents of SingleRouterConfig. --- .../org/wamblee/xmlrouter/config/Config.java | 4 +- .../wamblee/xmlrouter/impl/ConfigImpl.java | 48 +++++++++----- .../xmlrouter/impl/RobustIdentifiable.java | 2 +- .../xmlrouter/impl/SingleRouterConfig.java | 23 ++++++- .../xmlrouter/impl/ConfigImplTest.java | 51 +++++++++++++-- .../impl/SingleRouterConfigTest.java | 63 ++++++++++++++++++- .../impl/XMLRouterConfigServiceTest.java | 19 +++--- .../wamblee/xmlrouter/impl/XMLRouterTest.java | 4 +- 8 files changed, 176 insertions(+), 38 deletions(-) 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 17c0c41..baa4c8b 100644 --- a/config/src/main/java/org/wamblee/xmlrouter/config/Config.java +++ b/config/src/main/java/org/wamblee/xmlrouter/config/Config.java @@ -15,7 +15,7 @@ */ package org.wamblee.xmlrouter.config; -import java.util.List; +import java.util.Collection; import org.wamblee.xmlrouter.common.Id; @@ -51,5 +51,5 @@ public interface Config extends Identifiable { /** * @return All available items. */ - List values(); + Collection values(); } \ No newline at end of file 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 980bd07..a1733ab 100644 --- a/impl/src/main/java/org/wamblee/xmlrouter/impl/ConfigImpl.java +++ b/impl/src/main/java/org/wamblee/xmlrouter/impl/ConfigImpl.java @@ -17,13 +17,14 @@ package org.wamblee.xmlrouter.impl; import static org.wamblee.xmlrouter.impl.MessageUtil.*; -import java.util.ArrayList; +import java.util.Collection; import java.util.Collections; -import java.util.Iterator; -import java.util.List; +import java.util.HashMap; +import java.util.Map; import org.wamblee.xmlrouter.common.Id; import org.wamblee.xmlrouter.config.Config; +import org.wamblee.xmlrouter.config.ConfigException; import org.wamblee.xmlrouter.config.Identifiable; /** @@ -37,7 +38,7 @@ public abstract class ConfigImpl> implements ExtendedConfig { private Id id; - private List registered; + private Map, T> registered; /** * Constructs the object. @@ -45,7 +46,7 @@ public abstract class ConfigImpl> implements public ConfigImpl(Id aId) { notNull("id", aId); id = aId; - registered = new ArrayList(); + registered = new HashMap, T>(); } @Override @@ -61,7 +62,10 @@ public abstract class ConfigImpl> implements @Override public synchronized void add(T aT) { notNull("aT", aT); - registered.add(wrap(id.getId() + ".", aT)); + if (registered.containsKey(aT.getId())) { + throw new ConfigException("Duplicate id '" + aT.getId() + "'"); + } + registered.put(aT.getId(), wrap(id.getId() + ".", aT)); } /** @@ -83,19 +87,33 @@ public abstract class ConfigImpl> implements @Override public synchronized boolean remove(Id aId) { notNull("aId", aId); - Iterator i = registered.iterator(); - while (i.hasNext()) { - T t = i.next(); - if (t.getId().equals(aId)) { - i.remove(); - return true; - } + T value = registered.get(aId); + if (value != null) { + registered.remove(aId); + return true; } return false; } @Override - public List values() { - return Collections.unmodifiableList(registered); + public Collection values() { + return Collections.unmodifiableCollection(registered.values()); + } + + @Override + public boolean equals(Object aObj) { + if (aObj == null) { + return false; + } + if (!(aObj instanceof ConfigImpl)) { + return false; + } + ConfigImpl obj = (ConfigImpl) aObj; + return registered.keySet().equals(obj.registered.keySet()); + } + + @Override + public int hashCode() { + return registered.keySet().hashCode(); } } 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 4e54fe3..47b0000 100644 --- a/impl/src/main/java/org/wamblee/xmlrouter/impl/RobustIdentifiable.java +++ b/impl/src/main/java/org/wamblee/xmlrouter/impl/RobustIdentifiable.java @@ -55,7 +55,7 @@ public class RobustIdentifiable implements Identifiable { private void throwConfigException(String aMsg, Exception aException) { LOGGER.log(Level.WARNING, aMsg, aException); - throw new ConfigException("id is null"); + throw new ConfigException(aMsg); } @Override 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 40cbcc5..8eab34b 100644 --- a/impl/src/main/java/org/wamblee/xmlrouter/impl/SingleRouterConfig.java +++ b/impl/src/main/java/org/wamblee/xmlrouter/impl/SingleRouterConfig.java @@ -22,7 +22,6 @@ import org.wamblee.xmlrouter.config.Filter; import org.wamblee.xmlrouter.config.RouterConfig; import org.wamblee.xmlrouter.config.Transformation; -// TODO implement equality based on ids for the single routerconfig. // TODO implement copying of routerconfig. /** @@ -33,6 +32,7 @@ import org.wamblee.xmlrouter.config.Transformation; */ public class SingleRouterConfig implements ExtendedRouterConfig { private Id id; + private ExtendedConfig documentTypes; private ExtendedConfig transformations; private ExtendedConfig filters; @@ -88,4 +88,25 @@ public class SingleRouterConfig implements ExtendedRouterConfig { public Config filterConfig() { return filters; } + + @Override + public boolean equals(Object aObj) { + if (aObj == null) { + return false; + } + if (!(aObj instanceof SingleRouterConfig)) { + return false; + } + SingleRouterConfig obj = (SingleRouterConfig) aObj; + + return documentTypes.equals(obj.documentTypes) && + transformations.equals(obj.transformations) && + filters.equals(obj.filters); + } + + @Override + public int hashCode() { + return documentTypes.hashCode() + transformations.hashCode() + + filters.hashCode(); + } } 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 044cb1f..8ebdb9f 100644 --- a/impl/src/test/java/org/wamblee/xmlrouter/impl/ConfigImplTest.java +++ b/impl/src/test/java/org/wamblee/xmlrouter/impl/ConfigImplTest.java @@ -75,10 +75,12 @@ public class ConfigImplTest { config.add(type1); assertEquals(1, config.values().size()); - assertTrue(config.values().get(0) instanceof MyTypeWrapper); - assertSame(type1, ((MyTypeWrapper) config.values().get(0)).getType()); - assertEquals(CONFIG_TYPE + "." + type1.getId().getId(), config.values() - .get(0).getId().getId()); + MyType firstValue = config.values().iterator().next(); + + assertTrue(firstValue instanceof MyTypeWrapper); + assertSame(type1, ((MyTypeWrapper) firstValue).getType()); + assertEquals(CONFIG_TYPE + "." + type1.getId().getId(), firstValue + .getId().getId()); // add another one. MyType type2 = mock(MyType.class); @@ -97,7 +99,7 @@ public class ConfigImplTest { assertEquals(1, config.values().size()); - assertTrue(config.remove(new Id(CONFIG_TYPE + "." + "type1"))); + assertTrue(config.remove(new Id("type1"))); assertTrue(config.values().isEmpty()); } @@ -105,4 +107,43 @@ public class ConfigImplTest { public void testUnmodifiable() { config.values().add(mock(MyType.class)); } + + @Test + public void testEquals() { + + Config config1 = new ConfigImpl(new Id( + CONFIG_TYPE)) { + @Override + public MyType wrap(String aPrefix, MyType aT) { + return new MyTypeWrapper(aPrefix, aT); + } + }; + assertFalse(config1.equals(null)); + assertFalse(config1.equals("hello")); + Config config2 = new ConfigImpl(new Id( + CONFIG_TYPE)) { + @Override + public MyType wrap(String aPrefix, MyType aT) { + return new MyTypeWrapper(aPrefix, aT); + } + }; + assertEquals(config1, config2); + assertEquals(config1.hashCode(), config2.hashCode()); + + MyType type1 = mock(MyType.class); + when(type1.getId()).thenReturn(new Id("type1")); + + config1.add(type1); + assertFalse(config1.equals(config2)); + + MyType type2 = mock(MyType.class); + when(type2.getId()).thenReturn(new Id("type1")); + + config2.add(type2); + assertEquals(config1, config2); + assertEquals(config1.hashCode(), config2.hashCode()); + + assertTrue(config2.remove(type2.getId())); + assertFalse(config1.equals(config2)); + } } 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 6ec16b1..3df4293 100644 --- a/impl/src/test/java/org/wamblee/xmlrouter/impl/SingleRouterConfigTest.java +++ b/impl/src/test/java/org/wamblee/xmlrouter/impl/SingleRouterConfigTest.java @@ -47,7 +47,9 @@ public class SingleRouterConfigTest { config.documentTypeConfig().add(type2); assertEquals(2, config.documentTypeConfig().values().size()); - assertTrue(config.documentTypeConfig().values().get(0) instanceof RobustDocumentType); + Object firstValue = config.documentTypeConfig().values().iterator() + .next(); + assertTrue(firstValue instanceof RobustDocumentType); } @Test @@ -62,7 +64,9 @@ public class SingleRouterConfigTest { config.transformationConfig().add(transformation2); assertEquals(2, config.transformationConfig().values().size()); - assertTrue(config.transformationConfig().values().get(0) instanceof RobustTransformation); + Object firstValue = config.transformationConfig().values().iterator() + .next(); + assertTrue(firstValue instanceof RobustTransformation); } @Test @@ -77,6 +81,59 @@ public class SingleRouterConfigTest { config.filterConfig().add(filter2); assertEquals(2, config.filterConfig().values().size()); - assertTrue(config.filterConfig().values().get(0) instanceof RobustFilter); + Object firstValue = config.filterConfig().values().iterator().next(); + assertTrue(firstValue instanceof RobustFilter); + } + + @Test + public void testEqualityEmptyConfig() { + RouterConfig config1 = new SingleRouterConfig(new Id( + "routerconfig")); + RouterConfig config2 = new SingleRouterConfig(new Id( + "routerconfig")); + assertEquals(config1, config2); + } + + @Test + public void testEqualsNonEmpty() { + RouterConfig config1 = new SingleRouterConfig(new Id( + "routerconfig")); + + assertFalse(config1.equals(null)); + assertFalse(config.equals("hello")); + + RouterConfig config2 = new SingleRouterConfig(new Id( + "routerconfig")); + DocumentType type1 = mock(DocumentType.class); + when(type1.getId()).thenReturn(new Id("type1")); + DocumentType type2 = mock(DocumentType.class); + when(type2.getId()).thenReturn(new Id("type1")); + Filter filter1 = mock(Filter.class); + when(filter1.getId()).thenReturn(new Id("f1")); + Filter filter2 = mock(Filter.class); + when(filter2.getId()).thenReturn(new Id("f1")); + Transformation transformation1 = mock(Transformation.class); + when(transformation1.getId()).thenReturn(new Id("t1")); + Transformation transformation2 = mock(Transformation.class); + when(transformation2.getId()).thenReturn(new Id("t1")); + + config1.documentTypeConfig().add(type1); + assertFalse(config1.equals(config2)); + + config2.documentTypeConfig().add(type2); + assertEquals(config1, config2); + assertEquals(config1.hashCode(), config2.hashCode()); + + config1.transformationConfig().add(transformation1); + config2.transformationConfig().add(transformation2); + + assertEquals(config1, config2); + assertEquals(config1.hashCode(), config2.hashCode()); + + config1.filterConfig().add(filter1); + config2.filterConfig().add(filter2); + + assertEquals(config1, config2); + assertEquals(config1.hashCode(), config2.hashCode()); } } diff --git a/impl/src/test/java/org/wamblee/xmlrouter/impl/XMLRouterConfigServiceTest.java b/impl/src/test/java/org/wamblee/xmlrouter/impl/XMLRouterConfigServiceTest.java index 4086fa3..e56b5c0 100644 --- a/impl/src/test/java/org/wamblee/xmlrouter/impl/XMLRouterConfigServiceTest.java +++ b/impl/src/test/java/org/wamblee/xmlrouter/impl/XMLRouterConfigServiceTest.java @@ -18,7 +18,7 @@ package org.wamblee.xmlrouter.impl; import static junit.framework.Assert.*; import static org.mockito.Mockito.*; -import java.util.List; +import java.util.Collection; import org.junit.Before; import org.junit.Test; @@ -56,21 +56,22 @@ public class XMLRouterConfigServiceTest { verify(config).setRouterConfig(captor.capture()); RouterConfig received = captor.getValue(); - List docTypes = received.documentTypeConfig().values(); + Collection docTypes = received.documentTypeConfig() + .values(); assertEquals(1, docTypes.size()); assertEquals("myapp.id.documenttypes.doctype" + aSuffix, docTypes - .get(0).getId().toString()); + .iterator().next().getId().toString()); - List transformations = received.transformationConfig() - .values(); + Collection transformations = received + .transformationConfig().values(); assertEquals(1, transformations.size()); assertEquals("myapp.id.transformations.t1" + aSuffix, transformations - .get(0).getId().toString()); + .iterator().next().getId().toString()); - List filters = received.filterConfig().values(); + Collection filters = received.filterConfig().values(); assertEquals(1, filters.size()); - assertEquals("myapp.id.filters.f1" + aSuffix, filters.get(0).getId() - .toString()); + assertEquals("myapp.id.filters.f1" + aSuffix, filters.iterator().next() + .getId().toString()); } private RouterConfig createRouterConfig(String routerConfigId, 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 ae40327..c1776f4 100644 --- a/impl/src/test/java/org/wamblee/xmlrouter/impl/XMLRouterTest.java +++ b/impl/src/test/java/org/wamblee/xmlrouter/impl/XMLRouterTest.java @@ -341,8 +341,8 @@ public class XMLRouterTest { private Transformation createTransformation(String aFrom, String aTo, DOMSource aSource, DOMSource aTarget) { Transformation transformation = mock(Transformation.class); - when(transformation.getId()) - .thenReturn(new Id("trans")); + when(transformation.getId()).thenReturn( + new Id(UUID.randomUUID().toString())); when(transformation.getFromType()).thenReturn(aFrom); when(transformation.getToType()).thenReturn(aTo); when(transformation.transform(same(aSource))).thenReturn(aTarget); -- 2.31.1