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.Config;
26 import org.wamblee.xmlrouter.config.ConfigException;
27 import org.wamblee.xmlrouter.config.Identifiable;
29 public class CompositeConfigTest {
31 public static interface StringClassInterface extends
32 Identifiable<StringClassInterface> {
36 public static class StringClass implements StringClassInterface {
40 public StringClass(String aValue) {
44 public StringClass(int aValue) {
49 public Id<StringClassInterface> getId() {
50 return new Id<StringClassInterface>(value + "");
54 public int hashCode() {
55 return value.hashCode();
59 public boolean equals(Object aObj) {
63 if (!(aObj instanceof StringClass)) {
66 StringClass obj = (StringClass) aObj;
67 return value.equals(obj.value);
72 public void testEmptyConfig() {
73 Config<StringClassInterface> composite = composite();
74 assertTrue(composite.values().isEmpty());
77 @Test(expected = RuntimeException.class)
78 public void testAddNotAllowed() {
79 composite().add(new StringClass(10));
82 @Test(expected = RuntimeException.class)
83 public void testRemoveNotAllowed() {
84 composite().remove(new Id<StringClassInterface>("xxx"));
88 public void testAddConfig() {
89 CompositeConfig<StringClassInterface> composite = composite();
90 Config<StringClassInterface> c1 = new ConfigImpl(
91 StringClassInterface.class, id("c1")) {
93 public Identifiable wrap(String aPrefix, Identifiable aT) {
97 Config<StringClassInterface> c2 = new ConfigImpl(
98 StringClassInterface.class, id("c2")) {
100 public Identifiable wrap(String aPrefix, Identifiable aT) {
105 StringClass i1 = new StringClass(10);
106 StringClass i2 = new StringClass(20);
107 StringClass i3 = new StringClass(30);
108 StringClass i4 = new StringClass(40);
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());
121 assertTrue(ids.contains("c1.10"));
122 assertTrue(ids.contains("c1.20"));
124 composite.addConfig(c2);
125 values = composite.values();
126 assertEquals(4, values.size());
128 ids = new ArrayList<String>();
129 for (StringClassInterface intf : values) {
130 ids.add(intf.getId().getId());
132 assertTrue(ids.contains("c1.10"));
133 assertTrue(ids.contains("c1.20"));
134 assertTrue(ids.contains("c2.30"));
135 assertTrue(ids.contains("c2.40"));
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")) {
144 public Identifiable wrap(String aPrefix, Identifiable aT) {
148 Config<StringClassInterface> c2 = new ConfigImpl(
149 StringClassInterface.class, id("c1")) {
151 public Identifiable wrap(String aPrefix, Identifiable aT) {
155 composite.addConfig(c1);
156 composite.addConfig(c2);
160 public void testDuplicateItem() {
161 CompositeConfig<StringClassInterface> composite = composite();
162 Config<StringClassInterface> c1 = new ConfigImpl(
163 StringClassInterface.class, id("c.x")) {
165 public Identifiable wrap(String aPrefix, Identifiable aT) {
169 Config<StringClassInterface> c2 = new ConfigImpl(
170 StringClassInterface.class, id("c")) {
172 public Identifiable wrap(String aPrefix, Identifiable aT) {
177 StringClass i1 = new StringClass("y");
178 StringClass i2 = new StringClass("x.y");
181 composite.addConfig(c1);
183 composite.addConfig(c2);
185 } catch (ConfigException e) {
188 assertEquals(1, composite.values().size());
189 assertEquals("c.x.y", composite.values().iterator().next().getId()
193 private CompositeConfig<StringClassInterface> composite() {
194 return new CompositeConfig<StringClassInterface>(
195 StringClassInterface.class);
198 private Id<Config> id(String aId) {
199 return new Id<Config>(aId);