CachingTesseractOCRParser extends TesseractOCRParser { @Override public void parse(final InputStream in, final ContentHandler handler, final Metadata metadata, final ParseContext context) throws IOException, SAXException, TikaException { if (null != outputPath) { cachedParse(in, handler, metadata, context, context.get(TesseractOCRConfig.class, DEFAULT_CONFIG), false); } else { super.parse(in, handler, metadata, context); } } CachingTesseractOCRParser(final Path outputPath); @Override void parse(final InputStream in, final ContentHandler handler, final Metadata metadata,
	                  final ParseContext context); @Override void parseInline(final InputStream in, final XHTMLContentHandler xhtml, final ParseContext context,
	                        final TesseractOCRConfig config);  }
@Test public void testWriteToCache() throws Throwable { final Path simple = Paths.get(this.simple.toURI()); Writer writer = new StringWriter(); final AtomicInteger hit = new AtomicInteger(), miss = new AtomicInteger(); final Parser parser = new CachingTesseractOCRParser(tmpDir) { private static final long serialVersionUID = 6551690243986921730L; @Override public void cacheHit() { hit.incrementAndGet(); } @Override public void cacheMiss() { miss.incrementAndGet(); } }; try (final InputStream in = Files.newInputStream(simple)) { parser.parse(in, new WriteOutContentHandler(writer), new Metadata(), new ParseContext()); } Assert.assertEquals("HEAVY\nMETAL", writer.toString().trim()); Assert.assertEquals(0, hit.get()); Assert.assertEquals(1, miss.get()); writer = new StringWriter(); try (final InputStream in = Files.newInputStream(simple)) { parser.parse(in, new WriteOutContentHandler(writer), new Metadata(), new ParseContext()); } Assert.assertEquals("HEAVY\nMETAL", writer.toString().trim()); Assert.assertEquals(1, hit.get()); Assert.assertEquals(1, miss.get()); }