FEAT impl MimeType + MimeType parsing + Tests
parent
8d7f80b390
commit
e70e422c44
|
@ -24,4 +24,6 @@ pipeline {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,54 @@
|
|||
package de.nclazz.commons.io;
|
||||
|
||||
import lombok.NonNull;
|
||||
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* Simple implementation of a MimeType.
|
||||
*/
|
||||
public class MimeType {
|
||||
|
||||
public static final String MEDIA_APPLICATION = "application";
|
||||
public static final String MEDIA_AUDIO = "audio";
|
||||
public static final String MEDIA_EXAMPLE = "example";
|
||||
public static final String MEDIA_IMAGE = "image";
|
||||
public static final String MEDIA_MESSAGE = "message";
|
||||
public static final String MEDIA_MODEL = "model";
|
||||
public static final String MEDIA_MULTIPART = "multipart";
|
||||
public static final String MEDIA_TEXT = "text";
|
||||
public static final String MEDIA_VIDEO = "video";
|
||||
|
||||
private static final Pattern MIME_REGEX = Pattern.compile("^(?<media>\\p{Alnum}+)/(?<sub>\\p{Alnum}+)$");
|
||||
|
||||
private final String mediaType;
|
||||
private final String subType;
|
||||
|
||||
public MimeType(String mediaType, String subType) {
|
||||
this.mediaType = mediaType;
|
||||
this.subType = subType;
|
||||
}
|
||||
|
||||
public String getMediaType() {
|
||||
return mediaType;
|
||||
}
|
||||
|
||||
public String getSubType() {
|
||||
return subType;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("%s/%s", this.mediaType, this.subType);
|
||||
}
|
||||
|
||||
public static MimeType parse(@NonNull String mime) {
|
||||
Matcher matcher = MIME_REGEX.matcher(mime);
|
||||
if(!matcher.matches()) {
|
||||
throw new IllegalArgumentException(String.format("Invalid MimeType '%s'!", mime));
|
||||
}
|
||||
return new MimeType(matcher.group("media"), matcher.group("sub"));
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,47 @@
|
|||
package de.nclazz.commons.io;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class MimeTypeTest {
|
||||
|
||||
@Test
|
||||
void MimeTypeParsedFromString() {
|
||||
|
||||
MimeType type = MimeType.parse("application/json");
|
||||
assertEquals("application", type.getMediaType());
|
||||
assertEquals("json", type.getSubType());
|
||||
|
||||
type = MimeType.parse("image/jpeg");
|
||||
assertEquals("image", type.getMediaType());
|
||||
assertEquals("jpeg", type.getSubType());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
void MimeTypeParsedThrowsExceptionOnInvalidInput() {
|
||||
assertThrows(IllegalArgumentException.class, () -> MimeType.parse("application/"));
|
||||
assertThrows(IllegalArgumentException.class, () -> MimeType.parse("/json"));
|
||||
assertThrows(IllegalArgumentException.class, () -> MimeType.parse("/"));
|
||||
assertThrows(IllegalArgumentException.class, () -> MimeType.parse("application"));
|
||||
assertThrows(NullPointerException.class, () -> MimeType.parse(null));
|
||||
}
|
||||
|
||||
@Test
|
||||
void MimeTypeToStringEqualsOriginalString() {
|
||||
|
||||
String mimeStr = "application/json";
|
||||
MimeType type = MimeType.parse(mimeStr);
|
||||
assertEquals("application", type.getMediaType());
|
||||
assertEquals("json", type.getSubType());
|
||||
assertEquals(mimeStr, type.toString());
|
||||
|
||||
mimeStr = "image/jpeg";
|
||||
type = MimeType.parse(mimeStr);
|
||||
assertEquals("image", type.getMediaType());
|
||||
assertEquals("jpeg", type.getSubType());
|
||||
assertEquals(mimeStr, type.toString());
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue