X-Git-Url: http://wamblee.org/gitweb/?a=blobdiff_plain;f=router%2Fimpl%2Fsrc%2Ftest%2Fjava%2Forg%2Fwamblee%2Fxmlrouter%2Fimpl%2FRobustIdentifiableTest.java;fp=router%2Fimpl%2Fsrc%2Ftest%2Fjava%2Forg%2Fwamblee%2Fxmlrouter%2Fimpl%2FRobustIdentifiableTest.java;h=511f583196175a564b5ab64b418818b59469995d;hb=8e41cb29f75362a792292d21b481bd06a9506296;hp=0000000000000000000000000000000000000000;hpb=9dbc2844950b55ae552fe74840954ea71b754c7a;p=xmlrouter diff --git a/router/impl/src/test/java/org/wamblee/xmlrouter/impl/RobustIdentifiableTest.java b/router/impl/src/test/java/org/wamblee/xmlrouter/impl/RobustIdentifiableTest.java new file mode 100644 index 0000000..511f583 --- /dev/null +++ b/router/impl/src/test/java/org/wamblee/xmlrouter/impl/RobustIdentifiableTest.java @@ -0,0 +1,85 @@ +/* + * 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 ident; + + @Before + public void setUp() { + ident = mock(Identifiable.class); + } + + @Test(expected = ConfigException.class) + public void testIdIsNull() { + when(ident.getId()).thenReturn(null); + RobustIdentifiable robust = new RobustIdentifiable( + ident); + } + + @Test(expected = ConfigException.class) + public void testIdThrowsException() { + doThrow(new RuntimeException("xxx")).when(ident).getId(); + RobustIdentifiable robust = new RobustIdentifiable( + ident); + } + + @Test + public void testNormalCase() { + when(ident.getId()).thenReturn(new Id("myid")); + RobustIdentifiable robust = new RobustIdentifiable( + ident); + assertEquals("myid", robust.getId().toString()); + + // changes later do not have any effect, the id should be immutable. + when(ident.getId()).thenReturn(new Id("myid2")); + assertEquals("myid", robust.getId().toString()); + } + + @Test + public void testEqualsHashCode() { + Identifiable ident1 = mock(Identifiable.class); + Identifiable ident2 = mock(Identifiable.class); + Identifiable ident3 = mock(Identifiable.class); + + when(ident1.getId()).thenReturn(new Id("x")); + when(ident2.getId()).thenReturn(new Id("y")); + when(ident3.getId()).thenReturn(new Id("x")); + + RobustIdentifiable robust1 = new RobustIdentifiable( + ident1); + RobustIdentifiable robust2 = new RobustIdentifiable( + ident2); + RobustIdentifiable robust3 = new RobustIdentifiable( + ident3); + + assertEquals(robust1, robust1); + assertEquals(robust1, robust3); + assertFalse(robust1.equals(robust2)); + assertFalse(robust1.equals("x")); + + assertEquals(robust1.hashCode(), robust3.hashCode()); + } +}