044cb1f32f9240a00da2a2347d7e518ae13015df
[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 final String CONFIG_TYPE = "transformation";
32
33     private static interface MyType extends Identifiable<MyType> {
34
35     }
36
37     private static class MyTypeWrapper implements MyType {
38         private String prefix;
39         private MyType type;
40
41         public MyTypeWrapper(String aPrefix, MyType aType) {
42             prefix = aPrefix;
43             type = aType;
44         }
45
46         public MyType getType() {
47             return type;
48         }
49
50         @Override
51         public Id getId() {
52             return new Id(prefix + type.getId().getId());
53         }
54     }
55
56     private AtomicLong sequence;
57     private ExtendedConfig<MyType> config;
58
59     @Before
60     public void setUp() {
61         sequence = new AtomicLong(1L);
62         config = new ConfigImpl<MyType>(new Id<Config>(CONFIG_TYPE)) {
63             @Override
64             public MyType wrap(String aPrefix, MyType aT) {
65                 return new MyTypeWrapper(aPrefix, aT);
66             }
67         };
68     }
69
70     @Test
71     public void testAdd() {
72         MyType type1 = mock(MyType.class);
73         when(type1.getId()).thenReturn(new Id("type1"));
74
75         config.add(type1);
76
77         assertEquals(1, config.values().size());
78         assertTrue(config.values().get(0) instanceof MyTypeWrapper);
79         assertSame(type1, ((MyTypeWrapper) config.values().get(0)).getType());
80         assertEquals(CONFIG_TYPE + "." + type1.getId().getId(), config.values()
81             .get(0).getId().getId());
82
83         // add another one.
84         MyType type2 = mock(MyType.class);
85         when(type2.getId()).thenReturn(new Id("type2"));
86         config.add(type2);
87
88         assertEquals(2, config.values().size());
89     }
90
91     @Test
92     public void testRemove() {
93         MyType type1 = mock(MyType.class);
94         when(type1.getId()).thenReturn(new Id("type1"));
95
96         config.add(type1);
97
98         assertEquals(1, config.values().size());
99
100         assertTrue(config.remove(new Id(CONFIG_TYPE + "." + "type1")));
101         assertTrue(config.values().isEmpty());
102     }
103
104     @Test(expected = UnsupportedOperationException.class)
105     public void testUnmodifiable() {
106         config.values().add(mock(MyType.class));
107     }
108 }