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