Got more tests to pass.

git-svn-id: http://google-refine.googlecode.com/svn/trunk@2279 7d457c2a-affb-35e4-300a-418c747d4874
This commit is contained in:
David Huynh 2011-10-06 20:01:27 +00:00
parent 08fdc1982c
commit 58414f72df
3 changed files with 105 additions and 44 deletions

View File

@ -5,6 +5,7 @@ import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONObject;
import org.mockito.Mockito;
@ -98,7 +99,32 @@ abstract class ImporterTest extends RefineTest {
}
protected void parseOneFile(TreeImportingParserBase parser, InputStream inputStream, JSONObject options) {
parseOneInputStreamAsReader(parser, inputStream, options);
}
protected void parseOneInputStream(
TreeImportingParserBase parser, InputStream inputStream, JSONObject options) {
ImportColumnGroup rootColumnGroup = new ImportColumnGroup();
List<Exception> exceptions = new ArrayList<Exception>();
parser.parseOneFile(
project,
metadata,
job,
"file-source",
inputStream,
rootColumnGroup,
-1,
options,
exceptions
);
postProcessProject(project, rootColumnGroup, exceptions);
}
protected void parseOneInputStreamAsReader(
TreeImportingParserBase parser, InputStream inputStream, JSONObject options) {
ImportColumnGroup rootColumnGroup = new ImportColumnGroup();
List<Exception> exceptions = new ArrayList<Exception>();
Reader reader = new InputStreamReader(inputStream);
parser.parseOneFile(
@ -110,14 +136,25 @@ abstract class ImporterTest extends RefineTest {
rootColumnGroup,
-1,
options,
new ArrayList<Exception>()
exceptions
);
XmlImportUtilities.createColumnsFromImport(project, rootColumnGroup);
project.columnModel.update();
postProcessProject(project, rootColumnGroup, exceptions);
try {
reader.close();
} catch (IOException e) {
//ignore errors on close
}
}
protected void postProcessProject(
Project project, ImportColumnGroup rootColumnGroup, List<Exception> exceptions) {
XmlImportUtilities.createColumnsFromImport(project, rootColumnGroup);
project.columnModel.update();
for (Exception e : exceptions) {
e.printStackTrace();
}
}
}

View File

