SOLR-1473 -- Tests for using arbitrary variables and last_index_time in FileListEntityProcessor date filters

git-svn-id: https://svn.apache.org/repos/asf/lucene/solr/trunk@820237 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Shalin Shekhar Mangar 2009-09-30 10:44:10 +00:00
parent e282b76156
commit ac25895a1d
2 changed files with 49 additions and 1 deletions

View File

@ -25,6 +25,8 @@ import org.apache.solr.request.LocalSolrQueryRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Date;
import java.io.File;
/**
* <p>
@ -228,6 +230,29 @@ public class TestDocBuilder2 extends AbstractDataImportHandlerTest {
assertQ(req("id:3"), "//*[@numFound='1']");
}
@Test
public void testFileListEntityProcessor_lastIndexTime() throws Exception {
long time = System.currentTimeMillis();
File tmpdir = new File("." + time);
tmpdir.mkdir();
tmpdir.deleteOnExit();
Map<String, String> params = createMap("baseDir", tmpdir.getAbsolutePath());
TestFileListEntityProcessor.createFile(tmpdir, "a.xml", "a.xml".getBytes(), true);
TestFileListEntityProcessor.createFile(tmpdir, "b.xml", "b.xml".getBytes(), true);
TestFileListEntityProcessor.createFile(tmpdir, "c.props", "c.props".getBytes(), true);
super.runFullImport(dataConfigFileList, params);
assertQ(req("*:*"), "//*[@numFound='3']");
// Add a new file after a full index is done
TestFileListEntityProcessor.createFile(tmpdir, "t.xml", "t.xml".getBytes(), false);
super.runFullImport(dataConfigFileList, params);
// we should find only 1 because by default clean=true is passed
// and this particular import should find only one file t.xml
assertQ(req("*:*"), "//*[@numFound='1']");
}
public static class MockTransformer extends Transformer {
public Object transformRow(Map<String, Object> row, Context context) {
Assert.assertTrue("Context gave incorrect data source", context.getDataSource("mockDs") instanceof MockDataSource2);
@ -323,4 +348,14 @@ public class TestDocBuilder2 extends AbstractDataImportHandlerTest {
" </entity>\n" +
" </document>\n" +
"</dataConfig>";
private final String dataConfigFileList = "<dataConfig>\n" +
"\t<document>\n" +
"\t\t<entity name=\"x\" processor=\"FileListEntityProcessor\" \n" +
"\t\t\t\tfileName=\".*\" newerThan=\"${dih.last_index_time}\" \n" +
"\t\t\t\tbaseDir=\"${dih.request.baseDir}\" transformer=\"TemplateTransformer\">\n" +
"\t\t\t<field column=\"id\" template=\"${x.file}\" />\n" +
"\t\t</entity>\n" +
"\t</document>\n" +
"</dataConfig>";
}

View File

@ -116,7 +116,7 @@ public class TestFileListEntityProcessor {
}
@SuppressWarnings("unchecked")
private List<String> getFiles(VariableResolverImpl resolver, Map attrs) {
static List<String> getFiles(VariableResolverImpl resolver, Map attrs) {
Context c = AbstractDataImportHandlerTest.getContext(null,
resolver, null, Context.FULL_DUMP, Collections.EMPTY_LIST, attrs);
FileListEntityProcessor fileListEntityProcessor = new FileListEntityProcessor();
@ -152,6 +152,19 @@ public class TestFileListEntityProcessor {
FileListEntityProcessor.NEWER_THAN, "'NOW-2HOURS'");
fList = getFiles(null, attrs);
Assert.assertEquals(2, fList.size());
// Use a variable for newerThan
attrs = AbstractDataImportHandlerTest.createMap(
FileListEntityProcessor.FILE_NAME, ".xml$",
FileListEntityProcessor.BASE_DIR, tmpdir.getAbsolutePath(),
FileListEntityProcessor.NEWER_THAN, "${a.x}");
VariableResolverImpl resolver = new VariableResolverImpl();
String lastMod = DataImporter.DATE_TIME_FORMAT.get().format(new Date(System.currentTimeMillis() - 50000));
resolver.addNamespace("a", AbstractDataImportHandlerTest.createMap("x", lastMod));
createFile(tmpdir, "t.xml", "t.xml".getBytes(), false);
fList = getFiles(resolver, attrs);
Assert.assertEquals(1, fList.size());
Assert.assertEquals("File name must be t.xml", new File(tmpdir, "t.xml").getAbsolutePath(), fList.get(0));
}
@Test