2 * Copyright 2005-2011 the original author or authors.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.wamblee.xmlrouter.impl;
18 import static junit.framework.Assert.*;
19 import static org.mockito.Mockito.*;
21 import java.util.concurrent.atomic.AtomicLong;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.wamblee.xmlrouter.common.Id;
26 import org.wamblee.xmlrouter.config.Config;
27 import org.wamblee.xmlrouter.config.Identifiable;
29 public class ConfigImplTest {
31 private static final String CONFIG_TYPE = "transformation";
33 private static interface MyType extends Identifiable<MyType> {
37 private static class MyTypeWrapper implements MyType {
38 private String prefix;
41 public MyTypeWrapper(String aPrefix, MyType aType) {
46 public MyType getType() {
52 return new Id(prefix + type.getId().getId());
56 public static final class MyTypeConfig extends ConfigImpl<MyType> {
57 public MyTypeConfig(Id<Config> aId) {
61 public MyTypeConfig(MyTypeConfig aConfig) {
66 public MyType wrap(String aPrefix, MyType aT) {
67 return new MyTypeWrapper(aPrefix, aT);
71 private AtomicLong sequence;
72 private MyTypeConfig config;
76 sequence = new AtomicLong(1L);
77 config = new MyTypeConfig(new Id<Config>(CONFIG_TYPE));
81 public void testAdd() {
82 MyType type1 = mock(MyType.class);
83 when(type1.getId()).thenReturn(new Id("type1"));
87 assertEquals(1, config.values().size());
88 MyType firstValue = config.values().iterator().next();
90 assertTrue(firstValue instanceof MyTypeWrapper);
91 assertSame(type1, ((MyTypeWrapper) firstValue).getType());
92 assertEquals(CONFIG_TYPE + "." + type1.getId().getId(), firstValue
96 MyType type2 = mock(MyType.class);
97 when(type2.getId()).thenReturn(new Id("type2"));
100 assertEquals(2, config.values().size());
104 public void testRemove() {
105 MyType type1 = mock(MyType.class);
106 when(type1.getId()).thenReturn(new Id("type1"));
110 assertEquals(1, config.values().size());
112 assertTrue(config.remove(new Id("type1")));
113 assertTrue(config.values().isEmpty());
116 @Test(expected = UnsupportedOperationException.class)
117 public void testUnmodifiable() {
118 config.values().add(mock(MyType.class));
122 public void testEquals() {
124 Config<MyType> config1 = new ConfigImpl<MyType>(new Id<Config>(
127 public MyType wrap(String aPrefix, MyType aT) {
128 return new MyTypeWrapper(aPrefix, aT);
131 assertFalse(config1.equals(null));
132 assertFalse(config1.equals("hello"));
133 Config<MyType> config2 = new ConfigImpl<MyType>(new Id<Config>(
136 public MyType wrap(String aPrefix, MyType aT) {
137 return new MyTypeWrapper(aPrefix, aT);
140 assertEquals(config1, config2);
141 assertEquals(config1.hashCode(), config2.hashCode());
143 MyType type1 = mock(MyType.class);
144 when(type1.getId()).thenReturn(new Id("type1"));
147 assertFalse(config1.equals(config2));
149 MyType type2 = mock(MyType.class);
150 when(type2.getId()).thenReturn(new Id("type1"));
153 assertEquals(config1, config2);
154 assertEquals(config1.hashCode(), config2.hashCode());
156 assertTrue(config2.remove(type2.getId()));
157 assertFalse(config1.equals(config2));
161 public void testCopy() {
163 assertEquals(2, config.values().size());
164 MyTypeConfig copy = new MyTypeConfig(config);
165 assertEquals(config.getId(), config.getId());
166 assertEquals(config, copy);
168 // verify the copy is not shallow
169 assertTrue(config.remove(new Id<MyType>("type1")));
170 assertEquals(1, config.values().size());
171 assertFalse(config.equals(copy));