2 * Copyright 2005-2011 the original author or authors.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.wamblee.xmlrouter.impl;
18 import static junit.framework.Assert.*;
19 import static org.mockito.Mockito.*;
21 import java.util.Collection;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.mockito.ArgumentCaptor;
26 import org.wamblee.xmlrouter.common.Id;
27 import org.wamblee.xmlrouter.config.ConfigException;
28 import org.wamblee.xmlrouter.config.DocumentType;
29 import org.wamblee.xmlrouter.config.Filter;
30 import org.wamblee.xmlrouter.config.RouterConfig;
31 import org.wamblee.xmlrouter.config.Transformation;
33 public class XMLRouterConfigServiceTest {
35 private XMLRouterConfiguration config;
36 private ArgumentCaptor<ExtendedRouterConfig> captor;
37 private XMLRouterConfigService svc;
41 config = mock(XMLRouterConfiguration.class);
42 captor = ArgumentCaptor.forClass(ExtendedRouterConfig.class);
43 svc = new XMLRouterConfigService("myapp", config);
47 public void testPrefixingOfConfigItems() {
48 RouterConfig routerConfig = createRouterConfig("id", "");
50 svc.apply(routerConfig, null);
55 private void verifyResults(String aSuffix) {
56 verify(config).setRouterConfig(captor.capture());
57 RouterConfig received = captor.getValue();
59 Collection<DocumentType> docTypes = received.documentTypeConfig()
61 assertEquals(1, docTypes.size());
62 assertEquals("myapp.id.documenttypes.doctype" + aSuffix, docTypes
63 .iterator().next().getId().toString());
65 Collection<Transformation> transformations = received
66 .transformationConfig().values();
67 assertEquals(1, transformations.size());
68 assertEquals("myapp.id.transformations.t1" + aSuffix, transformations
69 .iterator().next().getId().toString());
71 Collection<Filter> filters = received.filterConfig().values();
72 assertEquals(1, filters.size());
73 assertEquals("myapp.id.filters.f1" + aSuffix, filters.iterator().next()
77 private RouterConfig createRouterConfig(String routerConfigId,
79 RouterConfig routerConfig = svc.emptyConfig(routerConfigId);
81 DocumentType type = mock(DocumentType.class);
82 when(type.getName()).thenReturn("doctype" + aSuffix);
84 .thenReturn(new Id<DocumentType>("doctype" + aSuffix));
86 Transformation trans = mock(Transformation.class);
87 when(trans.getId()).thenReturn(new Id<Transformation>("t1" + aSuffix));
89 Filter filter = mock(Filter.class);
90 when(filter.getId()).thenReturn(new Id<Filter>("f1" + aSuffix));
92 routerConfig.documentTypeConfig().add(type);
93 routerConfig.transformationConfig().add(trans);
94 routerConfig.filterConfig().add(filter);
98 @Test(expected = ConfigException.class)
99 public void testReplaceWithoutIndicatingOldConfig() {
100 RouterConfig routerConfig1 = createRouterConfig("id", "");
102 svc.apply(routerConfig1, null);
103 RouterConfig routerConfig2 = createRouterConfig("id", "");
105 svc.apply(routerConfig2, null);
109 public void testReplaceCorrectly() {
110 RouterConfig routerConfig1 = createRouterConfig("id", "");
112 svc.apply(routerConfig1, null);
113 RouterConfig routerConfig2 = createRouterConfig("id", "suffix");
116 svc.apply(routerConfig2, new Id<RouterConfig>("id"));
118 verifyResults("suffix");