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.List;
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;
28 public class CompositeConfigTest {
30 public static class IntClass implements Identifiable<IntClass> {
34 public IntClass(int aValue) {
39 public Id<IntClass> getId() {
40 return new Id<IntClass>(value + "");
44 public int hashCode() {
45 return ((Integer) value).hashCode();
49 public boolean equals(Object aObj) {
53 if (!(aObj instanceof IntClass)) {
56 IntClass obj = (IntClass) aObj;
57 return value == obj.value;
62 public void testEmptyConfig() {
63 Config<IntClass> composite = composite("c");
64 assertEquals(id("c"), composite.getId());
65 assertTrue(composite.values().isEmpty());
68 @Test(expected = RuntimeException.class)
69 public void testAddNotAllowed() {
70 composite("c").add(new IntClass(10));
73 @Test(expected = RuntimeException.class)
74 public void testRemoveNotAllowed() {
75 composite("c").remove(new Id<IntClass>("xxx"));
79 public void testAddConfig() {
80 CompositeConfig<IntClass> composite = composite("c");
81 Config<IntClass> c1 = new ConfigImpl(id("c1")) {
83 public Identifiable wrap(String aPrefix, Identifiable aT) {
87 Config<IntClass> c2 = new ConfigImpl(id("c2")) {
89 public Identifiable wrap(String aPrefix, Identifiable aT) {
94 IntClass i1 = new IntClass(10);
95 IntClass i2 = new IntClass(20);
96 IntClass i3 = new IntClass(30);
97 IntClass i4 = new IntClass(40);
104 composite.addConfig(c1);
105 List<IntClass> values = composite.values();
106 assertEquals(2, values.size());
107 assertTrue(values.contains(i1));
108 assertTrue(values.contains(i2));
110 composite.addConfig(c2);
111 values = composite.values();
112 assertEquals(4, values.size());
113 assertTrue(values.contains(i1));
114 assertTrue(values.contains(i2));
115 assertTrue(values.contains(i3));
116 assertTrue(values.contains(i4));
119 @Test(expected = ConfigException.class)
120 public void testDuplicatesNotAllowed() {
121 CompositeConfig<IntClass> composite = composite("c");
122 Config<IntClass> c1 = new ConfigImpl(id("c1")) {
124 public Identifiable wrap(String aPrefix, Identifiable aT) {
128 Config<IntClass> c2 = new ConfigImpl(id("c1")) {
130 public Identifiable wrap(String aPrefix, Identifiable aT) {
134 composite.addConfig(c1);
135 composite.addConfig(c2);
139 public void testDuplicateItem() {
140 CompositeConfig<IntClass> composite = composite("c");
141 Config<IntClass> c1 = new ConfigImpl(id("c1")) {
143 public Identifiable wrap(String aPrefix, Identifiable aT) {
147 Config<IntClass> c2 = new ConfigImpl(id("c2")) {
149 public Identifiable wrap(String aPrefix, Identifiable aT) {
154 IntClass i1 = new IntClass(10);
155 IntClass i2 = new IntClass(10);
158 composite.addConfig(c1);
160 composite.addConfig(c2);
162 } catch (ConfigException e) {
165 assertEquals(1, composite.values().size());
166 assertTrue(composite.values().contains(i1));
169 private CompositeConfig<IntClass> composite(String aId) {
170 return new CompositeConfig<IntClass>(id(aId));
173 private Id<Config> id(String aId) {
174 return new Id<Config>(aId);