From a49851d5ebcb5d3c65e324f279f3a8c33f4db16f Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Tue, 28 Aug 2012 17:24:31 -0400 Subject: [PATCH] Add equals/hashcode to ResponseEntity Issue: SPR-9714 --- .../org/springframework/http/HttpEntity.java | 22 +++++++++++++- .../springframework/http/ResponseEntity.java | 20 ++++++++++++- .../springframework/http/HttpEntityTests.java | 29 +++++++++++++++++-- 3 files changed, 66 insertions(+), 5 deletions(-) diff --git a/spring-web/src/main/java/org/springframework/http/HttpEntity.java b/spring-web/src/main/java/org/springframework/http/HttpEntity.java index cceafc8876..c876d8fe70 100644 --- a/spring-web/src/main/java/org/springframework/http/HttpEntity.java +++ b/spring-web/src/main/java/org/springframework/http/HttpEntity.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2011 the original author or authors. + * Copyright 2002-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. @@ -17,6 +17,7 @@ package org.springframework.http; import org.springframework.util.MultiValueMap; +import org.springframework.util.ObjectUtils; /** * Represents an HTTP request or response entity, consisting of headers and body. @@ -122,6 +123,24 @@ public class HttpEntity { return (this.body != null); } + @Override + public boolean equals(Object other) { + if (this == other) { + return true; + } + if (!(other instanceof HttpEntity)) { + return false; + } + HttpEntity otherEntity = (HttpEntity) other; + return (ObjectUtils.nullSafeEquals(this.headers, otherEntity.headers) && + ObjectUtils.nullSafeEquals(this.body, otherEntity.body)); + } + + @Override + public int hashCode() { + return ObjectUtils.nullSafeHashCode(this.headers) * 29 + ObjectUtils.nullSafeHashCode(this.body); + } + @Override public String toString() { StringBuilder builder = new StringBuilder("<"); @@ -137,4 +156,5 @@ public class HttpEntity { builder.append('>'); return builder.toString(); } + } diff --git a/spring-web/src/main/java/org/springframework/http/ResponseEntity.java b/spring-web/src/main/java/org/springframework/http/ResponseEntity.java index 26ff6e0f0a..458672a5cd 100644 --- a/spring-web/src/main/java/org/springframework/http/ResponseEntity.java +++ b/spring-web/src/main/java/org/springframework/http/ResponseEntity.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2011 the original author or authors. + * Copyright 2002-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. @@ -17,6 +17,7 @@ package org.springframework.http; import org.springframework.util.MultiValueMap; +import org.springframework.util.ObjectUtils; /** * Extension of {@link HttpEntity} that adds a {@link HttpStatus} status code. @@ -94,6 +95,23 @@ public class ResponseEntity extends HttpEntity { return statusCode; } + @Override + public boolean equals(Object other) { + if (this == other) { + return true; + } + if (!(other instanceof ResponseEntity)) { + return false; + } + ResponseEntity otherEntity = (ResponseEntity) other; + return (ObjectUtils.nullSafeEquals(this.statusCode, otherEntity.statusCode) && super.equals(other)); + } + + @Override + public int hashCode() { + return super.hashCode() * 29 + ObjectUtils.nullSafeHashCode(this.statusCode); + } + @Override public String toString() { StringBuilder builder = new StringBuilder("<"); diff --git a/spring-web/src/test/java/org/springframework/http/HttpEntityTests.java b/spring-web/src/test/java/org/springframework/http/HttpEntityTests.java index a769027a00..9b70cecfb1 100644 --- a/spring-web/src/test/java/org/springframework/http/HttpEntityTests.java +++ b/spring-web/src/test/java/org/springframework/http/HttpEntityTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2010 the original author or authors. + * Copyright 2002-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. @@ -34,7 +34,7 @@ public class HttpEntityTests { assertSame(body, entity.getBody()); assertTrue(entity.getHeaders().isEmpty()); } - + @Test public void httpHeaders() { HttpHeaders headers = new HttpHeaders(); @@ -56,7 +56,30 @@ public class HttpEntityTests { assertEquals(MediaType.TEXT_PLAIN, entity.getHeaders().getContentType()); assertEquals("text/plain", entity.getHeaders().getFirst("Content-Type")); } - + + @Test + public void testEquals() { + MultiValueMap map1 = new LinkedMultiValueMap(); + map1.set("Content-Type", "text/plain"); + + MultiValueMap map2 = new LinkedMultiValueMap(); + map2.set("Content-Type", "application/json"); + + assertTrue(new HttpEntity().equals(new HttpEntity())); + assertFalse(new HttpEntity(map1).equals(new HttpEntity())); + assertFalse(new HttpEntity().equals(new HttpEntity(map2))); + + assertTrue(new HttpEntity(map1).equals(new HttpEntity(map1))); + assertFalse(new HttpEntity(map1).equals(new HttpEntity(map2))); + + assertTrue(new HttpEntity(null, null).equals(new HttpEntity(null, null))); + assertFalse(new HttpEntity("foo", null).equals(new HttpEntity(null, null))); + assertFalse(new HttpEntity(null, null).equals(new HttpEntity("bar", null))); + + assertTrue(new HttpEntity("foo", map1).equals(new HttpEntity("foo", map1))); + assertFalse(new HttpEntity("foo", map1).equals(new HttpEntity("bar", map1))); + } + @Test public void responseEntity() { HttpHeaders headers = new HttpHeaders();