FolderReader extends BaleenCollectionReader { @Override public boolean doHasNext() throws IOException, CollectionException { WatchKey key; while ((key = watcher.poll()) != null) { for (WatchEvent<?> event : key.pollEvents()) { processEvent(key, event); getMonitor().meter("events").mark(); } key.reset(); } return !queue.isEmpty(); }  @Override void doInitialize(UimaContext context); @Override void doGetNext(JCas jCas); @Override void doClose(); @Override boolean doHasNext(); static final String PARAM_FOLDERS; static final String PARAM_REPROCESS_ON_MODIFY; static final String PARAM_RECURSIVE; static final String PARAM_ACCEPTED_PATTERNS; }
@Test public void testCreateFileDefaultDirectory() throws Exception { BaleenCollectionReader bcr = getCollectionReader(); assertTrue( bcr.doHasNext()); bcr.getNext(jCas.getCas()); assertTrue(getSource(jCas).contains(System.getProperty("user.dir"))); bcr.close(); }
@Test public void testCreateFile() throws Exception { BaleenCollectionReader bcr = getCollectionReader(FolderReader.PARAM_FOLDERS, new String[] {inputDir.getPath()}); assertFalse(bcr.doHasNext()); File f = new File(inputDir, TEXT1_FILE); f.createNewFile(); Thread.sleep(TIMEOUT); assertTrue(bcr.doHasNext()); bcr.getNext(jCas.getCas()); assertFilesEquals(f.getPath(), getSource(jCas)); assertFalse(bcr.doHasNext()); bcr.close(); }
@Test public void testModifiedFile() throws Exception { BaleenCollectionReader bcr = getCollectionReader( FolderReader.PARAM_FOLDERS, new String[] {inputDir.getPath()}, FolderReader.PARAM_REPROCESS_ON_MODIFY, true); assertFalse(bcr.doHasNext()); File f = new File(inputDir, TEXT1_FILE); f.createNewFile(); Thread.sleep(TIMEOUT); assertTrue(bcr.doHasNext()); bcr.getNext(jCas.getCas()); assertFilesEquals(f.getPath(), getSource(jCas)); jCas.reset(); Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(f))); writer.write("Test"); writer.close(); Thread.sleep(TIMEOUT); assertTrue(bcr.doHasNext()); bcr.getNext(jCas.getCas()); assertFilesEquals(f.getPath(), getSource(jCas)); assertEquals("Test", jCas.getDocumentText().trim()); assertFalse(bcr.doHasNext()); bcr.close(); }
@Test public void testDeleteFile() throws Exception { BaleenCollectionReader bcr = getCollectionReader(FolderReader.PARAM_FOLDERS, new String[] {inputDir.getPath()}); assertFalse(bcr.doHasNext()); File f = new File(inputDir, TEXT1_FILE); f.createNewFile(); Thread.sleep(TIMEOUT); f.delete(); Thread.sleep(TIMEOUT); assertFalse(bcr.doHasNext()); bcr.close(); }
@Test public void testExistingFiles() throws Exception { File f1 = new File(inputDir, TEXT1_FILE); f1.createNewFile(); File f2 = new File(inputDir, TEST2_FILE); f2.createNewFile(); BaleenCollectionReader bcr = getCollectionReader(FolderReader.PARAM_FOLDERS, new String[] {inputDir.getPath()}); assertNextSourceNotNull(bcr); assertNextSourceNotNull(bcr); assertFalse(bcr.doHasNext()); bcr.close(); }
@Test public void testFilenameFilter1() throws Exception { File f1 = new File(inputDir, TEXT1_FILE); f1.createNewFile(); File f2 = new File(inputDir, "test2.log"); f2.createNewFile(); File f3 = new File(inputDir, "test3.TXT"); f3.createNewFile(); BaleenCollectionReader bcr = getCollectionReader( FolderReader.PARAM_FOLDERS, new String[] {inputDir.getPath()}, FolderReader.PARAM_ACCEPTED_PATTERNS, new String[] {".*\\.txt"}); assertNextSourceNotNull(bcr); assertNextSourceNotNull(bcr); assertFalse(bcr.doHasNext()); bcr.close(); }
@Test public void testFilenameFilter2() throws Exception { File f1 = new File(inputDir, TEXT1_FILE); f1.createNewFile(); File f2 = new File(inputDir, "test2.log"); f2.createNewFile(); File f3 = new File(inputDir, "test3.TXT"); f3.createNewFile(); BaleenCollectionReader bcr = getCollectionReader( FolderReader.PARAM_FOLDERS, new String[] {inputDir.getPath()}, FolderReader.PARAM_ACCEPTED_PATTERNS, new String[] {".*[2-3].*"}); assertNextSourceNotNull(bcr); assertNextSourceNotNull(bcr); assertFalse(bcr.doHasNext()); bcr.close(); }
@Test public void testCreateDirectoryNotProcessed() throws Exception { BaleenCollectionReader bcr = getCollectionReader(FolderReader.PARAM_FOLDERS, new String[] {inputDir.getPath()}); assertFalse(bcr.doHasNext()); File folder = new File(inputDir, DIR); folder.mkdir(); Thread.sleep(TIMEOUT); assertFalse(bcr.doHasNext()); folder.delete(); }
@Test public void testCreateDirectoryIsWatched() throws Exception { BaleenCollectionReader bcr = getCollectionReader(FolderReader.PARAM_FOLDERS, new String[] {inputDir.getPath()}); assertFalse(bcr.doHasNext()); File folder = new File(inputDir, DIR); folder.mkdir(); Thread.sleep(TIMEOUT); assertFalse(bcr.doHasNext()); File f11 = new File(folder, TEXT1_FILE); f11.createNewFile(); Thread.sleep(TIMEOUT); assertNextSourceNotNull(bcr); f11.delete(); folder.delete(); }
@Test public void testCreateDirectoryIsNotWatchedIfNotRecursive() throws Exception { BaleenCollectionReader bcr = getCollectionReader( FolderReader.PARAM_RECURSIVE, false, FolderReader.PARAM_FOLDERS, new String[] {inputDir.getPath()}); assertFalse(bcr.doHasNext()); File folder = new File(inputDir, DIR); folder.mkdir(); Thread.sleep(TIMEOUT); assertFalse(bcr.doHasNext()); File f11 = new File(folder, TEXT1_FILE); f11.createNewFile(); Thread.sleep(TIMEOUT); assertFalse(bcr.doHasNext()); f11.delete(); folder.delete(); }