+/*
+ * Copyright 2005-2011 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.wamblee.xmlrouter.impl;
+
+import static junit.framework.Assert.*;
+import static org.mockito.Mockito.*;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.wamblee.xmlrouter.common.Id;
+import org.wamblee.xmlrouter.config.Identifiable;
+
+public class RobustIdentifiableTest {
+
+ private Identifiable<Integer> ident;
+
+ @Before
+ public void setUp() {
+ ident = mock(Identifiable.class);
+ }
+
+ @Test(expected = ConfigException.class)
+ public void testIdIsNull() {
+ when(ident.getId()).thenReturn(null);
+ RobustIdentifiable<Integer> robust = new RobustIdentifiable<Integer>(
+ ident);
+ }
+
+ @Test(expected = ConfigException.class)
+ public void testIdThrowsException() {
+ doThrow(new RuntimeException("xxx")).when(ident).getId();
+ RobustIdentifiable<Integer> robust = new RobustIdentifiable<Integer>(
+ ident);
+ }
+
+ @Test
+ public void testNormalCase() {
+ when(ident.getId()).thenReturn(new Id<Integer>("myid"));
+ RobustIdentifiable<Integer> robust = new RobustIdentifiable<Integer>(
+ ident);
+ assertEquals("myid", robust.getId().toString());
+
+ // changes later do not have any effect, the id should be immutable.
+ when(ident.getId()).thenReturn(new Id<Integer>("myid2"));
+ assertEquals("myid", robust.getId().toString());
+ }
+
+ @Test
+ public void testEqualsHashCode() {
+ Identifiable<Integer> ident1 = mock(Identifiable.class);
+ Identifiable<Integer> ident2 = mock(Identifiable.class);
+ Identifiable<Integer> ident3 = mock(Identifiable.class);
+
+ when(ident1.getId()).thenReturn(new Id<Integer>("x"));
+ when(ident2.getId()).thenReturn(new Id<Integer>("y"));
+ when(ident3.getId()).thenReturn(new Id<Integer>("x"));
+
+ RobustIdentifiable<Integer> robust1 = new RobustIdentifiable<Integer>(
+ ident1);
+ RobustIdentifiable<Integer> robust2 = new RobustIdentifiable<Integer>(
+ ident2);
+ RobustIdentifiable<Integer> robust3 = new RobustIdentifiable<Integer>(
+ ident3);
+
+ assertEquals(robust1, robust1);
+ assertEquals(robust1, robust3);
+ assertFalse(robust1.equals(robust2));
+ assertFalse(robust1.equals("x"));
+
+ assertEquals(robust1.hashCode(), robust3.hashCode());
+ }
+}