TikaIndexer { public void indexDir(String path) { File [] files = new File(path).listFiles(); AutoDetectParser parser = new AutoDetectParser(); logger.debug("Found {} files in dir", files.length); List<Talk> talks = new ArrayList<Talk>(); for (File file: files) { Metadata metadata = new Metadata(); metadata.add(Metadata.RESOURCE_NAME_KEY, file.getName()); BodyContentHandler contentHandler = new BodyContentHandler(); FileInputStream in = null; try { in = new FileInputStream(file); parser.parse(in, contentHandler, metadata); logMetadata(metadata); Optional<String> title = Optional.fromNullable(metadata.get(Metadata.TITLE)); Optional<String> speaker = Optional.fromNullable(metadata.get(Metadata.AUTHOR)); String content = contentHandler.toString(); Date date = new Date(); Talk talk = new Talk(file.getAbsolutePath(), title.or(file.getName()), Arrays.asList(speaker.or("")), date, contentHandler.toString(), Collections.<String>emptyList(), ""); talks.add(talk); } catch (FileNotFoundException e) { logger.warn(e.getMessage(), e); } catch (SAXException e) { logger.warn(e.getMessage(), e); } catch (TikaException e) { logger.warn(e.getMessage(), e); } catch (IOException e) { logger.warn(e.getMessage(), e); } finally { Utils.close(in); } } logger.debug("Extracted {} talks", talks.size()); indexer.index(talks.toArray(new Talk[talks.size()])); } TikaIndexer(Indexer indexer); void indexDir(String path); static void main(String [] args);  }
@Test public void pdfIsTransformed() throws IOException { Indexer indexer = mock(Indexer.class); TikaIndexer tikaIndexer = new TikaIndexer(indexer); tikaIndexer.indexDir(dataDir); ArgumentCaptor<Talk> argument = ArgumentCaptor.forClass(Talk.class); verify(indexer).index(argument.capture()); assertEquals("enter-the-gradle.pdf", argument.getValue().title); assertFalse(argument.getValue().content.isEmpty()); }
@Test public void gradleIsGroovy() throws ParseException { Directory dir = new RAMDirectory(); Indexer indexer = new Indexer(dir); TikaIndexer tikaIndexer = new TikaIndexer(indexer); tikaIndexer.indexDir(dataDir); Searcher searcher = new Searcher(dir); List<Result> results = searcher.search("Groovy"); assertEquals(1, results.size()); }