@ -212,7 +212,7 @@ public class XmlImportUtilitiesTests extends RefineTest {
log(project);
assertProjectCreated(project, 0, 6);
Assert.assertEquals(project.rows.get(0).cells.size(), 5);
Assert.assertEquals(project.rows.get(0).cells.size(), 4);
Assert.assertEquals(columnGroup.subgroups.size(), 1);
Assert.assertNotNull(columnGroup.subgroups.get("book"));
@ -231,8 +231,8 @@ public class XmlImportUtilitiesTests extends RefineTest {
log(project);
assertProjectCreated(project, 0, 6);
Assert.assertEquals(project.rows.get(0).cells.size(), 5);
Assert.assertEquals(project.rows.get(5).cells.size(), 6);
Assert.assertEquals(project.rows.get(0).cells.size(), 4);
Assert.assertEquals(project.rows.get(5).cells.size(), 5);
Assert.assertEquals(columnGroup.subgroups.size(), 1);
Assert.assertEquals(columnGroup.name, "");
@ -288,7 +288,7 @@ public class XmlImportUtilitiesTests extends RefineTest {
log(project);
assertProjectCreated(project, 0, 6);
Assert.assertEquals(project.rows.get(0).cells.size(), 5);
Assert.assertEquals(project.rows.get(0).cells.size(), 4);
//TODO
}
@ -308,8 +308,8 @@ public class XmlImportUtilitiesTests extends RefineTest {
Assert.assertEquals(project.rows.size(), 1);
Row row = project.rows.get(0);
Assert.assertNotNull(row);
Assert.assertNotNull(row.getCell(2));
Assert.assertEquals(row.getCell(2).value, "author1");
Assert.assertNotNull(row.getCell(1));
Assert.assertEquals(row.getCell(1).value, "author1");
}
@ -330,12 +330,12 @@ public class XmlImportUtilitiesTests extends RefineTest {
Row row = project.rows.get(0);
Assert.assertNotNull(row);
Assert.assertEquals(row.cells.size(), 4);
Assert.assertNotNull(row.getCell(2));
Assert.assertEquals(row.getCell(2).value, "author1");
Assert.assertEquals(row.cells.size(), 3);
Assert.assertNotNull(row.getCell(1));
Assert.assertEquals(row.getCell(1).value, "author1");
row = project.rows.get(1);
Assert.assertEquals(row.getCell(2).value, "author2");
Assert.assertEquals(row.getCell(1).value, "author2");
}
@Test
@ -354,11 +354,11 @@ public class XmlImportUtilitiesTests extends RefineTest {
Assert.assertEquals(project.rows.size(), 1);
Row row = project.rows.get(0);
Assert.assertNotNull(row);
Assert.assertEquals(row.cells.size(), 5);
Assert.assertEquals(row.cells.size(), 4);
Assert.assertNotNull(row.getCell(1));
Assert.assertEquals(row.getCell(1).value, "author1");
Assert.assertNotNull(row.getCell(2));
Assert.assertEquals(row.getCell(2).value, "author1");
Assert.assertNotNull(row.getCell(3));
Assert.assertEquals(row.getCell(3).value, "a date");
Assert.assertEquals(row.getCell(2).value, "a date");
}
@ -405,9 +405,9 @@ public class XmlImportUtilitiesTests extends RefineTest {
Assert.assertNotNull(record.rows.get(0));
//Assert.assertNotNull(record.columnEmptyRowIndices.get(0));
//Assert.assertNotNull(record.columnEmptyRowIndices.get(1));
Assert.assertEquals(record.rows.get(0).size(), 2);
Assert.assertNotNull(record.rows.get(0).get(1));
Assert.assertEquals(record.rows.get(0).get(1).value, "Author1, The");
Assert.assertEquals(record.rows.get(0).size(), 1);
Assert.assertNotNull(record.rows.get(0).get(0));
Assert.assertEquals(record.rows.get(0).get(0).value, "Author1, The");
//Assert.assertEquals(record.columnEmptyRowIndices.get(0).intValue(),0);
//Assert.assertEquals(record.columnEmptyRowIndices.get(1).intValue(),1);

View File

@ -35,6 +35,7 @@ package com.google.refine.tests.importers;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.LinkedList;
@ -99,34 +100,34 @@ public class XmlImporterTests extends ImporterTest {
Row row = project.rows.get(0);
Assert.assertNotNull(row);
Assert.assertNotNull(row.getCell(2));
Assert.assertEquals(row.getCell(2).value, "Author 1, The");
Assert.assertNotNull(row.getCell(1));
Assert.assertEquals(row.getCell(1).value, "Author 1, The");
}
@Test
public void canParseDeeplyNestedSample(){
RunTest(getDeeplyNestedSample());
RunTest(getDeeplyNestedSample(), getNestedOptions(job, SUT));
log(project);
assertProjectCreated(project, 4, 6);
Row row = project.rows.get(0);
Assert.assertNotNull(row);
Assert.assertNotNull(row.getCell(2));
Assert.assertEquals(row.getCell(2).value, "Author 1, The");
Assert.assertNotNull(row.getCell(1));
Assert.assertEquals(row.getCell(1).value, "Author 1, The");
}
@Test
public void canParseSampleWithMixedElement(){
RunTest(getMixedElementSample());
RunTest(getMixedElementSample(), getNestedOptions(job, SUT));
log(project);
assertProjectCreated(project, 4, 6);
Row row = project.rows.get(0);
Assert.assertNotNull(row);
Assert.assertNotNull(row.getCell(2));
Assert.assertEquals(row.getCell(2).value, "Author 1, The");
Assert.assertNotNull(row.getCell(1));
Assert.assertEquals(row.getCell(1).value, "Author 1, The");
}
@Test
@ -138,10 +139,10 @@ public class XmlImporterTests extends ImporterTest {
Row row = project.rows.get(0);
Assert.assertNotNull(row);
Assert.assertEquals(row.cells.size(), 5);
Assert.assertNotNull(row.getCell(2));
Assert.assertEquals(row.getCell(2).value, "Author 1, The");
Assert.assertEquals(project.rows.get(1).getCell(2).value, "Author 1, Another");
Assert.assertEquals(row.cells.size(), 4);
Assert.assertNotNull(row.getCell(1));
Assert.assertEquals(row.getCell(1).value, "Author 1, The");
Assert.assertEquals(project.rows.get(1).getCell(1).value, "Author 1, Another");
}
@Test
@ -154,9 +155,9 @@ public class XmlImporterTests extends ImporterTest {
Row row = project.rows.get(3);
Assert.assertNotNull(row);
Assert.assertEquals(row.cells.size(), 5);
Assert.assertNotNull(row.getCell(2));
Assert.assertEquals(row.getCell(2).value, "With line\n break");
Assert.assertEquals(row.cells.size(), 4);
Assert.assertNotNull(row.getCell(1));
Assert.assertEquals(row.getCell(1).value, "With line\n break");
}
@Test
@ -166,15 +167,15 @@ public class XmlImporterTests extends ImporterTest {
log(project);
assertProjectCreated(project, 5, 6);
Assert.assertEquals( project.columnModel.getColumnByCellIndex(5).getName(), "book - genre");
Assert.assertEquals(project.columnModel.getColumnByCellIndex(4).getName(), "book - genre");
Row row0 = project.rows.get(0);
Assert.assertNotNull(row0);
Assert.assertEquals(row0.cells.size(),5);
Assert.assertEquals(row0.cells.size(),4);
Row row5 = project.rows.get(5);
Assert.assertNotNull(row5);
Assert.assertEquals(row5.cells.size(),6);
Assert.assertEquals(row5.cells.size(),5);
}
@Test
@ -234,14 +235,28 @@ public class XmlImporterTests extends ImporterTest {
return options;
}
public static JSONObject getNestedOptions(ImportingJob job, TreeImportingParserBase parser) {
JSONObject options = parser.createParserUIInitializationData(
job, new LinkedList<JSONObject>(), "text/json");
JSONArray path = new JSONArray();
JSONUtilities.append(path, "nest");
JSONUtilities.append(path, "nest2");
JSONUtilities.append(path, "library");
JSONUtilities.append(path, "book");
JSONUtilities.safePut(options, "recordPath", path);
return options;
}
public static String getDeeplyNestedSample(){
StringBuilder sb = new StringBuilder();
sb.append("<?xml version=\"1.0\"?><nest><nest2><library>");
for(int i = 1; i < 7; i++){
sb.append(getTypicalElement(i));
}
sb.append("</nest2>");
sb.append("<anElement>asdf</anElement></nest></library>");
sb.append("</library></nest2>");
sb.append("<anElement>asdf</anElement></nest>");
return sb.toString();
}
@ -253,8 +268,8 @@ public class XmlImporterTests extends ImporterTest {
for(int i = 1; i < 7; i++){
sb.append(getTypicalElement(i));
}
sb.append("</nest2>");
sb.append("<anElement>asdf</anElement></nest></library>");
sb.append("</library></nest2>");
sb.append("<anElement>asdf</anElement></nest>");
return sb.toString();
}
@ -318,16 +333,25 @@ public class XmlImporterTests extends ImporterTest {
}
private void RunTest(String testString){
RunTest(testString, getOptions(job, SUT));
}
private void RunTest(String testString, JSONObject options) {
try {
inputStream = new ByteArrayInputStream( testString.getBytes( "UTF-8" ) );
inputStream = new ByteArrayInputStream(testString.getBytes( "UTF-8" ));
} catch (UnsupportedEncodingException e1) {
Assert.fail();
}
try {
parseOneFile(SUT, inputStream, getOptions(job, SUT));
parseOneFile(SUT, inputStream, options);
} catch (Exception e) {
e.printStackTrace();
Assert.fail();
}
}
protected void parseOneFile(TreeImportingParserBase parser, InputStream inputStream, JSONObject options) {
parseOneInputStream(parser, inputStream, options);
}
}