TikaTextExtractor implements TextExtractor { @Override public ParsedContent extractContent(InputStream inputStream, ContentType contentType) throws Exception { if (contentType.mediaType().equals(TEXT)) { return jsoupTextExtractor.extractContent(inputStream, contentType); } return metricFactory.decorateSupplierWithTimerMetric("tikaTextExtraction", Throwing.supplier( () -> performContentExtraction(inputStream, contentType)) .sneakyThrow()); } @Inject  TikaTextExtractor(MetricFactory metricFactory, TikaHttpClient tikaHttpClient); @Override ParsedContent extractContent(InputStream inputStream, ContentType contentType); ParsedContent performContentExtraction(InputStream inputStream, ContentType contentType);  }
@Test void textualContentShouldReturnEmptyWhenInputStreamIsEmpty() throws Exception { assertThat(textExtractor.extractContent(IOUtils.toInputStream("", StandardCharsets.UTF_8), ContentType.of("text/plain")) .getTextualContent()) .contains(""); }
@Test void textTest() throws Exception { InputStream inputStream = ClassLoader.getSystemResourceAsStream("documents/Text.txt"); assertThat(inputStream).isNotNull(); assertThat(textExtractor.extractContent(inputStream, ContentType.of("text/plain")).getTextualContent()) .isPresent() .asString() .contains("This is some awesome text text."); }
@Test void textMicrosoftWorldTest() throws Exception { InputStream inputStream = ClassLoader.getSystemResourceAsStream("documents/writter.docx"); assertThat(inputStream).isNotNull(); assertThat(textExtractor.extractContent(inputStream, ContentType.of("application/vnd.openxmlformats-officedocument.wordprocessingml.document")) .getTextualContent()) .isPresent() .asString() .contains("This is an awesome document on libroffice writter !"); }
@Test void textOdtTest() throws Exception { InputStream inputStream = ClassLoader.getSystemResourceAsStream("documents/writter.odt"); assertThat(inputStream).isNotNull(); assertThat(textExtractor.extractContent(inputStream, ContentType.of("application/vnd.oasis.opendocument.text")) .getTextualContent()) .isPresent() .asString() .contains("This is an awesome document on libroffice writter !"); }
@Test void documentWithBadDeclaredMetadataShouldBeWellHandled() throws Exception { InputStream inputStream = ClassLoader.getSystemResourceAsStream("documents/fake.txt"); assertThat(inputStream).isNotNull(); assertThat(textExtractor.extractContent(inputStream, ContentType.of("application/vnd.oasis.opendocument.text")) .getTextualContent()) .isPresent() .asString() .contains("This is an awesome document on libroffice writter !"); }
@Test void slidePowerPointTest() throws Exception { InputStream inputStream = ClassLoader.getSystemResourceAsStream("documents/slides.pptx"); assertThat(inputStream).isNotNull(); assertThat(textExtractor.extractContent(inputStream, ContentType.of("application/vnd.openxmlformats-officedocument.presentationml.presentation")).getTextualContent()) .isPresent() .asString() .contains("James is awesome") .contains("It manages attachments so well !"); }
@Test void slideOdpTest() throws Exception { InputStream inputStream = ClassLoader.getSystemResourceAsStream("documents/slides.odp"); assertThat(inputStream).isNotNull(); assertThat(textExtractor.extractContent(inputStream, ContentType.of("application/vnd.oasis.opendocument.presentation")) .getTextualContent()) .isPresent() .asString() .contains("James is awesome") .contains("It manages attachments so well !"); }
@Test void pdfTest() throws Exception { InputStream inputStream = ClassLoader.getSystemResourceAsStream("documents/PDF.pdf"); assertThat(inputStream).isNotNull(); assertThat(textExtractor.extractContent(inputStream, ContentType.of("application/pdf")) .getTextualContent()) .isPresent() .asString() .contains("This is an awesome document on libroffice writter !"); }
@Test void odsTest() throws Exception { InputStream inputStream = ClassLoader.getSystemResourceAsStream("documents/calc.ods"); assertThat(inputStream).isNotNull(); assertThat(textExtractor.extractContent(inputStream, ContentType.of("application/vnd.oasis.opendocument.spreadsheet")) .getTextualContent()) .isPresent() .asString() .contains("This is an aesome LibreOffice document !"); }
@Test void excelTest() throws Exception { InputStream inputStream = ClassLoader.getSystemResourceAsStream("documents/calc.xlsx"); assertThat(inputStream).isNotNull(); assertThat(textExtractor.extractContent(inputStream, ContentType.of("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")) .getTextualContent()) .isPresent() .asString() .contains("Feuille1") .contains("This is an aesome LibreOffice document !"); }
@Test void deserializerShouldNotThrowWhenMoreThanOneNode() throws Exception { TikaTextExtractor textExtractor = new TikaTextExtractor( new RecordingMetricFactory(), (inputStream, contentType) -> Optional.of(new ByteArrayInputStream(("[{\"X-TIKA:content\": \"This is an awesome LibreOffice document !\"}, " + "{\"Chroma BlackIsZero\": \"true\"}]") .getBytes(StandardCharsets.UTF_8)))); InputStream inputStream = null; textExtractor.extractContent(inputStream, ContentType.of("text/plain")); }
@Test void deserializerShouldTakeFirstNodeWhenSeveral() throws Exception { String expectedExtractedContent = "content A"; TikaTextExtractor textExtractor = new TikaTextExtractor( new RecordingMetricFactory(), (inputStream, contentType) -> Optional.of(new ByteArrayInputStream(("[{\"X-TIKA:content\": \"" + expectedExtractedContent + "\"}, " + "{\"X-TIKA:content\": \"content B\"}]") .getBytes(StandardCharsets.UTF_8)))); InputStream inputStream = new ByteArrayInputStream("toto".getBytes(StandardCharsets.UTF_8)); ParsedContent parsedContent = textExtractor.extractContent(inputStream, ContentType.of("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")); assertThat(parsedContent.getTextualContent()).contains(expectedExtractedContent); }
@Test void deserializerShouldThrowWhenNodeIsNotAnObject() { TikaTextExtractor textExtractor = new TikaTextExtractor( new RecordingMetricFactory(), (inputStream, contentType) -> Optional.of(new ByteArrayInputStream("[\"value1\"]" .getBytes(StandardCharsets.UTF_8)))); InputStream inputStream = new ByteArrayInputStream("toto".getBytes(StandardCharsets.UTF_8)); assertThatThrownBy(() -> textExtractor.extractContent(inputStream, ContentType.of("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"))) .isInstanceOf(IllegalStateException.class) .hasMessage("The element should be a Json object"); }