85860122b71c75bcac18fb61a6b469e000ffb780
[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(MyType.class, 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 MyTypeConfig(new Id<Config>(CONFIG_TYPE));
125         assertFalse(config1.equals(null));
126         assertFalse(config1.equals("hello"));
127         Config<MyType> config2 = new MyTypeConfig(new Id<Config>(CONFIG_TYPE));
128
129         assertEquals(config1, config2);
130         assertEquals(config1.hashCode(), config2.hashCode());
131
132         MyType type1 = mock(MyType.class);
133         when(type1.getId()).thenReturn(new Id("type1"));
134
135         config1.add(type1);
136         assertFalse(config1.equals(config2));
137
138         MyType type2 = mock(MyType.class);
139         when(type2.getId()).thenReturn(new Id("type1"));
140
141         config2.add(type2);
142         assertEquals(config1, config2);
143         assertEquals(config1.hashCode(), config2.hashCode());
144
145         assertTrue(config2.remove(type2.getId()));
146         assertFalse(config1.equals(config2));
147     }
148
149     @Test
150     public void testCopy() {
151         testAdd();
152         assertEquals(2, config.values().size());
153         MyTypeConfig copy = new MyTypeConfig(config);
154         assertEquals(config.getId(), config.getId());
155         assertEquals(config, copy);
156
157         // verify the copy is not shallow
158         assertTrue(config.remove(new Id<MyType>("type1")));
159         assertEquals(1, config.values().size());
160         assertFalse(config.equals(copy));
161     }
162 }