/* * 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 final String CONFIG_TYPE = "transformation"; private static interface MyType extends Identifiable { } private static class MyTypeWrapper implements MyType { private String prefix; private MyType type; public MyTypeWrapper(String aPrefix, MyType aType) { prefix = aPrefix; type = aType; } public MyType getType() { return type; } @Override public Id getId() { return new Id(prefix + type.getId().getId()); } } private AtomicLong sequence; private ExtendedConfig config; @Before public void setUp() { sequence = new AtomicLong(1L); config = new ConfigImpl(new Id(CONFIG_TYPE)) { @Override public MyType wrap(String aPrefix, MyType aT) { return new MyTypeWrapper(aPrefix, 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()); assertEquals(CONFIG_TYPE + "." + type1.getId().getId(), config.values() .get(0).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(CONFIG_TYPE + "." + "type1"))); assertTrue(config.values().isEmpty()); } @Test(expected = UnsupportedOperationException.class) public void testUnmodifiable() { config.values().add(mock(MyType.class)); } }