From 6c368faa0ec1081a74358cb77afd1b9d7828675a Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Wed, 22 Aug 2012 18:33:35 +0200 Subject: [PATCH] =?UTF-8?q?#5=20-=20Added=20equals(=E2=80=A6)=20/=20hashCo?= =?UTF-8?q?de()=20to=20Resource/Resources.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../org/springframework/hateoas/Resource.java | 33 +++++++++ .../springframework/hateoas/Resources.java | 35 +++++++++ .../hateoas/ResourceUnitTest.java | 67 +++++++++++++++++ .../hateoas/ResourcesUnitTest.java | 73 +++++++++++++++++++ 4 files changed, 208 insertions(+) create mode 100644 src/test/java/org/springframework/hateoas/ResourceUnitTest.java create mode 100644 src/test/java/org/springframework/hateoas/ResourcesUnitTest.java diff --git a/src/main/java/org/springframework/hateoas/Resource.java b/src/main/java/org/springframework/hateoas/Resource.java index 773b54b7..57f35797 100644 --- a/src/main/java/org/springframework/hateoas/Resource.java +++ b/src/main/java/org/springframework/hateoas/Resource.java @@ -54,4 +54,37 @@ public class Resource extends ResourceSupport { public T getContent() { return content; } + + /* + * (non-Javadoc) + * @see org.springframework.hateoas.ResourceSupport#equals(java.lang.Object) + */ + @Override + public boolean equals(Object obj) { + + if (this == obj) { + return true; + } + + if (obj == null || !obj.getClass().equals(getClass())) { + return false; + } + + Resource that = (Resource) obj; + + boolean contentEqual = this.content == null ? that.content == null : this.content.equals(that.content); + return contentEqual ? super.equals(obj) : false; + } + + /* + * (non-Javadoc) + * @see org.springframework.hateoas.ResourceSupport#hashCode() + */ + @Override + public int hashCode() { + + int result = super.hashCode(); + result += content == null ? 0 : 17 * content.hashCode(); + return result; + } } diff --git a/src/main/java/org/springframework/hateoas/Resources.java b/src/main/java/org/springframework/hateoas/Resources.java index 68bb4b8f..e90ec5a3 100644 --- a/src/main/java/org/springframework/hateoas/Resources.java +++ b/src/main/java/org/springframework/hateoas/Resources.java @@ -95,4 +95,39 @@ public class Resources> extends ResourceSupport implements public Iterator iterator() { return content.iterator(); } + + /* + * (non-Javadoc) + * @see org.springframework.hateoas.ResourceSupport#equals(java.lang.Object) + */ + @Override + public boolean equals(Object obj) { + + if (obj == this) { + return true; + } + + if (obj == null || !obj.getClass().equals(getClass())) { + return false; + } + + Resources that = (Resources) obj; + + boolean contentEqual = this.content == null ? that.content == null : this.content.equals(that.content); + return contentEqual ? super.equals(obj) : false; + } + + /* + * (non-Javadoc) + * @see org.springframework.hateoas.ResourceSupport#hashCode() + */ + @Override + public int hashCode() { + + int result = super.hashCode(); + result += content == null ? 0 : 17 * content.hashCode(); + + return result; + } + } diff --git a/src/test/java/org/springframework/hateoas/ResourceUnitTest.java b/src/test/java/org/springframework/hateoas/ResourceUnitTest.java new file mode 100644 index 00000000..25c5c560 --- /dev/null +++ b/src/test/java/org/springframework/hateoas/ResourceUnitTest.java @@ -0,0 +1,67 @@ +/* + * 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.hateoas; + +import static org.hamcrest.CoreMatchers.*; +import static org.junit.Assert.*; + +import org.junit.Test; + +/** + * Unit tests for {@link Resource}. + * + * @author Oliver Gierke + */ +public class ResourceUnitTest { + + @Test + public void equalsForSelfReference() { + + Resource resource = new Resource("foo"); + assertThat(resource, is(resource)); + } + + @Test + public void equalsWithEqualContent() { + + Resource left = new Resource("foo"); + Resource right = new Resource("foo"); + + assertThat(left, is(right)); + assertThat(right, is(left)); + } + + @Test + public void notEqualForDifferentContent() { + + Resource left = new Resource("foo"); + Resource right = new Resource("bar"); + + assertThat(left, is(not(right))); + assertThat(right, is(not(left))); + } + + @Test + public void notEqualForDifferentLinks() { + + Resource left = new Resource("foo"); + Resource right = new Resource("foo"); + right.add(new Link("localhost")); + + assertThat(left, is(not(right))); + assertThat(right, is(not(left))); + } +} diff --git a/src/test/java/org/springframework/hateoas/ResourcesUnitTest.java b/src/test/java/org/springframework/hateoas/ResourcesUnitTest.java new file mode 100644 index 00000000..e79166c6 --- /dev/null +++ b/src/test/java/org/springframework/hateoas/ResourcesUnitTest.java @@ -0,0 +1,73 @@ +/* + * 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.hateoas; + +import static org.hamcrest.CoreMatchers.*; +import static org.junit.Assert.*; + +import java.util.Collections; +import java.util.Set; + +import org.junit.Test; + +/** + * Unit tests for {@link Resources}. + * + * @author Oliver Gierke + */ +public class ResourcesUnitTest { + + Set> foo = Collections.singleton(new Resource("foo")); + Set> bar = Collections.singleton(new Resource("bar")); + + @Test + public void equalsForSelfReference() { + + Resources> resource = new Resources>(foo); + assertThat(resource, is(resource)); + } + + @Test + public void equalsWithEqualContent() { + + Resources> left = new Resources>(foo); + Resources> right = new Resources>(foo); + + assertThat(left, is(right)); + assertThat(right, is(left)); + } + + @Test + public void notEqualForDifferentContent() { + + Resources> left = new Resources>(foo); + Resources> right = new Resources>(bar); + + assertThat(left, is(not(right))); + assertThat(right, is(not(left))); + } + + @Test + public void notEqualForDifferentLinks() { + + Resources> left = new Resources>(foo); + Resources> right = new Resources>(bar); + right.add(new Link("localhost")); + + assertThat(left, is(not(right))); + assertThat(right, is(not(left))); + } +}