X-Git-Url: http://wamblee.org/gitweb/?a=blobdiff_plain;f=impl%2Fsrc%2Ftest%2Fjava%2Forg%2Fwamblee%2Fxmlrouter%2Fimpl%2FXMLRouterConfigServiceTest.java;fp=impl%2Fsrc%2Ftest%2Fjava%2Forg%2Fwamblee%2Fxmlrouter%2Fimpl%2FXMLRouterConfigServiceTest.java;h=4086fa36482ce044f21be82821b8c998a1feac08;hb=6c41c1cabffcc509c5b736f73578930808f6616f;hp=0000000000000000000000000000000000000000;hpb=6524a285da907680a5ac0d02219811ad681461b5;p=xmlrouter diff --git a/impl/src/test/java/org/wamblee/xmlrouter/impl/XMLRouterConfigServiceTest.java b/impl/src/test/java/org/wamblee/xmlrouter/impl/XMLRouterConfigServiceTest.java new file mode 100644 index 0000000..4086fa3 --- /dev/null +++ b/impl/src/test/java/org/wamblee/xmlrouter/impl/XMLRouterConfigServiceTest.java @@ -0,0 +1,120 @@ +/* + * 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 java.util.List; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.ArgumentCaptor; +import org.wamblee.xmlrouter.common.Id; +import org.wamblee.xmlrouter.config.ConfigException; +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 XMLRouterConfigServiceTest { + + private XMLRouterConfiguration config; + private ArgumentCaptor captor; + private XMLRouterConfigService svc; + + @Before + public void setUp() { + config = mock(XMLRouterConfiguration.class); + captor = ArgumentCaptor.forClass(ExtendedRouterConfig.class); + svc = new XMLRouterConfigService("myapp", config); + } + + @Test + public void testPrefixingOfConfigItems() { + RouterConfig routerConfig = createRouterConfig("id", ""); + + svc.apply(routerConfig, null); + + verifyResults(""); + } + + private void verifyResults(String aSuffix) { + verify(config).setRouterConfig(captor.capture()); + RouterConfig received = captor.getValue(); + + List docTypes = received.documentTypeConfig().values(); + assertEquals(1, docTypes.size()); + assertEquals("myapp.id.documenttypes.doctype" + aSuffix, docTypes + .get(0).getId().toString()); + + List transformations = received.transformationConfig() + .values(); + assertEquals(1, transformations.size()); + assertEquals("myapp.id.transformations.t1" + aSuffix, transformations + .get(0).getId().toString()); + + List filters = received.filterConfig().values(); + assertEquals(1, filters.size()); + assertEquals("myapp.id.filters.f1" + aSuffix, filters.get(0).getId() + .toString()); + } + + private RouterConfig createRouterConfig(String routerConfigId, + String aSuffix) { + RouterConfig routerConfig = svc.emptyConfig(routerConfigId); + + DocumentType type = mock(DocumentType.class); + when(type.getName()).thenReturn("doctype" + aSuffix); + when(type.getId()) + .thenReturn(new Id("doctype" + aSuffix)); + + Transformation trans = mock(Transformation.class); + when(trans.getId()).thenReturn(new Id("t1" + aSuffix)); + + Filter filter = mock(Filter.class); + when(filter.getId()).thenReturn(new Id("f1" + aSuffix)); + + routerConfig.documentTypeConfig().add(type); + routerConfig.transformationConfig().add(trans); + routerConfig.filterConfig().add(filter); + return routerConfig; + } + + @Test(expected = ConfigException.class) + public void testReplaceWithoutIndicatingOldConfig() { + RouterConfig routerConfig1 = createRouterConfig("id", ""); + + svc.apply(routerConfig1, null); + RouterConfig routerConfig2 = createRouterConfig("id", ""); + + svc.apply(routerConfig2, null); + } + + @Test + public void testReplaceCorrectly() { + RouterConfig routerConfig1 = createRouterConfig("id", ""); + + svc.apply(routerConfig1, null); + RouterConfig routerConfig2 = createRouterConfig("id", "suffix"); + + reset(config); + svc.apply(routerConfig2, new Id("id")); + + verifyResults("suffix"); + } + +}