From 747a76582ebfd3e0696c40e6d0ca21fcd2e1be60 Mon Sep 17 00:00:00 2001 From: Erik Brakkee Date: Sun, 7 Aug 2011 22:46:54 +0200 Subject: [PATCH] equals and hashcode for robust identifiable. --- .../xmlrouter/impl/RobustIdentifiable.java | 8 ++---- .../impl/RobustIdentifiableTest.java | 25 +++++++++++++++++++ 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/impl/src/main/java/org/wamblee/xmlrouter/impl/RobustIdentifiable.java b/impl/src/main/java/org/wamblee/xmlrouter/impl/RobustIdentifiable.java index 6622e32..d5ba9f9 100644 --- a/impl/src/main/java/org/wamblee/xmlrouter/impl/RobustIdentifiable.java +++ b/impl/src/main/java/org/wamblee/xmlrouter/impl/RobustIdentifiable.java @@ -25,7 +25,8 @@ import org.wamblee.xmlrouter.config.ConfigException; import org.wamblee.xmlrouter.config.Identifiable; /** - * Robust identifiable provides robustness for identifiable objects. + * Robust identifiable provides robustness for identifiable objects. It adds + * equality based on the id. * * @author Erik Brakkee * @@ -37,14 +38,9 @@ public class RobustIdentifiable implements Identifiable { private Id id; - // TODO test this class. - // TODO test that id is constant even though delegated changes its id. - public RobustIdentifiable(String aPrefix, Identifiable aIdentifiable) { notNull("prefix", aPrefix); notNull("identifiable", aIdentifiable); - // TODO test id is null - // TODO getId() throws exception try { id = aIdentifiable.getId(); if (id == null) { diff --git a/impl/src/test/java/org/wamblee/xmlrouter/impl/RobustIdentifiableTest.java b/impl/src/test/java/org/wamblee/xmlrouter/impl/RobustIdentifiableTest.java index f8fcd5c..257ea93 100644 --- a/impl/src/test/java/org/wamblee/xmlrouter/impl/RobustIdentifiableTest.java +++ b/impl/src/test/java/org/wamblee/xmlrouter/impl/RobustIdentifiableTest.java @@ -58,4 +58,29 @@ public class RobustIdentifiableTest { when(ident.getId()).thenReturn(new Id("myid2")); assertEquals("prefix.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( + "prefix.", ident1); + RobustIdentifiable robust2 = new RobustIdentifiable( + "prefix.", ident2); + RobustIdentifiable robust3 = new RobustIdentifiable( + "prefix.", ident3); + + assertEquals(robust1, robust1); + assertEquals(robust1, robust3); + assertFalse(robust1.equals(robust2)); + assertFalse(robust1.equals("x")); + + assertEquals(robust1.hashCode(), robust3.hashCode()); + } } -- 2.31.1