From d5f50958f38d10e7654b03fee4163a6badffdcfd Mon Sep 17 00:00:00 2001 From: Michael Hunger Date: Thu, 22 Mar 2012 13:10:33 +0100 Subject: [PATCH] #40 hashCode and equals in unit-tests --- .../aspects/support/node/Neo4jNodeBacking.aj | 9 +++-- .../support/EntityWithoutAspectSetupTest.java | 39 +++++++++++++++++++ 2 files changed, 45 insertions(+), 3 deletions(-) create mode 100644 spring-data-neo4j-aspects/src/test/java/org/springframework/data/neo4j/aspects/support/EntityWithoutAspectSetupTest.java diff --git a/spring-data-neo4j-aspects/src/main/java/org/springframework/data/neo4j/aspects/support/node/Neo4jNodeBacking.aj b/spring-data-neo4j-aspects/src/main/java/org/springframework/data/neo4j/aspects/support/node/Neo4jNodeBacking.aj index b8371194a..217978bb2 100644 --- a/spring-data-neo4j-aspects/src/main/java/org/springframework/data/neo4j/aspects/support/node/Neo4jNodeBacking.aj +++ b/spring-data-neo4j-aspects/src/main/java/org/springframework/data/neo4j/aspects/support/node/Neo4jNodeBacking.aj @@ -231,18 +231,21 @@ public privileged aspect Neo4jNodeBacking { // extends AbstractTypeAnnotatingMix * @return result of equals operation fo the underlying node, false if there is none */ public boolean NodeBacked.equals(Object obj) { - return entityStateHandler().equals(this, obj); + final EntityStateHandler entityStateHandler = entityStateHandler(); + return entityStateHandler!=null ? entityStateHandler.equals(this, obj) : obj == this; } public static EntityStateHandler entityStateHandler() { - return template().getEntityStateHandler(); + final Neo4jTemplate template = template(); + return template!=null ? template.getEntityStateHandler() : null; } /** * @return result of the hashCode of the underlying node (if any, otherwise identityHashCode) */ public int NodeBacked.hashCode() { - return entityStateHandler().hashCode(this); + final EntityStateHandler entityStateHandler = entityStateHandler(); + return entityStateHandler!=null ? entityStateHandler.hashCode(this) : System.identityHashCode(this); } /** diff --git a/spring-data-neo4j-aspects/src/test/java/org/springframework/data/neo4j/aspects/support/EntityWithoutAspectSetupTest.java b/spring-data-neo4j-aspects/src/test/java/org/springframework/data/neo4j/aspects/support/EntityWithoutAspectSetupTest.java new file mode 100644 index 000000000..510248947 --- /dev/null +++ b/spring-data-neo4j-aspects/src/test/java/org/springframework/data/neo4j/aspects/support/EntityWithoutAspectSetupTest.java @@ -0,0 +1,39 @@ +/** + * Copyright 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.springframework.data.neo4j.aspects.support; + +import org.junit.Test; +import org.springframework.data.neo4j.aspects.Person; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +public class EntityWithoutAspectSetupTest { + @Test + public void testEquals() throws Exception { + final Person p1 = new Person(); + final Person p2 = new Person(); + assertEquals(p1, p1); + assertEquals(false, p1.equals(p2)); + } + @Test + public void testHashCode() throws Exception { + final Person p1 = new Person(); + final Person p2 = new Person(); + assertTrue(p1.hashCode() > 0); + assertEquals(false, p1.hashCode() == p2.hashCode()); + } +}