8ebdb9f4aa16c2246dc5ff50b494239fab9dc1c8
[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     private AtomicLong sequence;
57     private ExtendedConfig<MyType> config;
58
59     @Before
60     public void setUp() {
61         sequence = new AtomicLong(1L);
62         config = new ConfigImpl<MyType>(new Id<Config>(CONFIG_TYPE)) {
63             @Override
64             public MyType wrap(String aPrefix, MyType aT) {
65                 return new MyTypeWrapper(aPrefix, aT);
66             }
67         };
68     }
69
70     @Test
71     public void testAdd() {
72         MyType type1 = mock(MyType.class);
73         when(type1.getId()).thenReturn(new Id("type1"));
74
75         config.add(type1);
76
77         assertEquals(1, config.values().size());
78         MyType firstValue = config.values().iterator().next();
79
80         assertTrue(firstValue instanceof MyTypeWrapper);
81         assertSame(type1, ((MyTypeWrapper) firstValue).getType());
82         assertEquals(CONFIG_TYPE + "." + type1.getId().getId(), firstValue
83             .getId().getId());
84
85         // add another one.
86         MyType type2 = mock(MyType.class);
87         when(type2.getId()).thenReturn(new Id("type2"));
88         config.add(type2);
89
90         assertEquals(2, config.values().size());
91     }
92
93     @Test
94     public void testRemove() {
95         MyType type1 = mock(MyType.class);
96         when(type1.getId()).thenReturn(new Id("type1"));
97
98         config.add(type1);
99
100         assertEquals(1, config.values().size());
101
102         assertTrue(config.remove(new Id("type1")));
103         assertTrue(config.values().isEmpty());
104     }
105
106     @Test(expected = UnsupportedOperationException.class)
107     public void testUnmodifiable() {
108         config.values().add(mock(MyType.class));
109     }
110
111     @Test
112     public void testEquals() {
113
114         Config<MyType> config1 = new ConfigImpl<MyType>(new Id<Config>(
115             CONFIG_TYPE)) {
116             @Override
117             public MyType wrap(String aPrefix, MyType aT) {
118                 return new MyTypeWrapper(aPrefix, aT);
119             }
120         };
121         assertFalse(config1.equals(null));
122         assertFalse(config1.equals("hello"));
123         Config<MyType> config2 = new ConfigImpl<MyType>(new Id<Config>(
124             CONFIG_TYPE)) {
125             @Override
126             public MyType wrap(String aPrefix, MyType aT) {
127                 return new MyTypeWrapper(aPrefix, aT);
128             }
129         };
130         assertEquals(config1, config2);
131         assertEquals(config1.hashCode(), config2.hashCode());
132
133         MyType type1 = mock(MyType.class);
134         when(type1.getId()).thenReturn(new Id("type1"));
135
136         config1.add(type1);
137         assertFalse(config1.equals(config2));
138
139         MyType type2 = mock(MyType.class);
140         when(type2.getId()).thenReturn(new Id("type1"));
141
142         config2.add(type2);
143         assertEquals(config1, config2);
144         assertEquals(config1.hashCode(), config2.hashCode());
145
146         assertTrue(config2.remove(type2.getId()));
147         assertFalse(config1.equals(config2));
148     }
149 }