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.Identifiable;
28 public class ConfigImplTest {
30 private static final String CONFIG_TYPE = "transformation";
32 private static interface MyType extends Identifiable<MyType> {
36 private static class MyTypeWrapper implements MyType {
39 public MyTypeWrapper(MyType aType) {
43 public MyType getType() {
53 public static final class MyTypeConfig extends ConfigImpl<MyType> {
54 public MyTypeConfig(String aId) {
55 super(MyType.class, aId);
58 public MyTypeConfig(MyTypeConfig aConfig) {
63 public MyType wrap(MyType aT) {
64 return new MyTypeWrapper(aT);
68 private AtomicLong sequence;
69 private MyTypeConfig config;
73 sequence = new AtomicLong(1L);
74 config = new MyTypeConfig(CONFIG_TYPE);
78 public void testAdd() {
79 MyType type1 = mock(MyType.class);
80 when(type1.getId()).thenReturn(new Id("type1"));
84 assertEquals(1, config.values().size());
85 MyType firstValue = config.values().iterator().next();
87 assertTrue(firstValue instanceof MyTypeWrapper);
88 assertSame(type1, ((MyTypeWrapper) firstValue).getType());
89 assertEquals(type1.getId().getId(), firstValue.getId().getId());
92 MyType type2 = mock(MyType.class);
93 when(type2.getId()).thenReturn(new Id("type2"));
96 assertEquals(2, config.values().size());
100 public void testRemove() {
101 MyType type1 = mock(MyType.class);
102 when(type1.getId()).thenReturn(new Id("type1"));
106 assertEquals(1, config.values().size());
108 assertTrue(config.remove(new Id("type1")));
109 assertTrue(config.values().isEmpty());
112 @Test(expected = UnsupportedOperationException.class)
113 public void testUnmodifiable() {
114 config.values().add(mock(MyType.class));
118 public void testEquals() {
120 Config<MyType> config1 = new MyTypeConfig(CONFIG_TYPE);
121 assertFalse(config1.equals(null));
122 assertFalse(config1.equals("hello"));
123 Config<MyType> config2 = new MyTypeConfig(CONFIG_TYPE);
125 assertEquals(config1, config2);
126 assertEquals(config1.hashCode(), config2.hashCode());
128 MyType type1 = mock(MyType.class);
129 when(type1.getId()).thenReturn(new Id("type1"));
132 assertFalse(config1.equals(config2));
134 MyType type2 = mock(MyType.class);
135 when(type2.getId()).thenReturn(new Id("type1"));
138 assertEquals(config1, config2);
139 assertEquals(config1.hashCode(), config2.hashCode());
141 assertTrue(config2.remove(type2.getId()));
142 assertFalse(config1.equals(config2));
146 public void testCopy() {
148 assertEquals(2, config.values().size());
149 MyTypeConfig copy = new MyTypeConfig(config);
150 assertEquals(config.getPrefix(), copy.getPrefix());
151 assertEquals(config, copy);
153 // verify the copy is not shallow
154 assertTrue(config.remove(new Id<MyType>("type1")));
155 assertEquals(1, config.values().size());
156 assertFalse(config.equals(copy));