First version after introduction of meaningful ids and Identifiable interface.
[xmlrouter] / impl / src / test / java / org / wamblee / xmlrouter / impl / ConfigImplTest.java
1 /*
2  * Copyright 2005-2011 the original author or authors.
3  * 
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
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
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.
15  */
16 package org.wamblee.xmlrouter.impl;
17
18 import static junit.framework.Assert.*;
19 import static org.mockito.Mockito.*;
20
21 import java.util.concurrent.atomic.AtomicLong;
22
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;
28
29 public class ConfigImplTest {
30
31     private static interface MyType extends Identifiable {
32
33     }
34
35     private static class MyTypeWrapper implements MyType {
36         private MyType type;
37
38         public MyTypeWrapper(MyType aType) {
39             type = aType;
40         }
41
42         public MyType getType() {
43             return type;
44         }
45
46         @Override
47         public Id getId() {
48             return type.getId();
49         }
50     }
51
52     private AtomicLong sequence;
53     private ExtendedConfig<MyType> config;
54
55     @Before
56     public void setUp() {
57         sequence = new AtomicLong(1L);
58         config = new ConfigImpl<MyType>(new Id<Config>("mytype")) {
59             @Override
60             public MyType wrap(MyType aT) {
61                 return new MyTypeWrapper(aT);
62             }
63         };
64     }
65
66     @Test
67     public void testAdd() {
68         MyType type1 = mock(MyType.class);
69         when(type1.getId()).thenReturn(new Id("type1"));
70
71         config.add(type1);
72
73         assertEquals(1, config.values().size());
74         assertTrue(config.values().get(0) instanceof MyTypeWrapper);
75         assertSame(type1, ((MyTypeWrapper) config.values().get(0)).getType());
76
77         // add another one.
78         MyType type2 = mock(MyType.class);
79         when(type2.getId()).thenReturn(new Id("type2"));
80         config.add(type2);
81
82         assertEquals(2, config.values().size());
83     }
84
85     @Test
86     public void testRemove() {
87         MyType type1 = mock(MyType.class);
88         when(type1.getId()).thenReturn(new Id("type1"));
89
90         config.add(type1);
91
92         assertEquals(1, config.values().size());
93
94         assertTrue(config.remove(new Id("type1")));
95         assertTrue(config.values().isEmpty());
96     }
97
98     @Test(expected = UnsupportedOperationException.class)
99     public void testUnmodifiable() {
100         config.values().add(mock(MyType.class));
101     }
102 }