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