X-Git-Url: http://wamblee.org/gitweb/?a=blobdiff_plain;ds=sidebyside;f=impl%2Fsrc%2Ftest%2Fjava%2Forg%2Fwamblee%2Fxmlrouter%2Fimpl%2FCompositeConfigTest.java;fp=impl%2Fsrc%2Ftest%2Fjava%2Forg%2Fwamblee%2Fxmlrouter%2Fimpl%2FCompositeConfigTest.java;h=1875d4b96d6ab65238eb6b1629a77f22242acaae;hb=03a6b404471945aed9d48fc1e5b8447b4a9d9413;hp=0000000000000000000000000000000000000000;hpb=7b7444ecb86a1af3eb11013e94776c2860205f2a;p=xmlrouter diff --git a/impl/src/test/java/org/wamblee/xmlrouter/impl/CompositeConfigTest.java b/impl/src/test/java/org/wamblee/xmlrouter/impl/CompositeConfigTest.java new file mode 100644 index 0000000..1875d4b --- /dev/null +++ b/impl/src/test/java/org/wamblee/xmlrouter/impl/CompositeConfigTest.java @@ -0,0 +1,176 @@ +/* + * 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.List; + +import org.junit.Test; +import org.wamblee.xmlrouter.common.Id; +import org.wamblee.xmlrouter.config.Config; +import org.wamblee.xmlrouter.config.DuplicateException; +import org.wamblee.xmlrouter.config.Identifiable; + +public class CompositeConfigTest { + + public static class IntClass implements Identifiable { + + private int value; + + public IntClass(int aValue) { + value = aValue; + } + + @Override + public Id getId() { + return new Id(value + ""); + } + + @Override + public int hashCode() { + return ((Integer) value).hashCode(); + } + + @Override + public boolean equals(Object aObj) { + if (aObj == null) { + return false; + } + if (!(aObj instanceof IntClass)) { + return false; + } + IntClass obj = (IntClass) aObj; + return value == obj.value; + } + } + + @Test + public void testEmptyConfig() { + Config composite = composite("c"); + assertEquals(id("c"), composite.getId()); + assertTrue(composite.values().isEmpty()); + } + + @Test(expected = RuntimeException.class) + public void testAddNotAllowed() { + composite("c").add(new IntClass(10)); + } + + @Test(expected = RuntimeException.class) + public void testRemoveNotAllowed() { + composite("c").remove(new Id("xxx")); + } + + @Test + public void testAddConfig() { + CompositeConfig composite = composite("c"); + Config c1 = new ConfigImpl(id("c1")) { + @Override + public Identifiable wrap(String aPrefix, Identifiable aT) { + return aT; + } + }; + Config c2 = new ConfigImpl(id("c2")) { + @Override + public Identifiable wrap(String aPrefix, Identifiable aT) { + return aT; + } + }; + + IntClass i1 = new IntClass(10); + IntClass i2 = new IntClass(20); + IntClass i3 = new IntClass(30); + IntClass i4 = new IntClass(40); + + c1.add(i1); + c1.add(i2); + c2.add(i3); + c2.add(i4); + + composite.addConfig(c1); + List values = composite.values(); + assertEquals(2, values.size()); + assertTrue(values.contains(i1)); + assertTrue(values.contains(i2)); + + composite.addConfig(c2); + values = composite.values(); + assertEquals(4, values.size()); + assertTrue(values.contains(i1)); + assertTrue(values.contains(i2)); + assertTrue(values.contains(i3)); + assertTrue(values.contains(i4)); + } + + @Test(expected = DuplicateException.class) + public void testDuplicatesNotAllowed() { + CompositeConfig composite = composite("c"); + Config c1 = new ConfigImpl(id("c1")) { + @Override + public Identifiable wrap(String aPrefix, Identifiable aT) { + return aT; + } + }; + Config c2 = new ConfigImpl(id("c1")) { + @Override + public Identifiable wrap(String aPrefix, Identifiable aT) { + return aT; + } + }; + composite.addConfig(c1); + composite.addConfig(c2); + } + + @Test + public void testDuplicateItem() { + CompositeConfig composite = composite("c"); + Config c1 = new ConfigImpl(id("c1")) { + @Override + public Identifiable wrap(String aPrefix, Identifiable aT) { + return aT; + } + }; + Config c2 = new ConfigImpl(id("c2")) { + @Override + public Identifiable wrap(String aPrefix, Identifiable aT) { + return aT; + } + }; + + IntClass i1 = new IntClass(10); + IntClass i2 = new IntClass(10); + c1.add(i1); + c2.add(i2); + composite.addConfig(c1); + try { + composite.addConfig(c2); + fail(); + } catch (DuplicateException e) { + // ok. + } + assertEquals(1, composite.values().size()); + assertTrue(composite.values().contains(i1)); + } + + private CompositeConfig composite(String aId) { + return new CompositeConfig(id(aId)); + } + + private Id id(String aId) { + return new Id(aId); + } +}