From 5c248983d2fb53d808052003352157d11ac53954 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Tue, 3 Apr 2012 10:32:52 +0200 Subject: [PATCH] =?UTF-8?q?DATACMNS-120=20-=20Added=20equals(=E2=80=A6)=20?= =?UTF-8?q?and=20hashCode()=20methods=20to=20Parameter.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../data/mapping/PreferredConstructor.java | 44 +++++++++ .../data/mapping/ParameterUnitTests.java | 96 +++++++++++++++++++ 2 files changed, 140 insertions(+) create mode 100644 spring-data-commons-core/src/test/java/org/springframework/data/mapping/ParameterUnitTests.java diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/mapping/PreferredConstructor.java b/spring-data-commons-core/src/main/java/org/springframework/data/mapping/PreferredConstructor.java index 82515bea5..0c5ea7d7f 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/mapping/PreferredConstructor.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/mapping/PreferredConstructor.java @@ -15,6 +15,8 @@ */ package org.springframework.data.mapping; +import static org.springframework.util.ObjectUtils.*; + import java.lang.annotation.Annotation; import java.lang.reflect.Constructor; import java.util.Arrays; @@ -245,5 +247,47 @@ public class PreferredConstructor> { Class owningType = entity.getType(); return owningType.isMemberClass() && type.getType().equals(owningType.getEnclosingClass()); } + + /* + * (non-Javadoc) + * @see java.lang.Object#equals(java.lang.Object) + */ + @Override + public boolean equals(Object obj) { + + if (this == obj) { + return true; + } + + if (!(obj instanceof Parameter)) { + return false; + } + + Parameter that = (Parameter) obj; + + boolean nameEquals = this.name == null ? that.name == null : this.name.equals(that.name); + boolean keyEquals = this.key == null ? that.key == null : this.key.equals(that.key); + boolean typeEquals = nullSafeEquals(this.type, that.type); + boolean entityEquals = this.entity == null ? that.entity == null : this.entity.equals(that.entity); + + return nameEquals && keyEquals && typeEquals && entityEquals; + } + + /* + * (non-Javadoc) + * @see java.lang.Object#hashCode() + */ + @Override + public int hashCode() { + + int result = 17; + + result += 31 * nullSafeHashCode(this.name); + result += 31 * nullSafeHashCode(this.key); + result += 31 * nullSafeHashCode(this.type); + result += 31 * nullSafeHashCode(this.entity); + + return result; + } } } diff --git a/spring-data-commons-core/src/test/java/org/springframework/data/mapping/ParameterUnitTests.java b/spring-data-commons-core/src/test/java/org/springframework/data/mapping/ParameterUnitTests.java new file mode 100644 index 000000000..02b8f3d31 --- /dev/null +++ b/spring-data-commons-core/src/test/java/org/springframework/data/mapping/ParameterUnitTests.java @@ -0,0 +1,96 @@ +/* + * Copyright 2012 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.mapping; + +import static org.hamcrest.CoreMatchers.*; +import static org.junit.Assert.*; + +import java.lang.annotation.Annotation; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.runners.MockitoJUnitRunner; +import org.springframework.data.mapping.PreferredConstructor.Parameter; +import org.springframework.data.util.ClassTypeInformation; +import org.springframework.data.util.TypeInformation; + +/** + * Unit tests for {@link Parameter}. + * + * @author Oliver Gierke + */ +@RunWith(MockitoJUnitRunner.class) +public class ParameterUnitTests

> { + + @Mock + PersistentEntity entity; + @Mock + PersistentEntity stringEntity; + + TypeInformation type = ClassTypeInformation.from(Object.class); + Annotation[] annotations = new Annotation[0]; + + @Test + public void twoParametersWithIdenticalSetupEqual() { + + Parameter left = new Parameter("name", type, annotations, entity); + Parameter right = new Parameter("name", type, annotations, entity); + + assertThat(left, is(right)); + assertThat(left.hashCode(), is(right.hashCode())); + } + + @Test + public void twoParametersWithIdenticalSetupAndNullNameEqual() { + + Parameter left = new Parameter(null, type, annotations, entity); + Parameter right = new Parameter(null, type, annotations, entity); + + assertThat(left, is(right)); + assertThat(left.hashCode(), is(right.hashCode())); + } + + @Test + public void twoParametersWithIdenticalAndNullEntitySetupEqual() { + + Parameter left = new Parameter("name", type, annotations, null); + Parameter right = new Parameter("name", type, annotations, null); + + assertThat(left, is(right)); + assertThat(left.hashCode(), is(right.hashCode())); + } + + @Test + public void twoParametersWithDifferentNameAreNotEqual() { + + Parameter left = new Parameter("first", type, annotations, entity); + Parameter right = new Parameter("second", type, annotations, entity); + + assertThat(left, is(not(right))); + } + + @Test + @SuppressWarnings("rawtypes") + public void twoParametersWithDifferenTypeAreNotEqual() { + + Parameter left = new Parameter("name", type, annotations, entity); + Parameter right = new Parameter("name", ClassTypeInformation.from(String.class), annotations, + stringEntity); + + assertThat(left, is(not(right))); + } +}