simplifications.
[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.ConfigException;
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();
64         assertTrue(composite.values().isEmpty());
65     }
66
67     @Test(expected = RuntimeException.class)
68     public void testAddNotAllowed() {
69         composite().add(new IntClass(10));
70     }
71
72     @Test(expected = RuntimeException.class)
73     public void testRemoveNotAllowed() {
74         composite().remove(new Id<IntClass>("xxx"));
75     }
76
77     @Test
78     public void testAddConfig() {
79         CompositeConfig<IntClass> composite = composite();
80         Config<IntClass> c1 = new ConfigImpl(id("c1")) {
81             @Override
82             public Identifiable wrap(String aPrefix, Identifiable aT) {
83                 return aT;
84             }
85         };
86         Config<IntClass> c2 = new ConfigImpl(id("c2")) {
87             @Override
88             public Identifiable wrap(String aPrefix, Identifiable aT) {
89                 return aT;
90             }
91         };
92
93         IntClass i1 = new IntClass(10);
94         IntClass i2 = new IntClass(20);
95         IntClass i3 = new IntClass(30);
96         IntClass i4 = new IntClass(40);
97
98         c1.add(i1);
99         c1.add(i2);
100         c2.add(i3);
101         c2.add(i4);
102
103         composite.addConfig(c1);
104         List<IntClass> values = composite.values();
105         assertEquals(2, values.size());
106         assertTrue(values.contains(i1));
107         assertTrue(values.contains(i2));
108
109         composite.addConfig(c2);
110         values = composite.values();
111         assertEquals(4, values.size());
112         assertTrue(values.contains(i1));
113         assertTrue(values.contains(i2));
114         assertTrue(values.contains(i3));
115         assertTrue(values.contains(i4));
116     }
117
118     @Test(expected = ConfigException.class)
119     public void testDuplicatesNotAllowed() {
120         CompositeConfig<IntClass> composite = composite();
121         Config<IntClass> c1 = new ConfigImpl(id("c1")) {
122             @Override
123             public Identifiable wrap(String aPrefix, Identifiable aT) {
124                 return aT;
125             }
126         };
127         Config<IntClass> c2 = new ConfigImpl(id("c1")) {
128             @Override
129             public Identifiable wrap(String aPrefix, Identifiable aT) {
130                 return aT;
131             }
132         };
133         composite.addConfig(c1);
134         composite.addConfig(c2);
135     }
136
137     @Test
138     public void testDuplicateItem() {
139         CompositeConfig<IntClass> composite = composite();
140         Config<IntClass> c1 = new ConfigImpl(id("c1")) {
141             @Override
142             public Identifiable wrap(String aPrefix, Identifiable aT) {
143                 return aT;
144             }
145         };
146         Config<IntClass> c2 = new ConfigImpl(id("c2")) {
147             @Override
148             public Identifiable wrap(String aPrefix, Identifiable aT) {
149                 return aT;
150             }
151         };
152
153         IntClass i1 = new IntClass(10);
154         IntClass i2 = new IntClass(10);
155         c1.add(i1);
156         c2.add(i2);
157         composite.addConfig(c1);
158         try {
159             composite.addConfig(c2);
160             fail();
161         } catch (ConfigException e) {
162             // ok.
163         }
164         assertEquals(1, composite.values().size());
165         assertTrue(composite.values().contains(i1));
166     }
167
168     private CompositeConfig<IntClass> composite() {
169         return new CompositeConfig<IntClass>();
170     }
171
172     private Id<Config> id(String aId) {
173         return new Id<Config>(aId);
174     }
175 }