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();
64 assertTrue(composite.values().isEmpty());
67 @Test(expected = RuntimeException.class)
68 public void testAddNotAllowed() {
69 composite().add(new IntClass(10));
72 @Test(expected = RuntimeException.class)
73 public void testRemoveNotAllowed() {
74 composite().remove(new Id<IntClass>("xxx"));
78 public void testAddConfig() {
79 CompositeConfig<IntClass> composite = composite();
80 Config<IntClass> c1 = new ConfigImpl(id("c1")) {
82 public Identifiable wrap(String aPrefix, Identifiable aT) {
86 Config<IntClass> c2 = new ConfigImpl(id("c2")) {
88 public Identifiable wrap(String aPrefix, Identifiable aT) {
93 IntClass i1 = new IntClass(10);
94 IntClass i2 = new IntClass(20);
95 IntClass i3 = new IntClass(30);
96 IntClass i4 = new IntClass(40);
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));
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));
118 @Test(expected = ConfigException.class)
119 public void testDuplicatesNotAllowed() {
120 CompositeConfig<IntClass> composite = composite();
121 Config<IntClass> c1 = new ConfigImpl(id("c1")) {
123 public Identifiable wrap(String aPrefix, Identifiable aT) {
127 Config<IntClass> c2 = new ConfigImpl(id("c1")) {
129 public Identifiable wrap(String aPrefix, Identifiable aT) {
133 composite.addConfig(c1);
134 composite.addConfig(c2);
138 public void testDuplicateItem() {
139 CompositeConfig<IntClass> composite = composite();
140 Config<IntClass> c1 = new ConfigImpl(id("c1")) {
142 public Identifiable wrap(String aPrefix, Identifiable aT) {
146 Config<IntClass> c2 = new ConfigImpl(id("c2")) {
148 public Identifiable wrap(String aPrefix, Identifiable aT) {
153 IntClass i1 = new IntClass(10);
154 IntClass i2 = new IntClass(10);
157 composite.addConfig(c1);
159 composite.addConfig(c2);
161 } catch (ConfigException e) {
164 assertEquals(1, composite.values().size());
165 assertTrue(composite.values().contains(i1));
168 private CompositeConfig<IntClass> composite() {
169 return new CompositeConfig<IntClass>();
172 private Id<Config> id(String aId) {
173 return new Id<Config>(aId);