|
1 | 1 | package com.baeldung.extension; |
2 | 2 |
|
| 3 | +import static org.assertj.core.api.Assertions.assertThat; |
3 | 4 | import static org.junit.Assert.assertArrayEquals; |
4 | 5 | import static org.junit.Assert.assertEquals; |
5 | 6 |
|
|
17 | 18 | import com.j256.simplemagic.ContentInfo; |
18 | 19 |
|
19 | 20 | public class ExtensionFromMimeTypeUnitTest { |
20 | | - private static final String IMG_MIME_TYPE = "image/jpeg"; |
| 21 | + private static final String IMAGE_JPEG_MIME_TYPE = "image/jpeg"; |
21 | 22 | @Test |
22 | 23 | public void whenUsingTika_thenGetFileExtension() throws MimeTypeException { |
23 | | - List<String> SUPPORTED_EXTENSIONS = Arrays.asList(".jpg", ".jpeg", ".jpe", ".jif", ".jfif", ".jfi"); |
| 24 | + List<String> expectedExtensions = Arrays.asList(".jpg", ".jpeg", ".jpe", ".jif", ".jfif", ".jfi"); |
24 | 25 | org.apache.tika.mime.MimeTypes allTypes = org.apache.tika.mime.MimeTypes.getDefaultMimeTypes(); |
25 | | - org.apache.tika.mime.MimeType type = allTypes.forName(IMG_MIME_TYPE); |
26 | | - String extension = type.getExtension(); |
27 | | - assertEquals(".jpg", extension); |
28 | | - List<String> supportedExtensions = type.getExtensions(); |
29 | | - assertEquals(SUPPORTED_EXTENSIONS, supportedExtensions); |
| 26 | + org.apache.tika.mime.MimeType type = allTypes.forName(IMAGE_JPEG_MIME_TYPE); |
| 27 | + String primaryExtension = type.getExtension(); |
| 28 | + assertEquals(".jpg", primaryExtension); |
| 29 | + List<String> detectedExtensions = type.getExtensions(); |
| 30 | + assertThat(detectedExtensions).containsExactlyElementsOf(expectedExtensions); |
30 | 31 | } |
31 | 32 |
|
32 | 33 | @Test |
33 | 34 | public void whenUsingJodd_thenGetFileExtension() { |
34 | | - String[] supportedExtensions = {"jpeg","jpg","jpe"}; |
35 | | - String[] extensionsByMimeTypes = jodd.net.MimeTypes.findExtensionsByMimeTypes(IMG_MIME_TYPE, false); |
36 | | - assertArrayEquals(supportedExtensions, extensionsByMimeTypes); |
| 35 | + String[] expectedExtensions = {"jpeg","jpg","jpe"}; |
| 36 | + String[] detectedExtensions = jodd.net.MimeTypes.findExtensionsByMimeTypes(IMAGE_JPEG_MIME_TYPE, false); |
| 37 | + assertThat(detectedExtensions).containsExactly(expectedExtensions); |
37 | 38 | } |
38 | 39 |
|
39 | 40 | @Test |
40 | 41 | public void whenUsingMimetypesFileTypeMap_thenGetFileExtension() { |
41 | | - String[] supportedExtensions = {"jpeg","jpg","jpe"}; |
42 | | - ContentInfo contentInfo = new ContentInfo("", IMG_MIME_TYPE, "", true); |
43 | | - String[] fileExtensions = contentInfo.getFileExtensions(); |
44 | | - assertArrayEquals(supportedExtensions, fileExtensions); |
| 42 | + String[] expectedExtensions = {"jpeg","jpg","jpe"}; |
| 43 | + ContentInfo contentInfo = new ContentInfo("", IMAGE_JPEG_MIME_TYPE, "", true); |
| 44 | + String[] detectedExtensions = contentInfo.getFileExtensions(); |
| 45 | + assertThat(detectedExtensions).containsExactly(expectedExtensions); |
45 | 46 | } |
46 | 47 |
|
47 | 48 | @Test |
48 | 49 | public void whenUsingCustomLogic_thenGetFileExtension() { |
49 | 50 | Map<String, Set<String>> mimeExtensionsMap = new HashMap<>(); |
50 | | - Set<String> supportedExtensions = new HashSet<>(Arrays.asList(".jpeg",".jpg",".jpe")); |
| 51 | + Set<String> expectedExtensions = new HashSet<>(Arrays.asList(".jpeg",".jpg",".jpe")); |
51 | 52 | addMimeExtensions(mimeExtensionsMap, "image/jpeg", ".jpeg"); |
52 | 53 | addMimeExtensions(mimeExtensionsMap, "image/jpeg", ".jpg"); |
53 | 54 | addMimeExtensions(mimeExtensionsMap, "image/jpeg", ".jpe"); |
54 | 55 |
|
55 | 56 | String mimeTypeToLookup = "image/jpeg"; |
56 | | - Set<String> extensions = mimeExtensionsMap.get(mimeTypeToLookup); |
57 | | - assertEquals(supportedExtensions, extensions); |
| 57 | + Set<String> detectedExtensions = mimeExtensionsMap.get(mimeTypeToLookup); |
| 58 | + assertThat(detectedExtensions).containsExactlyElementsOf(expectedExtensions); |
58 | 59 | } |
59 | 60 |
|
60 | 61 | private void addMimeExtensions(Map<String, Set<String>> map, String mimeType, String extension) { |
|
0 commit comments