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 org.junit.Before;
22 import org.junit.Test;
23 import org.wamblee.xmlrouter.common.Id;
24 import org.wamblee.xmlrouter.config.DocumentType;
25 import org.wamblee.xmlrouter.config.Filter;
26 import org.wamblee.xmlrouter.config.Transformation;
28 public class SingleRouterConfigTest {
30 private SingleRouterConfig config;
34 config = new SingleRouterConfig("routerconfig");
35 assertEquals("routerconfig", config.getId().getId());
39 public void testDocumentType() {
40 DocumentType type1 = mock(DocumentType.class);
41 when(type1.getId()).thenReturn(new Id<DocumentType>("type1"));
42 DocumentType type2 = mock(DocumentType.class);
43 when(type2.getId()).thenReturn(new Id<DocumentType>("type2"));
45 config.documentTypeConfig().add(type1);
46 config.documentTypeConfig().add(type2);
48 assertEquals(2, config.documentTypeConfig().values().size());
49 Object firstValue = config.documentTypeConfig().values().iterator()
51 assertTrue(firstValue instanceof RobustDocumentType);
55 public void testTransformation() {
56 Transformation transformation1 = mock(Transformation.class);
57 when(transformation1.getId()).thenReturn(new Id<Transformation>("t1"));
58 Transformation transformation2 = mock(Transformation.class);
59 when(transformation2.getId()).thenReturn(new Id<Transformation>("t2"));
61 config.transformationConfig().add(transformation1);
63 config.transformationConfig().add(transformation2);
65 assertEquals(2, config.transformationConfig().values().size());
66 Object firstValue = config.transformationConfig().values().iterator()
68 assertTrue(firstValue instanceof RobustTransformation);
72 public void testFilter() {
73 Filter filter1 = mock(Filter.class);
74 when(filter1.getId()).thenReturn(new Id<Filter>("f1"));
75 Filter filter2 = mock(Filter.class);
76 when(filter2.getId()).thenReturn(new Id<Filter>("f2"));
78 config.filterConfig().add(filter1);
80 config.filterConfig().add(filter2);
82 assertEquals(2, config.filterConfig().values().size());
83 Object firstValue = config.filterConfig().values().iterator().next();
84 assertTrue(firstValue instanceof RobustFilter);
88 public void testEqualityEmptyConfig() {
89 RouterConfig config1 = new SingleRouterConfig("routerconfig");
90 RouterConfig config2 = new SingleRouterConfig("routerconfig");
91 assertEquals(config1, config2);
95 public void testEqualsNonEmpty() {
96 RouterConfig config1 = new SingleRouterConfig("routerconfig");
98 assertFalse(config1.equals(null));
99 assertFalse(config.equals("hello"));
101 RouterConfig config2 = new SingleRouterConfig("routerconfig");
102 DocumentType type1 = mock(DocumentType.class);
103 when(type1.getId()).thenReturn(new Id<DocumentType>("type1"));
104 DocumentType type2 = mock(DocumentType.class);
105 when(type2.getId()).thenReturn(new Id<DocumentType>("type1"));
106 Filter filter1 = mock(Filter.class);
107 when(filter1.getId()).thenReturn(new Id<Filter>("f1"));
108 Filter filter2 = mock(Filter.class);
109 when(filter2.getId()).thenReturn(new Id<Filter>("f1"));
110 Transformation transformation1 = mock(Transformation.class);
111 when(transformation1.getId()).thenReturn(new Id<Transformation>("t1"));
112 Transformation transformation2 = mock(Transformation.class);
113 when(transformation2.getId()).thenReturn(new Id<Transformation>("t1"));
115 config1.documentTypeConfig().add(type1);
116 assertFalse(config1.equals(config2));
118 config2.documentTypeConfig().add(type2);
119 assertEquals(config1, config2);
120 assertEquals(config1.hashCode(), config2.hashCode());
122 config1.transformationConfig().add(transformation1);
123 config2.transformationConfig().add(transformation2);
125 assertEquals(config1, config2);
126 assertEquals(config1.hashCode(), config2.hashCode());
128 config1.filterConfig().add(filter1);
129 config2.filterConfig().add(filter2);
131 assertEquals(config1, config2);
132 assertEquals(config1.hashCode(), config2.hashCode());
136 public void testCopy() {
139 testTransformation();
141 SingleRouterConfig copy = new SingleRouterConfig(config);
142 assertEquals(config.getId(), copy.getId());
143 assertEquals(config, copy);
145 // verify the copy is not shallow.
147 config.documentTypeConfig().remove(new Id<DocumentType>("type1"));
148 config.transformationConfig().remove(new Id<Transformation>("t1"));
149 config.filterConfig().remove(new Id<Filter>("f1"));
150 assertEquals(1, config.documentTypeConfig().values().size());
151 assertEquals(1, config.transformationConfig().values().size());
152 assertEquals(1, config.filterConfig().values().size());
153 assertFalse(config.equals(copy));