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