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 {
40 public MyTypeWrapper(MyType aType) {
44 public MyType getType() {
54 public static final class MyTypeConfig extends ConfigImpl<MyType> {
55 public MyTypeConfig(Id<Config> aId) {
56 super(MyType.class, aId);
59 public MyTypeConfig(MyTypeConfig aConfig) {
64 public MyType wrap(MyType aT) {
65 return new MyTypeWrapper(aT);
69 private AtomicLong sequence;
70 private MyTypeConfig config;
74 sequence = new AtomicLong(1L);
75 config = new MyTypeConfig(new Id<Config>(CONFIG_TYPE));
79 public void testAdd() {
80 MyType type1 = mock(MyType.class);
81 when(type1.getId()).thenReturn(new Id("type1"));
85 assertEquals(1, config.values().size());
86 MyType firstValue = config.values().iterator().next();
88 assertTrue(firstValue instanceof MyTypeWrapper);
89 assertSame(type1, ((MyTypeWrapper) firstValue).getType());
90 assertEquals(type1.getId().getId(), firstValue.getId().getId());
93 MyType type2 = mock(MyType.class);
94 when(type2.getId()).thenReturn(new Id("type2"));
97 assertEquals(2, config.values().size());
101 public void testRemove() {
102 MyType type1 = mock(MyType.class);
103 when(type1.getId()).thenReturn(new Id("type1"));
107 assertEquals(1, config.values().size());
109 assertTrue(config.remove(new Id("type1")));
110 assertTrue(config.values().isEmpty());
113 @Test(expected = UnsupportedOperationException.class)
114 public void testUnmodifiable() {
115 config.values().add(mock(MyType.class));
119 public void testEquals() {
121 Config<MyType> config1 = new MyTypeConfig(new Id<Config>(CONFIG_TYPE));
122 assertFalse(config1.equals(null));
123 assertFalse(config1.equals("hello"));
124 Config<MyType> config2 = new MyTypeConfig(new Id<Config>(CONFIG_TYPE));
126 assertEquals(config1, config2);
127 assertEquals(config1.hashCode(), config2.hashCode());
129 MyType type1 = mock(MyType.class);
130 when(type1.getId()).thenReturn(new Id("type1"));
133 assertFalse(config1.equals(config2));
135 MyType type2 = mock(MyType.class);
136 when(type2.getId()).thenReturn(new Id("type1"));
139 assertEquals(config1, config2);
140 assertEquals(config1.hashCode(), config2.hashCode());
142 assertTrue(config2.remove(type2.getId()));
143 assertFalse(config1.equals(config2));
147 public void testCopy() {
149 assertEquals(2, config.values().size());
150 MyTypeConfig copy = new MyTypeConfig(config);
151 assertEquals(config.getId(), config.getId());
152 assertEquals(config, copy);
154 // verify the copy is not shallow
155 assertTrue(config.remove(new Id<MyType>("type1")));
156 assertEquals(1, config.values().size());
157 assertFalse(config.equals(copy));