SPR-6788: fixed compareTo() consistency with equals

This commit is contained in:
Arjen Poutsma 2010-02-04 14:19:11 +00:00
parent 0f7f749ada
commit 7832381dd2
2 changed files with 18 additions and 6 deletions

View File

@ -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> {
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));
}

View File

@ -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 {
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