5f151d29536fd0a4a89874403d99d11be466161a
[xmlrouter] / impl / src / test / java / org / wamblee / xmlrouter / impl / SingleRouterConfigTest.java
1 /*
2  * Copyright 2005-2011 the original author or authors.
3  * 
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
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
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.
15  */
16 package org.wamblee.xmlrouter.impl;
17
18 import static junit.framework.Assert.*;
19 import static org.mockito.Mockito.*;
20
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.RouterConfig;
27 import org.wamblee.xmlrouter.config.Transformation;
28
29 public class SingleRouterConfigTest {
30
31     private SingleRouterConfig config;
32
33     @Before
34     public void setUp() {
35         config = new SingleRouterConfig(new Id<RouterConfig>("routerconfig"));
36         assertEquals("routerconfig", config.getId().getId());
37     }
38
39     @Test
40     public void testDocumentType() {
41         DocumentType type1 = mock(DocumentType.class);
42         when(type1.getId()).thenReturn(new Id<DocumentType>("type1"));
43         DocumentType type2 = mock(DocumentType.class);
44         when(type2.getId()).thenReturn(new Id<DocumentType>("type2"));
45
46         config.documentTypeConfig().add(type1);
47         config.documentTypeConfig().add(type2);
48
49         assertEquals(2, config.documentTypeConfig().values().size());
50         Object firstValue = config.documentTypeConfig().values().iterator()
51             .next();
52         assertTrue(firstValue instanceof RobustDocumentType);
53     }
54
55     @Test
56     public void testTransformation() {
57         Transformation transformation1 = mock(Transformation.class);
58         when(transformation1.getId()).thenReturn(new Id<Transformation>("t1"));
59         Transformation transformation2 = mock(Transformation.class);
60         when(transformation2.getId()).thenReturn(new Id<Transformation>("t2"));
61
62         config.transformationConfig().add(transformation1);
63
64         config.transformationConfig().add(transformation2);
65
66         assertEquals(2, config.transformationConfig().values().size());
67         Object firstValue = config.transformationConfig().values().iterator()
68             .next();
69         assertTrue(firstValue instanceof RobustTransformation);
70     }
71
72     @Test
73     public void testFilter() {
74         Filter filter1 = mock(Filter.class);
75         when(filter1.getId()).thenReturn(new Id<Filter>("f1"));
76         Filter filter2 = mock(Filter.class);
77         when(filter2.getId()).thenReturn(new Id<Filter>("f2"));
78
79         config.filterConfig().add(filter1);
80
81         config.filterConfig().add(filter2);
82
83         assertEquals(2, config.filterConfig().values().size());
84         Object firstValue = config.filterConfig().values().iterator().next();
85         assertTrue(firstValue instanceof RobustFilter);
86     }
87
88     @Test
89     public void testEqualityEmptyConfig() {
90         RouterConfig config1 = new SingleRouterConfig(new Id<RouterConfig>(
91             "routerconfig"));
92         RouterConfig config2 = new SingleRouterConfig(new Id<RouterConfig>(
93             "routerconfig"));
94         assertEquals(config1, config2);
95     }
96
97     @Test
98     public void testEqualsNonEmpty() {
99         RouterConfig config1 = new SingleRouterConfig(new Id<RouterConfig>(
100             "routerconfig"));
101
102         assertFalse(config1.equals(null));
103         assertFalse(config.equals("hello"));
104
105         RouterConfig config2 = new SingleRouterConfig(new Id<RouterConfig>(
106             "routerconfig"));
107         DocumentType type1 = mock(DocumentType.class);
108         when(type1.getId()).thenReturn(new Id<DocumentType>("type1"));
109         DocumentType type2 = mock(DocumentType.class);
110         when(type2.getId()).thenReturn(new Id<DocumentType>("type1"));
111         Filter filter1 = mock(Filter.class);
112         when(filter1.getId()).thenReturn(new Id<Filter>("f1"));
113         Filter filter2 = mock(Filter.class);
114         when(filter2.getId()).thenReturn(new Id<Filter>("f1"));
115         Transformation transformation1 = mock(Transformation.class);
116         when(transformation1.getId()).thenReturn(new Id<Transformation>("t1"));
117         Transformation transformation2 = mock(Transformation.class);
118         when(transformation2.getId()).thenReturn(new Id<Transformation>("t1"));
119
120         config1.documentTypeConfig().add(type1);
121         assertFalse(config1.equals(config2));
122
123         config2.documentTypeConfig().add(type2);
124         assertEquals(config1, config2);
125         assertEquals(config1.hashCode(), config2.hashCode());
126
127         config1.transformationConfig().add(transformation1);
128         config2.transformationConfig().add(transformation2);
129
130         assertEquals(config1, config2);
131         assertEquals(config1.hashCode(), config2.hashCode());
132
133         config1.filterConfig().add(filter1);
134         config2.filterConfig().add(filter2);
135
136         assertEquals(config1, config2);
137         assertEquals(config1.hashCode(), config2.hashCode());
138     }
139
140     @Test
141     public void testCopy() {
142         testDocumentType();
143         testFilter();
144         testTransformation();
145
146         SingleRouterConfig copy = new SingleRouterConfig(config);
147         assertEquals(config.getId(), copy.getId());
148         assertEquals(config, copy);
149
150         // verify the copy is not shallow.
151
152         config.documentTypeConfig().remove(new Id<DocumentType>("type1"));
153         config.transformationConfig().remove(new Id<Transformation>("t1"));
154         config.filterConfig().remove(new Id<Filter>("f1"));
155         assertEquals(1, config.documentTypeConfig().values().size());
156         assertEquals(1, config.transformationConfig().values().size());
157         assertEquals(1, config.filterConfig().values().size());
158         assertFalse(config.equals(copy));
159     }
160 }