CompositeConfig tested.
[xmlrouter] / impl / src / test / java / org / wamblee / xmlrouter / impl / CompositeConfigTest.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
20 import java.util.List;
21
22 import org.junit.Test;
23 import org.wamblee.xmlrouter.common.Id;
24 import org.wamblee.xmlrouter.config.Config;
25 import org.wamblee.xmlrouter.config.DuplicateException;
26 import org.wamblee.xmlrouter.config.Identifiable;
27
28 public class CompositeConfigTest {
29
30     public static class IntClass implements Identifiable<IntClass> {
31
32         private int value;
33
34         public IntClass(int aValue) {
35             value = aValue;
36         }
37
38         @Override
39         public Id<IntClass> getId() {
40             return new Id<IntClass>(value + "");
41         }
42
43         @Override
44         public int hashCode() {
45             return ((Integer) value).hashCode();
46         }
47
48         @Override
49         public boolean equals(Object aObj) {
50             if (aObj == null) {
51                 return false;
52             }
53             if (!(aObj instanceof IntClass)) {
54                 return false;
55             }
56             IntClass obj = (IntClass) aObj;
57             return value == obj.value;
58         }
59     }
60
61     @Test
62     public void testEmptyConfig() {
63         Config<IntClass> composite = composite("c");
64         assertEquals(id("c"), composite.getId());
65         assertTrue(composite.values().isEmpty());
66     }
67
68     @Test(expected = RuntimeException.class)
69     public void testAddNotAllowed() {
70         composite("c").add(new IntClass(10));
71     }
72
73     @Test(expected = RuntimeException.class)
74     public void testRemoveNotAllowed() {
75         composite("c").remove(new Id<IntClass>("xxx"));
76     }
77
78     @Test
79     public void testAddConfig() {
80         CompositeConfig<IntClass> composite = composite("c");
81         Config<IntClass> c1 = new ConfigImpl(id("c1")) {
82             @Override
83             public Identifiable wrap(String aPrefix, Identifiable aT) {
84                 return aT;
85             }
86         };
87         Config<IntClass> c2 = new ConfigImpl(id("c2")) {
88             @Override
89             public Identifiable wrap(String aPrefix, Identifiable aT) {
90                 return aT;
91             }
92         };
93
94         IntClass i1 = new IntClass(10);
95         IntClass i2 = new IntClass(20);
96         IntClass i3 = new IntClass(30);
97         IntClass i4 = new IntClass(40);
98
99         c1.add(i1);
100         c1.add(i2);
101         c2.add(i3);
102         c2.add(i4);
103
104         composite.addConfig(c1);
105         List<IntClass> values = composite.values();
106         assertEquals(2, values.size());
107         assertTrue(values.contains(i1));
108         assertTrue(values.contains(i2));
109
110         composite.addConfig(c2);
111         values = composite.values();
112         assertEquals(4, values.size());
113         assertTrue(values.contains(i1));
114         assertTrue(values.contains(i2));
115         assertTrue(values.contains(i3));
116         assertTrue(values.contains(i4));
117     }
118
119     @Test(expected = DuplicateException.class)
120     public void testDuplicatesNotAllowed() {
121         CompositeConfig<IntClass> composite = composite("c");
122         Config<IntClass> c1 = new ConfigImpl(id("c1")) {
123             @Override
124             public Identifiable wrap(String aPrefix, Identifiable aT) {
125                 return aT;
126             }
127         };
128         Config<IntClass> c2 = new ConfigImpl(id("c1")) {
129             @Override
130             public Identifiable wrap(String aPrefix, Identifiable aT) {
131                 return aT;
132             }
133         };
134         composite.addConfig(c1);
135         composite.addConfig(c2);
136     }
137
138     @Test
139     public void testDuplicateItem() {
140         CompositeConfig<IntClass> composite = composite("c");
141         Config<IntClass> c1 = new ConfigImpl(id("c1")) {
142             @Override
143             public Identifiable wrap(String aPrefix, Identifiable aT) {
144                 return aT;
145             }
146         };
147         Config<IntClass> c2 = new ConfigImpl(id("c2")) {
148             @Override
149             public Identifiable wrap(String aPrefix, Identifiable aT) {
150                 return aT;
151             }
152         };
153
154         IntClass i1 = new IntClass(10);
155         IntClass i2 = new IntClass(10);
156         c1.add(i1);
157         c2.add(i2);
158         composite.addConfig(c1);
159         try {
160             composite.addConfig(c2);
161             fail();
162         } catch (DuplicateException e) {
163             // ok.
164         }
165         assertEquals(1, composite.values().size());
166         assertTrue(composite.values().contains(i1));
167     }
168
169     private CompositeConfig<IntClass> composite(String aId) {
170         return new CompositeConfig<IntClass>(id(aId));
171     }
172
173     private Id<Config> id(String aId) {
174         return new Id<Config>(aId);
175     }
176 }