RobustIdentifiable implemented and tested + test impacts.
[xmlrouter] / impl / src / test / java / org / wamblee / xmlrouter / impl / RobustIdentifiableTest.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 org.junit.Before;
22 import org.junit.Test;
23 import org.wamblee.xmlrouter.common.Id;
24 import org.wamblee.xmlrouter.config.ConfigException;
25 import org.wamblee.xmlrouter.config.Identifiable;
26
27 public class RobustIdentifiableTest {
28
29     private Identifiable<Integer> ident;
30
31     @Before
32     public void setUp() {
33         ident = mock(Identifiable.class);
34     }
35
36     @Test(expected = ConfigException.class)
37     public void testIdIsNull() {
38         when(ident.getId()).thenReturn(null);
39         RobustIdentifiable<Integer> robust = new RobustIdentifiable<Integer>(
40             "prefix", ident);
41     }
42
43     @Test(expected = ConfigException.class)
44     public void testIdThrowsException() {
45         doThrow(new RuntimeException("xxx")).when(ident).getId();
46         RobustIdentifiable<Integer> robust = new RobustIdentifiable<Integer>(
47             "prefix", ident);
48     }
49
50     @Test
51     public void testNormalCase() {
52         when(ident.getId()).thenReturn(new Id<Integer>("myid"));
53         RobustIdentifiable<Integer> robust = new RobustIdentifiable<Integer>(
54             "prefix.", ident);
55         assertEquals("prefix.myid", robust.getId().toString());
56
57         // changes later do not have any effect, the id should be immutable.
58         when(ident.getId()).thenReturn(new Id<Integer>("myid2"));
59         assertEquals("prefix.myid", robust.getId().toString());
60     }
61 }