From af272c212419a373d09078eb717e5f2195962f53 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Wed, 15 Apr 2015 14:58:16 +0200 Subject: [PATCH] HttpEntity and its subclasses insist on same target type for equality Issue: SPR-12910 --- .../org/springframework/http/HttpEntity.java | 9 ++-- .../springframework/http/RequestEntity.java | 4 +- .../springframework/http/ResponseEntity.java | 3 +- .../springframework/http/HttpEntityTests.java | 41 ++++++++++++++++--- .../http/RequestEntityTests.java | 4 +- .../http/ResponseEntityTests.java | 3 -- 6 files changed, 47 insertions(+), 17 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 98d566f1ff..d9697697f0 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-2014 the original author or authors. + * Copyright 2002-2015 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. @@ -22,7 +22,8 @@ import org.springframework.util.ObjectUtils; /** * Represents an HTTP request or response entity, consisting of headers and body. * - *

Typically used in combination with the {@link org.springframework.web.client.RestTemplate RestTemplate}, like so: + *

Typically used in combination with the {@link org.springframework.web.client.RestTemplate}, + * like so: *

  * HttpHeaders headers = new HttpHeaders();
  * headers.setContentType(MediaType.TEXT_PLAIN);
@@ -46,6 +47,7 @@ import org.springframework.util.ObjectUtils;
  * 
* * @author Arjen Poutsma + * @author Juergen Hoeller * @since 3.0.2 * @see org.springframework.web.client.RestTemplate * @see #getBody() @@ -123,12 +125,13 @@ public class HttpEntity { return (this.body != null); } + @Override public boolean equals(Object other) { if (this == other) { return true; } - if (!(other instanceof HttpEntity)) { + if (other == null || !other.getClass().equals(getClass())) { return false; } HttpEntity otherEntity = (HttpEntity) other; diff --git a/spring-web/src/main/java/org/springframework/http/RequestEntity.java b/spring-web/src/main/java/org/springframework/http/RequestEntity.java index 79fb09e9e4..3ddb55d634 100644 --- a/spring-web/src/main/java/org/springframework/http/RequestEntity.java +++ b/spring-web/src/main/java/org/springframework/http/RequestEntity.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2015 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. @@ -133,7 +133,7 @@ public class RequestEntity extends HttpEntity { if (this == other) { return true; } - if (!(other instanceof RequestEntity) || !super.equals(other)) { + if (!super.equals(other)) { return false; } RequestEntity otherEntity = (RequestEntity) other; 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 c751dcbd17..50027872cd 100644 --- a/spring-web/src/main/java/org/springframework/http/ResponseEntity.java +++ b/spring-web/src/main/java/org/springframework/http/ResponseEntity.java @@ -117,12 +117,13 @@ public class ResponseEntity extends HttpEntity { return this.statusCode; } + @Override public boolean equals(Object other) { if (this == other) { return true; } - if (!(other instanceof ResponseEntity) || !super.equals(other)) { + if (!super.equals(other)) { return false; } ResponseEntity otherEntity = (ResponseEntity) other; 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 039396c021..5db52000a6 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-2012 the original author or authors. + * Copyright 2002-2015 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. @@ -16,6 +16,8 @@ package org.springframework.http; +import java.net.URI; + import org.junit.Test; import org.springframework.util.LinkedMultiValueMap; @@ -86,12 +88,39 @@ public class HttpEntityTests { HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.TEXT_PLAIN); String body = "foo"; - ResponseEntity entity = new ResponseEntity(body, headers, HttpStatus.OK); - assertEquals(body, entity.getBody()); - assertEquals(MediaType.TEXT_PLAIN, entity.getHeaders().getContentType()); - assertEquals("text/plain", entity.getHeaders().getFirst("Content-Type")); - assertEquals("text/plain", entity.getHeaders().getFirst("Content-Type")); + HttpEntity httpEntity = new HttpEntity(body, headers); + ResponseEntity responseEntity = new ResponseEntity(body, headers, HttpStatus.OK); + ResponseEntity responseEntity2 = new ResponseEntity(body, headers, HttpStatus.OK); + + assertEquals(body, responseEntity.getBody()); + assertEquals(MediaType.TEXT_PLAIN, responseEntity.getHeaders().getContentType()); + assertEquals("text/plain", responseEntity.getHeaders().getFirst("Content-Type")); + assertEquals("text/plain", responseEntity.getHeaders().getFirst("Content-Type")); + + assertFalse(httpEntity.equals(responseEntity)); + assertFalse(responseEntity.equals(httpEntity)); + assertTrue(responseEntity.equals(responseEntity2)); + assertTrue(responseEntity2.equals(responseEntity)); + } + @Test + public void requestEntity() throws Exception { + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(MediaType.TEXT_PLAIN); + String body = "foo"; + HttpEntity httpEntity = new HttpEntity(body, headers); + RequestEntity requestEntity = new RequestEntity(body, headers, HttpMethod.GET, new URI("/")); + RequestEntity requestEntity2 = new RequestEntity(body, headers, HttpMethod.GET, new URI("/")); + + assertEquals(body, requestEntity.getBody()); + assertEquals(MediaType.TEXT_PLAIN, requestEntity.getHeaders().getContentType()); + assertEquals("text/plain", requestEntity.getHeaders().getFirst("Content-Type")); + assertEquals("text/plain", requestEntity.getHeaders().getFirst("Content-Type")); + + assertFalse(httpEntity.equals(requestEntity)); + assertFalse(requestEntity.equals(httpEntity)); + assertTrue(requestEntity.equals(requestEntity2)); + assertTrue(requestEntity2.equals(requestEntity)); } } diff --git a/spring-web/src/test/java/org/springframework/http/RequestEntityTests.java b/spring-web/src/test/java/org/springframework/http/RequestEntityTests.java index 7409c412f3..de235366ba 100644 --- a/spring-web/src/test/java/org/springframework/http/RequestEntityTests.java +++ b/spring-web/src/test/java/org/springframework/http/RequestEntityTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2015 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. @@ -148,4 +148,4 @@ public class RequestEntityTests { } -} \ No newline at end of file +} diff --git a/spring-web/src/test/java/org/springframework/http/ResponseEntityTests.java b/spring-web/src/test/java/org/springframework/http/ResponseEntityTests.java index 5ce43e8dad..16a0ad4c7e 100644 --- a/spring-web/src/test/java/org/springframework/http/ResponseEntityTests.java +++ b/spring-web/src/test/java/org/springframework/http/ResponseEntityTests.java @@ -26,7 +26,6 @@ import org.junit.Test; import static org.junit.Assert.*; - /** * @author Arjen Poutsma * @author Marcel Overdijk @@ -210,7 +209,6 @@ public class ResponseEntityTests { @Test public void cacheControl() { - Integer entity = new Integer(42); ResponseEntity responseEntity = @@ -229,7 +227,6 @@ public class ResponseEntityTests { @Test public void cacheControlNoCache() { - Integer entity = new Integer(42); ResponseEntity responseEntity =