IOReader { static String mimeTypeFromExtension(String path) { if (path == null || path.length() == 0) { return null; } final int indexOfDot = path.lastIndexOf('.'); if (indexOfDot == -1) { return null; } final String ext = path.substring(indexOfDot + 1) .toLowerCase(Locale.getDefault()); switch (ext) { case "json": return "application/json"; case "xml": return "text/xml"; case "txt": return "text/plain"; case "html": return "text/html"; case "js": return "text/javascript"; case "htm": return "text/html"; case "css": return "text/css"; case "png": return "image/png"; case "jpg": return "image/jpeg"; case "gif": return "image/gif"; case "pdf": return "application/pdf"; default: return null; } } private  IOReader();   }
@Test public void canCorrectlyIdentifyMimeTypeFromPath() { assertThat(IOReader.mimeTypeFromExtension("file.json"), is("application/json")); assertThat(IOReader.mimeTypeFromExtension("file.xml"), is("text/xml")); assertThat(IOReader.mimeTypeFromExtension("file.html"), is("text/html")); assertThat(IOReader.mimeTypeFromExtension("file.htm"), is("text/html")); assertThat(IOReader.mimeTypeFromExtension("file.js"), is("text/javascript")); assertThat(IOReader.mimeTypeFromExtension("file.txt"), is("text/plain")); assertThat(IOReader.mimeTypeFromExtension("file.css"), is("text/css")); assertThat(IOReader.mimeTypeFromExtension("file.jpg"), is("image/jpeg")); }
@Test public void returnNullWhenCantFindMatch() { assertThat(IOReader.mimeTypeFromExtension("file.zip"), is(nullValue())); assertThat(IOReader.mimeTypeFromExtension("file.abc"), is(nullValue())); }