X-Git-Url: http://wamblee.org/gitweb/?a=blobdiff_plain;f=impl%2Fsrc%2Ftest%2Fjava%2Forg%2Fwamblee%2Fxmlrouter%2Fimpl%2FConfigImplTest.java;fp=impl%2Fsrc%2Ftest%2Fjava%2Forg%2Fwamblee%2Fxmlrouter%2Fimpl%2FConfigImplTest.java;h=98296d4d8cc110bf2174340fbe13478576b0ef06;hb=19413a6699295b4bbebc1b3bdb9838fd4370e581;hp=0000000000000000000000000000000000000000;hpb=f77e29e9ae2505009ffc9270f4c4adfde331fa62;p=xmlrouter diff --git a/impl/src/test/java/org/wamblee/xmlrouter/impl/ConfigImplTest.java b/impl/src/test/java/org/wamblee/xmlrouter/impl/ConfigImplTest.java new file mode 100644 index 0000000..98296d4 --- /dev/null +++ b/impl/src/test/java/org/wamblee/xmlrouter/impl/ConfigImplTest.java @@ -0,0 +1,107 @@ +/* + * 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; + +public class ConfigImplTest { + + private static interface MyType { + + } + + private static class MyTypeWrapper implements MyType { + private Id id; + private MyType type; + + public MyTypeWrapper(Id aId, MyType aType) { + id = aId; + type = aType; + } + + public MyType getType() { + return type; + } + } + + private AtomicLong sequence; + private ExtendedConfig config; + + @Before + public void setUp() { + sequence = new AtomicLong(1L); + config = new ConfigImpl(sequence) { + @Override + public MyType wrap(Id aId, MyType aT) { + return new MyTypeWrapper(aId, aT); + } + }; + } + + @Test + public void testAdd() { + MyType type1 = mock(MyType.class); + assertFalse(config.isDirty()); + + Id id1 = config.add(type1); + + assertNotNull(id1); + assertEquals(1, config.map().size()); + assertTrue(config.map().get(id1) instanceof MyTypeWrapper); + assertSame(type1, ((MyTypeWrapper) config.map().get(id1)).getType()); + assertTrue(config.isDirty()); + + config.resetDirty(); + assertFalse(config.isDirty()); + + // add another one. + MyType type2 = mock(MyType.class); + Id id2 = config.add(type2); + assertNotNull(id2); + assertEquals(2, config.map().size()); + assertFalse(id1.equals(id2)); + assertTrue(config.isDirty()); + + } + + @Test + public void testRemove() { + MyType type1 = mock(MyType.class); + Id id1 = config.add(type1); + + assertNotNull(id1); + assertEquals(1, config.map().size()); + + config.resetDirty(); + assertFalse(config.isDirty()); + + config.remove(id1); + assertTrue(config.map().isEmpty()); + assertTrue(config.isDirty()); + } + + @Test(expected = UnsupportedOperationException.class) + public void testUnmodifiable() { + config.map().put(new Id(100L), mock(MyType.class)); + } +}