Browse Source

SPR-6788: fixed compareTo() consistency with equals

git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@2921 50f2f4bb-b051-0410-bef5-90022cba6387
pull/1/head
Arjen Poutsma 16 years ago
parent
commit
5882930735
  1. 16
      org.springframework.web/src/main/java/org/springframework/http/MediaType.java
  2. 8
      org.springframework.web/src/test/java/org/springframework/http/MediaTypeTests.java

16
org.springframework.web/src/main/java/org/springframework/http/MediaType.java

@ -324,11 +324,15 @@ public class MediaType implements Comparable<MediaType> { @@ -324,11 +324,15 @@ public class MediaType implements Comparable<MediaType> {
if (comp != 0) {
return comp;
}
Iterator<String> thisAttributes = new TreeSet<String>(this.parameters.keySet()).iterator();
Iterator<String> otherAttributes = new TreeSet<String>(other.parameters.keySet()).iterator();
while (thisAttributes.hasNext()) {
String thisAttribute = thisAttributes.next();
String otherAttribute = otherAttributes.next();
TreeSet<String> thisAttributes = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER);
thisAttributes.addAll(this.parameters.keySet());
TreeSet<String> otherAttributes = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER);
otherAttributes.addAll(other.parameters.keySet());
Iterator<String> thisAttributesIterator = thisAttributes.iterator();
Iterator<String> otherAttributesIterator = otherAttributes.iterator();
while (thisAttributesIterator.hasNext()) {
String thisAttribute = thisAttributesIterator.next();
String otherAttribute = otherAttributesIterator.next();
comp = thisAttribute.compareToIgnoreCase(otherAttribute);
if (comp != 0) {
return comp;
@ -355,7 +359,7 @@ public class MediaType implements Comparable<MediaType> { @@ -355,7 +359,7 @@ public class MediaType implements Comparable<MediaType> {
return false;
}
MediaType otherType = (MediaType) other;
return (this.type.equals(otherType.type) && this.subtype.equals(otherType.subtype) &&
return (this.type.equalsIgnoreCase(otherType.type) && this.subtype.equalsIgnoreCase(otherType.subtype) &&
this.parameters.equals(otherType.parameters));
}

8
org.springframework.web/src/test/java/org/springframework/http/MediaTypeTests.java

@ -214,6 +214,12 @@ public class MediaTypeTests { @@ -214,6 +214,12 @@ public class MediaTypeTests {
assertEquals("Media types not equal", m1, m2);
assertEquals("compareTo() not consistent with equals", 0, m1.compareTo(m2));
assertEquals("compareTo() not consistent with equals", 0, m2.compareTo(m1));
m1 = MediaType.parseMediaType("text/html; q=0.7; charset=iso-8859-1");
m2 = MediaType.parseMediaType("text/html; Q=0.7; charset=iso-8859-1");
assertEquals("Media types not equal", m1, m2);
assertEquals("compareTo() not consistent with equals", 0, m1.compareTo(m2));
assertEquals("compareTo() not consistent with equals", 0, m2.compareTo(m1));
}
@Test
@ -232,6 +238,8 @@ public class MediaTypeTests { @@ -232,6 +238,8 @@ public class MediaTypeTests {
m2 = new MediaType("audio", "basic", Collections.singletonMap("foo", "Bar"));
assertTrue("Invalid comparison result", m1.compareTo(m2) != 0);
assertTrue("Invalid comparison result", m2.compareTo(m1) != 0);
}
@Test

Loading…
Cancel
Save