Config no longer implements Identifiable because this was in violation of the contrac...
[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.Transformation;
27
28 public class SingleRouterConfigTest {
29
30     private SingleRouterConfig config;
31
32     @Before
33     public void setUp() {
34         config = new SingleRouterConfig("routerconfig");
35         assertEquals("routerconfig", config.getId().getId());
36     }
37
38     @Test
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"));
44
45         config.documentTypeConfig().add(type1);
46         config.documentTypeConfig().add(type2);
47
48         assertEquals(2, config.documentTypeConfig().values().size());
49         Object firstValue = config.documentTypeConfig().values().iterator()
50             .next();
51         assertTrue(firstValue instanceof RobustDocumentType);
52     }
53
54     @Test
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"));
60
61         config.transformationConfig().add(transformation1);
62
63         config.transformationConfig().add(transformation2);
64
65         assertEquals(2, config.transformationConfig().values().size());
66         Object firstValue = config.transformationConfig().values().iterator()
67             .next();
68         assertTrue(firstValue instanceof RobustTransformation);
69     }
70
71     @Test
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"));
77
78         config.filterConfig().add(filter1);
79
80         config.filterConfig().add(filter2);
81
82         assertEquals(2, config.filterConfig().values().size());
83         Object firstValue = config.filterConfig().values().iterator().next();
84         assertTrue(firstValue instanceof RobustFilter);
85     }
86
87     @Test
88     public void testEqualityEmptyConfig() {
89         RouterConfig config1 = new SingleRouterConfig("routerconfig");
90         RouterConfig config2 = new SingleRouterConfig("routerconfig");
91         assertEquals(config1, config2);
92     }
93
94     @Test
95     public void testEqualsNonEmpty() {
96         RouterConfig config1 = new SingleRouterConfig("routerconfig");
97
98         assertFalse(config1.equals(null));
99         assertFalse(config.equals("hello"));
100
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"));
114
115         config1.documentTypeConfig().add(type1);
116         assertFalse(config1.equals(config2));
117
118         config2.documentTypeConfig().add(type2);
119         assertEquals(config1, config2);
120         assertEquals(config1.hashCode(), config2.hashCode());
121
122         config1.transformationConfig().add(transformation1);
123         config2.transformationConfig().add(transformation2);
124
125         assertEquals(config1, config2);
126         assertEquals(config1.hashCode(), config2.hashCode());
127
128         config1.filterConfig().add(filter1);
129         config2.filterConfig().add(filter2);
130
131         assertEquals(config1, config2);
132         assertEquals(config1.hashCode(), config2.hashCode());
133     }
134
135     @Test
136     public void testCopy() {
137         testDocumentType();
138         testFilter();
139         testTransformation();
140
141         SingleRouterConfig copy = new SingleRouterConfig(config);
142         assertEquals(config.getId(), copy.getId());
143         assertEquals(config, copy);
144
145         // verify the copy is not shallow.
146
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));
154     }
155 }