/* * 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)); } }