Browse Source
MimeType is available in core-spring and does not include support for quality parameters and media used in HTTP content negotiation. The MediaType sub-class in org.springframework.http adds q-parameters.pull/331/head
11 changed files with 1263 additions and 577 deletions
@ -0,0 +1,52 @@
@@ -0,0 +1,52 @@
|
||||
/* |
||||
* Copyright 2002-2013 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. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.util; |
||||
|
||||
/** |
||||
* Exception thrown from {@link MediaType#parseMimeType(String)} in case of |
||||
* encountering an invalid content type specification String. |
||||
* |
||||
* @author Juergen Hoeller |
||||
* @author Rossen Stoyanchev |
||||
* @since 4.0 |
||||
*/ |
||||
@SuppressWarnings("serial") |
||||
public class InvalidMimeTypeException extends IllegalArgumentException { |
||||
|
||||
private String mimeType; |
||||
|
||||
|
||||
/** |
||||
* Create a new InvalidContentTypeException for the given content type. |
||||
* @param mimeType the offending media type |
||||
* @param message a detail message indicating the invalid part |
||||
*/ |
||||
public InvalidMimeTypeException(String mimeType, String message) { |
||||
super("Invalid mime type \"" + mimeType + "\": " + message); |
||||
this.mimeType = mimeType; |
||||
|
||||
} |
||||
|
||||
|
||||
/** |
||||
* Return the offending content type. |
||||
*/ |
||||
public String getMimeType() { |
||||
return this.mimeType; |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,502 @@
@@ -0,0 +1,502 @@
|
||||
/* |
||||
* Copyright 2002-2013 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. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.util; |
||||
|
||||
import java.nio.charset.Charset; |
||||
import java.util.BitSet; |
||||
import java.util.Collections; |
||||
import java.util.Comparator; |
||||
import java.util.Iterator; |
||||
import java.util.List; |
||||
import java.util.Locale; |
||||
import java.util.Map; |
||||
import java.util.TreeSet; |
||||
|
||||
|
||||
/** |
||||
* Represents a MIME Type, as originally defined in RFC 2046 and subsequently used in |
||||
* other Internet protocols including HTTP. This class however does not contain support |
||||
* the q-parameters used in HTTP content negotiation. Those can be found in the sub-class
|
||||
* {@code org.springframework.http.MediaType} in the {@code spring-web} module. |
||||
* <p> |
||||
* Consists of a {@linkplain #getType() type} and a {@linkplain #getSubtype() subtype}. |
||||
* Also has functionality to parse media types from a string using |
||||
* {@link #parseMimeType(String)}, or multiple comma-separated media types using |
||||
* {@link #parseMimeTypes(String)}. |
||||
* |
||||
* @author Arjen Poutsma |
||||
* @author Juergen Hoeller |
||||
* @author Rossen Stoyanchev |
||||
* @since 4.0 |
||||
* |
||||
* @see MimeTypeUtils |
||||
*/ |
||||
public class MimeType implements Comparable<MimeType> { |
||||
|
||||
protected static final String WILDCARD_TYPE = "*"; |
||||
|
||||
private static final BitSet TOKEN; |
||||
|
||||
private static final String PARAM_CHARSET = "charset"; |
||||
|
||||
|
||||
private final String type; |
||||
|
||||
private final String subtype; |
||||
|
||||
private final Map<String, String> parameters; |
||||
|
||||
|
||||
static { |
||||
// variable names refer to RFC 2616, section 2.2
|
||||
BitSet ctl = new BitSet(128); |
||||
for (int i=0; i <= 31; i++) { |
||||
ctl.set(i); |
||||
} |
||||
ctl.set(127); |
||||
|
||||
BitSet separators = new BitSet(128); |
||||
separators.set('('); |
||||
separators.set(')'); |
||||
separators.set('<'); |
||||
separators.set('>'); |
||||
separators.set('@'); |
||||
separators.set(','); |
||||
separators.set(';'); |
||||
separators.set(':'); |
||||
separators.set('\\'); |
||||
separators.set('\"'); |
||||
separators.set('/'); |
||||
separators.set('['); |
||||
separators.set(']'); |
||||
separators.set('?'); |
||||
separators.set('='); |
||||
separators.set('{'); |
||||
separators.set('}'); |
||||
separators.set(' '); |
||||
separators.set('\t'); |
||||
|
||||
TOKEN = new BitSet(128); |
||||
TOKEN.set(0, 128); |
||||
TOKEN.andNot(ctl); |
||||
TOKEN.andNot(separators); |
||||
} |
||||
|
||||
|
||||
/** |
||||
* Create a new {@code MimeType} for the given primary type. |
||||
* <p>The {@linkplain #getSubtype() subtype} is set to "*", parameters empty. |
||||
* @param type the primary type |
||||
* @throws IllegalArgumentException if any of the parameters contain illegal characters |
||||
*/ |
||||
public MimeType(String type) { |
||||
this(type, WILDCARD_TYPE); |
||||
} |
||||
|
||||
/** |
||||
* Create a new {@code MimeType} for the given primary type and subtype. |
||||
* <p>The parameters are empty. |
||||
* @param type the primary type |
||||
* @param subtype the subtype |
||||
* @throws IllegalArgumentException if any of the parameters contain illegal characters |
||||
*/ |
||||
public MimeType(String type, String subtype) { |
||||
this(type, subtype, Collections.<String, String>emptyMap()); |
||||
} |
||||
|
||||
/** |
||||
* Create a new {@code MimeType} for the given type, subtype, and character set. |
||||
* @param type the primary type |
||||
* @param subtype the subtype |
||||
* @param charSet the character set |
||||
* @throws IllegalArgumentException if any of the parameters contain illegal characters |
||||
*/ |
||||
public MimeType(String type, String subtype, Charset charSet) { |
||||
this(type, subtype, Collections.singletonMap(PARAM_CHARSET, charSet.name())); |
||||
} |
||||
|
||||
/** |
||||
* Copy-constructor that copies the type and subtype of the given {@code MimeType}, |
||||
* and allows for different parameter. |
||||
* @param other the other media type |
||||
* @param parameters the parameters, may be {@code null} |
||||
* @throws IllegalArgumentException if any of the parameters contain illegal characters |
||||
*/ |
||||
public MimeType(MimeType other, Map<String, String> parameters) { |
||||
this(other.getType(), other.getSubtype(), parameters); |
||||
} |
||||
|
||||
/** |
||||
* Create a new {@code MimeType} for the given type, subtype, and parameters. |
||||
* @param type the primary type |
||||
* @param subtype the subtype |
||||
* @param parameters the parameters, may be {@code null} |
||||
* @throws IllegalArgumentException if any of the parameters contain illegal characters |
||||
*/ |
||||
public MimeType(String type, String subtype, Map<String, String> parameters) { |
||||
Assert.hasLength(type, "type must not be empty"); |
||||
Assert.hasLength(subtype, "subtype must not be empty"); |
||||
checkToken(type); |
||||
checkToken(subtype); |
||||
this.type = type.toLowerCase(Locale.ENGLISH); |
||||
this.subtype = subtype.toLowerCase(Locale.ENGLISH); |
||||
if (!CollectionUtils.isEmpty(parameters)) { |
||||
Map<String, String> m = new LinkedCaseInsensitiveMap<String>(parameters.size(), Locale.ENGLISH); |
||||
for (Map.Entry<String, String> entry : parameters.entrySet()) { |
||||
String attribute = entry.getKey(); |
||||
String value = entry.getValue(); |
||||
checkParameters(attribute, value); |
||||
m.put(attribute, value); |
||||
} |
||||
this.parameters = Collections.unmodifiableMap(m); |
||||
} |
||||
else { |
||||
this.parameters = Collections.emptyMap(); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* Checks the given token string for illegal characters, as defined in RFC 2616, section 2.2. |
||||
* @throws IllegalArgumentException in case of illegal characters |
||||
* @see <a href="http://tools.ietf.org/html/rfc2616#section-2.2">HTTP 1.1, section 2.2</a> |
||||
*/ |
||||
private void checkToken(String token) { |
||||
for (int i=0; i < token.length(); i++ ) { |
||||
char ch = token.charAt(i); |
||||
if (!TOKEN.get(ch)) { |
||||
throw new IllegalArgumentException("Invalid token character '" + ch + "' in token \"" + token + "\""); |
||||
} |
||||
} |
||||
} |
||||
|
||||
protected void checkParameters(String attribute, String value) { |
||||
Assert.hasLength(attribute, "parameter attribute must not be empty"); |
||||
Assert.hasLength(value, "parameter value must not be empty"); |
||||
checkToken(attribute); |
||||
if (PARAM_CHARSET.equals(attribute)) { |
||||
value = unquote(value); |
||||
Charset.forName(value); |
||||
} |
||||
else if (!isQuotedString(value)) { |
||||
checkToken(value); |
||||
} |
||||
} |
||||
|
||||
private boolean isQuotedString(String s) { |
||||
if (s.length() < 2) { |
||||
return false; |
||||
} |
||||
else { |
||||
return ((s.startsWith("\"") && s.endsWith("\"")) || (s.startsWith("'") && s.endsWith("'"))); |
||||
} |
||||
} |
||||
|
||||
protected String unquote(String s) { |
||||
if (s == null) { |
||||
return null; |
||||
} |
||||
return isQuotedString(s) ? s.substring(1, s.length() - 1) : s; |
||||
} |
||||
|
||||
/** |
||||
* Indicates whether the {@linkplain #getType() type} is the wildcard character {@code *} or not. |
||||
*/ |
||||
public boolean isWildcardType() { |
||||
return WILDCARD_TYPE.equals(getType()); |
||||
} |
||||
|
||||
/** |
||||
* Indicates whether the {@linkplain #getSubtype() subtype} is the wildcard character {@code *} |
||||
* or the wildcard character followed by a sufiix (e.g. {@code *+xml}), or not. |
||||
* @return whether the subtype is {@code *} |
||||
*/ |
||||
public boolean isWildcardSubtype() { |
||||
return WILDCARD_TYPE.equals(getSubtype()) || getSubtype().startsWith("*+"); |
||||
} |
||||
|
||||
/** |
||||
* Indicates whether this media type is concrete, i.e. whether neither the type or subtype is a wildcard |
||||
* character {@code *}. |
||||
* @return whether this media type is concrete |
||||
*/ |
||||
public boolean isConcrete() { |
||||
return !isWildcardType() && !isWildcardSubtype(); |
||||
} |
||||
|
||||
/** |
||||
* Return the primary type. |
||||
*/ |
||||
public String getType() { |
||||
return this.type; |
||||
} |
||||
|
||||
/** |
||||
* Return the subtype. |
||||
*/ |
||||
public String getSubtype() { |
||||
return this.subtype; |
||||
} |
||||
|
||||
/** |
||||
* Return the character set, as indicated by a {@code charset} parameter, if any. |
||||
* @return the character set; or {@code null} if not available |
||||
*/ |
||||
public Charset getCharSet() { |
||||
String charSet = getParameter(PARAM_CHARSET); |
||||
return (charSet != null ? Charset.forName(unquote(charSet)) : null); |
||||
} |
||||
|
||||
/** |
||||
* Return a generic parameter value, given a parameter name. |
||||
* @param name the parameter name |
||||
* @return the parameter value; or {@code null} if not present |
||||
*/ |
||||
public String getParameter(String name) { |
||||
return this.parameters.get(name); |
||||
} |
||||
|
||||
/** |
||||
* Return all generic parameter values. |
||||
* @return a read-only map, possibly empty, never {@code null} |
||||
*/ |
||||
public Map<String, String> getParameters() { |
||||
return parameters; |
||||
} |
||||
|
||||
/** |
||||
* Indicate whether this {@code MediaType} includes the given media type. |
||||
* <p>For instance, {@code text/*} includes {@code text/plain} and {@code text/html}, and {@code application/*+xml} |
||||
* includes {@code application/soap+xml}, etc. This method is <b>not</b> symmetric. |
||||
* @param other the reference media type with which to compare |
||||
* @return {@code true} if this media type includes the given media type; {@code false} otherwise |
||||
*/ |
||||
public boolean includes(MimeType other) { |
||||
if (other == null) { |
||||
return false; |
||||
} |
||||
if (this.isWildcardType()) { |
||||
// */* includes anything
|
||||
return true; |
||||
} |
||||
else if (getType().equals(other.getType())) { |
||||
if (getSubtype().equals(other.getSubtype())) { |
||||
return true; |
||||
} |
||||
if (this.isWildcardSubtype()) { |
||||
// wildcard with suffix, e.g. application/*+xml
|
||||
int thisPlusIdx = getSubtype().indexOf('+'); |
||||
if (thisPlusIdx == -1) { |
||||
return true; |
||||
} |
||||
else { |
||||
// application/*+xml includes application/soap+xml
|
||||
int otherPlusIdx = other.getSubtype().indexOf('+'); |
||||
if (otherPlusIdx != -1) { |
||||
String thisSubtypeNoSuffix = getSubtype().substring(0, thisPlusIdx); |
||||
String thisSubtypeSuffix = getSubtype().substring(thisPlusIdx + 1); |
||||
String otherSubtypeSuffix = other.getSubtype().substring(otherPlusIdx + 1); |
||||
if (thisSubtypeSuffix.equals(otherSubtypeSuffix) && WILDCARD_TYPE.equals(thisSubtypeNoSuffix)) { |
||||
return true; |
||||
} |
||||
} |
||||
} |
||||
} |
||||
} |
||||
return false; |
||||
} |
||||
|
||||
/** |
||||
* Indicate whether this {@code MediaType} is compatible with the given media type. |
||||
* <p>For instance, {@code text/*} is compatible with {@code text/plain}, {@code text/html}, and vice versa. |
||||
* In effect, this method is similar to {@link #includes(MediaType)}, except that it <b>is</b> symmetric. |
||||
* @param other the reference media type with which to compare |
||||
* @return {@code true} if this media type is compatible with the given media type; {@code false} otherwise |
||||
*/ |
||||
public boolean isCompatibleWith(MimeType other) { |
||||
if (other == null) { |
||||
return false; |
||||
} |
||||
if (isWildcardType() || other.isWildcardType()) { |
||||
return true; |
||||
} |
||||
else if (getType().equals(other.getType())) { |
||||
if (getSubtype().equals(other.getSubtype())) { |
||||
return true; |
||||
} |
||||
// wildcard with suffix? e.g. application/*+xml
|
||||
if (this.isWildcardSubtype() || other.isWildcardSubtype()) { |
||||
|
||||
int thisPlusIdx = getSubtype().indexOf('+'); |
||||
int otherPlusIdx = other.getSubtype().indexOf('+'); |
||||
|
||||
if (thisPlusIdx == -1 && otherPlusIdx == -1) { |
||||
return true; |
||||
} |
||||
else if (thisPlusIdx != -1 && otherPlusIdx != -1) { |
||||
String thisSubtypeNoSuffix = getSubtype().substring(0, thisPlusIdx); |
||||
String otherSubtypeNoSuffix = other.getSubtype().substring(0, otherPlusIdx); |
||||
|
||||
String thisSubtypeSuffix = getSubtype().substring(thisPlusIdx + 1); |
||||
String otherSubtypeSuffix = other.getSubtype().substring(otherPlusIdx + 1); |
||||
|
||||
if (thisSubtypeSuffix.equals(otherSubtypeSuffix) && |
||||
(WILDCARD_TYPE.equals(thisSubtypeNoSuffix) || WILDCARD_TYPE.equals(otherSubtypeNoSuffix))) { |
||||
return true; |
||||
} |
||||
} |
||||
} |
||||
} |
||||
return false; |
||||
} |
||||
|
||||
/** |
||||
* Compares this {@code MediaType} to another alphabetically. |
||||
* @param other media type to compare to |
||||
* @see #sortBySpecificity(List) |
||||
*/ |
||||
@Override |
||||
public int compareTo(MimeType other) { |
||||
int comp = getType().compareToIgnoreCase(other.getType()); |
||||
if (comp != 0) { |
||||
return comp; |
||||
} |
||||
comp = getSubtype().compareToIgnoreCase(other.getSubtype()); |
||||
if (comp != 0) { |
||||
return comp; |
||||
} |
||||
comp = getParameters().size() - other.getParameters().size(); |
||||
if (comp != 0) { |
||||
return comp; |
||||
} |
||||
TreeSet<String> thisAttributes = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER); |
||||
thisAttributes.addAll(getParameters().keySet()); |
||||
TreeSet<String> otherAttributes = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER); |
||||
otherAttributes.addAll(other.getParameters().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; |
||||
} |
||||
String thisValue = getParameters().get(thisAttribute); |
||||
String otherValue = other.getParameters().get(otherAttribute); |
||||
if (otherValue == null) { |
||||
otherValue = ""; |
||||
} |
||||
comp = thisValue.compareTo(otherValue); |
||||
if (comp != 0) { |
||||
return comp; |
||||
} |
||||
} |
||||
return 0; |
||||
} |
||||
|
||||
@Override |
||||
public boolean equals(Object other) { |
||||
if (this == other) { |
||||
return true; |
||||
} |
||||
if (!(other instanceof MimeType)) { |
||||
return false; |
||||
} |
||||
MimeType otherType = (MimeType) other; |
||||
return (this.type.equalsIgnoreCase(otherType.type) && this.subtype.equalsIgnoreCase(otherType.subtype) && |
||||
this.parameters.equals(otherType.parameters)); |
||||
} |
||||
|
||||
@Override |
||||
public int hashCode() { |
||||
int result = this.type.hashCode(); |
||||
result = 31 * result + this.subtype.hashCode(); |
||||
result = 31 * result + this.parameters.hashCode(); |
||||
return result; |
||||
} |
||||
|
||||
@Override |
||||
public String toString() { |
||||
StringBuilder builder = new StringBuilder(); |
||||
appendTo(builder); |
||||
return builder.toString(); |
||||
} |
||||
|
||||
protected void appendTo(StringBuilder builder) { |
||||
builder.append(this.type); |
||||
builder.append('/'); |
||||
builder.append(this.subtype); |
||||
appendTo(this.parameters, builder); |
||||
} |
||||
|
||||
private void appendTo(Map<String, String> map, StringBuilder builder) { |
||||
for (Map.Entry<String, String> entry : map.entrySet()) { |
||||
builder.append(';'); |
||||
builder.append(entry.getKey()); |
||||
builder.append('='); |
||||
builder.append(entry.getValue()); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* Parse the given String value into a {@code MimeType} object, |
||||
* with this method name following the 'valueOf' naming convention |
||||
* (as supported by {@link org.springframework.core.convert.ConversionService}. |
||||
* @see #parseMimeType(String) |
||||
*/ |
||||
public static MimeType valueOf(String value) { |
||||
return MimeTypeUtils.parseMimeType(value); |
||||
} |
||||
|
||||
|
||||
public static class SpecificityComparator<T extends MimeType> implements Comparator<T> { |
||||
|
||||
@Override |
||||
public int compare(T mimeType1, T mimeType2) { |
||||
if (mimeType1.isWildcardType() && !mimeType2.isWildcardType()) { // */* < audio/*
|
||||
return 1; |
||||
} |
||||
else if (mimeType2.isWildcardType() && !mimeType1.isWildcardType()) { // audio/* > */*
|
||||
return -1; |
||||
} |
||||
else if (!mimeType1.getType().equals(mimeType2.getType())) { // audio/basic == text/html
|
||||
return 0; |
||||
} |
||||
else { // mediaType1.getType().equals(mediaType2.getType())
|
||||
if (mimeType1.isWildcardSubtype() && !mimeType2.isWildcardSubtype()) { // audio/* < audio/basic
|
||||
return 1; |
||||
} |
||||
else if (mimeType2.isWildcardSubtype() && !mimeType1.isWildcardSubtype()) { // audio/basic > audio/*
|
||||
return -1; |
||||
} |
||||
else if (!mimeType1.getSubtype().equals(mimeType2.getSubtype())) { // audio/basic == audio/wave
|
||||
return 0; |
||||
} |
||||
else { // mediaType2.getSubtype().equals(mediaType2.getSubtype())
|
||||
return compareParameters(mimeType1, mimeType2); |
||||
} |
||||
} |
||||
} |
||||
|
||||
protected int compareParameters(T mimeType1, T mimeType2) { |
||||
int paramsSize1 = mimeType1.getParameters().size(); |
||||
int paramsSize2 = mimeType2.getParameters().size(); |
||||
return (paramsSize2 < paramsSize1 ? -1 : (paramsSize2 == paramsSize1 ? 0 : 1)); // audio/basic;level=1 < audio/basic
|
||||
} |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,332 @@
@@ -0,0 +1,332 @@
|
||||
/* |
||||
* Copyright 2002-2013 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. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.util; |
||||
|
||||
import java.nio.charset.UnsupportedCharsetException; |
||||
import java.util.ArrayList; |
||||
import java.util.Collection; |
||||
import java.util.Collections; |
||||
import java.util.Comparator; |
||||
import java.util.Iterator; |
||||
import java.util.LinkedHashMap; |
||||
import java.util.List; |
||||
import java.util.Map; |
||||
|
||||
import org.springframework.util.MimeType.SpecificityComparator; |
||||
|
||||
|
||||
|
||||
/** |
||||
* |
||||
* @author Arjen Poutsma |
||||
* @author Rossen Stoyanchev |
||||
* @since 4.0 |
||||
*/ |
||||
public class MimeTypeUtils { |
||||
|
||||
/** |
||||
* Public constant mime type that includes all media ranges (i.e. "*/*"). |
||||
*/ |
||||
public static final MimeType ALL; |
||||
|
||||
/** |
||||
* A String equivalent of {@link MediaType#ALL}. |
||||
*/ |
||||
public static final String ALL_VALUE = "*/*"; |
||||
|
||||
/** |
||||
* Public constant mime type for {@code application/atom+xml}. |
||||
*/ |
||||
public final static MimeType APPLICATION_ATOM_XML; |
||||
|
||||
/** |
||||
* A String equivalent of {@link MimeType#APPLICATION_ATOM_XML}. |
||||
*/ |
||||
public final static String APPLICATION_ATOM_XML_VALUE = "application/atom+xml"; |
||||
|
||||
/** |
||||
* Public constant mime type for {@code application/x-www-form-urlencoded}. |
||||
* */ |
||||
public final static MimeType APPLICATION_FORM_URLENCODED; |
||||
|
||||
/** |
||||
* A String equivalent of {@link MimeType#APPLICATION_FORM_URLENCODED}. |
||||
*/ |
||||
public final static String APPLICATION_FORM_URLENCODED_VALUE = "application/x-www-form-urlencoded"; |
||||
|
||||
/** |
||||
* Public constant mime type for {@code application/json}. |
||||
* */ |
||||
public final static MimeType APPLICATION_JSON; |
||||
|
||||
/** |
||||
* A String equivalent of {@link MimeType#APPLICATION_JSON}. |
||||
*/ |
||||
public final static String APPLICATION_JSON_VALUE = "application/json"; |
||||
|
||||
/** |
||||
* Public constant mime type for {@code application/octet-stream}. |
||||
* */ |
||||
public final static MimeType APPLICATION_OCTET_STREAM; |
||||
|
||||
/** |
||||
* A String equivalent of {@link MimeType#APPLICATION_OCTET_STREAM}. |
||||
*/ |
||||
public final static String APPLICATION_OCTET_STREAM_VALUE = "application/octet-stream"; |
||||
|
||||
/** |
||||
* Public constant mime type for {@code application/xhtml+xml}. |
||||
* */ |
||||
public final static MimeType APPLICATION_XHTML_XML; |
||||
|
||||
/** |
||||
* A String equivalent of {@link MimeType#APPLICATION_XHTML_XML}. |
||||
*/ |
||||
public final static String APPLICATION_XHTML_XML_VALUE = "application/xhtml+xml"; |
||||
|
||||
/** |
||||
* Public constant mime type for {@code application/xml}. |
||||
*/ |
||||
public final static MimeType APPLICATION_XML; |
||||
|
||||
/** |
||||
* A String equivalent of {@link MimeType#APPLICATION_XML}. |
||||
*/ |
||||
public final static String APPLICATION_XML_VALUE = "application/xml"; |
||||
|
||||
/** |
||||
* Public constant mime type for {@code image/gif}. |
||||
*/ |
||||
public final static MimeType IMAGE_GIF; |
||||
|
||||
/** |
||||
* A String equivalent of {@link MimeType#IMAGE_GIF}. |
||||
*/ |
||||
public final static String IMAGE_GIF_VALUE = "image/gif"; |
||||
|
||||
/** |
||||
* Public constant mime type for {@code image/jpeg}. |
||||
*/ |
||||
public final static MimeType IMAGE_JPEG; |
||||
|
||||
/** |
||||
* A String equivalent of {@link MimeType#IMAGE_JPEG}. |
||||
*/ |
||||
public final static String IMAGE_JPEG_VALUE = "image/jpeg"; |
||||
|
||||
/** |
||||
* Public constant mime type for {@code image/png}. |
||||
*/ |
||||
public final static MimeType IMAGE_PNG; |
||||
|
||||
/** |
||||
* A String equivalent of {@link MimeType#IMAGE_PNG}. |
||||
*/ |
||||
public final static String IMAGE_PNG_VALUE = "image/png"; |
||||
|
||||
/** |
||||
* Public constant mime type for {@code multipart/form-data}. |
||||
* */ |
||||
public final static MimeType MULTIPART_FORM_DATA; |
||||
|
||||
/** |
||||
* A String equivalent of {@link MimeType#MULTIPART_FORM_DATA}. |
||||
*/ |
||||
public final static String MULTIPART_FORM_DATA_VALUE = "multipart/form-data"; |
||||
|
||||
/** |
||||
* Public constant mime type for {@code text/html}. |
||||
* */ |
||||
public final static MimeType TEXT_HTML; |
||||
|
||||
/** |
||||
* A String equivalent of {@link MimeType#TEXT_HTML}. |
||||
*/ |
||||
public final static String TEXT_HTML_VALUE = "text/html"; |
||||
|
||||
/** |
||||
* Public constant mime type for {@code text/plain}. |
||||
* */ |
||||
public final static MimeType TEXT_PLAIN; |
||||
|
||||
/** |
||||
* A String equivalent of {@link MimeType#TEXT_PLAIN}. |
||||
*/ |
||||
public final static String TEXT_PLAIN_VALUE = "text/plain"; |
||||
|
||||
/** |
||||
* Public constant mime type for {@code text/xml}. |
||||
* */ |
||||
public final static MimeType TEXT_XML; |
||||
|
||||
/** |
||||
* A String equivalent of {@link MimeType#TEXT_XML}. |
||||
*/ |
||||
public final static String TEXT_XML_VALUE = "text/xml"; |
||||
|
||||
|
||||
static { |
||||
ALL = MimeType.valueOf(ALL_VALUE); |
||||
APPLICATION_ATOM_XML = MimeType.valueOf(APPLICATION_ATOM_XML_VALUE); |
||||
APPLICATION_FORM_URLENCODED = MimeType.valueOf(APPLICATION_FORM_URLENCODED_VALUE); |
||||
APPLICATION_JSON = MimeType.valueOf(APPLICATION_JSON_VALUE); |
||||
APPLICATION_OCTET_STREAM = MimeType.valueOf(APPLICATION_OCTET_STREAM_VALUE); |
||||
APPLICATION_XHTML_XML = MimeType.valueOf(APPLICATION_XHTML_XML_VALUE); |
||||
APPLICATION_XML = MimeType.valueOf(APPLICATION_XML_VALUE); |
||||
IMAGE_GIF = MimeType.valueOf(IMAGE_GIF_VALUE); |
||||
IMAGE_JPEG = MimeType.valueOf(IMAGE_JPEG_VALUE); |
||||
IMAGE_PNG = MimeType.valueOf(IMAGE_PNG_VALUE); |
||||
MULTIPART_FORM_DATA = MimeType.valueOf(MULTIPART_FORM_DATA_VALUE); |
||||
TEXT_HTML = MimeType.valueOf(TEXT_HTML_VALUE); |
||||
TEXT_PLAIN = MimeType.valueOf(TEXT_PLAIN_VALUE); |
||||
TEXT_XML = MimeType.valueOf(TEXT_XML_VALUE); |
||||
} |
||||
|
||||
|
||||
/** |
||||
* Parse the given String into a single {@code MimeType}. |
||||
* @param mimeType the string to parse |
||||
* @return the mime type |
||||
* @throws InvalidMimeTypeException if the string cannot be parsed |
||||
*/ |
||||
public static MimeType parseMimeType(String mimeType) { |
||||
if (!StringUtils.hasLength(mimeType)) { |
||||
throw new InvalidMimeTypeException(mimeType, "'mimeType' must not be empty"); |
||||
} |
||||
String[] parts = StringUtils.tokenizeToStringArray(mimeType, ";"); |
||||
|
||||
String fullType = parts[0].trim(); |
||||
// java.net.HttpURLConnection returns a *; q=.2 Accept header
|
||||
if (MimeType.WILDCARD_TYPE.equals(fullType)) { |
||||
fullType = "*/*"; |
||||
} |
||||
int subIndex = fullType.indexOf('/'); |
||||
if (subIndex == -1) { |
||||
throw new InvalidMimeTypeException(mimeType, "does not contain '/'"); |
||||
} |
||||
if (subIndex == fullType.length() - 1) { |
||||
throw new InvalidMimeTypeException(mimeType, "does not contain subtype after '/'"); |
||||
} |
||||
String type = fullType.substring(0, subIndex); |
||||
String subtype = fullType.substring(subIndex + 1, fullType.length()); |
||||
if (MimeType.WILDCARD_TYPE.equals(type) && !MimeType.WILDCARD_TYPE.equals(subtype)) { |
||||
throw new InvalidMimeTypeException(mimeType, "wildcard type is legal only in '*/*' (all mime types)"); |
||||
} |
||||
|
||||
Map<String, String> parameters = null; |
||||
if (parts.length > 1) { |
||||
parameters = new LinkedHashMap<String, String>(parts.length - 1); |
||||
for (int i = 1; i < parts.length; i++) { |
||||
String parameter = parts[i]; |
||||
int eqIndex = parameter.indexOf('='); |
||||
if (eqIndex != -1) { |
||||
String attribute = parameter.substring(0, eqIndex); |
||||
String value = parameter.substring(eqIndex + 1, parameter.length()); |
||||
parameters.put(attribute, value); |
||||
} |
||||
} |
||||
} |
||||
|
||||
try { |
||||
return new MimeType(type, subtype, parameters); |
||||
} |
||||
catch (UnsupportedCharsetException ex) { |
||||
throw new InvalidMimeTypeException(mimeType, "unsupported charset '" + ex.getCharsetName() + "'"); |
||||
} |
||||
catch (IllegalArgumentException ex) { |
||||
throw new InvalidMimeTypeException(mimeType, ex.getMessage()); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* Parse the given, comma-separated string into a list of {@code MimeType} objects. |
||||
* @param mimeTypes the string to parse |
||||
* @return the list of mime types |
||||
* @throws IllegalArgumentException if the string cannot be parsed |
||||
*/ |
||||
public static List<MimeType> parseMimeTypes(String mimeTypes) { |
||||
if (!StringUtils.hasLength(mimeTypes)) { |
||||
return Collections.emptyList(); |
||||
} |
||||
String[] tokens = mimeTypes.split(",\\s*"); |
||||
List<MimeType> result = new ArrayList<MimeType>(tokens.length); |
||||
for (String token : tokens) { |
||||
result.add(parseMimeType(token)); |
||||
} |
||||
return result; |
||||
} |
||||
|
||||
/** |
||||
* Return a string representation of the given list of {@code MimeType} objects. |
||||
* @param mimeTypes the string to parse |
||||
* @return the list of mime types |
||||
* @throws IllegalArgumentException if the String cannot be parsed |
||||
*/ |
||||
public static String toString(Collection<? extends MimeType> mimeTypes) { |
||||
StringBuilder builder = new StringBuilder(); |
||||
for (Iterator<? extends MimeType> iterator = mimeTypes.iterator(); iterator.hasNext();) { |
||||
MimeType mimeType = iterator.next(); |
||||
mimeType.appendTo(builder); |
||||
if (iterator.hasNext()) { |
||||
builder.append(", "); |
||||
} |
||||
} |
||||
return builder.toString(); |
||||
} |
||||
|
||||
|
||||
/** |
||||
* Sorts the given list of {@code MimeType} objects by specificity. |
||||
* <p> |
||||
* Given two mime types: |
||||
* <ol> |
||||
* <li>if either mime type has a {@linkplain #isWildcardType() wildcard type}, then |
||||
* the mime type without the wildcard is ordered before the other.</li> |
||||
* <li>if the two mime types have different {@linkplain #getType() types}, then |
||||
* they are considered equal and remain their current order.</li> |
||||
* <li>if either mime type has a {@linkplain #isWildcardSubtype() wildcard subtype} |
||||
* , then the mime type without the wildcard is sorted before the other.</li> |
||||
* <li>if the two mime types have different {@linkplain #getSubtype() subtypes}, |
||||
* then they are considered equal and remain their current order.</li> |
||||
* <li>if the two mime types have a different amount of |
||||
* {@linkplain #getParameter(String) parameters}, then the mime type with the most |
||||
* parameters is ordered before the other.</li> |
||||
* </ol> |
||||
* <p> |
||||
* For example: <blockquote>audio/basic < audio/* < */*</blockquote> |
||||
* <blockquote>audio/basic;level=1 < audio/basic</blockquote> |
||||
* <blockquote>audio/basic == text/html</blockquote> <blockquote>audio/basic == |
||||
* audio/wave</blockquote> |
||||
* |
||||
* @param mimeTypes the list of mime types to be sorted |
||||
* @see <a href="http://tools.ietf.org/html/rfc2616#section-14.1">HTTP 1.1, section |
||||
* 14.1</a> |
||||
*/ |
||||
public static void sortBySpecificity(List<MimeType> mimeTypes) { |
||||
Assert.notNull(mimeTypes, "'mimeTypes' must not be null"); |
||||
if (mimeTypes.size() > 1) { |
||||
Collections.sort(mimeTypes, SPECIFICITY_COMPARATOR); |
||||
} |
||||
} |
||||
|
||||
|
||||
/** |
||||
* Comparator used by {@link #sortBySpecificity(List)}. |
||||
*/ |
||||
public static final Comparator<MimeType> SPECIFICITY_COMPARATOR = new SpecificityComparator<MimeType>(); |
||||
|
||||
} |
||||
@ -0,0 +1,299 @@
@@ -0,0 +1,299 @@
|
||||
/* |
||||
* Copyright 2002-2013 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. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.util; |
||||
|
||||
import java.nio.charset.Charset; |
||||
import java.util.ArrayList; |
||||
import java.util.Collections; |
||||
import java.util.List; |
||||
import java.util.Random; |
||||
|
||||
import org.junit.Test; |
||||
import org.springframework.core.convert.ConversionService; |
||||
import org.springframework.core.convert.support.DefaultConversionService; |
||||
|
||||
import static org.junit.Assert.*; |
||||
|
||||
|
||||
/** |
||||
* @author Arjen Poutsma |
||||
* @author Juergen Hoeller |
||||
*/ |
||||
public class MimeTypeTests { |
||||
|
||||
|
||||
@Test(expected = IllegalArgumentException.class) |
||||
public void slashInSubtype() { |
||||
new MimeType("text", "/"); |
||||
} |
||||
|
||||
@Test(expected = InvalidMimeTypeException.class) |
||||
public void valueOfNoSubtype() { |
||||
MimeType.valueOf("audio"); |
||||
} |
||||
|
||||
@Test(expected = InvalidMimeTypeException.class) |
||||
public void valueOfNoSubtypeSlash() { |
||||
MimeType.valueOf("audio/"); |
||||
} |
||||
|
||||
@Test(expected = InvalidMimeTypeException.class) |
||||
public void valueOfIllegalType() { |
||||
MimeType.valueOf("audio(/basic"); |
||||
} |
||||
|
||||
@Test(expected = InvalidMimeTypeException.class) |
||||
public void valueOfIllegalSubtype() { |
||||
MimeType.valueOf("audio/basic)"); |
||||
} |
||||
|
||||
@Test(expected = InvalidMimeTypeException.class) |
||||
public void valueOfIllegalCharset() { |
||||
MimeType.valueOf("text/html; charset=foo-bar"); |
||||
} |
||||
|
||||
@Test |
||||
public void parseCharset() throws Exception { |
||||
String s = "text/html; charset=iso-8859-1"; |
||||
MimeType mimeType = MimeType.valueOf(s); |
||||
assertEquals("Invalid type", "text", mimeType.getType()); |
||||
assertEquals("Invalid subtype", "html", mimeType.getSubtype()); |
||||
assertEquals("Invalid charset", Charset.forName("ISO-8859-1"), mimeType.getCharSet()); |
||||
} |
||||
|
||||
@Test |
||||
public void parseQuotedCharset() { |
||||
String s = "application/xml;charset=\"utf-8\""; |
||||
MimeType mimeType = MimeType.valueOf(s); |
||||
assertEquals("Invalid type", "application", mimeType.getType()); |
||||
assertEquals("Invalid subtype", "xml", mimeType.getSubtype()); |
||||
assertEquals("Invalid charset", Charset.forName("UTF-8"), mimeType.getCharSet()); |
||||
} |
||||
|
||||
@Test |
||||
public void testWithConversionService() { |
||||
ConversionService conversionService = new DefaultConversionService(); |
||||
assertTrue(conversionService.canConvert(String.class, MimeType.class)); |
||||
MimeType mimeType = MimeType.valueOf("application/xml"); |
||||
assertEquals(mimeType, conversionService.convert("application/xml", MimeType.class)); |
||||
} |
||||
|
||||
@Test |
||||
public void includes() throws Exception { |
||||
MimeType textPlain = MimeTypeUtils.TEXT_PLAIN; |
||||
assertTrue("Equal types is not inclusive", textPlain.includes(textPlain)); |
||||
MimeType allText = new MimeType("text"); |
||||
|
||||
assertTrue("All subtypes is not inclusive", allText.includes(textPlain)); |
||||
assertFalse("All subtypes is inclusive", textPlain.includes(allText)); |
||||
|
||||
assertTrue("All types is not inclusive", MimeTypeUtils.ALL.includes(textPlain)); |
||||
assertFalse("All types is inclusive", textPlain.includes(MimeTypeUtils.ALL)); |
||||
|
||||
assertTrue("All types is not inclusive", MimeTypeUtils.ALL.includes(textPlain)); |
||||
assertFalse("All types is inclusive", textPlain.includes(MimeTypeUtils.ALL)); |
||||
|
||||
MimeType applicationSoapXml = new MimeType("application", "soap+xml"); |
||||
MimeType applicationWildcardXml = new MimeType("application", "*+xml"); |
||||
|
||||
assertTrue(applicationSoapXml.includes(applicationSoapXml)); |
||||
assertTrue(applicationWildcardXml.includes(applicationWildcardXml)); |
||||
|
||||
assertTrue(applicationWildcardXml.includes(applicationSoapXml)); |
||||
assertFalse(applicationSoapXml.includes(applicationWildcardXml)); |
||||
|
||||
assertFalse(applicationWildcardXml.includes(MimeTypeUtils.APPLICATION_JSON)); |
||||
} |
||||
|
||||
@Test |
||||
public void isCompatible() throws Exception { |
||||
MimeType textPlain = MimeTypeUtils.TEXT_PLAIN; |
||||
assertTrue("Equal types is not compatible", textPlain.isCompatibleWith(textPlain)); |
||||
MimeType allText = new MimeType("text"); |
||||
|
||||
assertTrue("All subtypes is not compatible", allText.isCompatibleWith(textPlain)); |
||||
assertTrue("All subtypes is not compatible", textPlain.isCompatibleWith(allText)); |
||||
|
||||
assertTrue("All types is not compatible", MimeTypeUtils.ALL.isCompatibleWith(textPlain)); |
||||
assertTrue("All types is not compatible", textPlain.isCompatibleWith(MimeTypeUtils.ALL)); |
||||
|
||||
assertTrue("All types is not compatible", MimeTypeUtils.ALL.isCompatibleWith(textPlain)); |
||||
assertTrue("All types is compatible", textPlain.isCompatibleWith(MimeTypeUtils.ALL)); |
||||
|
||||
MimeType applicationSoapXml = new MimeType("application", "soap+xml"); |
||||
MimeType applicationWildcardXml = new MimeType("application", "*+xml"); |
||||
|
||||
assertTrue(applicationSoapXml.isCompatibleWith(applicationSoapXml)); |
||||
assertTrue(applicationWildcardXml.isCompatibleWith(applicationWildcardXml)); |
||||
|
||||
assertTrue(applicationWildcardXml.isCompatibleWith(applicationSoapXml)); |
||||
assertTrue(applicationSoapXml.isCompatibleWith(applicationWildcardXml)); |
||||
|
||||
assertFalse(applicationWildcardXml.isCompatibleWith(MimeTypeUtils.APPLICATION_JSON)); |
||||
} |
||||
|
||||
@Test |
||||
public void testToString() throws Exception { |
||||
MimeType mimeType = new MimeType("text", "plain"); |
||||
String result = mimeType.toString(); |
||||
assertEquals("Invalid toString() returned", "text/plain", result); |
||||
} |
||||
|
||||
@Test |
||||
public void parseMimeType() throws Exception { |
||||
String s = "audio/*"; |
||||
MimeType mimeType = MimeTypeUtils.parseMimeType(s); |
||||
assertEquals("Invalid type", "audio", mimeType.getType()); |
||||
assertEquals("Invalid subtype", "*", mimeType.getSubtype()); |
||||
} |
||||
|
||||
@Test(expected = InvalidMimeTypeException.class) |
||||
public void parseMimeTypeNoSubtype() { |
||||
MimeTypeUtils.parseMimeType("audio"); |
||||
} |
||||
|
||||
@Test(expected = InvalidMimeTypeException.class) |
||||
public void parseMimeTypeNoSubtypeSlash() { |
||||
MimeTypeUtils.parseMimeType("audio/"); |
||||
} |
||||
|
||||
@Test(expected = InvalidMimeTypeException.class) |
||||
public void parseMimeTypeTypeRange() { |
||||
MimeTypeUtils.parseMimeType("*/json"); |
||||
} |
||||
|
||||
@Test(expected = InvalidMimeTypeException.class) |
||||
public void parseMimeTypeIllegalType() { |
||||
MimeTypeUtils.parseMimeType("audio(/basic"); |
||||
} |
||||
|
||||
@Test(expected = InvalidMimeTypeException.class) |
||||
public void parseMimeTypeIllegalSubtype() { |
||||
MimeTypeUtils.parseMimeType("audio/basic)"); |
||||
} |
||||
|
||||
@Test(expected = InvalidMimeTypeException.class) |
||||
public void parseMimeTypeEmptyParameterAttribute() { |
||||
MimeTypeUtils.parseMimeType("audio/*;=value"); |
||||
} |
||||
|
||||
@Test(expected = InvalidMimeTypeException.class) |
||||
public void parseMimeTypeEmptyParameterValue() { |
||||
MimeTypeUtils.parseMimeType("audio/*;attr="); |
||||
} |
||||
|
||||
@Test(expected = InvalidMimeTypeException.class) |
||||
public void parseMimeTypeIllegalParameterAttribute() { |
||||
MimeTypeUtils.parseMimeType("audio/*;attr<=value"); |
||||
} |
||||
|
||||
@Test(expected = InvalidMimeTypeException.class) |
||||
public void parseMimeTypeIllegalParameterValue() { |
||||
MimeTypeUtils.parseMimeType("audio/*;attr=v>alue"); |
||||
} |
||||
|
||||
@Test(expected = InvalidMimeTypeException.class) |
||||
public void parseMimeTypeIllegalCharset() { |
||||
MimeTypeUtils.parseMimeType("text/html; charset=foo-bar"); |
||||
} |
||||
|
||||
// SPR-8917
|
||||
|
||||
@Test |
||||
public void parseMimeTypeQuotedParameterValue() { |
||||
MimeType mimeType = MimeTypeUtils.parseMimeType("audio/*;attr=\"v>alue\""); |
||||
assertEquals("\"v>alue\"", mimeType.getParameter("attr")); |
||||
} |
||||
|
||||
// SPR-8917
|
||||
|
||||
@Test |
||||
public void parseMimeTypeSingleQuotedParameterValue() { |
||||
MimeType mimeType = MimeTypeUtils.parseMimeType("audio/*;attr='v>alue'"); |
||||
assertEquals("'v>alue'", mimeType.getParameter("attr")); |
||||
} |
||||
|
||||
@Test(expected = InvalidMimeTypeException.class) |
||||
public void parseMimeTypeIllegalQuotedParameterValue() { |
||||
MimeTypeUtils.parseMimeType("audio/*;attr=\""); |
||||
} |
||||
|
||||
@Test |
||||
public void parseMimeTypes() throws Exception { |
||||
String s = "text/plain, text/html, text/x-dvi, text/x-c"; |
||||
List<MimeType> mimeTypes = MimeTypeUtils.parseMimeTypes(s); |
||||
assertNotNull("No mime types returned", mimeTypes); |
||||
assertEquals("Invalid amount of mime types", 4, mimeTypes.size()); |
||||
|
||||
mimeTypes = MimeTypeUtils.parseMimeTypes(null); |
||||
assertNotNull("No mime types returned", mimeTypes); |
||||
assertEquals("Invalid amount of mime types", 0, mimeTypes.size()); |
||||
} |
||||
|
||||
@Test |
||||
public void compareTo() { |
||||
MimeType audioBasic = new MimeType("audio", "basic"); |
||||
MimeType audio = new MimeType("audio"); |
||||
MimeType audioWave = new MimeType("audio", "wave"); |
||||
MimeType audioBasicLevel = new MimeType("audio", "basic", Collections.singletonMap("level", "1")); |
||||
|
||||
// equal
|
||||
assertEquals("Invalid comparison result", 0, audioBasic.compareTo(audioBasic)); |
||||
assertEquals("Invalid comparison result", 0, audio.compareTo(audio)); |
||||
assertEquals("Invalid comparison result", 0, audioBasicLevel.compareTo(audioBasicLevel)); |
||||
|
||||
assertTrue("Invalid comparison result", audioBasicLevel.compareTo(audio) > 0); |
||||
|
||||
List<MimeType> expected = new ArrayList<MimeType>(); |
||||
expected.add(audio); |
||||
expected.add(audioBasic); |
||||
expected.add(audioBasicLevel); |
||||
expected.add(audioWave); |
||||
|
||||
List<MimeType> result = new ArrayList<MimeType>(expected); |
||||
Random rnd = new Random(); |
||||
// shuffle & sort 10 times
|
||||
for (int i = 0; i < 10; i++) { |
||||
Collections.shuffle(result, rnd); |
||||
Collections.sort(result); |
||||
|
||||
for (int j = 0; j < result.size(); j++) { |
||||
assertSame("Invalid media type at " + j + ", run " + i, expected.get(j), result.get(j)); |
||||
} |
||||
} |
||||
} |
||||
|
||||
@Test |
||||
public void compareToCaseSensitivity() { |
||||
MimeType m1 = new MimeType("audio", "basic"); |
||||
MimeType m2 = new MimeType("Audio", "Basic"); |
||||
assertEquals("Invalid comparison result", 0, m1.compareTo(m2)); |
||||
assertEquals("Invalid comparison result", 0, m2.compareTo(m1)); |
||||
|
||||
m1 = new MimeType("audio", "basic", Collections.singletonMap("foo", "bar")); |
||||
m2 = new MimeType("audio", "basic", Collections.singletonMap("Foo", "bar")); |
||||
assertEquals("Invalid comparison result", 0, m1.compareTo(m2)); |
||||
assertEquals("Invalid comparison result", 0, m2.compareTo(m1)); |
||||
|
||||
m1 = new MimeType("audio", "basic", Collections.singletonMap("foo", "bar")); |
||||
m2 = new MimeType("audio", "basic", Collections.singletonMap("foo", "Bar")); |
||||
assertTrue("Invalid comparison result", m1.compareTo(m2) != 0); |
||||
assertTrue("Invalid comparison result", m2.compareTo(m1) != 0); |
||||
} |
||||
|
||||
|
||||
} |
||||
Loading…
Reference in new issue