Adding a test case for reading multi valued fields from xml

git-svn-id: https://svn.apache.org/repos/asf/lucene/solr/trunk@698707 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Shalin Shekhar Mangar 2008-09-24 20:03:58 +00:00
parent db7ca44431
commit 0507ce4512
1 changed files with 27 additions and 5 deletions

View File

@ -52,7 +52,7 @@ public class TestXPathEntityProcessor {
fields.add(createMap("column", "artist", "xpath", "/catalog/cd/artist"));
fields.add(createMap("column", "year", "xpath", "/catalog/cd/year"));
Context c = AbstractDataImportHandlerTest.getContext(null,
new VariableResolverImpl(), getds(), 0, fields, entityAttrs);
new VariableResolverImpl(), getDataSource(cdData), 0, fields, entityAttrs);
XPathEntityProcessor xPathEntityProcessor = new XPathEntityProcessor();
xPathEntityProcessor.init(c);
List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
@ -68,6 +68,26 @@ public class TestXPathEntityProcessor {
Assert.assertEquals("1982", result.get(2).get("year"));
}
@Test
public void testMultiValued() throws Exception {
Map entityAttrs = createMap("name", "e", "url", "testdata.xml",
XPathEntityProcessor.FOR_EACH, "/root");
List fields = new ArrayList();
fields.add(createMap("column", "a", "xpath", "/root/a", DataImporter.MULTI_VALUED, "true"));
Context c = AbstractDataImportHandlerTest.getContext(null,
new VariableResolverImpl(), getDataSource(testXml), 0, fields, entityAttrs);
XPathEntityProcessor xPathEntityProcessor = new XPathEntityProcessor();
xPathEntityProcessor.init(c);
List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
while (true) {
Map<String, Object> row = xPathEntityProcessor.nextRow();
if (row == null)
break;
result.add(row);
}
Assert.assertEquals(2, ((List)result.get(0).get("a")).size());
}
@Test
public void withFieldsAndXpathStream() throws Exception {
Map entityAttrs = createMap("name", "e", "url", "cd.xml",
@ -77,7 +97,7 @@ public class TestXPathEntityProcessor {
fields.add(createMap("column", "artist", "xpath", "/catalog/cd/artist"));
fields.add(createMap("column", "year", "xpath", "/catalog/cd/year"));
Context c = AbstractDataImportHandlerTest.getContext(null,
new VariableResolverImpl(), getds(), 0, fields, entityAttrs);
new VariableResolverImpl(), getDataSource(cdData), 0, fields, entityAttrs);
XPathEntityProcessor xPathEntityProcessor = new XPathEntityProcessor();
xPathEntityProcessor.init(c);
List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
@ -105,7 +125,7 @@ public class TestXPathEntityProcessor {
XPathEntityProcessor.USE_SOLR_ADD_SCHEMA, "true", "xsl", ""
+ new File(tmpdir, "x.xsl").getAbsolutePath(), "url", "cd.xml");
Context c = AbstractDataImportHandlerTest.getContext(null,
new VariableResolverImpl(), getds(), 0, null, entityAttrs);
new VariableResolverImpl(), getDataSource(cdData), 0, null, entityAttrs);
XPathEntityProcessor xPathEntityProcessor = new XPathEntityProcessor();
xPathEntityProcessor.init(c);
List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
@ -121,7 +141,7 @@ public class TestXPathEntityProcessor {
Assert.assertEquals("1982", result.get(2).get("year"));
}
private DataSource<Reader> getds() {
private DataSource<Reader> getDataSource(final String xml) {
return new DataSource<Reader>() {
public void init(Context context, Properties initProps) {
@ -131,7 +151,7 @@ public class TestXPathEntityProcessor {
}
public Reader getData(String query) {
return new StringReader(cdData);
return new StringReader(xml);
}
};
}
@ -182,4 +202,6 @@ public class TestXPathEntityProcessor {
+ "\t\t<company>RCA</company>\n"
+ "\t\t<price>9.90</price>\n"
+ "\t\t<year>1982</year>\n" + "\t</cd>\n" + "</catalog>\t";
private static final String testXml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><root><a>1</a><a>2</a></root>";
}