#40 hashCode and equals in unit-tests

This commit is contained in:
Michael Hunger
2012-03-22 13:10:33 +01:00
parent 4a8364e996
commit d5f50958f3
2 changed files with 45 additions and 3 deletions

View File

@@ -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);
}
/**

View File

@@ -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());
}
}