X-Git-Url: http://wamblee.org/gitweb/?a=blobdiff_plain;f=router%2Fimpl%2Fsrc%2Ftest%2Fjava%2Forg%2Fwamblee%2Fxmlrouter%2Fimpl%2FConfigImplTest.java;fp=router%2Fimpl%2Fsrc%2Ftest%2Fjava%2Forg%2Fwamblee%2Fxmlrouter%2Fimpl%2FConfigImplTest.java;h=c7b1d9c8660d2d95849411741734017d20620ba0;hb=8e41cb29f75362a792292d21b481bd06a9506296;hp=0000000000000000000000000000000000000000;hpb=9dbc2844950b55ae552fe74840954ea71b754c7a;p=xmlrouter diff --git a/router/impl/src/test/java/org/wamblee/xmlrouter/impl/ConfigImplTest.java b/router/impl/src/test/java/org/wamblee/xmlrouter/impl/ConfigImplTest.java new file mode 100644 index 0000000..c7b1d9c --- /dev/null +++ b/router/impl/src/test/java/org/wamblee/xmlrouter/impl/ConfigImplTest.java @@ -0,0 +1,158 @@ +/* + * 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 static org.mockito.Mockito.*; + +import java.util.concurrent.atomic.AtomicLong; + +import org.junit.Before; +import org.junit.Test; +import org.wamblee.xmlrouter.common.Id; +import org.wamblee.xmlrouter.config.Identifiable; + +public class ConfigImplTest { + + private static final String CONFIG_TYPE = "transformation"; + + private static interface MyType extends Identifiable { + + } + + private static class MyTypeWrapper implements MyType { + private MyType type; + + public MyTypeWrapper(MyType aType) { + type = aType; + } + + public MyType getType() { + return type; + } + + @Override + public Id getId() { + return type.getId(); + } + } + + public static final class MyTypeConfig extends ConfigImpl { + public MyTypeConfig(String aId) { + super(MyType.class, aId); + } + + public MyTypeConfig(MyTypeConfig aConfig) { + super(aConfig); + } + + @Override + public MyType wrap(MyType aT) { + return new MyTypeWrapper(aT); + } + } + + private AtomicLong sequence; + private MyTypeConfig config; + + @Before + public void setUp() { + sequence = new AtomicLong(1L); + config = new MyTypeConfig(CONFIG_TYPE); + } + + @Test + public void testAdd() { + MyType type1 = mock(MyType.class); + when(type1.getId()).thenReturn(new Id("type1")); + + config.add(type1); + + assertEquals(1, config.values().size()); + MyType firstValue = config.values().iterator().next(); + + assertTrue(firstValue instanceof MyTypeWrapper); + assertSame(type1, ((MyTypeWrapper) firstValue).getType()); + assertEquals(type1.getId().getId(), firstValue.getId().getId()); + + // add another one. + MyType type2 = mock(MyType.class); + when(type2.getId()).thenReturn(new Id("type2")); + config.add(type2); + + assertEquals(2, config.values().size()); + } + + @Test + public void testRemove() { + MyType type1 = mock(MyType.class); + when(type1.getId()).thenReturn(new Id("type1")); + + config.add(type1); + + assertEquals(1, config.values().size()); + + assertTrue(config.remove(new Id("type1"))); + assertTrue(config.values().isEmpty()); + } + + @Test(expected = UnsupportedOperationException.class) + public void testUnmodifiable() { + config.values().add(mock(MyType.class)); + } + + @Test + public void testEquals() { + + Config config1 = new MyTypeConfig(CONFIG_TYPE); + assertFalse(config1.equals(null)); + assertFalse(config1.equals("hello")); + Config config2 = new MyTypeConfig(CONFIG_TYPE); + + assertEquals(config1, config2); + assertEquals(config1.hashCode(), config2.hashCode()); + + MyType type1 = mock(MyType.class); + when(type1.getId()).thenReturn(new Id("type1")); + + config1.add(type1); + assertFalse(config1.equals(config2)); + + MyType type2 = mock(MyType.class); + when(type2.getId()).thenReturn(new Id("type1")); + + config2.add(type2); + assertEquals(config1, config2); + assertEquals(config1.hashCode(), config2.hashCode()); + + assertTrue(config2.remove(type2.getId())); + assertFalse(config1.equals(config2)); + } + + @Test + public void testCopy() { + testAdd(); + assertEquals(2, config.values().size()); + MyTypeConfig copy = new MyTypeConfig(config); + assertEquals(config.getPrefix(), copy.getPrefix()); + assertEquals(config, copy); + + // verify the copy is not shallow + assertTrue(config.remove(new Id("type1"))); + assertEquals(1, config.values().size()); + assertFalse(config.equals(copy)); + } +}