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