|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.tika.extractor; |
| 18 | + |
| 19 | +import static java.nio.charset.StandardCharsets.UTF_8; |
| 20 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 21 | + |
| 22 | +import org.junit.jupiter.api.Test; |
| 23 | + |
| 24 | +import org.apache.tika.io.TikaInputStream; |
| 25 | +import org.apache.tika.metadata.Metadata; |
| 26 | +import org.apache.tika.mime.MediaType; |
| 27 | +import org.apache.tika.mime.MimeTypes; |
| 28 | +import org.apache.tika.parser.ParseContext; |
| 29 | + |
| 30 | +/** |
| 31 | + * TIKA-4808 -- a media type carrying parameters must still resolve to the |
| 32 | + * extension of its base type. |
| 33 | + */ |
| 34 | +public class EmbeddedDocumentUtilExtensionTest { |
| 35 | + |
| 36 | + @Test |
| 37 | + public void testParametersDoNotSuppressExtension() { |
| 38 | + //the corpus regression: Pkcs7Parser refines the coarse family label to the |
| 39 | + //exact smime-type, which turned /embedded-2.p7s into /embedded-2 |
| 40 | + assertEquals(".p7m", |
| 41 | + EmbeddedDocumentUtil.getExtensionForMediaType( |
| 42 | + "application/pkcs7-mime; smime-type=signed-data")); |
| 43 | + assertEquals(".txt", |
| 44 | + EmbeddedDocumentUtil.getExtensionForMediaType("text/plain; charset=UTF-8")); |
| 45 | + assertEquals(".js", |
| 46 | + EmbeddedDocumentUtil.getExtensionForMediaType( |
| 47 | + "text/javascript; charset=UTF-8")); |
| 48 | + assertEquals(".css", |
| 49 | + EmbeddedDocumentUtil.getExtensionForMediaType( |
| 50 | + "text/css; charset=ISO-2022-JP")); |
| 51 | + assertEquals(".html", |
| 52 | + EmbeddedDocumentUtil.getExtensionForMediaType( |
| 53 | + "text/html; charset=windows-1252")); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + public void testUnparameterizedStillWorks() { |
| 58 | + assertEquals(".p7s", |
| 59 | + EmbeddedDocumentUtil.getExtensionForMediaType("application/pkcs7-signature")); |
| 60 | + assertEquals(".png", EmbeddedDocumentUtil.getExtensionForMediaType("image/png")); |
| 61 | + assertEquals(".txt", EmbeddedDocumentUtil.getExtensionForMediaType("text/plain")); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * A registered type that genuinely has parameters must win over its base type. |
| 66 | + */ |
| 67 | + @Test |
| 68 | + public void testRegisteredParameterizedTypeWinsOverBaseType() { |
| 69 | + assertEquals(".ditamap", |
| 70 | + EmbeddedDocumentUtil.getExtensionForMediaType( |
| 71 | + "application/dita+xml;format=map")); |
| 72 | + assertEquals(".dita", |
| 73 | + EmbeddedDocumentUtil.getExtensionForMediaType( |
| 74 | + "application/dita+xml;format=topic")); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + public void testUnknownAndNull() { |
| 79 | + assertEquals("", EmbeddedDocumentUtil.getExtensionForMediaType(null)); |
| 80 | + assertEquals("", |
| 81 | + EmbeddedDocumentUtil.getExtensionForMediaType("application/tika-bogus-xyz")); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + public void testOcrRoutingTypeIsNormalized() { |
| 86 | + assertEquals(".png", EmbeddedDocumentUtil.getExtensionForMediaType("image/ocr-png")); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * The lookup must not register anything: forName() would add one glob-less entry |
| 91 | + * per distinct parameter value seen, which on a large crawl grows without bound. |
| 92 | + */ |
| 93 | + @Test |
| 94 | + public void testLookupDoesNotPolluteRegistry() { |
| 95 | + MimeTypes mimeTypes = MimeTypes.getDefaultMimeTypes(); |
| 96 | + int before = mimeTypes.getMediaTypeRegistry().getTypes().size(); |
| 97 | + for (int i = 0; i < 50; i++) { |
| 98 | + EmbeddedDocumentUtil.getExtensionForMediaType("text/plain; charset=made-up-" + i); |
| 99 | + } |
| 100 | + assertEquals(before, mimeTypes.getMediaTypeRegistry().getTypes().size()); |
| 101 | + assertEquals(".txt", |
| 102 | + EmbeddedDocumentUtil.getExtensionForMediaType("text/plain; charset=made-up-0")); |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + public void testGeneratedResourceNameKeepsExtension() { |
| 107 | + assertEquals("embedded-2.p7m", EmbeddedDocumentUtil.generateResourceName( |
| 108 | + EmbeddedDocumentUtil.EmbeddedResourcePrefix.EMBEDDED, 2, |
| 109 | + "application/pkcs7-mime; smime-type=signed-data")); |
| 110 | + assertEquals("image-0.png", EmbeddedDocumentUtil.generateResourceName( |
| 111 | + EmbeddedDocumentUtil.EmbeddedResourcePrefix.IMAGE, 0, "image/png")); |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * A declared type that simply has no glob must not trigger detection -- that would |
| 116 | + * overwrite a CONTENT_TYPE the calling parser set deliberately. |
| 117 | + */ |
| 118 | + @Test |
| 119 | + public void testDeclaredUnregisteredTypeIsNotOverwritten() throws Exception { |
| 120 | + Metadata metadata = new Metadata(); |
| 121 | + metadata.set(Metadata.CONTENT_TYPE, "application/tika-bogus-xyz"); |
| 122 | + EmbeddedDocumentUtil util = new EmbeddedDocumentUtil(new ParseContext()); |
| 123 | + try (TikaInputStream tis = TikaInputStream.get("%PDF-1.4\n".getBytes(UTF_8))) { |
| 124 | + assertEquals("", util.getExtension(tis, metadata)); |
| 125 | + assertEquals("application/tika-bogus-xyz", metadata.get(Metadata.CONTENT_TYPE)); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + @Test |
| 130 | + public void testDeclaredParameterizedTypeResolvesAndIsPreserved() throws Exception { |
| 131 | + Metadata metadata = new Metadata(); |
| 132 | + metadata.set(Metadata.CONTENT_TYPE, "text/plain; charset=UTF-8"); |
| 133 | + EmbeddedDocumentUtil util = new EmbeddedDocumentUtil(new ParseContext()); |
| 134 | + try (TikaInputStream tis = TikaInputStream.get("hello".getBytes(UTF_8))) { |
| 135 | + assertEquals(".txt", util.getExtension(tis, metadata)); |
| 136 | + assertEquals("text/plain; charset=UTF-8", metadata.get(Metadata.CONTENT_TYPE)); |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * Guard the assumption the fix rests on: normalize() deliberately preserves |
| 142 | + * parameters, which is why forName() misses the registry for parameterized names. |
| 143 | + */ |
| 144 | + @Test |
| 145 | + public void testNormalizePreservesParameters() { |
| 146 | + MediaType withParams = MediaType.parse("text/plain; charset=UTF-8"); |
| 147 | + assertEquals(withParams, |
| 148 | + MimeTypes.getDefaultMimeTypes().getMediaTypeRegistry().normalize(withParams)); |
| 149 | + } |
| 150 | +} |
0 commit comments