You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The class MediaType has a natural ordering that is inconsistent with equals, which is generally recommended or should otherwise at least be indicated in the javadoc [SPR-6788] #11454
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);
}