Changes needed to get FixedWidthImporter test to pass
git-svn-id: http://google-refine.googlecode.com/svn/trunk@2274 7d457c2a-affb-35e4-300a-418c747d4874
This commit is contained in:
parent
e064dcce3e
commit
850e147779
@ -127,4 +127,15 @@ public class RefineTest {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
// Works for both int, String, and JSON arrays
|
||||
static public void verifyGetArrayOption(String name, JSONObject options){
|
||||
verify(options, times(1)).has(name);
|
||||
try {
|
||||
verify(options, times(1)).getJSONArray(name);
|
||||
} catch (JSONException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -7,6 +7,7 @@ import static org.mockito.Mockito.verify;
|
||||
import java.io.StringReader;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.AfterMethod;
|
||||
@ -53,12 +54,19 @@ public class FixedWidthImporterTests extends ImporterTest {
|
||||
JSONUtilities.append(columnWidths, 6);
|
||||
JSONUtilities.append(columnWidths, 9);
|
||||
JSONUtilities.append(columnWidths, 5);
|
||||
|
||||
whenGetArrayOption("columnWidths", options, columnWidths);
|
||||
|
||||
JSONArray columnNames = new JSONArray();
|
||||
JSONUtilities.append(columnNames, "Col 1");
|
||||
JSONUtilities.append(columnNames, "Col 2");
|
||||
JSONUtilities.append(columnNames, "Col 3");
|
||||
whenGetArrayOption("columnNames", options, columnNames);
|
||||
|
||||
whenGetIntegerOption("ignoreLines", options, 0);
|
||||
whenGetIntegerOption("headerLines", options, 0);
|
||||
whenGetIntegerOption("skipDataLines", options, 0);
|
||||
whenGetIntegerOption("limit", options, -1);
|
||||
whenGetBooleanOption("storeBlankCellsAsNulls",options,true);
|
||||
|
||||
try {
|
||||
parseOneFile(SUT, reader);
|
||||
@ -66,20 +74,27 @@ public class FixedWidthImporterTests extends ImporterTest {
|
||||
Assert.fail(e.getMessage());
|
||||
}
|
||||
|
||||
Assert.assertEquals(project.rows.size(), 2);
|
||||
Assert.assertEquals(project.rows.get(0).cells.size(), 3);
|
||||
Assert.assertEquals((String)project.rows.get(0).cells.get(0).value, "NDB_No");
|
||||
Assert.assertEquals((String)project.rows.get(0).cells.get(1).value, "Shrt_Desc");
|
||||
Assert.assertEquals((String)project.rows.get(0).cells.get(2).value, "Water");
|
||||
Assert.assertEquals(project.rows.get(1).cells.size(), 3);
|
||||
Assert.assertEquals((String)project.rows.get(1).cells.get(0).value, "TooSho");
|
||||
Assert.assertEquals((String)project.rows.get(1).cells.get(1).value, "rt");
|
||||
Assert.assertNull(project.rows.get(1).cells.get(2));
|
||||
Assert.assertEquals(project.rows.size(), 3); // Column names count as a row?
|
||||
// TODO: each row has a null cell at the beginning because 1-based indexes are used
|
||||
Assert.assertEquals(project.rows.get(1).cells.size(), 4);
|
||||
Assert.assertEquals((String)project.rows.get(1).getCellValue(1), "NDB_No");
|
||||
Assert.assertEquals((String)project.rows.get(1).getCellValue(2), "Shrt_Desc");
|
||||
Assert.assertEquals((String)project.rows.get(1).getCellValue(3), "Water");
|
||||
Assert.assertEquals(project.rows.get(2).cells.size(), 4);
|
||||
Assert.assertEquals((String)project.rows.get(2).getCellValue(1), "TooSho");
|
||||
Assert.assertEquals((String)project.rows.get(2).getCellValue(2), "rt");
|
||||
Assert.assertNull(project.rows.get(2).getCellValue(3));
|
||||
|
||||
JSONUtilities.getIntArray(verify(options, times(1)), "columnWidths");
|
||||
verifyGetOption("ignore", options);
|
||||
verifyGetOption("header-lines", options);
|
||||
verifyGetOption("limit", options);
|
||||
verifyGetOption("skip", options);
|
||||
verifyGetArrayOption("columnNames", options);
|
||||
try {
|
||||
verify(options, times(1)).getJSONArray("columnWidths");
|
||||
verify(options, times(1)).getInt("ignoreLines");
|
||||
verify(options, times(1)).getInt("headerLines");
|
||||
verify(options, times(1)).getInt("skipDataLines");
|
||||
verify(options, times(1)).getInt("limit");
|
||||
verify(options, times(1)).getBoolean("storeBlankCellsAsNulls");
|
||||
} catch (JSONException e) {
|
||||
Assert.fail("JSON exception",e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -2,13 +2,16 @@ package com.google.refine.tests.importers;
|
||||
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.Reader;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import org.json.JSONObject;
|
||||
|
||||
import com.google.refine.ProjectMetadata;
|
||||
import com.google.refine.RefineServlet;
|
||||
import com.google.refine.importers.ImportingParserBase;
|
||||
import com.google.refine.importers.tree.ImportColumnGroup;
|
||||
import com.google.refine.importers.tree.TreeImportingParserBase;
|
||||
@ -16,6 +19,7 @@ import com.google.refine.importers.tree.XmlImportUtilities;
|
||||
import com.google.refine.importing.ImportingJob;
|
||||
import com.google.refine.importing.ImportingManager;
|
||||
import com.google.refine.model.Project;
|
||||
import com.google.refine.tests.RefineServletStub;
|
||||
import com.google.refine.tests.RefineTest;
|
||||
|
||||
abstract class ImporterTest extends RefineTest {
|
||||
@ -23,11 +27,15 @@ abstract class ImporterTest extends RefineTest {
|
||||
protected Project project;
|
||||
protected ProjectMetadata metadata;
|
||||
protected ImportingJob job;
|
||||
protected RefineServlet servlet;
|
||||
|
||||
protected JSONObject options;
|
||||
|
||||
public void SetUp(){
|
||||
//FIXME - should we try and use mock(Project.class); - seems unnecessary complexity
|
||||
|
||||
servlet = new RefineServletStub();
|
||||
ImportingManager.initialize(servlet);
|
||||
project = new Project();
|
||||
metadata = new ProjectMetadata();
|
||||
job = ImportingManager.createJob();
|
||||
@ -92,12 +100,13 @@ abstract class ImporterTest extends RefineTest {
|
||||
|
||||
protected void parseOneFile(TreeImportingParserBase parser, InputStream inputStream) {
|
||||
ImportColumnGroup rootColumnGroup = new ImportColumnGroup();
|
||||
Reader reader = new InputStreamReader(inputStream);
|
||||
parser.parseOneFile(
|
||||
project,
|
||||
metadata,
|
||||
job,
|
||||
"file-source",
|
||||
inputStream,
|
||||
reader,
|
||||
rootColumnGroup,
|
||||
-1,
|
||||
options,
|
||||
@ -105,5 +114,10 @@ abstract class ImporterTest extends RefineTest {
|
||||
);
|
||||
XmlImportUtilities.createColumnsFromImport(project, rootColumnGroup);
|
||||
project.columnModel.update();
|
||||
try {
|
||||
reader.close();
|
||||
} catch (IOException e) {
|
||||
//ignore errors on close
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user