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