Restructured the project creating a router directory for the router bundle implementa...
[xmlrouter] / router / impl / src / test / java / org / wamblee / xmlrouter / impl / ConfigImplTest.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 java.util.concurrent.atomic.AtomicLong;
22
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.wamblee.xmlrouter.common.Id;
26 import org.wamblee.xmlrouter.config.Identifiable;
27
28 public class ConfigImplTest {
29
30     private static final String CONFIG_TYPE = "transformation";
31
32     private static interface MyType extends Identifiable<MyType> {
33
34     }
35
36     private static class MyTypeWrapper implements MyType {
37         private MyType type;
38
39         public MyTypeWrapper(MyType aType) {
40             type = aType;
41         }
42
43         public MyType getType() {
44             return type;
45         }
46
47         @Override
48         public Id getId() {
49             return type.getId();
50         }
51     }
52
53     public static final class MyTypeConfig extends ConfigImpl<MyType> {
54         public MyTypeConfig(String aId) {
55             super(MyType.class, aId);
56         }
57
58         public MyTypeConfig(MyTypeConfig aConfig) {
59             super(aConfig);
60         }
61
62         @Override
63         public MyType wrap(MyType aT) {
64             return new MyTypeWrapper(aT);
65         }
66     }
67
68     private AtomicLong sequence;
69     private MyTypeConfig config;
70
71     @Before
72     public void setUp() {
73         sequence = new AtomicLong(1L);
74         config = new MyTypeConfig(CONFIG_TYPE);
75     }
76
77     @Test
78     public void testAdd() {
79         MyType type1 = mock(MyType.class);
80         when(type1.getId()).thenReturn(new Id("type1"));
81
82         config.add(type1);
83
84         assertEquals(1, config.values().size());
85         MyType firstValue = config.values().iterator().next();
86
87         assertTrue(firstValue instanceof MyTypeWrapper);
88         assertSame(type1, ((MyTypeWrapper) firstValue).getType());
89         assertEquals(type1.getId().getId(), firstValue.getId().getId());
90
91         // add another one.
92         MyType type2 = mock(MyType.class);
93         when(type2.getId()).thenReturn(new Id("type2"));
94         config.add(type2);
95
96         assertEquals(2, config.values().size());
97     }
98
99     @Test
100     public void testRemove() {
101         MyType type1 = mock(MyType.class);
102         when(type1.getId()).thenReturn(new Id("type1"));
103
104         config.add(type1);
105
106         assertEquals(1, config.values().size());
107
108         assertTrue(config.remove(new Id("type1")));
109         assertTrue(config.values().isEmpty());
110     }
111
112     @Test(expected = UnsupportedOperationException.class)
113     public void testUnmodifiable() {
114         config.values().add(mock(MyType.class));
115     }
116
117     @Test
118     public void testEquals() {
119
120         Config<MyType> config1 = new MyTypeConfig(CONFIG_TYPE);
121         assertFalse(config1.equals(null));
122         assertFalse(config1.equals("hello"));
123         Config<MyType> config2 = new MyTypeConfig(CONFIG_TYPE);
124
125         assertEquals(config1, config2);
126         assertEquals(config1.hashCode(), config2.hashCode());
127
128         MyType type1 = mock(MyType.class);
129         when(type1.getId()).thenReturn(new Id("type1"));
130
131         config1.add(type1);
132         assertFalse(config1.equals(config2));
133
134         MyType type2 = mock(MyType.class);
135         when(type2.getId()).thenReturn(new Id("type1"));
136
137         config2.add(type2);
138         assertEquals(config1, config2);
139         assertEquals(config1.hashCode(), config2.hashCode());
140
141         assertTrue(config2.remove(type2.getId()));
142         assertFalse(config1.equals(config2));
143     }
144
145     @Test
146     public void testCopy() {
147         testAdd();
148         assertEquals(2, config.values().size());
149         MyTypeConfig copy = new MyTypeConfig(config);
150         assertEquals(config.getPrefix(), copy.getPrefix());
151         assertEquals(config, copy);
152
153         // verify the copy is not shallow
154         assertTrue(config.remove(new Id<MyType>("type1")));
155         assertEquals(1, config.values().size());
156         assertFalse(config.equals(copy));
157     }
158 }