Tomas Johansson opened SPR-6788 and commented
For example, if you use these two strings:
"text/html; q=0.7; charset=iso-8859-1"
"text/html; q=0.7"
then the compareTo method will return zero but equals will return false.
I suggest that a consistent implementation should either be fixed, or otherwise (if necessary for some reason ?) at least it should be mentioned in the javadoc that the natural ordering that is inconsistent with equals.
Example of JUnit test that I think should pass:
@Test
public void verifyConsistentImplementationOf_equals_And_compareTo() {
verifyConsistentImplementationOf_equals_And_compareTo(
MediaType.parseMediaType("text/html; q=0.7; charset=iso-8859-1"),
MediaType.parseMediaType("text/html; q=0.7")
);
verifyConsistentImplementationOf_equals_And_compareTo(
[add more test cases ...]
);
[add more test cases ...]
}
private void verifyConsistentImplementationOf_equals_And_compareTo(MediaType mediaType1, MediaType mediaType2) {
assertEquals(mediaType1.equals(mediaType2), mediaType1.compareTo(mediaType2) == 0);
}
/ Tomas Johansson
Affects: 3.0 GA