/*
* Test pour void index(String) with document handler registered
*/
final public void testIndexStringListener() throws Exception {
SimpleAnalyzer analyzer = new SimpleAnalyzer();
MockControl indexFactoryControl = MockControl.createStrictControl(IndexFactory.class);
IndexFactory indexFactory = (IndexFactory)indexFactoryControl.getMock();
MockControl indexWriterControl = MockControl.createStrictControl(LuceneIndexWriter.class);
LuceneIndexWriter indexWriter = (LuceneIndexWriter)indexWriterControl.getMock();
MockControl listener1Control = MockControl.createStrictControl(FileDocumentIndexingListener.class);
FileDocumentIndexingListener listener1 = (FileDocumentIndexingListener)listener1Control.getMock();
MockControl listener2Control = MockControl.createStrictControl(FileDocumentIndexingListener.class);
FileDocumentIndexingListener listener2 = (FileDocumentIndexingListener)listener2Control.getMock();
//document
Document document = new Document();
document.add(new Field("field", "a sample", Field.Store.YES, Field.Index.TOKENIZED));
document.add(new Field("filter", "a sample filter", Field.Store.YES, Field.Index.TOKENIZED));
document.add(new Field("sort", "2", Field.Store.YES, Field.Index.UN_TOKENIZED));
indexFactory.getIndexWriter();
indexFactoryControl.setReturnValue(indexWriter, 1);
listener1.beforeIndexingDirectory(getBaseDirectoryToIndex());
listener1Control.setVoidCallable(1);
listener2.beforeIndexingDirectory(getBaseDirectoryToIndex());
listener2Control.setVoidCallable(1);
listener1.beforeIndexingFile(getFileFromClasspath("test.foo"));
listener1Control.setVoidCallable(1);
listener2.beforeIndexingFile(getFileFromClasspath("test.foo"));
listener2Control.setVoidCallable(1);
listener1.onNotAvailableHandler(getFileFromClasspath("test.foo"));
listener1Control.setVoidCallable(1);
listener2.onNotAvailableHandler(getFileFromClasspath("test.foo"));
listener2Control.setVoidCallable(1);
listener1.beforeIndexingFile(getFileFromClasspath("test.txt"));
listener1Control.setVoidCallable(1);
listener2.beforeIndexingFile(getFileFromClasspath("test.txt"));
listener2Control.setVoidCallable(1);
indexWriter.addDocument(document);
indexWriterControl.setMatcher(new AbstractMatcher() {
protected boolean argumentMatches(Object expected, Object actual) {
if( expected instanceof Document && actual instanceof Document ) {
return true;
} else {
return expected.equals(actual);
}
}
});
indexWriterControl.setVoidCallable(1);
listener1.afterIndexingFile(getFileFromClasspath("test.txt"));
listener1Control.setVoidCallable(1);
listener2.afterIndexingFile(getFileFromClasspath("test.txt"));
listener2Control.setVoidCallable(1);
listener1.afterIndexingDirectory(getBaseDirectoryToIndex());
listener1Control.setVoidCallable(1);
listener2.afterIndexingDirectory(getBaseDirectoryToIndex());
listener2Control.setVoidCallable(1);
indexWriter.close();
indexWriterControl.setVoidCallable(1);
indexFactoryControl.replay();
indexWriterControl.replay();
listener1Control.replay();
listener2Control.replay();
//Indexer
DefaultDirectoryIndexer indexer = new DefaultDirectoryIndexer(indexFactory);
indexer.addListener(listener1);
indexer.addListener(listener2);
File baseDirectory = getBaseDirectoryToIndex();
indexer.index(baseDirectory.getAbsolutePath());
indexFactoryControl.verify();
indexWriterControl.verify();
listener1Control.verify();
listener2Control.verify();
}