Browse Source

HttpEntity and its subclasses insist on same target type for equality

Issue: SPR-12910
pull/777/head
Juergen Hoeller 10 years ago
parent
commit
af272c2124
  1. 9
      spring-web/src/main/java/org/springframework/http/HttpEntity.java
  2. 4
      spring-web/src/main/java/org/springframework/http/RequestEntity.java
  3. 3
      spring-web/src/main/java/org/springframework/http/ResponseEntity.java
  4. 41
      spring-web/src/test/java/org/springframework/http/HttpEntityTests.java
  5. 4
      spring-web/src/test/java/org/springframework/http/RequestEntityTests.java
  6. 3
      spring-web/src/test/java/org/springframework/http/ResponseEntityTests.java

9
spring-web/src/main/java/org/springframework/http/HttpEntity.java

@ -1,5 +1,5 @@ @@ -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; @@ -22,7 +22,8 @@ import org.springframework.util.ObjectUtils;
/**
* Represents an HTTP request or response entity, consisting of headers and body.
*
* <p>Typically used in combination with the {@link org.springframework.web.client.RestTemplate RestTemplate}, like so:
* <p>Typically used in combination with the {@link org.springframework.web.client.RestTemplate},
* like so:
* <pre class="code">
* HttpHeaders headers = new HttpHeaders();
* headers.setContentType(MediaType.TEXT_PLAIN);
@ -46,6 +47,7 @@ import org.springframework.util.ObjectUtils; @@ -46,6 +47,7 @@ import org.springframework.util.ObjectUtils;
* </pre>
*
* @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<T> { @@ -123,12 +125,13 @@ public class HttpEntity<T> {
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;

4
spring-web/src/main/java/org/springframework/http/RequestEntity.java

@ -1,5 +1,5 @@ @@ -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<T> extends HttpEntity<T> { @@ -133,7 +133,7 @@ public class RequestEntity<T> extends HttpEntity<T> {
if (this == other) {
return true;
}
if (!(other instanceof RequestEntity) || !super.equals(other)) {
if (!super.equals(other)) {
return false;
}
RequestEntity<?> otherEntity = (RequestEntity<?>) other;

3
spring-web/src/main/java/org/springframework/http/ResponseEntity.java

@ -117,12 +117,13 @@ public class ResponseEntity<T> extends HttpEntity<T> { @@ -117,12 +117,13 @@ public class ResponseEntity<T> extends HttpEntity<T> {
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;

41
spring-web/src/test/java/org/springframework/http/HttpEntityTests.java

@ -1,5 +1,5 @@ @@ -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 @@ @@ -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 { @@ -86,12 +88,39 @@ public class HttpEntityTests {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.TEXT_PLAIN);
String body = "foo";
ResponseEntity<String> entity = new ResponseEntity<String>(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<String> httpEntity = new HttpEntity<String>(body, headers);
ResponseEntity<String> responseEntity = new ResponseEntity<String>(body, headers, HttpStatus.OK);
ResponseEntity<String> responseEntity2 = new ResponseEntity<String>(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<String> httpEntity = new HttpEntity<String>(body, headers);
RequestEntity<String> requestEntity = new RequestEntity<String>(body, headers, HttpMethod.GET, new URI("/"));
RequestEntity<String> requestEntity2 = new RequestEntity<String>(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));
}
}

4
spring-web/src/test/java/org/springframework/http/RequestEntityTests.java

@ -1,5 +1,5 @@ @@ -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 { @@ -148,4 +148,4 @@ public class RequestEntityTests {
}
}
}

3
spring-web/src/test/java/org/springframework/http/ResponseEntityTests.java

@ -26,7 +26,6 @@ import org.junit.Test; @@ -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 { @@ -210,7 +209,6 @@ public class ResponseEntityTests {
@Test
public void cacheControl() {
Integer entity = new Integer(42);
ResponseEntity<Integer> responseEntity =
@ -229,7 +227,6 @@ public class ResponseEntityTests { @@ -229,7 +227,6 @@ public class ResponseEntityTests {
@Test
public void cacheControlNoCache() {
Integer entity = new Integer(42);
ResponseEntity<Integer> responseEntity =

Loading…
Cancel
Save