TikaConfig { public Parser getParser(MediaType mimeType) { return parser.getParsers().get(mimeType); } TikaConfig(String file);  TikaConfig(Path path);  TikaConfig(Path path, ServiceLoader loader);  TikaConfig(File file);  TikaConfig(File file, ServiceLoader loader);  TikaConfig(URL url);  TikaConfig(URL url, ClassLoader loader);  TikaConfig(URL url, ServiceLoader loader);  TikaConfig(InputStream stream);  TikaConfig(Document document);  TikaConfig(Document document, ServiceLoader loader);  TikaConfig(Element element);  TikaConfig(Element element, ClassLoader loader); private  TikaConfig(Element element, ServiceLoader loader);  TikaConfig(ClassLoader loader);  TikaConfig(); Parser getParser(MediaType mimeType); Parser getParser(); Detector getDetector(); EncodingDetector getEncodingDetector(); Translator getTranslator(); ExecutorService getExecutorService(); MimeTypes getMimeRepository(); MediaTypeRegistry getMediaTypeRegistry(); ServiceLoader getServiceLoader(); MetadataFilter getMetadataFilter(); static TikaConfig getDefaultConfig();  }
@Test public void testUnknownParser() throws Exception { ServiceLoader ignoreLoader = new ServiceLoader( getClass().getClassLoader(), LoadErrorHandler.IGNORE); ServiceLoader warnLoader = new ServiceLoader( getClass().getClassLoader(), LoadErrorHandler.WARN); ServiceLoader throwLoader = new ServiceLoader( getClass().getClassLoader(), LoadErrorHandler.THROW); Path configPath = Paths.get(new URI(getConfigPath("TIKA-1700-unknown-parser.xml"))); TikaConfig ignore = new TikaConfig(configPath, ignoreLoader); assertNotNull(ignore); assertNotNull(ignore.getParser()); assertEquals(1, ((CompositeParser)ignore.getParser()).getAllComponentParsers().size()); TikaConfig warn = new TikaConfig(configPath, warnLoader); assertNotNull(warn); assertNotNull(warn.getParser()); assertEquals(1, ((CompositeParser)warn.getParser()).getAllComponentParsers().size()); try { new TikaConfig(configPath, throwLoader); fail("Shouldn't get here, invalid parser class"); } catch (TikaException expected) {} }
@Test public void defaultParserWithExcludes() throws Exception { try { TikaConfig config = getConfig("TIKA-1445-default-except.xml"); CompositeParser cp = (CompositeParser)config.getParser(); List<Parser> parsers = cp.getAllComponentParsers(); Parser p; assertEquals(3, parsers.size()); p = parsers.get(0); assertTrue(p.toString(), p instanceof ParserDecorator); assertEquals(DefaultParser.class, ((ParserDecorator)p).getWrappedParser().getClass()); p = parsers.get(1); assertTrue(p.toString(), p instanceof ParserDecorator); assertEquals(EmptyParser.class, ((ParserDecorator)p).getWrappedParser().getClass()); assertEquals("hello/world", p.getSupportedTypes(null).iterator().next().toString()); p = parsers.get(2); assertTrue(p.toString(), p instanceof ParserDecorator); assertEquals(ErrorParser.class, ((ParserDecorator)p).getWrappedParser().getClass()); assertEquals("fail/world", p.getSupportedTypes(null).iterator().next().toString()); } catch (TikaException e) { fail("Unexpected TikaException: " + e); } }
@Test public void parserWithChildParsers() throws Exception { try { TikaConfig config = getConfig("TIKA-1653-norepeat.xml"); CompositeParser cp = (CompositeParser)config.getParser(); List<Parser> parsers = cp.getAllComponentParsers(); Parser p; assertEquals(2, parsers.size()); p = parsers.get(0); assertTrue(p.toString(), p instanceof CompositeParser); assertEquals(2, ((CompositeParser)p).getAllComponentParsers().size()); p = parsers.get(1); assertTrue(p.toString(), p instanceof ParserDecorator); assertEquals(EmptyParser.class, ((ParserDecorator)p).getWrappedParser().getClass()); assertEquals("hello/world", p.getSupportedTypes(null).iterator().next().toString()); } catch (TikaException e) { fail("Unexpected TikaException: " + e); } }
@Test public void testDynamicServiceLoaderFromConfig() throws Exception { URL url = TikaConfigTest.class.getResource("TIKA-1700-dynamic.xml"); TikaConfig config = new TikaConfig(url); DummyParser parser = (DummyParser)config.getParser(); ServiceLoader loader = parser.getLoader(); boolean dynamicValue = loader.isDynamic(); assertTrue("Dynamic Service Loading Should be true", dynamicValue); }
@Test public void testMultipleWithFallback() throws Exception { TikaConfig config = getConfig("TIKA-1509-multiple-fallback.xml"); CompositeParser parser = (CompositeParser)config.getParser(); assertEquals(2, parser.getAllComponentParsers().size()); Parser p; p = parser.getAllComponentParsers().get(0); assertTrue(p.toString(), p instanceof ParserDecorator); assertEquals(DefaultParser.class, ((ParserDecorator)p).getWrappedParser().getClass()); p = parser.getAllComponentParsers().get(1); assertTrue(p.toString(), p instanceof ParserDecorator); assertEquals(FallbackParser.class, ((ParserDecorator)p).getWrappedParser().getClass()); FallbackParser fbp = (FallbackParser)((ParserDecorator)p).getWrappedParser(); assertEquals("DISCARD_ALL", fbp.getMetadataPolicy().toString()); }