/* * Copyright 2005-2011 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.wamblee.xmlrouter.impl; import static junit.framework.Assert.*; import java.util.ArrayList; import java.util.List; import org.junit.Test; import org.wamblee.xmlrouter.common.Id; import org.wamblee.xmlrouter.config.Config; import org.wamblee.xmlrouter.config.ConfigException; import org.wamblee.xmlrouter.config.Identifiable; public class CompositeConfigTest { public static interface StringClassInterface extends Identifiable { } public static class StringClass implements StringClassInterface { private String value; public StringClass(String aValue) { value = aValue; } public StringClass(int aValue) { this(aValue + ""); } @Override public Id getId() { return new Id(value + ""); } @Override public int hashCode() { return value.hashCode(); } @Override public boolean equals(Object aObj) { if (aObj == null) { return false; } if (!(aObj instanceof StringClass)) { return false; } StringClass obj = (StringClass) aObj; return value.equals(obj.value); } } @Test public void testEmptyConfig() { Config composite = composite(); assertTrue(composite.values().isEmpty()); } @Test(expected = RuntimeException.class) public void testAddNotAllowed() { composite().add(new StringClass(10)); } @Test(expected = RuntimeException.class) public void testRemoveNotAllowed() { composite().remove(new Id("xxx")); } @Test public void testAddConfig() { CompositeConfig composite = composite(); Config c1 = new ConfigImpl( StringClassInterface.class, id("c1")) { @Override public Identifiable wrap(Identifiable aT) { return aT; } }; Config c2 = new ConfigImpl( StringClassInterface.class, id("c2")) { @Override public Identifiable wrap(Identifiable aT) { return aT; } }; StringClass i1 = new StringClass(10); StringClass i2 = new StringClass(20); StringClass i3 = new StringClass(30); StringClass i4 = new StringClass(40); c1.add(i1); c1.add(i2); c2.add(i3); c2.add(i4); composite.addConfig(c1); List values = composite.values(); List ids = new ArrayList(); for (StringClassInterface intf : values) { ids.add(intf.getId().getId()); } assertTrue(ids.contains("c1.10")); assertTrue(ids.contains("c1.20")); composite.addConfig(c2); values = composite.values(); assertEquals(4, values.size()); ids = new ArrayList(); for (StringClassInterface intf : values) { ids.add(intf.getId().getId()); } assertTrue(ids.contains("c1.10")); assertTrue(ids.contains("c1.20")); assertTrue(ids.contains("c2.30")); assertTrue(ids.contains("c2.40")); } @Test(expected = ConfigException.class) public void testDuplicatesNotAllowed() { CompositeConfig composite = composite(); Config c1 = new ConfigImpl( StringClassInterface.class, id("c1")) { @Override public Identifiable wrap(Identifiable aT) { return aT; } }; Config c2 = new ConfigImpl( StringClassInterface.class, id("c1")) { @Override public Identifiable wrap(Identifiable aT) { return aT; } }; composite.addConfig(c1); composite.addConfig(c2); } @Test public void testDuplicateItem() { CompositeConfig composite = composite(); Config c1 = new ConfigImpl( StringClassInterface.class, id("c.x")) { @Override public Identifiable wrap(Identifiable aT) { return aT; } }; Config c2 = new ConfigImpl( StringClassInterface.class, id("c")) { @Override public Identifiable wrap(Identifiable aT) { return aT; } }; StringClass i1 = new StringClass("y"); StringClass i2 = new StringClass("x.y"); c1.add(i1); c2.add(i2); composite.addConfig(c1); try { composite.addConfig(c2); fail(); } catch (ConfigException e) { // ok. } assertEquals(1, composite.values().size()); assertEquals("c.x.y", composite.values().iterator().next().getId() .getId()); } private CompositeConfig composite() { return new CompositeConfig( StringClassInterface.class); } private Id id(String aId) { return new Id(aId); } }