/* * 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.Config; import org.wamblee.xmlrouter.config.Identifiable; public class ConfigImplTest { 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(); } } private AtomicLong sequence; private ExtendedConfig config; @Before public void setUp() { sequence = new AtomicLong(1L); config = new ConfigImpl(new Id("mytype")) { @Override public MyType wrap(MyType aT) { return new MyTypeWrapper(aT); } }; } @Test public void testAdd() { MyType type1 = mock(MyType.class); when(type1.getId()).thenReturn(new Id("type1")); config.add(type1); assertEquals(1, config.values().size()); assertTrue(config.values().get(0) instanceof MyTypeWrapper); assertSame(type1, ((MyTypeWrapper) config.values().get(0)).getType()); // 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)); } }