removed the prefix argument from ConfigImpl.wrap().
[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             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             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             ident);
55         assertEquals("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("myid", robust.getId().toString());
60     }
61
62     @Test
63     public void testEqualsHashCode() {
64         Identifiable<Integer> ident1 = mock(Identifiable.class);
65         Identifiable<Integer> ident2 = mock(Identifiable.class);
66         Identifiable<Integer> ident3 = mock(Identifiable.class);
67
68         when(ident1.getId()).thenReturn(new Id<Integer>("x"));
69         when(ident2.getId()).thenReturn(new Id<Integer>("y"));
70         when(ident3.getId()).thenReturn(new Id<Integer>("x"));
71
72         RobustIdentifiable<Integer> robust1 = new RobustIdentifiable<Integer>(
73             ident1);
74         RobustIdentifiable<Integer> robust2 = new RobustIdentifiable<Integer>(
75             ident2);
76         RobustIdentifiable<Integer> robust3 = new RobustIdentifiable<Integer>(
77             ident3);
78
79         assertEquals(robust1, robust1);
80         assertEquals(robust1, robust3);
81         assertFalse(robust1.equals(robust2));
82         assertFalse(robust1.equals("x"));
83
84         assertEquals(robust1.hashCode(), robust3.hashCode());
85     }
86 }