|
| 1 | +package com.baeldung.java.mimetype; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | + |
| 5 | +import java.io.File; |
| 6 | +import java.io.IOException; |
| 7 | +import java.net.FileNameMap; |
| 8 | +import java.net.MalformedURLException; |
| 9 | +import java.net.URLConnection; |
| 10 | +import java.nio.file.Files; |
| 11 | +import java.nio.file.Path; |
| 12 | + |
| 13 | +import javax.activation.MimetypesFileTypeMap; |
| 14 | + |
| 15 | +import org.apache.tika.Tika; |
| 16 | +import org.junit.Test; |
| 17 | + |
| 18 | +import net.sf.jmimemagic.Magic; |
| 19 | +import net.sf.jmimemagic.MagicException; |
| 20 | +import net.sf.jmimemagic.MagicMatch; |
| 21 | +import net.sf.jmimemagic.MagicMatchNotFoundException; |
| 22 | +import net.sf.jmimemagic.MagicParseException; |
| 23 | + |
| 24 | +/** |
| 25 | + * Test class demonstrating various strategies to resolve MIME type of a file. |
| 26 | + * @author tritty |
| 27 | + * |
| 28 | + */ |
| 29 | +public class MimeTypeUnitTest { |
| 30 | + /** |
| 31 | + * Expected Ouput. |
| 32 | + */ |
| 33 | + public static final String PNG_EXT = "image/png"; |
| 34 | + |
| 35 | + /** |
| 36 | + * The location of the file. |
| 37 | + */ |
| 38 | + public static final String FILE_LOC = "src/test/resources/product.png"; |
| 39 | + |
| 40 | + /** |
| 41 | + * Test method, demonstrating usage in Java 7. |
| 42 | + * |
| 43 | + * @throws IOException |
| 44 | + */ |
| 45 | + @Test |
| 46 | + public void whenUsingJava7_thenSuccess() throws IOException { |
| 47 | + final Path path = new File(FILE_LOC).toPath(); |
| 48 | + final String mimeType = Files.probeContentType(path); |
| 49 | + assertEquals(mimeType, PNG_EXT); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Test method demonstrating the usage of URLConnection to resolve MIME type. |
| 54 | + * |
| 55 | + * @throws MalformedURLException |
| 56 | + * @throws IOException |
| 57 | + */ |
| 58 | + @Test |
| 59 | + public void whenUsingGetContentType_thenSuccess() throws MalformedURLException, IOException { |
| 60 | + final File file = new File(FILE_LOC); |
| 61 | + final URLConnection connection = file.toURL() |
| 62 | + .openConnection(); |
| 63 | + final String mimeType = connection.getContentType(); |
| 64 | + assertEquals(mimeType, PNG_EXT); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Test method demonstrating the usage of URLConnection to resolve MIME type. |
| 69 | + * |
| 70 | + */ |
| 71 | + @Test |
| 72 | + public void whenUsingGuessContentTypeFromName_thenSuccess() { |
| 73 | + final File file = new File(FILE_LOC); |
| 74 | + final String mimeType = URLConnection.guessContentTypeFromName(file.getName()); |
| 75 | + assertEquals(mimeType, PNG_EXT); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Test method demonstrating the usage of FileNameMap from URLConnection |
| 80 | + * to resolve MIME type of a file. |
| 81 | + * |
| 82 | + */ |
| 83 | + @Test |
| 84 | + public void whenUsingGetFileNameMap_thenSuccess() { |
| 85 | + final File file = new File(FILE_LOC); |
| 86 | + final FileNameMap fileNameMap = URLConnection.getFileNameMap(); |
| 87 | + final String mimeType = fileNameMap.getContentTypeFor(file.getName()); |
| 88 | + assertEquals(mimeType, PNG_EXT); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Test method demonstrating the usage of MimeTypesFileTypeMap for resolution of |
| 93 | + * MIME type. |
| 94 | + * |
| 95 | + */ |
| 96 | + @Test |
| 97 | + public void whenUsingMimeTypesFileTypeMap_thenSuccess() { |
| 98 | + final File file = new File(FILE_LOC); |
| 99 | + final MimetypesFileTypeMap fileTypeMap = new MimetypesFileTypeMap(); |
| 100 | + final String mimeType = fileTypeMap.getContentType(file.getName()); |
| 101 | + assertEquals(mimeType, PNG_EXT); |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Test method demonstrating usage of jMimeMagic. |
| 106 | + * |
| 107 | + * @throws MagicParseException |
| 108 | + * @throws MagicMatchNotFoundException |
| 109 | + * @throws MagicException |
| 110 | + */ |
| 111 | + @Test |
| 112 | + public void whenUsingJmimeMagic_thenSuccess() throws MagicParseException, MagicMatchNotFoundException, MagicException { |
| 113 | + final File file = new File(FILE_LOC); |
| 114 | + final Magic magic = new Magic(); |
| 115 | + final MagicMatch match = magic.getMagicMatch(file, false); |
| 116 | + assertEquals(match.getMimeType(), PNG_EXT); |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Test method demonstrating usage of Apache Tika. |
| 121 | + * |
| 122 | + * @throws IOException |
| 123 | + */ |
| 124 | + @Test |
| 125 | + public void whenUsingTika_thenSuccess() throws IOException { |
| 126 | + final File file = new File(FILE_LOC); |
| 127 | + final Tika tika = new Tika(); |
| 128 | + final String mimeType = tika.detect(file); |
| 129 | + assertEquals(mimeType, PNG_EXT); |
| 130 | + } |
| 131 | +} |
0 commit comments