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.ConfigException;
25 import org.wamblee.xmlrouter.config.Identifiable;
27 public class RobustIdentifiableTest {
29 private Identifiable<Integer> ident;
33 ident = mock(Identifiable.class);
36 @Test(expected = ConfigException.class)
37 public void testIdIsNull() {
38 when(ident.getId()).thenReturn(null);
39 RobustIdentifiable<Integer> robust = new RobustIdentifiable<Integer>(
43 @Test(expected = ConfigException.class)
44 public void testIdThrowsException() {
45 doThrow(new RuntimeException("xxx")).when(ident).getId();
46 RobustIdentifiable<Integer> robust = new RobustIdentifiable<Integer>(
51 public void testNormalCase() {
52 when(ident.getId()).thenReturn(new Id<Integer>("myid"));
53 RobustIdentifiable<Integer> robust = new RobustIdentifiable<Integer>(
55 assertEquals("prefix.myid", robust.getId().toString());
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());
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);
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"));
72 RobustIdentifiable<Integer> robust1 = new RobustIdentifiable<Integer>(
74 RobustIdentifiable<Integer> robust2 = new RobustIdentifiable<Integer>(
76 RobustIdentifiable<Integer> robust3 = new RobustIdentifiable<Integer>(
79 assertEquals(robust1, robust1);
80 assertEquals(robust1, robust3);
81 assertFalse(robust1.equals(robust2));
82 assertFalse(robust1.equals("x"));
84 assertEquals(robust1.hashCode(), robust3.hashCode());