From e52385618670b54a5c6a4f2fbfab381bef43a905 Mon Sep 17 00:00:00 2001 From: Erik Brakkee Date: Mon, 1 Aug 2011 22:14:40 +0200 Subject: [PATCH] First version after introduction of meaningful ids and Identifiable interface. Now more testing. --- .../java/org/wamblee/xmlrouter/common/Id.java | 17 ++-- .../org/wamblee/xmlrouter/common/IdTest.java | 17 ++-- .../org/wamblee/xmlrouter/config/Config.java | 13 ++- .../xmlrouter/config/DocumentType.java | 2 +- .../org/wamblee/xmlrouter/config/Filter.java | 2 +- .../xmlrouter/config/Identifiable.java | 39 +++++++++ .../xmlrouter/config/RouterConfig.java | 2 +- .../xmlrouter/config/RouterConfigService.java | 2 +- .../xmlrouter/config/Transformation.java | 7 +- .../xmlrouter/impl/CompositeConfig.java | 36 +++++---- .../xmlrouter/impl/CompositeRouterConfig.java | 22 ++++- .../wamblee/xmlrouter/impl/ConfigImpl.java | 56 +++++++------ .../org/wamblee/xmlrouter/impl/Constants.java | 2 +- .../xmlrouter/impl/RobustDocumentType.java | 17 ++-- .../wamblee/xmlrouter/impl/RobustFilter.java | 10 +-- .../xmlrouter/impl/RobustIdentifiable.java | 80 +++++++++++++++++++ .../xmlrouter/impl/RobustTransformation.java | 29 ++----- .../xmlrouter/impl/SingleRouterConfig.java | 36 +++++---- .../xmlrouter/impl/TransformationPaths.java | 13 +-- .../org/wamblee/xmlrouter/impl/XMLRouter.java | 16 ++-- .../impl/XMLRouterConfigService.java | 17 ++-- .../impl/XMLRouterConfigurationImpl.java | 8 +- .../xmlrouter/impl/ConfigImplTest.java | 50 +++++++----- .../xmlrouter/impl/RobustDestinationTest.java | 2 +- .../impl/RobustDocumentTypeTest.java | 3 +- .../xmlrouter/impl/RobustFilterTest.java | 3 +- .../impl/RobustTransformationTest.java | 4 +- .../impl/SingleRouterConfigTest.java | 44 +++++----- .../impl/TransformationPathsTest.java | 20 +++-- .../xmlrouter/impl/XMLRouterFunctionTest.java | 6 +- .../wamblee/xmlrouter/impl/XMLRouterTest.java | 41 +++++----- .../listener/CompositeEventListener.java | 6 +- .../xmlrouter/listener/EventListener.java | 4 +- .../listener/LoggingEventListener.java | 12 ++- .../listener/CompositeEventListenerTest.java | 10 +-- 35 files changed, 388 insertions(+), 260 deletions(-) create mode 100644 config/src/main/java/org/wamblee/xmlrouter/config/Identifiable.java create mode 100644 impl/src/main/java/org/wamblee/xmlrouter/impl/RobustIdentifiable.java diff --git a/common/src/main/java/org/wamblee/xmlrouter/common/Id.java b/common/src/main/java/org/wamblee/xmlrouter/common/Id.java index f057b41..1d31c8b 100644 --- a/common/src/main/java/org/wamblee/xmlrouter/common/Id.java +++ b/common/src/main/java/org/wamblee/xmlrouter/common/Id.java @@ -25,28 +25,33 @@ package org.wamblee.xmlrouter.common; */ public class Id implements Comparable> { - private long id; + private String id; /** * Constructs the id. * * @param aId * Integer id. + * @throws NullPointerException + * in case the id is null. */ - public Id(long aId) { + public Id(String aId) { + if (aId == null) { + throw new NullPointerException("id is null"); + } id = aId; } /** * @return The underlying id. */ - public long getId() { + public String getId() { return id; } @Override public int hashCode() { - return ((Long) id).hashCode(); + return id.hashCode(); } @Override @@ -62,11 +67,11 @@ public class Id implements Comparable> { @Override public String toString() { - return id + ""; + return id; } @Override public int compareTo(Id aId) { - return ((Long) id).compareTo((Long) aId.getId()); + return id.compareTo(aId.getId()); } } diff --git a/common/src/test/java/org/wamblee/xmlrouter/common/IdTest.java b/common/src/test/java/org/wamblee/xmlrouter/common/IdTest.java index 183d885..5f8f444 100644 --- a/common/src/test/java/org/wamblee/xmlrouter/common/IdTest.java +++ b/common/src/test/java/org/wamblee/xmlrouter/common/IdTest.java @@ -23,23 +23,28 @@ public class IdTest { @Test public void testGetSet() { - Id id = new Id(100L); - assertEquals(100L, id.getId()); + Id id = new Id("hello"); + assertEquals("hello", id.getId()); } @Test public void testEqualsHashCodeCompare() { - Id id1 = new Id(100L); - Id id2 = new Id(200L); - Id id3 = new Id(100L); + Id id1 = new Id("a"); + Id id2 = new Id("b"); + Id id3 = new Id("a"); assertEquals(id1, id3); assertFalse(id1.equals(id2)); assertFalse(id1.equals(null)); - assertFalse(id1.equals("hello")); + assertFalse(id1.equals("a")); assertEquals(id1.hashCode(), id3.hashCode()); assertTrue(id1.compareTo(id2) < 0); assertTrue(id2.compareTo(id1) > 0); assertEquals(0, id1.compareTo(id3)); } + + @Test(expected = NullPointerException.class) + public void testNullNotAccepted() { + Id id = new Id(null); + } } 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 263b0b6..39ecfae 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.Map; +import java.util.List; import org.wamblee.xmlrouter.common.Id; @@ -28,16 +28,15 @@ import org.wamblee.xmlrouter.common.Id; * @param * Type for which ids are generated. */ -public interface Config { +public interface Config extends Identifiable { /** - * Adds a item + * Adds an item. No item with the same id may exist. * * @param aT * item - * @return Unique id. */ - Id add(T aT); + void add(T aT); /** * Removes the item with a given id. @@ -49,7 +48,7 @@ public interface Config { boolean remove(Id aId); /** - * @return All available ids. + * @return All available items. */ - Map, T> map(); + List values(); } \ No newline at end of file diff --git a/config/src/main/java/org/wamblee/xmlrouter/config/DocumentType.java b/config/src/main/java/org/wamblee/xmlrouter/config/DocumentType.java index 4ffc5f8..d4d12ef 100644 --- a/config/src/main/java/org/wamblee/xmlrouter/config/DocumentType.java +++ b/config/src/main/java/org/wamblee/xmlrouter/config/DocumentType.java @@ -24,7 +24,7 @@ import javax.xml.transform.dom.DOMSource; * @author Erik Brakkee * */ -public interface DocumentType { +public interface DocumentType extends Identifiable { /** * Symbolic name for the document type. diff --git a/config/src/main/java/org/wamblee/xmlrouter/config/Filter.java b/config/src/main/java/org/wamblee/xmlrouter/config/Filter.java index 212c65f..7e2ea6c 100644 --- a/config/src/main/java/org/wamblee/xmlrouter/config/Filter.java +++ b/config/src/main/java/org/wamblee/xmlrouter/config/Filter.java @@ -24,7 +24,7 @@ import javax.xml.transform.dom.DOMSource; * @author Erik Brakkee * */ -public interface Filter { +public interface Filter extends Identifiable { /** * Determines if a given document will be processed or not. diff --git a/config/src/main/java/org/wamblee/xmlrouter/config/Identifiable.java b/config/src/main/java/org/wamblee/xmlrouter/config/Identifiable.java new file mode 100644 index 0000000..dc1f993 --- /dev/null +++ b/config/src/main/java/org/wamblee/xmlrouter/config/Identifiable.java @@ -0,0 +1,39 @@ +/* + * 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; + +import org.wamblee.xmlrouter.common.Id; + +/** + * Classes implementing this interface must provide a unique id to identify + * their contents and behavior. If the content or behavior of an object changes, + * then a different id should be returned. Conversely, it is required for the id + * to keep as constant as possible. Also, a given object should return the same + * id throughout its lifecycle (from the moment it's {@link #getId()} method is + * called until the moment it can no longer be referenced. + * + * @author Erik Brakkee + * + */ +public interface Identifiable { + + /** + * Returns the unique id for the object. + * + * @return Unique id. + */ + Id getId(); +} diff --git a/config/src/main/java/org/wamblee/xmlrouter/config/RouterConfig.java b/config/src/main/java/org/wamblee/xmlrouter/config/RouterConfig.java index 57ddae7..1462250 100644 --- a/config/src/main/java/org/wamblee/xmlrouter/config/RouterConfig.java +++ b/config/src/main/java/org/wamblee/xmlrouter/config/RouterConfig.java @@ -20,7 +20,7 @@ package org.wamblee.xmlrouter.config; * * @author Erik Brakkee */ -public interface RouterConfig { +public interface RouterConfig extends Identifiable { /** * @return Document types. diff --git a/config/src/main/java/org/wamblee/xmlrouter/config/RouterConfigService.java b/config/src/main/java/org/wamblee/xmlrouter/config/RouterConfigService.java index baefd94..5fc7f2e 100644 --- a/config/src/main/java/org/wamblee/xmlrouter/config/RouterConfigService.java +++ b/config/src/main/java/org/wamblee/xmlrouter/config/RouterConfigService.java @@ -40,7 +40,7 @@ public interface RouterConfigService { * configuration. * @return Id of the applied configuration. */ - Id apply(RouterConfig aConfig, Id aOldConfig); + void apply(RouterConfig aConfig, Id aOldConfig); /** * Clears the configuration for a given id. diff --git a/config/src/main/java/org/wamblee/xmlrouter/config/Transformation.java b/config/src/main/java/org/wamblee/xmlrouter/config/Transformation.java index 9f20084..9867392 100644 --- a/config/src/main/java/org/wamblee/xmlrouter/config/Transformation.java +++ b/config/src/main/java/org/wamblee/xmlrouter/config/Transformation.java @@ -23,12 +23,7 @@ import javax.xml.transform.dom.DOMSource; * @author Erik Brakkee * */ -public interface Transformation { - - /** - * @return Name for the transformation. - */ - String getName(); +public interface Transformation extends Identifiable { /** * From type that can be transformed. 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 b378052..5166315 100644 --- a/impl/src/main/java/org/wamblee/xmlrouter/impl/CompositeConfig.java +++ b/impl/src/main/java/org/wamblee/xmlrouter/impl/CompositeConfig.java @@ -15,28 +15,37 @@ */ package org.wamblee.xmlrouter.impl; -import java.util.LinkedHashMap; -import java.util.Map; +import java.util.ArrayList; +import java.util.List; import org.wamblee.xmlrouter.common.Id; import org.wamblee.xmlrouter.config.Config; public class CompositeConfig implements ExtendedConfig { - private Map, T> configs; + private Id id; + private List configs; - public CompositeConfig() { - configs = new LinkedHashMap, T>(); + public CompositeConfig(Id aId) { + id = aId; + configs = new ArrayList(); + } + + @Override + public Id getId() { + // TODO test id. + return id; } public void add(Config aConfig) { - for (Id id : aConfig.map().keySet()) { - configs.put(id, aConfig.map().get(id)); + // TODO check duplicate config. + for (T item : aConfig.values()) { + configs.add(item); } } @Override - public Map, T> map() { + public List values() { return configs; } @@ -46,13 +55,12 @@ public class CompositeConfig implements ExtendedConfig { return false; } - private void notSupported() { - throw new RuntimeException("readonly instance"); - } - @Override - public Id add(T aT) { + public void add(T aT) { notSupported(); - return null; + } + + private void notSupported() { + throw new RuntimeException("readonly 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 5ba6571..2b40e58 100644 --- a/impl/src/main/java/org/wamblee/xmlrouter/impl/CompositeRouterConfig.java +++ b/impl/src/main/java/org/wamblee/xmlrouter/impl/CompositeRouterConfig.java @@ -17,22 +17,29 @@ package org.wamblee.xmlrouter.impl; import java.util.Collection; +import org.wamblee.xmlrouter.common.Id; import org.wamblee.xmlrouter.config.Config; import org.wamblee.xmlrouter.config.DocumentType; import org.wamblee.xmlrouter.config.Filter; import org.wamblee.xmlrouter.config.RouterConfig; import org.wamblee.xmlrouter.config.Transformation; +// TODO test this class. public class CompositeRouterConfig implements ExtendedRouterConfig { + private Id id; private CompositeConfig documentTypes; private CompositeConfig transformations; private CompositeConfig filters; - public CompositeRouterConfig(Collection aConfigs) { - documentTypes = new CompositeConfig(); - transformations = new CompositeConfig(); - filters = new CompositeConfig(); + public CompositeRouterConfig(Id aId, + Collection aConfigs) { + id = aId; + documentTypes = new CompositeConfig(new Id( + "documentTypes")); + transformations = new CompositeConfig(new Id( + "transformations")); + filters = new CompositeConfig(new Id("filters")); for (RouterConfig config : aConfigs) { documentTypes.add(config.documentTypeConfig()); transformations.add(config.transformationConfig()); @@ -40,6 +47,13 @@ public class CompositeRouterConfig implements ExtendedRouterConfig { } } + // TODO test id. + + @Override + public Id getId() { + return id; + } + @Override public Config documentTypeConfig() { return documentTypes; 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 e29725e..c561e36 100644 --- a/impl/src/main/java/org/wamblee/xmlrouter/impl/ConfigImpl.java +++ b/impl/src/main/java/org/wamblee/xmlrouter/impl/ConfigImpl.java @@ -15,13 +15,14 @@ */ package org.wamblee.xmlrouter.impl; +import java.util.ArrayList; import java.util.Collections; -import java.util.LinkedHashMap; -import java.util.Map; -import java.util.concurrent.atomic.AtomicLong; +import java.util.Iterator; +import java.util.List; import org.wamblee.xmlrouter.common.Id; import org.wamblee.xmlrouter.config.Config; +import org.wamblee.xmlrouter.config.Identifiable; /** * Default implementation of the {@link Config} interface. @@ -30,19 +31,24 @@ import org.wamblee.xmlrouter.config.Config; * * @param */ -public abstract class ConfigImpl implements ExtendedConfig { +public abstract class ConfigImpl implements + ExtendedConfig { - private boolean dirty; - private AtomicLong next; - private Map, T> registered; + private Id id; + private List registered; /** * Constructs the object. */ - public ConfigImpl(AtomicLong aNext) { - dirty = false; - next = aNext; - registered = new LinkedHashMap, T>(); + public ConfigImpl(Id aId) { + // TODO test for null. + id = aId; + registered = new ArrayList(); + } + + @Override + public Id getId() { + return id; } /* @@ -51,25 +57,20 @@ public abstract class ConfigImpl implements ExtendedConfig { * @see org.wamblee.xmlrouter.config.Config#add(T) */ @Override - public synchronized Id add(T aT) { + public synchronized void add(T aT) { + // TODO test duplicate ids. notNull(aT); - long seqno = next.incrementAndGet(); - Id id = new Id(seqno); - registered.put(id, wrap(id, aT)); - dirty = true; - return id; + registered.add(wrap(aT)); } /** * This is called to wrap the given object by a safer version. * - * @param aId - * Id. * @param aT * Object to wrap. * @return Wrapped object. */ - public abstract T wrap(Id aId, T aT); + public abstract T wrap(T aT); /* * (non-Javadoc) @@ -81,13 +82,20 @@ public abstract class ConfigImpl implements ExtendedConfig { @Override public synchronized boolean remove(Id aId) { notNull(aId); - dirty = true; - return registered.remove(aId) != null; + Iterator i = registered.iterator(); + while (i.hasNext()) { + T t = i.next(); + if (t.getId().equals(aId)) { + i.remove(); + return true; + } + } + return false; } @Override - public Map, T> map() { - return Collections.unmodifiableMap(registered); + public List values() { + return Collections.unmodifiableList(registered); } private void notNull(T aT) { diff --git a/impl/src/main/java/org/wamblee/xmlrouter/impl/Constants.java b/impl/src/main/java/org/wamblee/xmlrouter/impl/Constants.java index c51c115..3b181a8 100644 --- a/impl/src/main/java/org/wamblee/xmlrouter/impl/Constants.java +++ b/impl/src/main/java/org/wamblee/xmlrouter/impl/Constants.java @@ -22,5 +22,5 @@ package org.wamblee.xmlrouter.impl; * */ public enum Constants { - UNKNOWN_DOCUMENT_TYPE, UNKNOWN_DESTINATION_NAME, UNKNOWN_NAME + UNKNOWN_DOCUMENT_TYPE, UNKNOWN_DESTINATION_NAME, UNKNOWN_NAME, UNKNOWN_ID } diff --git a/impl/src/main/java/org/wamblee/xmlrouter/impl/RobustDocumentType.java b/impl/src/main/java/org/wamblee/xmlrouter/impl/RobustDocumentType.java index 34664e8..5224483 100644 --- a/impl/src/main/java/org/wamblee/xmlrouter/impl/RobustDocumentType.java +++ b/impl/src/main/java/org/wamblee/xmlrouter/impl/RobustDocumentType.java @@ -20,7 +20,6 @@ import java.util.logging.Logger; import javax.xml.transform.dom.DOMSource; -import org.wamblee.xmlrouter.common.Id; import org.wamblee.xmlrouter.config.DocumentType; /** @@ -30,12 +29,12 @@ import org.wamblee.xmlrouter.config.DocumentType; * @author Erik Brakkee * */ -public class RobustDocumentType implements DocumentType { +public class RobustDocumentType extends RobustIdentifiable + implements DocumentType { private static final Logger LOGGER = Logger .getLogger(RobustDocumentType.class.getName()); - private Id id; private DocumentType type; /** @@ -46,8 +45,8 @@ public class RobustDocumentType implements DocumentType { * @param aType * Document type to wrap. */ - public RobustDocumentType(Id aId, DocumentType aType) { - id = aId; + public RobustDocumentType(DocumentType aType) { + super(aType); type = aType; } @@ -56,13 +55,13 @@ public class RobustDocumentType implements DocumentType { try { String name = type.getName(); if (name == null) { - LOGGER.log(Level.WARNING, "Document type " + id + + LOGGER.log(Level.WARNING, "Document type " + getId() + " returned null for name"); return Constants.UNKNOWN_DOCUMENT_TYPE.toString(); } return name; } catch (Exception e) { - LOGGER.log(Level.WARNING, "Document type " + id + + LOGGER.log(Level.WARNING, "Document type " + getId() + " threw exception", e); return Constants.UNKNOWN_DOCUMENT_TYPE.toString(); } @@ -73,7 +72,7 @@ public class RobustDocumentType implements DocumentType { try { return type.isInstance(aSource); } catch (Exception e) { - LOGGER.log(Level.WARNING, "Document type " + id + + LOGGER.log(Level.WARNING, "Document type " + getId() + " threw exception", e); return false; } @@ -84,7 +83,7 @@ public class RobustDocumentType implements DocumentType { try { return type.validate(aSource); } catch (Exception e) { - LOGGER.log(Level.WARNING, "Document type " + id + + LOGGER.log(Level.WARNING, "Document type " + getId() + " threw exception", e); return false; } diff --git a/impl/src/main/java/org/wamblee/xmlrouter/impl/RobustFilter.java b/impl/src/main/java/org/wamblee/xmlrouter/impl/RobustFilter.java index 4bf4cf2..056690a 100644 --- a/impl/src/main/java/org/wamblee/xmlrouter/impl/RobustFilter.java +++ b/impl/src/main/java/org/wamblee/xmlrouter/impl/RobustFilter.java @@ -20,7 +20,6 @@ import java.util.logging.Logger; import javax.xml.transform.dom.DOMSource; -import org.wamblee.xmlrouter.common.Id; import org.wamblee.xmlrouter.config.Filter; /** @@ -29,12 +28,11 @@ import org.wamblee.xmlrouter.config.Filter; * @author Erik Brakkee * */ -public class RobustFilter implements Filter { +public class RobustFilter extends RobustIdentifiable implements Filter { private static final Logger LOGGER = Logger.getLogger(RobustFilter.class .getName()); - private Id id; private Filter filter; /** @@ -45,8 +43,8 @@ public class RobustFilter implements Filter { * @param aFilter * Filter to wrap. */ - public RobustFilter(Id aId, Filter aFilter) { - id = aId; + public RobustFilter(Filter aFilter) { + super(aFilter); filter = aFilter; } @@ -55,7 +53,7 @@ public class RobustFilter implements Filter { try { return filter.isAllowed(aDocumentType, aSource); } catch (Exception e) { - LOGGER.log(Level.WARNING, "Filter " + id + + LOGGER.log(Level.WARNING, "Filter " + getId() + " threw exception, assuming filter returns true", e); return true; } diff --git a/impl/src/main/java/org/wamblee/xmlrouter/impl/RobustIdentifiable.java b/impl/src/main/java/org/wamblee/xmlrouter/impl/RobustIdentifiable.java new file mode 100644 index 0000000..f3da5f4 --- /dev/null +++ b/impl/src/main/java/org/wamblee/xmlrouter/impl/RobustIdentifiable.java @@ -0,0 +1,80 @@ +/* + * 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 java.util.logging.Level; +import java.util.logging.Logger; + +import org.wamblee.xmlrouter.common.Id; +import org.wamblee.xmlrouter.config.Identifiable; + +/** + * Robust identifiable provides robustness for identifiable objects. + * + * @author Erik Brakkee + * + * @param + */ +public class RobustIdentifiable implements Identifiable { + private static final Logger LOGGER = Logger + .getLogger(RobustIdentifiable.class.getName()); + + private Id id; + + // TODO test this class. + // TODO test that id is constant even though delegated changes its id. + + public RobustIdentifiable(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()); + throw new RuntimeException( + "Temporary to catch nulls during refactoring"); + } + } catch (Exception e) { + LOGGER + .log(Level.WARNING, "Identifiable getId() threw exception", e); + } + + } + + @Override + public Id getId() { + return id; + } + + // TODO test equals, hashcode. + + @Override + public int hashCode() { + return id.hashCode(); + } + + @Override + public boolean equals(Object aObj) { + if (aObj == null) { + return false; + } + if (!getClass().isInstance(aObj)) { + return false; + } + RobustIdentifiable obj = (RobustIdentifiable) aObj; + return id.equals(obj.getId()); + } +} diff --git a/impl/src/main/java/org/wamblee/xmlrouter/impl/RobustTransformation.java b/impl/src/main/java/org/wamblee/xmlrouter/impl/RobustTransformation.java index 81748e0..2fa025e 100644 --- a/impl/src/main/java/org/wamblee/xmlrouter/impl/RobustTransformation.java +++ b/impl/src/main/java/org/wamblee/xmlrouter/impl/RobustTransformation.java @@ -21,7 +21,6 @@ import java.util.logging.Logger; import javax.xml.transform.dom.DOMSource; import org.wamblee.xml.XMLDocument; -import org.wamblee.xmlrouter.common.Id; import org.wamblee.xmlrouter.config.Transformation; /** @@ -30,12 +29,12 @@ import org.wamblee.xmlrouter.config.Transformation; * @author Erik Brakkee * */ -public class RobustTransformation implements Transformation { +public class RobustTransformation extends RobustIdentifiable + implements Transformation { private static final Logger LOGGER = Logger .getLogger(RobustTransformation.class.getName()); - private Id id; private Transformation transformation; /** @@ -46,27 +45,11 @@ public class RobustTransformation implements Transformation { * @param aTransformation * Wrapped transformation. */ - public RobustTransformation(Id aId, - Transformation aTransformation) { - id = aId; + public RobustTransformation(Transformation aTransformation) { + super(aTransformation); transformation = aTransformation; } - @Override - public String getName() { - try { - String name = transformation.getName(); - if (name == null) { - logTypeReturnedNull("from"); - return Constants.UNKNOWN_DOCUMENT_TYPE.toString(); - } - return name; - } catch (Exception e) { - logNameThrewException(e); - return Constants.UNKNOWN_DOCUMENT_TYPE.toString(); - } - } - @Override public String getFromType() { try { @@ -132,12 +115,12 @@ public class RobustTransformation implements Transformation { private void logTranformationThrewException(DOMSource aEvent, Exception aE) { LOGGER.log(Level.WARNING, - "transformation " + id + " threw exception for event " + + "transformation " + getId() + " threw exception for event " + new XMLDocument(aEvent).print(true), aE); } private void logTransformationReturnedNull(DOMSource aEvent) { - LOGGER.log(Level.WARNING, "transformation " + id + + LOGGER.log(Level.WARNING, "transformation " + getId() + " returned null for event " + new XMLDocument(aEvent).print(true)); } 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 c58ef89..5e2fffe 100644 --- a/impl/src/main/java/org/wamblee/xmlrouter/impl/SingleRouterConfig.java +++ b/impl/src/main/java/org/wamblee/xmlrouter/impl/SingleRouterConfig.java @@ -15,12 +15,11 @@ */ package org.wamblee.xmlrouter.impl; -import java.util.concurrent.atomic.AtomicLong; - import org.wamblee.xmlrouter.common.Id; import org.wamblee.xmlrouter.config.Config; import org.wamblee.xmlrouter.config.DocumentType; import org.wamblee.xmlrouter.config.Filter; +import org.wamblee.xmlrouter.config.RouterConfig; import org.wamblee.xmlrouter.config.Transformation; /** @@ -30,7 +29,7 @@ import org.wamblee.xmlrouter.config.Transformation; * @author Erik Brakkee */ public class SingleRouterConfig implements ExtendedRouterConfig { - private AtomicLong sequenceNumbers; + private Id id; private ExtendedConfig documentTypes; private ExtendedConfig transformations; private ExtendedConfig filters; @@ -41,29 +40,36 @@ public class SingleRouterConfig implements ExtendedRouterConfig { * @param aSequenceNumberGenerator * Sequence number generator to use. */ - public SingleRouterConfig(AtomicLong aSequenceNumberGenerator) { - sequenceNumbers = aSequenceNumberGenerator; - documentTypes = new ConfigImpl(sequenceNumbers) { + public SingleRouterConfig(Id aId) { + id = aId; + documentTypes = new ConfigImpl(new Id( + "documentTypes")) { @Override - public DocumentType wrap(Id aId, DocumentType aT) { - return new RobustDocumentType(aId, aT); + public DocumentType wrap(DocumentType aT) { + return new RobustDocumentType(aT); } }; - transformations = new ConfigImpl(sequenceNumbers) { + transformations = new ConfigImpl(new Id( + "transformations")) { @Override - public Transformation wrap(Id aId, - Transformation aTransformation) { - return new RobustTransformation(aId, aTransformation); + public Transformation wrap(Transformation aTransformation) { + return new RobustTransformation(aTransformation); } }; - filters = new ConfigImpl(sequenceNumbers) { + filters = new ConfigImpl(new Id("filters")) { @Override - public Filter wrap(Id aId, Filter aFilter) { - return new RobustFilter(aId, aFilter); + public Filter wrap(Filter aFilter) { + return new RobustFilter(aFilter); } }; } + // TODO test getId. + @Override + public Id getId() { + return id; + } + @Override public Config documentTypeConfig() { return documentTypes; diff --git a/impl/src/main/java/org/wamblee/xmlrouter/impl/TransformationPaths.java b/impl/src/main/java/org/wamblee/xmlrouter/impl/TransformationPaths.java index caca48c..e6956d3 100644 --- a/impl/src/main/java/org/wamblee/xmlrouter/impl/TransformationPaths.java +++ b/impl/src/main/java/org/wamblee/xmlrouter/impl/TransformationPaths.java @@ -19,10 +19,8 @@ import java.util.ArrayList; import java.util.Collection; import java.util.HashSet; import java.util.List; -import java.util.Map; import java.util.Set; -import org.wamblee.xmlrouter.common.Id; import org.wamblee.xmlrouter.config.Transformation; /** @@ -34,17 +32,14 @@ import org.wamblee.xmlrouter.config.Transformation; */ public class TransformationPaths { - private Map, Transformation> transformations; + private Collection transformations; private List vertices; private TransformationPath[][] matrix; - private Map> sequences; - /** * Construct the transformations. */ - public TransformationPaths( - Map, Transformation> aTransformations) { + public TransformationPaths(Collection aTransformations) { transformations = aTransformations; vertices = new ArrayList(); matrix = new TransformationPath[0][0]; @@ -107,7 +102,7 @@ public class TransformationPaths { // Obtain possible starting points. Set v = new HashSet(); - for (Transformation transformation : transformations.values()) { + for (Transformation transformation : transformations) { v.add(transformation.getFromType()); v.add(transformation.getToType()); } @@ -121,7 +116,7 @@ public class TransformationPaths { for (int i = 0; i < nvertices; i++) { matrix[i][i] = new TransformationPath(); } - for (Transformation transformation : transformations.values()) { + for (Transformation transformation : transformations) { int from = vertices.indexOf(transformation.getFromType()); int to = vertices.indexOf(transformation.getToType()); TransformationPath path = new TransformationPath(transformation); diff --git a/impl/src/main/java/org/wamblee/xmlrouter/impl/XMLRouter.java b/impl/src/main/java/org/wamblee/xmlrouter/impl/XMLRouter.java index a203f93..ea44601 100644 --- a/impl/src/main/java/org/wamblee/xmlrouter/impl/XMLRouter.java +++ b/impl/src/main/java/org/wamblee/xmlrouter/impl/XMLRouter.java @@ -83,7 +83,7 @@ public class XMLRouter implements Gateway, DestinationRegistry { private void publishImpl(String aSource, DOMSource aEvent) { long time = clock.currentTimeMillis(); - Id id = new Id(nextEventId.getAndIncrement()); + Id id = new Id(nextEventId.getAndIncrement() + ""); List types = determineDocumentTypes(aEvent); EventInfo info = new EventInfo(time, aSource, id, types, aEvent); @@ -163,7 +163,7 @@ public class XMLRouter implements Gateway, DestinationRegistry { // allow the event. boolean result = destination.receive(transformed); listener.delivered(aInfo, ts, destinationId.getId(), - destination.getName(), result); + result); delivered = delivered || result; } @@ -189,8 +189,7 @@ public class XMLRouter implements Gateway, DestinationRegistry { private boolean isAllowedByFilters(String aType, DOMSource aEvent) { boolean allowed = true; - for (Filter filter : config.getRouterConfig().filterConfig().map() - .values()) { + for (Filter filter : config.getRouterConfig().filterConfig().values()) { if (!filter.isAllowed(aType, aEvent)) { allowed = false; } @@ -201,7 +200,7 @@ public class XMLRouter implements Gateway, DestinationRegistry { private List determineDocumentTypes(DOMSource aEvent) { List res = new ArrayList(); for (DocumentType type : config.getRouterConfig().documentTypeConfig() - .map().values()) { + .values()) { if (type.isInstance(aEvent)) { res.add(type.getName()); } @@ -209,11 +208,6 @@ public class XMLRouter implements Gateway, DestinationRegistry { return res; } - private void logEvent(String aMessage, String aSource, DOMSource aEvent) { - LOGGER.log(Level.WARNING, - aMessage + ": " + eventToString(aSource, aEvent)); - } - private String eventToString(String aSource, DOMSource aEvent) { return "source '" + aSource + "': Event: '" + new XMLDocument(aEvent).print(true) + "'"; @@ -236,7 +230,7 @@ public class XMLRouter implements Gateway, DestinationRegistry { public Id registerDestination(Destination aDestination) { notNull("destination", aDestination); long seqno = sequenceNumbers.getAndIncrement(); - Id id = new Id(seqno); + Id id = new Id(seqno + ""); destinations.put(id, new RobustDestination(id, aDestination)); return id; } diff --git a/impl/src/main/java/org/wamblee/xmlrouter/impl/XMLRouterConfigService.java b/impl/src/main/java/org/wamblee/xmlrouter/impl/XMLRouterConfigService.java index 230a6dc..054956d 100644 --- a/impl/src/main/java/org/wamblee/xmlrouter/impl/XMLRouterConfigService.java +++ b/impl/src/main/java/org/wamblee/xmlrouter/impl/XMLRouterConfigService.java @@ -15,6 +15,7 @@ */ package org.wamblee.xmlrouter.impl; +import java.util.UUID; import java.util.concurrent.atomic.AtomicLong; import org.wamblee.xmlrouter.common.Id; @@ -37,8 +38,8 @@ public class XMLRouterConfigService implements RouterConfigService { public XMLRouterConfigService(XMLRouterConfiguration aConfig) { sequence = new AtomicLong(1L); config = aConfig; - routerConfigs = new ConfigImpl(sequence) { - public RouterConfig wrap(Id aId, RouterConfig aT) { + routerConfigs = new ConfigImpl(new Id("config")) { + public RouterConfig wrap(RouterConfig aT) { return aT; } }; @@ -46,18 +47,18 @@ public class XMLRouterConfigService implements RouterConfigService { @Override public RouterConfig emptyConfig() { - return new SingleRouterConfig(sequence); + // TODO check and document API impacts. + String id = UUID.randomUUID().toString(); + return new SingleRouterConfig(new Id(id)); } @Override - public Id apply(RouterConfig aConfig, - Id aOldConfig) { + public void apply(RouterConfig aConfig, Id aOldConfig) { if (aOldConfig != null) { routerConfigs.remove(aOldConfig); } - Id id = routerConfigs.add(aConfig); + routerConfigs.add(aConfig); update(); - return id; } @Override @@ -68,7 +69,7 @@ public class XMLRouterConfigService implements RouterConfigService { private void update() { ExtendedRouterConfig newconfig = new CompositeRouterConfig( - routerConfigs.map().values()); + new Id("routerconfig"), routerConfigs.values()); config.setRouterConfig(newconfig); } diff --git a/impl/src/main/java/org/wamblee/xmlrouter/impl/XMLRouterConfigurationImpl.java b/impl/src/main/java/org/wamblee/xmlrouter/impl/XMLRouterConfigurationImpl.java index fe6d507..679e55d 100644 --- a/impl/src/main/java/org/wamblee/xmlrouter/impl/XMLRouterConfigurationImpl.java +++ b/impl/src/main/java/org/wamblee/xmlrouter/impl/XMLRouterConfigurationImpl.java @@ -18,6 +18,7 @@ package org.wamblee.xmlrouter.impl; import java.util.ArrayList; import java.util.concurrent.locks.ReentrantReadWriteLock; +import org.wamblee.xmlrouter.common.Id; import org.wamblee.xmlrouter.config.RouterConfig; /** @@ -39,11 +40,12 @@ public class XMLRouterConfigurationImpl implements XMLRouterConfiguration { public XMLRouterConfigurationImpl(ExtendedRouterConfig aConfig) { config = aConfig; transformations = new TransformationPaths(config.transformationConfig() - .map()); + .values()); } public XMLRouterConfigurationImpl() { - this(new CompositeRouterConfig(new ArrayList())); + this(new CompositeRouterConfig(new Id("routerconfig"), + new ArrayList())); } @Override @@ -65,7 +67,7 @@ public class XMLRouterConfigurationImpl implements XMLRouterConfiguration { public void setRouterConfig(ExtendedRouterConfig aConfig) { TransformationPaths newTransformations = new TransformationPaths( - aConfig.transformationConfig().map()); + aConfig.transformationConfig().values()); wlock.lock(); try { 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 1e8cfc3..5e3ad92 100644 --- a/impl/src/test/java/org/wamblee/xmlrouter/impl/ConfigImplTest.java +++ b/impl/src/test/java/org/wamblee/xmlrouter/impl/ConfigImplTest.java @@ -23,25 +23,30 @@ import java.util.concurrent.atomic.AtomicLong; import org.junit.Before; import org.junit.Test; import org.wamblee.xmlrouter.common.Id; +import org.wamblee.xmlrouter.config.Config; +import org.wamblee.xmlrouter.config.Identifiable; public class ConfigImplTest { - private static interface MyType { + private static interface MyType extends Identifiable { } private static class MyTypeWrapper implements MyType { - private Id id; private MyType type; - public MyTypeWrapper(Id aId, MyType aType) { - id = aId; + public MyTypeWrapper(MyType aType) { type = aType; } public MyType getType() { return type; } + + @Override + public Id getId() { + return type.getId(); + } } private AtomicLong sequence; @@ -50,10 +55,10 @@ public class ConfigImplTest { @Before public void setUp() { sequence = new AtomicLong(1L); - config = new ConfigImpl(sequence) { + config = new ConfigImpl(new Id("mytype")) { @Override - public MyType wrap(Id aId, MyType aT) { - return new MyTypeWrapper(aId, aT); + public MyType wrap(MyType aT) { + return new MyTypeWrapper(aT); } }; } @@ -61,36 +66,37 @@ public class ConfigImplTest { @Test public void testAdd() { MyType type1 = mock(MyType.class); + when(type1.getId()).thenReturn(new Id("type1")); - Id id1 = config.add(type1); + config.add(type1); - assertNotNull(id1); - assertEquals(1, config.map().size()); - assertTrue(config.map().get(id1) instanceof MyTypeWrapper); - assertSame(type1, ((MyTypeWrapper) config.map().get(id1)).getType()); + assertEquals(1, config.values().size()); + assertTrue(config.values().get(0) instanceof MyTypeWrapper); + assertSame(type1, ((MyTypeWrapper) config.values().get(0)).getType()); // add another one. MyType type2 = mock(MyType.class); - Id id2 = config.add(type2); - assertNotNull(id2); - assertEquals(2, config.map().size()); - assertFalse(id1.equals(id2)); + when(type2.getId()).thenReturn(new Id("type2")); + config.add(type2); + + assertEquals(2, config.values().size()); } @Test public void testRemove() { MyType type1 = mock(MyType.class); - Id id1 = config.add(type1); + when(type1.getId()).thenReturn(new Id("type1")); + + config.add(type1); - assertNotNull(id1); - assertEquals(1, config.map().size()); + assertEquals(1, config.values().size()); - config.remove(id1); - assertTrue(config.map().isEmpty()); + assertTrue(config.remove(new Id("type1"))); + assertTrue(config.values().isEmpty()); } @Test(expected = UnsupportedOperationException.class) public void testUnmodifiable() { - config.map().put(new Id(100L), mock(MyType.class)); + config.values().add(mock(MyType.class)); } } diff --git a/impl/src/test/java/org/wamblee/xmlrouter/impl/RobustDestinationTest.java b/impl/src/test/java/org/wamblee/xmlrouter/impl/RobustDestinationTest.java index a42f1c4..b92e488 100644 --- a/impl/src/test/java/org/wamblee/xmlrouter/impl/RobustDestinationTest.java +++ b/impl/src/test/java/org/wamblee/xmlrouter/impl/RobustDestinationTest.java @@ -39,7 +39,7 @@ public class RobustDestinationTest { @Before public void setUp() { destination = mock(Destination.class); - robust = new RobustDestination(new Id(100), destination); + robust = new RobustDestination(new Id("100"), destination); source = mock(DOMSource.class); } 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 57289dc..254826c 100644 --- a/impl/src/test/java/org/wamblee/xmlrouter/impl/RobustDocumentTypeTest.java +++ b/impl/src/test/java/org/wamblee/xmlrouter/impl/RobustDocumentTypeTest.java @@ -23,7 +23,6 @@ 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 { @@ -35,7 +34,7 @@ public class RobustDocumentTypeTest { @Before public void setUp() { documentType = mock(DocumentType.class); - robust = new RobustDocumentType(new Id(10), documentType); + robust = new RobustDocumentType(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 2c7e742..3725112 100644 --- a/impl/src/test/java/org/wamblee/xmlrouter/impl/RobustFilterTest.java +++ b/impl/src/test/java/org/wamblee/xmlrouter/impl/RobustFilterTest.java @@ -23,7 +23,6 @@ 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 { @@ -35,7 +34,7 @@ public class RobustFilterTest { @Before public void setUp() { filter = mock(Filter.class); - robust = new RobustFilter(new Id(10), filter); + robust = new RobustFilter(filter); source = mock(DOMSource.class); } 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 d26e9de..25327cb 100644 --- a/impl/src/test/java/org/wamblee/xmlrouter/impl/RobustTransformationTest.java +++ b/impl/src/test/java/org/wamblee/xmlrouter/impl/RobustTransformationTest.java @@ -23,7 +23,6 @@ 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 { @@ -35,8 +34,7 @@ public class RobustTransformationTest { @Before public void setUp() { transformation = mock(Transformation.class); - robust = new RobustTransformation(new Id(100), - transformation); + robust = new RobustTransformation(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 af34737..280e6da 100644 --- a/impl/src/test/java/org/wamblee/xmlrouter/impl/SingleRouterConfigTest.java +++ b/impl/src/test/java/org/wamblee/xmlrouter/impl/SingleRouterConfigTest.java @@ -18,68 +18,64 @@ package org.wamblee.xmlrouter.impl; import static junit.framework.Assert.*; import static org.mockito.Mockito.*; -import java.util.concurrent.atomic.AtomicLong; - import org.junit.Before; import org.junit.Test; import org.wamblee.xmlrouter.common.Id; import org.wamblee.xmlrouter.config.DocumentType; import org.wamblee.xmlrouter.config.Filter; +import org.wamblee.xmlrouter.config.RouterConfig; import org.wamblee.xmlrouter.config.Transformation; public class SingleRouterConfigTest { - private AtomicLong sequence; private ExtendedRouterConfig config; @Before public void setUp() { - sequence = new AtomicLong(1L); - config = new SingleRouterConfig(sequence); + config = new SingleRouterConfig(new Id("routerconfig")); } @Test public void testDocumentType() { DocumentType type1 = mock(DocumentType.class); + when(type1.getId()).thenReturn(new Id("type1")); DocumentType type2 = mock(DocumentType.class); + when(type1.getId()).thenReturn(new Id("type2")); - Id id1 = config.documentTypeConfig().add(type1); - - Id id2 = config.documentTypeConfig().add(type2); - assertFalse(id1.equals(id2)); - - assertEquals(2, config.documentTypeConfig().map().size()); - assertTrue(config.documentTypeConfig().map().get(id1) instanceof RobustDocumentType); + config.documentTypeConfig().add(type1); + config.documentTypeConfig().add(type2); + assertEquals(2, config.documentTypeConfig().values().size()); + assertTrue(config.documentTypeConfig().values().get(0) instanceof RobustDocumentType); } @Test public void testTransformation() { Transformation transformation1 = mock(Transformation.class); + when(transformation1.getId()).thenReturn(new Id("t1")); Transformation transformation2 = mock(Transformation.class); + when(transformation1.getId()).thenReturn(new Id("t2")); - Id id1 = config.transformationConfig().add( - transformation1); + config.transformationConfig().add(transformation1); - Id id2 = config.transformationConfig().add( - transformation2); - assertFalse(id1.equals(id2)); + config.transformationConfig().add(transformation2); - assertEquals(2, config.transformationConfig().map().size()); - assertTrue(config.transformationConfig().map().get(id1) instanceof RobustTransformation); + assertEquals(2, config.transformationConfig().values().size()); + assertTrue(config.transformationConfig().values().get(0) instanceof RobustTransformation); } @Test public void testFilter() { Filter filter1 = mock(Filter.class); + when(filter1.getId()).thenReturn(new Id("f1")); Filter filter2 = mock(Filter.class); + when(filter1.getId()).thenReturn(new Id("f2")); - Id id1 = config.filterConfig().add(filter1); + config.filterConfig().add(filter1); - Id id2 = config.filterConfig().add(filter2); - assertFalse(id1.equals(id2)); + config.filterConfig().add(filter2); - assertEquals(2, config.filterConfig().map().size()); - assertTrue(config.filterConfig().map().get(id1) instanceof RobustFilter); + assertEquals(2, config.filterConfig().values().size()); + assertTrue(config.filterConfig().values().get(0) instanceof RobustFilter); } } diff --git a/impl/src/test/java/org/wamblee/xmlrouter/impl/TransformationPathsTest.java b/impl/src/test/java/org/wamblee/xmlrouter/impl/TransformationPathsTest.java index 18e065f..33e16bf 100644 --- a/impl/src/test/java/org/wamblee/xmlrouter/impl/TransformationPathsTest.java +++ b/impl/src/test/java/org/wamblee/xmlrouter/impl/TransformationPathsTest.java @@ -17,10 +17,9 @@ package org.wamblee.xmlrouter.impl; import static junit.framework.Assert.*; +import java.util.ArrayList; import java.util.Collection; -import java.util.HashMap; -import java.util.LinkedHashMap; -import java.util.Map; +import java.util.List; import javax.xml.transform.dom.DOMSource; @@ -41,8 +40,8 @@ public class TransformationPathsTest { } @Override - public String getName() { - return "myname"; + public Id getId() { + return new Id("myname"); } @Override @@ -68,13 +67,12 @@ public class TransformationPathsTest { private TransformationPaths transformations; - private Map, Transformation> createTransformations( - long aStartId, Transformation... aTransformations) { - Map, Transformation> res = new LinkedHashMap, Transformation>(); + private List createTransformations(long aStartId, + Transformation... aTransformations) { + List res = new ArrayList(); - long id = aStartId; for (Transformation t : aTransformations) { - res.put(new Id(id++), t); + res.add(t); } return res; } @@ -124,7 +122,7 @@ public class TransformationPathsTest { @Test public void testWithoutTransformations() { transformations = new TransformationPaths( - new HashMap, Transformation>()); + new ArrayList()); Collection res = transformations.getPossibleTargetTypes("a"); assertEquals(1, res.size()); 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 46e36c0..5d84107 100644 --- a/impl/src/test/java/org/wamblee/xmlrouter/impl/XMLRouterFunctionTest.java +++ b/impl/src/test/java/org/wamblee/xmlrouter/impl/XMLRouterFunctionTest.java @@ -27,6 +27,7 @@ import javax.xml.transform.dom.DOMSource; import org.junit.Before; import org.junit.Test; import org.wamblee.general.SystemClock; +import org.wamblee.xmlrouter.common.Id; import org.wamblee.xmlrouter.config.DocumentType; import org.wamblee.xmlrouter.config.RouterConfig; import org.wamblee.xmlrouter.config.Transformation; @@ -71,7 +72,8 @@ public class XMLRouterFunctionTest { public void testOneTransformationOneDestination() { RouterConfig routerConfig = registerDocumentType("any"); Transformation transformation = mock(Transformation.class); - when(transformation.getName()).thenReturn("trans"); + when(transformation.getId()) + .thenReturn(new Id("trans")); when(transformation.getFromType()).thenReturn("any"); when(transformation.getToType()).thenReturn("bla"); when(transformation.transform(same(source1))).thenReturn(source2); @@ -89,7 +91,7 @@ public class XMLRouterFunctionTest { when(destination.receive(any(DOMSource.class))).thenReturn(true); routerService.publish("bla", source1); verify(listener).delivered(any(EventInfo.class), - anyListOf(Transformation.class), anyLong(), anyString(), eq(true)); + anyListOf(Transformation.class), anyString(), eq(true)); verify(transformation).transform(source1); verify(destination).receive(same(source2)); 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 64aefa1..2746253 100644 --- a/impl/src/test/java/org/wamblee/xmlrouter/impl/XMLRouterTest.java +++ b/impl/src/test/java/org/wamblee/xmlrouter/impl/XMLRouterTest.java @@ -20,7 +20,6 @@ import static org.mockito.Mockito.*; import java.util.Arrays; import java.util.Collection; -import java.util.concurrent.atomic.AtomicLong; import java.util.logging.Level; import javax.xml.transform.dom.DOMSource; @@ -31,6 +30,7 @@ import org.wamblee.general.SystemClock; import org.wamblee.xmlrouter.common.Id; import org.wamblee.xmlrouter.config.DocumentType; import org.wamblee.xmlrouter.config.Filter; +import org.wamblee.xmlrouter.config.RouterConfig; import org.wamblee.xmlrouter.config.Transformation; import org.wamblee.xmlrouter.listener.EventInfo; import org.wamblee.xmlrouter.listener.EventListener; @@ -79,7 +79,8 @@ public class XMLRouterTest { @Before public void setUp() { - routerConfig = new SingleRouterConfig(new AtomicLong(1L)); + routerConfig = new SingleRouterConfig(new Id( + "routerconfig")); config = new XMLRouterConfigurationImpl(routerConfig); EventListener logListener = new LoggingEventListener(Level.INFO); listener = spy(logListener); @@ -129,7 +130,7 @@ public class XMLRouterTest { router.publish("any", source1); verify(listener).delivered(any(EventInfo.class), - anyListOf(Transformation.class), anyLong(), anyString(), eq(true)); + anyListOf(Transformation.class), anyString(), eq(true)); verify(destinationSpy).receive(same(source1)); // Unregister the destination. @@ -149,14 +150,14 @@ public class XMLRouterTest { DocumentType type = mock(DocumentType.class); when(type.isInstance(any(DOMSource.class))).thenReturn(true); when(type.getName()).thenReturn(aType); - Id typeId = routerConfig.documentTypeConfig().add(type); + routerConfig.documentTypeConfig().add(type); } private void registerDocumentType(String aType, DOMSource aSource) { DocumentType type = mock(DocumentType.class); when(type.isInstance(same(aSource))).thenReturn(true); when(type.getName()).thenReturn(aType); - Id typeId = routerConfig.documentTypeConfig().add(type); + routerConfig.documentTypeConfig().add(type); } private Destination registerDestination(boolean aResult, String... types) { @@ -200,7 +201,7 @@ public class XMLRouterTest { router.publish("any", source1); verify(listener).delivered(any(EventInfo.class), - anyListOf(Transformation.class), anyLong(), anyString(), eq(true)); + anyListOf(Transformation.class), anyString(), eq(true)); verify(destinationSpy2).receive(same(source1)); @@ -229,7 +230,7 @@ public class XMLRouterTest { .registerDestination(destinationSpy2); router.publish("any", source1); verify(listener).delivered(any(EventInfo.class), - anyListOf(Transformation.class), anyLong(), anyString(), eq(true)); + anyListOf(Transformation.class), anyString(), eq(true)); verify(destinationSpy, never()).receive(same(source1)); verify(destinationSpy2).receive(same(source1)); @@ -260,7 +261,7 @@ public class XMLRouterTest { .registerDestination(destinationSpy2); router.publish("any", source1); verify(listener).delivered(any(EventInfo.class), - anyListOf(Transformation.class), anyLong(), anyString(), eq(true)); + anyListOf(Transformation.class), anyString(), eq(true)); verify(destinationSpy, never()).receive(same(source1)); verify(destinationSpy2).receive(same(source1)); @@ -270,7 +271,8 @@ public class XMLRouterTest { public void testOneTransformationOneDestination() { registerDocumentType("any"); Transformation transformation = mock(Transformation.class); - when(transformation.getName()).thenReturn("trans"); + when(transformation.getId()) + .thenReturn(new Id("trans")); when(transformation.getFromType()).thenReturn("any"); when(transformation.getToType()).thenReturn("bla"); when(transformation.transform(same(source1))).thenReturn(source2); @@ -287,7 +289,7 @@ public class XMLRouterTest { when(destination.receive(any(DOMSource.class))).thenReturn(true); router.publish("bla", source1); verify(listener).delivered(any(EventInfo.class), - anyListOf(Transformation.class), anyLong(), anyString(), eq(true)); + anyListOf(Transformation.class), anyString(), eq(true)); verify(transformation).transform(source1); verify(destination).receive(same(source2)); @@ -302,7 +304,8 @@ public class XMLRouterTest { public void testMisbehavingTransformationOneDestination() { registerDocumentType("any"); Transformation transformation = mock(Transformation.class); - when(transformation.getName()).thenReturn("trans"); + when(transformation.getId()) + .thenReturn(new Id("trans")); when(transformation.getFromType()).thenReturn("any"); when(transformation.getToType()).thenReturn("bla"); doThrow(new RuntimeException("x")).when(transformation).transform( @@ -324,7 +327,8 @@ public class XMLRouterTest { private Transformation createTransformation(String aFrom, String aTo, DOMSource aSource, DOMSource aTarget) { Transformation transformation = mock(Transformation.class); - when(transformation.getName()).thenReturn("trans"); + when(transformation.getId()) + .thenReturn(new Id("trans")); when(transformation.getFromType()).thenReturn(aFrom); when(transformation.getToType()).thenReturn(aTo); when(transformation.transform(same(aSource))).thenReturn(aTarget); @@ -362,7 +366,8 @@ public class XMLRouterTest { .thenReturn(Arrays.asList("bla", "bla2")); reset(transformation); - when(transformation.getName()).thenReturn("trans"); + when(transformation.getId()) + .thenReturn(new Id("trans")); when(transformation.getFromType()).thenReturn("any"); when(transformation.getToType()).thenReturn("bla"); when(transformation.transform(same(source1))).thenReturn(null); @@ -370,7 +375,7 @@ public class XMLRouterTest { when(destination.receive(any(DOMSource.class))).thenReturn(true); router.publish("bla", source1); verify(listener).delivered(any(EventInfo.class), - anyListOf(Transformation.class), anyLong(), anyString(), eq(true)); + anyListOf(Transformation.class), anyString(), eq(true)); verify(transformation).transform(source1); verify(transformation2).transform(source1); @@ -387,7 +392,7 @@ public class XMLRouterTest { router.publish("source", source1); verify(listener, times(2)).delivered(any(EventInfo.class), - anyListOf(Transformation.class), anyLong(), anyString(), eq(true)); + anyListOf(Transformation.class), anyString(), eq(true)); verify(dest1).receive(same(source1)); verify(dest2).receive(same(source1)); @@ -405,7 +410,7 @@ public class XMLRouterTest { router.publish("source", source1); verify(listener, times(2)).delivered(any(EventInfo.class), - anyListOf(Transformation.class), anyLong(), anyString(), eq(true)); + anyListOf(Transformation.class), anyString(), eq(true)); verify(dest).receive(same(source1)); verify(dest).receive(same(source2)); @@ -427,7 +432,7 @@ public class XMLRouterTest { router.publish("source", source1); verify(listener).delivered(any(EventInfo.class), - anyListOf(Transformation.class), anyLong(), anyString(), eq(true)); + anyListOf(Transformation.class), anyString(), eq(true)); verify(dest).receive(same(source3)); } @@ -447,7 +452,7 @@ public class XMLRouterTest { router.publish("source", source1); verify(listener).delivered(any(EventInfo.class), - anyListOf(Transformation.class), anyLong(), anyString(), eq(false)); + anyListOf(Transformation.class), anyString(), eq(false)); } } diff --git a/listener/src/main/java/org/wamblee/xmlrouter/listener/CompositeEventListener.java b/listener/src/main/java/org/wamblee/xmlrouter/listener/CompositeEventListener.java index 791d9d1..e54832b 100644 --- a/listener/src/main/java/org/wamblee/xmlrouter/listener/CompositeEventListener.java +++ b/listener/src/main/java/org/wamblee/xmlrouter/listener/CompositeEventListener.java @@ -69,11 +69,11 @@ public class CompositeEventListener implements EventListener { @Override @ReadLock public void delivered(EventInfo aInfo, List aSequence, - long aDestinationId, String aDestinationName, boolean aSuccessFlag) { + String aDestinationId, boolean aSuccessFlag) { for (EventListener logger : loggers) { try { - logger.delivered(aInfo, aSequence, aDestinationId, - aDestinationName, aSuccessFlag); + logger + .delivered(aInfo, aSequence, aDestinationId, aSuccessFlag); } catch (Exception e) { LOGGER.log(Level.WARNING, "Logger threw exception", e); } diff --git a/listener/src/main/java/org/wamblee/xmlrouter/listener/EventListener.java b/listener/src/main/java/org/wamblee/xmlrouter/listener/EventListener.java index 251417a..a2e289a 100644 --- a/listener/src/main/java/org/wamblee/xmlrouter/listener/EventListener.java +++ b/listener/src/main/java/org/wamblee/xmlrouter/listener/EventListener.java @@ -36,13 +36,11 @@ public interface EventListener { * Sequence of transformations performed. * @param aDestinationId * Id of the destination the event was delivered to. - * @param aDestinationName - * Destination name. * @param aSuccessFlag * Whether or not event delivery succeeded. */ void delivered(EventInfo aInfo, List aSequence, - long aDestinationId, String aDestinationName, boolean aSuccessFlag); + String aDestinationId, boolean aSuccessFlag); /** * Called when an event could not be delivered to any destination. diff --git a/listener/src/main/java/org/wamblee/xmlrouter/listener/LoggingEventListener.java b/listener/src/main/java/org/wamblee/xmlrouter/listener/LoggingEventListener.java index 24c6be6..2c917ab 100644 --- a/listener/src/main/java/org/wamblee/xmlrouter/listener/LoggingEventListener.java +++ b/listener/src/main/java/org/wamblee/xmlrouter/listener/LoggingEventListener.java @@ -39,13 +39,11 @@ public class LoggingEventListener implements EventListener { @Override public void delivered(EventInfo aEvent, List aSequence, - long aDestinationId, String aDestinationName, boolean aSuccessFlag) { + String aDestinationId, boolean aSuccessFlag) { if (LOGGER.isLoggable(level)) { - LOGGER - .log(level, "event delivered: " + aEvent + ", sequence '" + - getSequenceString(aSequence) + "', destinationId " + - aDestinationId + ", destinationName '" + aDestinationName + - "'"); + LOGGER.log(level, "event delivered: " + aEvent + ", sequence '" + + getSequenceString(aSequence) + "', destinationId " + + aDestinationId + "'"); } } @@ -56,7 +54,7 @@ public class LoggingEventListener implements EventListener { buf.append(", "); } Transformation transformation = aSequence.get(i); - buf.append(transformation.getName()); + buf.append(transformation.getId()); buf.append("("); buf.append(transformation.getFromType()); buf.append("->"); diff --git a/listener/src/test/java/org/wamblee/xmlrouter/listener/CompositeEventListenerTest.java b/listener/src/test/java/org/wamblee/xmlrouter/listener/CompositeEventListenerTest.java index 19608b8..8d2b135 100644 --- a/listener/src/test/java/org/wamblee/xmlrouter/listener/CompositeEventListenerTest.java +++ b/listener/src/test/java/org/wamblee/xmlrouter/listener/CompositeEventListenerTest.java @@ -27,8 +27,7 @@ import org.wamblee.xmlrouter.config.Transformation; public class CompositeEventListenerTest { - private static final String DESTINATION_NAME = "dest"; - private static final long DESTINATION_ID = 12L; + private static final String DESTINATION_ID = "destid"; private CompositeEventListener composite; private EventInfo source; @@ -43,7 +42,7 @@ public class CompositeEventListenerTest { public void testNoListeners() { // verify no exceptions occur. composite.delivered(mock(EventInfo.class), getTransformations(), - DESTINATION_ID, DESTINATION_NAME, true); + DESTINATION_ID, true); composite.notDelivered(mock(EventInfo.class)); } @@ -73,8 +72,7 @@ public class CompositeEventListenerTest { } private void invokeDelivered() { - composite.delivered(source, getTransformations(), DESTINATION_ID, - DESTINATION_NAME, true); + composite.delivered(source, getTransformations(), DESTINATION_ID, true); } private void invokeNotDelivered() { @@ -83,7 +81,7 @@ public class CompositeEventListenerTest { private void checkInvokeDelivered(EventListener listener) { verify(listener).delivered(same(source), eq(getTransformations()), - eq(DESTINATION_ID), eq(DESTINATION_NAME), eq(true)); + eq(DESTINATION_ID), eq(true)); } @Test -- 2.31.1