*
* @param aT
* item
- * @throws DuplicateException
+ * @throws ConfigException
* In case an object with the same id already exists.
*/
void add(T aT);
*
* @author Erik Brakkee
*/
-public class DuplicateException extends RuntimeException {
+public class ConfigException extends RuntimeException {
- public DuplicateException(String aMsg) {
+ public ConfigException(String aMsg) {
super(aMsg);
}
- public DuplicateException(String aMsg, Throwable aCause) {
+ public ConfigException(String aMsg, Throwable aCause) {
super(aMsg, aCause);
}
}
import org.wamblee.xmlrouter.common.Id;
import org.wamblee.xmlrouter.config.Config;
-import org.wamblee.xmlrouter.config.DuplicateException;
+import org.wamblee.xmlrouter.config.ConfigException;
import org.wamblee.xmlrouter.config.Identifiable;
/**
public void addConfig(Config<T> aConfig) {
notNull("aConfig", aConfig);
if (ids.contains(aConfig.getId())) {
- throw new DuplicateException(aConfig.getId().toString());
+ throw new ConfigException("duplicate id '" +
+ aConfig.getId().toString() + "'");
}
for (T item : aConfig.values()) {
if (valueIds.contains(item.getId())) {
- throw new DuplicateException(item.getId().toString());
+ throw new ConfigException("duplicate id '" +
+ item.getId().toString() + "'");
}
}
*/
package org.wamblee.xmlrouter.impl;
+import static org.wamblee.xmlrouter.impl.MessageUtil.*;
+
import java.util.logging.Level;
import java.util.logging.Logger;
import org.wamblee.xmlrouter.common.Id;
+import org.wamblee.xmlrouter.config.ConfigException;
import org.wamblee.xmlrouter.config.Identifiable;
/**
// TODO test that id is constant even though delegated changes its id.
public RobustIdentifiable(String aPrefix, Identifiable<T> aIdentifiable) {
+ notNull("prefix", aPrefix);
+ notNull("identifiable", aIdentifiable);
// TODO test id is null
// TODO getId() throws exception
try {
id = aIdentifiable.getId();
if (id == null) {
- id = new Id<T>(Constants.UNKNOWN_ID.toString());
- temporarilyThrowException();
- } else {
- id = new Id<T>(aPrefix + id.getId());
+ throwConfigException("identifiable.getId() returned null", null);
}
+ id = new Id<T>(aPrefix + id.getId());
} catch (Exception e) {
- LOGGER
- .log(Level.WARNING, "Identifiable getId() threw exception", e);
+ throwConfigException("identifiable.getId() threw exception", e);
}
}
- private void temporarilyThrowException() {
- throw new RuntimeException(
- "Temporary to catch nulls during refactoring");
+ private void throwConfigException(String aMsg, Exception aException) {
+ LOGGER.log(Level.WARNING, aMsg, aException);
+ throw new ConfigException("id is null");
}
@Override
import org.junit.Test;
import org.wamblee.xmlrouter.common.Id;
import org.wamblee.xmlrouter.config.Config;
-import org.wamblee.xmlrouter.config.DuplicateException;
+import org.wamblee.xmlrouter.config.ConfigException;
import org.wamblee.xmlrouter.config.Identifiable;
public class CompositeConfigTest {
assertTrue(values.contains(i4));
}
- @Test(expected = DuplicateException.class)
+ @Test(expected = ConfigException.class)
public void testDuplicatesNotAllowed() {
CompositeConfig<IntClass> composite = composite("c");
Config<IntClass> c1 = new ConfigImpl(id("c1")) {
try {
composite.addConfig(c2);
fail();
- } catch (DuplicateException e) {
+ } catch (ConfigException e) {
// ok.
}
assertEquals(1, composite.values().size());
import org.junit.Before;
import org.junit.Test;
+import org.wamblee.xmlrouter.common.Id;
import org.wamblee.xmlrouter.config.DocumentType;
public class RobustDocumentTypeTest {
@Before
public void setUp() {
documentType = mock(DocumentType.class);
+ when(documentType.getId()).thenReturn(new Id<DocumentType>("docid"));
robust = new RobustDocumentType("app1", documentType);
source = mock(DOMSource.class);
}
import static org.mockito.Matchers.*;
import static org.mockito.Mockito.*;
+import java.util.UUID;
+
import javax.xml.transform.dom.DOMSource;
import org.junit.Before;
import org.junit.Test;
+import org.wamblee.xmlrouter.common.Id;
import org.wamblee.xmlrouter.config.Filter;
public class RobustFilterTest {
@Before
public void setUp() {
filter = mock(Filter.class);
+ when(filter.getId()).thenReturn(
+ new Id<Filter>(UUID.randomUUID().toString()));
robust = new RobustFilter("filter", filter);
source = mock(DOMSource.class);
}
--- /dev/null
+/*
+ * Copyright 2005-2011 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.wamblee.xmlrouter.impl;
+
+import static junit.framework.Assert.*;
+import static org.mockito.Mockito.*;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.wamblee.xmlrouter.common.Id;
+import org.wamblee.xmlrouter.config.ConfigException;
+import org.wamblee.xmlrouter.config.Identifiable;
+
+public class RobustIdentifiableTest {
+
+ private Identifiable<Integer> ident;
+
+ @Before
+ public void setUp() {
+ ident = mock(Identifiable.class);
+ }
+
+ @Test(expected = ConfigException.class)
+ public void testIdIsNull() {
+ when(ident.getId()).thenReturn(null);
+ RobustIdentifiable<Integer> robust = new RobustIdentifiable<Integer>(
+ "prefix", ident);
+ }
+
+ @Test(expected = ConfigException.class)
+ public void testIdThrowsException() {
+ doThrow(new RuntimeException("xxx")).when(ident).getId();
+ RobustIdentifiable<Integer> robust = new RobustIdentifiable<Integer>(
+ "prefix", ident);
+ }
+
+ @Test
+ public void testNormalCase() {
+ when(ident.getId()).thenReturn(new Id<Integer>("myid"));
+ RobustIdentifiable<Integer> robust = new RobustIdentifiable<Integer>(
+ "prefix.", ident);
+ assertEquals("prefix.myid", robust.getId().toString());
+
+ // changes later do not have any effect, the id should be immutable.
+ when(ident.getId()).thenReturn(new Id<Integer>("myid2"));
+ assertEquals("prefix.myid", robust.getId().toString());
+ }
+}
import org.junit.Before;
import org.junit.Test;
+import org.wamblee.xmlrouter.common.Id;
import org.wamblee.xmlrouter.config.Transformation;
public class RobustTransformationTest {
@Before
public void setUp() {
transformation = mock(Transformation.class);
+ when(transformation.getId()).thenReturn(new Id<Transformation>("t1"));
robust = new RobustTransformation("transformation", transformation);
source = mock(DOMSource.class);
resSource = mock(DOMSource.class);
DocumentType type1 = mock(DocumentType.class);
when(type1.getId()).thenReturn(new Id<DocumentType>("type1"));
DocumentType type2 = mock(DocumentType.class);
- when(type1.getId()).thenReturn(new Id<DocumentType>("type2"));
+ when(type2.getId()).thenReturn(new Id<DocumentType>("type2"));
config.documentTypeConfig().add(type1);
config.documentTypeConfig().add(type2);
Transformation transformation1 = mock(Transformation.class);
when(transformation1.getId()).thenReturn(new Id<Transformation>("t1"));
Transformation transformation2 = mock(Transformation.class);
- when(transformation1.getId()).thenReturn(new Id<Transformation>("t2"));
+ when(transformation2.getId()).thenReturn(new Id<Transformation>("t2"));
config.transformationConfig().add(transformation1);
Filter filter1 = mock(Filter.class);
when(filter1.getId()).thenReturn(new Id<Filter>("f1"));
Filter filter2 = mock(Filter.class);
- when(filter1.getId()).thenReturn(new Id<Filter>("f2"));
+ when(filter2.getId()).thenReturn(new Id<Filter>("f2"));
config.filterConfig().add(filter1);
DocumentType type = mock(DocumentType.class);
when(type.isInstance(any(DOMSource.class))).thenReturn(true);
when(type.getName()).thenReturn(aType);
+ when(type.getId()).thenReturn(new Id<DocumentType>(aType));
RouterConfig routerConfig = configService.emptyConfig("app");
routerConfig.documentTypeConfig().add(type);
return routerConfig;
import java.util.Arrays;
import java.util.Collection;
+import java.util.UUID;
import java.util.logging.Level;
import javax.xml.transform.dom.DOMSource;
@Test
public void testMisBehavingDocumentType() {
- DocumentType type = mock(DocumentType.class);
+ DocumentType type = mockDocument("docid");
doThrow(new RuntimeException("x")).when(type).isInstance(
any(DOMSource.class));
routerConfig.documentTypeConfig().add(type);
// no exception should occur.
}
+ private DocumentType mockDocument(String docid) {
+ DocumentType type = mock(DocumentType.class);
+ when(type.getId()).thenReturn(new Id<DocumentType>(docid));
+ return type;
+ }
+
@Test
public void testMisBehavingFilter() {
registerDocumentType("any");
- Filter filter = mock(Filter.class);
+ Filter filter = mockFilter("filterid");
doThrow(new RuntimeException("x")).when(filter).isAllowed(anyString(),
any(DOMSource.class));
routerConfig.filterConfig().add(filter);
// no exception should occur.
}
+ private Filter mockFilter(String filterId) {
+ Filter filter = mock(Filter.class);
+ when(filter.getId()).thenReturn(new Id<Filter>(filterId));
+ return filter;
+ }
+
@Test
public void testOneDestinationNoTransformationSuccess() {
destinationSpy = registerDestination(true, "any");
}
private void registerDocumentType(String aType) {
- DocumentType type = mock(DocumentType.class);
+ DocumentType type = mockDocument(UUID.randomUUID().toString());
when(type.isInstance(any(DOMSource.class))).thenReturn(true);
when(type.getName()).thenReturn(aType);
routerConfig.documentTypeConfig().add(type);
DocumentType type = mock(DocumentType.class);
when(type.isInstance(same(aSource))).thenReturn(true);
when(type.getName()).thenReturn(aType);
+ when(type.getId()).thenReturn(new Id<DocumentType>(aType));
routerConfig.documentTypeConfig().add(type);
}