|
16 | 16 | */ |
17 | 17 | package org.apache.tika.extractor; |
18 | 18 |
|
| 19 | +import static java.nio.charset.StandardCharsets.UTF_8; |
19 | 20 | import static org.junit.jupiter.api.Assertions.assertEquals; |
20 | 21 |
|
21 | 22 | import org.junit.jupiter.api.Test; |
22 | 23 |
|
| 24 | +import org.apache.tika.io.TikaInputStream; |
| 25 | +import org.apache.tika.metadata.Metadata; |
23 | 26 | import org.apache.tika.mime.MediaType; |
24 | 27 | import org.apache.tika.mime.MimeTypes; |
| 28 | +import org.apache.tika.parser.ParseContext; |
25 | 29 |
|
26 | 30 | /** |
27 | 31 | * TIKA-4808 -- a media type carrying parameters must still resolve to the |
@@ -107,6 +111,32 @@ public void testGeneratedResourceNameKeepsExtension() { |
107 | 111 | EmbeddedDocumentUtil.EmbeddedResourcePrefix.IMAGE, 0, "image/png")); |
108 | 112 | } |
109 | 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 | + |
110 | 140 | /** |
111 | 141 | * Guard the assumption the fix rests on: normalize() deliberately preserves |
112 | 142 | * parameters, which is why forName() misses the registry for parameterized names. |
|
0 commit comments