2 * Copyright 2005-2011 the original author or authors.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.wamblee.xmlrouter.impl;
18 import static junit.framework.Assert.*;
19 import static org.mockito.Mockito.*;
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.wamblee.xmlrouter.common.Id;
24 import org.wamblee.xmlrouter.config.Identifiable;
26 public class RobustIdentifiableTest {
28 private Identifiable<Integer> ident;
32 ident = mock(Identifiable.class);
35 @Test(expected = ConfigException.class)
36 public void testIdIsNull() {
37 when(ident.getId()).thenReturn(null);
38 RobustIdentifiable<Integer> robust = new RobustIdentifiable<Integer>(
42 @Test(expected = ConfigException.class)
43 public void testIdThrowsException() {
44 doThrow(new RuntimeException("xxx")).when(ident).getId();
45 RobustIdentifiable<Integer> robust = new RobustIdentifiable<Integer>(
50 public void testNormalCase() {
51 when(ident.getId()).thenReturn(new Id<Integer>("myid"));
52 RobustIdentifiable<Integer> robust = new RobustIdentifiable<Integer>(
54 assertEquals("myid", robust.getId().toString());
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());
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);
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"));
71 RobustIdentifiable<Integer> robust1 = new RobustIdentifiable<Integer>(
73 RobustIdentifiable<Integer> robust2 = new RobustIdentifiable<Integer>(
75 RobustIdentifiable<Integer> robust3 = new RobustIdentifiable<Integer>(
78 assertEquals(robust1, robust1);
79 assertEquals(robust1, robust3);
80 assertFalse(robust1.equals(robust2));
81 assertFalse(robust1.equals("x"));
83 assertEquals(robust1.hashCode(), robust3.hashCode());