added first version of configuraiton api and simple function test.
[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
27 public class ConfigImplTest {
28
29     private static interface MyType {
30
31     }
32
33     private static class MyTypeWrapper implements MyType {
34         private Id<MyType> id;
35         private MyType type;
36
37         public MyTypeWrapper(Id<MyType> aId, MyType aType) {
38             id = aId;
39             type = aType;
40         }
41
42         public MyType getType() {
43             return type;
44         }
45     }
46
47     private AtomicLong sequence;
48     private ExtendedConfig<MyType> config;
49
50     @Before
51     public void setUp() {
52         sequence = new AtomicLong(1L);
53         config = new ConfigImpl<MyType>(sequence) {
54             @Override
55             public MyType wrap(Id<MyType> aId, MyType aT) {
56                 return new MyTypeWrapper(aId, aT);
57             }
58         };
59     }
60
61     @Test
62     public void testAdd() {
63         MyType type1 = mock(MyType.class);
64
65         Id<MyType> id1 = config.add(type1);
66
67         assertNotNull(id1);
68         assertEquals(1, config.map().size());
69         assertTrue(config.map().get(id1) instanceof MyTypeWrapper);
70         assertSame(type1, ((MyTypeWrapper) config.map().get(id1)).getType());
71
72         // add another one.
73         MyType type2 = mock(MyType.class);
74         Id<MyType> id2 = config.add(type2);
75         assertNotNull(id2);
76         assertEquals(2, config.map().size());
77         assertFalse(id1.equals(id2));
78     }
79
80     @Test
81     public void testRemove() {
82         MyType type1 = mock(MyType.class);
83         Id<MyType> id1 = config.add(type1);
84
85         assertNotNull(id1);
86         assertEquals(1, config.map().size());
87
88         config.remove(id1);
89         assertTrue(config.map().isEmpty());
90     }
91
92     @Test(expected = UnsupportedOperationException.class)
93     public void testUnmodifiable() {
94         config.map().put(new Id<MyType>(100L), mock(MyType.class));
95     }
96 }