2 * Copyright 2005-2011 the original author or authors.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.wamblee.xmlrouter.impl;
18 import static junit.framework.Assert.*;
20 import java.util.ArrayList;
21 import java.util.List;
23 import org.junit.Test;
24 import org.wamblee.xmlrouter.common.Id;
25 import org.wamblee.xmlrouter.config.Identifiable;
27 public class CompositeConfigTest {
29 public static interface StringClassInterface extends
30 Identifiable<StringClassInterface> {
34 public static class StringClass implements StringClassInterface {
38 public StringClass(String aValue) {
42 public StringClass(int aValue) {
47 public Id<StringClassInterface> getId() {
48 return new Id<StringClassInterface>(value + "");
52 public int hashCode() {
53 return value.hashCode();
57 public boolean equals(Object aObj) {
61 if (!(aObj instanceof StringClass)) {
64 StringClass obj = (StringClass) aObj;
65 return value.equals(obj.value);
70 public void testEmptyConfig() {
71 Config<StringClassInterface> composite = composite();
72 assertTrue(composite.values().isEmpty());
75 @Test(expected = RuntimeException.class)
76 public void testAddNotAllowed() {
77 composite().add(new StringClass(10));
80 @Test(expected = RuntimeException.class)
81 public void testRemoveNotAllowed() {
82 composite().remove(new Id<StringClassInterface>("xxx"));
86 public void testAddConfig() {
87 CompositeConfig<StringClassInterface> composite = composite();
88 Config<StringClassInterface> c1 = new ConfigImpl(
89 StringClassInterface.class, "c1") {
91 public Identifiable wrap(Identifiable aT) {
95 Config<StringClassInterface> c2 = new ConfigImpl(
96 StringClassInterface.class, "c2") {
98 public Identifiable wrap(Identifiable aT) {
103 StringClass i1 = new StringClass(10);
104 StringClass i2 = new StringClass(20);
105 StringClass i3 = new StringClass(30);
106 StringClass i4 = new StringClass(40);
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());
119 assertTrue(ids.contains("c1.10"));
120 assertTrue(ids.contains("c1.20"));
122 composite.addConfig(c2);
123 values = composite.values();
124 assertEquals(4, values.size());
126 ids = new ArrayList<String>();
127 for (StringClassInterface intf : values) {
128 ids.add(intf.getId().getId());
130 assertTrue(ids.contains("c1.10"));
131 assertTrue(ids.contains("c1.20"));
132 assertTrue(ids.contains("c2.30"));
133 assertTrue(ids.contains("c2.40"));
136 @Test(expected = ConfigException.class)
137 public void testDuplicatesNotAllowed() {
138 CompositeConfig<StringClassInterface> composite = composite();
139 Config<StringClassInterface> c1 = new ConfigImpl(
140 StringClassInterface.class, "c1") {
142 public Identifiable wrap(Identifiable aT) {
146 Config<StringClassInterface> c2 = new ConfigImpl(
147 StringClassInterface.class, "c1") {
149 public Identifiable wrap(Identifiable aT) {
153 composite.addConfig(c1);
154 composite.addConfig(c2);
158 public void testDuplicateItem() {
159 CompositeConfig<StringClassInterface> composite = composite();
160 Config<StringClassInterface> c1 = new ConfigImpl(
161 StringClassInterface.class, "c.x") {
163 public Identifiable wrap(Identifiable aT) {
167 Config<StringClassInterface> c2 = new ConfigImpl(
168 StringClassInterface.class, "c") {
170 public Identifiable wrap(Identifiable aT) {
175 StringClass i1 = new StringClass("y");
176 StringClass i2 = new StringClass("x.y");
179 composite.addConfig(c1);
181 composite.addConfig(c2);
183 } catch (ConfigException e) {
186 assertEquals(1, composite.values().size());
187 assertEquals("c.x.y", composite.values().iterator().next().getId()
191 private CompositeConfig<StringClassInterface> composite() {
192 return new CompositeConfig<StringClassInterface>(
193 StringClassInterface.class);
196 private Id<Config> id(String aId) {
197 return new Id<Config>(aId);