Turned the API packages into bundles.
[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.Identifiable;
25
26 public class RobustIdentifiableTest {
27
28     private Identifiable<Integer> ident;
29
30     @Before
31     public void setUp() {
32         ident = mock(Identifiable.class);
33     }
34
35     @Test(expected = ConfigException.class)
36     public void testIdIsNull() {
37         when(ident.getId()).thenReturn(null);
38         RobustIdentifiable<Integer> robust = new RobustIdentifiable<Integer>(
39             ident);
40     }
41
42     @Test(expected = ConfigException.class)
43     public void testIdThrowsException() {
44         doThrow(new RuntimeException("xxx")).when(ident).getId();
45         RobustIdentifiable<Integer> robust = new RobustIdentifiable<Integer>(
46             ident);
47     }
48
49     @Test
50     public void testNormalCase() {
51         when(ident.getId()).thenReturn(new Id<Integer>("myid"));
52         RobustIdentifiable<Integer> robust = new RobustIdentifiable<Integer>(
53             ident);
54         assertEquals("myid", robust.getId().toString());
55
56         // changes later do not have any effect, the id should be immutable.
57         when(ident.getId()).thenReturn(new Id<Integer>("myid2"));
58         assertEquals("myid", robust.getId().toString());
59     }
60
61     @Test
62     public void testEqualsHashCode() {
63         Identifiable<Integer> ident1 = mock(Identifiable.class);
64         Identifiable<Integer> ident2 = mock(Identifiable.class);
65         Identifiable<Integer> ident3 = mock(Identifiable.class);
66
67         when(ident1.getId()).thenReturn(new Id<Integer>("x"));
68         when(ident2.getId()).thenReturn(new Id<Integer>("y"));
69         when(ident3.getId()).thenReturn(new Id<Integer>("x"));
70
71         RobustIdentifiable<Integer> robust1 = new RobustIdentifiable<Integer>(
72             ident1);
73         RobustIdentifiable<Integer> robust2 = new RobustIdentifiable<Integer>(
74             ident2);
75         RobustIdentifiable<Integer> robust3 = new RobustIdentifiable<Integer>(
76             ident3);
77
78         assertEquals(robust1, robust1);
79         assertEquals(robust1, robust3);
80         assertFalse(robust1.equals(robust2));
81         assertFalse(robust1.equals("x"));
82
83         assertEquals(robust1.hashCode(), robust3.hashCode());
84     }
85 }