*/
package org.wamblee.xmlrouter.config;
-import java.util.List;
+import java.util.Collection;
import org.wamblee.xmlrouter.common.Id;
/**
* @return All available items.
*/
- List<T> values();
+ Collection<T> values();
}
\ No newline at end of file
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;
/**
ExtendedConfig<T> {
private Id<Config> id;
- private List<T> registered;
+ private Map<Id<T>, T> registered;
/**
* Constructs the object.
public ConfigImpl(Id<Config> aId) {
notNull("id", aId);
id = aId;
- registered = new ArrayList<T>();
+ registered = new HashMap<Id<T>, T>();
}
@Override
@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));
}
/**
@Override
public synchronized boolean remove(Id<T> aId) {
notNull("aId", aId);
- Iterator<T> 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<T> values() {
- return Collections.unmodifiableList(registered);
+ public Collection<T> 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();
}
}
private void throwConfigException(String aMsg, Exception aException) {
LOGGER.log(Level.WARNING, aMsg, aException);
- throw new ConfigException("id is null");
+ throw new ConfigException(aMsg);
}
@Override
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.
/**
*/
public class SingleRouterConfig implements ExtendedRouterConfig {
private Id<RouterConfig> id;
+
private ExtendedConfig<DocumentType> documentTypes;
private ExtendedConfig<Transformation> transformations;
private ExtendedConfig<Filter> filters;
public Config<Filter> 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();
+ }
}
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);
assertEquals(1, config.values().size());
- assertTrue(config.remove(new Id(CONFIG_TYPE + "." + "type1")));
+ assertTrue(config.remove(new Id("type1")));
assertTrue(config.values().isEmpty());
}
public void testUnmodifiable() {
config.values().add(mock(MyType.class));
}
+
+ @Test
+ public void testEquals() {
+
+ Config<MyType> config1 = new ConfigImpl<MyType>(new Id<Config>(
+ CONFIG_TYPE)) {
+ @Override
+ public MyType wrap(String aPrefix, MyType aT) {
+ return new MyTypeWrapper(aPrefix, aT);
+ }
+ };
+ assertFalse(config1.equals(null));
+ assertFalse(config1.equals("hello"));
+ Config<MyType> config2 = new ConfigImpl<MyType>(new Id<Config>(
+ 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));
+ }
}
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
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
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"));
+ RouterConfig config2 = new SingleRouterConfig(new Id<RouterConfig>(
+ "routerconfig"));
+ assertEquals(config1, config2);
+ }
+
+ @Test
+ public void testEqualsNonEmpty() {
+ RouterConfig config1 = new SingleRouterConfig(new Id<RouterConfig>(
+ "routerconfig"));
+
+ assertFalse(config1.equals(null));
+ assertFalse(config.equals("hello"));
+
+ RouterConfig config2 = new SingleRouterConfig(new Id<RouterConfig>(
+ "routerconfig"));
+ DocumentType type1 = mock(DocumentType.class);
+ when(type1.getId()).thenReturn(new Id<DocumentType>("type1"));
+ DocumentType type2 = mock(DocumentType.class);
+ when(type2.getId()).thenReturn(new Id<DocumentType>("type1"));
+ Filter filter1 = mock(Filter.class);
+ when(filter1.getId()).thenReturn(new Id<Filter>("f1"));
+ Filter filter2 = mock(Filter.class);
+ when(filter2.getId()).thenReturn(new Id<Filter>("f1"));
+ Transformation transformation1 = mock(Transformation.class);
+ when(transformation1.getId()).thenReturn(new Id<Transformation>("t1"));
+ Transformation transformation2 = mock(Transformation.class);
+ when(transformation2.getId()).thenReturn(new Id<Transformation>("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());
}
}
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;
verify(config).setRouterConfig(captor.capture());
RouterConfig received = captor.getValue();
- List<DocumentType> docTypes = received.documentTypeConfig().values();
+ Collection<DocumentType> docTypes = received.documentTypeConfig()
+ .values();
assertEquals(1, docTypes.size());
assertEquals("myapp.id.documenttypes.doctype" + aSuffix, docTypes
- .get(0).getId().toString());
+ .iterator().next().getId().toString());
- List<Transformation> transformations = received.transformationConfig()
- .values();
+ Collection<Transformation> transformations = received
+ .transformationConfig().values();
assertEquals(1, transformations.size());
assertEquals("myapp.id.transformations.t1" + aSuffix, transformations
- .get(0).getId().toString());
+ .iterator().next().getId().toString());
- List<Filter> filters = received.filterConfig().values();
+ Collection<Filter> 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,
private Transformation createTransformation(String aFrom, String aTo,
DOMSource aSource, DOMSource aTarget) {
Transformation transformation = mock(Transformation.class);
- when(transformation.getId())
- .thenReturn(new Id<Transformation>("trans"));
+ when(transformation.getId()).thenReturn(
+ new Id<Transformation>(UUID.randomUUID().toString()));
when(transformation.getFromType()).thenReturn(aFrom);
when(transformation.getToType()).thenReturn(aTo);
when(transformation.transform(same(aSource))).thenReturn(aTarget);