Added tests for TsvExporter - all are broken (may be the tests themselves, as they are copied from CsvExporterTests).

CsvExporter now closes writer.  CsvExporterTests includes test for a string with a comma.

git-svn-id: http://google-refine.googlecode.com/svn/trunk@847 7d457c2a-affb-35e4-300a-418c747d4874
This commit is contained in:
Iain Sproat 2010-05-24 18:37:52 +00:00
parent 38d5b3dbde
commit 433a047fb6
3 changed files with 186 additions and 2 deletions

View File

@ -5,6 +5,9 @@ import java.io.OutputStream;
import java.io.Writer;
import java.util.Properties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.metaweb.gridworks.browsing.Engine;
import com.metaweb.gridworks.browsing.FilteredRows;
import com.metaweb.gridworks.browsing.RowVisitor;
@ -17,6 +20,8 @@ import au.com.bytecode.opencsv.CSVWriter;
public class CsvExporter implements Exporter{
final static Logger logger = LoggerFactory.getLogger("CsvExporter");
@Override
public void export(Project project, Properties options, Engine engine, OutputStream outputStream)
throws IOException {
@ -66,8 +71,11 @@ public class CsvExporter implements Exporter{
@Override
public void end(Project project) {
// nothing to do
try {
csvWriter.close();
} catch (IOException e) {
logger.error("CsvExporter could not close writer : " + e.getMessage());
}
}
}.init(new CSVWriter(writer));

View File

@ -80,6 +80,23 @@ public class CsvExporterTests {
"\"row2cell0\",\"row2cell1\",\"row2cell2\"\n");
}
@Test
public void exportCsvWithComma(){
CreateGrid(3,3);
project.rows.get(1).cells.set(1, new Cell("with, comma", null));
try {
SUT.export(project, options, engine, writer);
} catch (IOException e) {
Assert.fail();
}
Assert.assertEquals(writer.toString(), "\"column0\",\"column1\",\"column2\"\n" +
"\"row0cell0\",\"row0cell1\",\"row0cell2\"\n" +
"\"row1cell0\",\"with, comma\",\"row1cell2\"\n" +
"\"row2cell0\",\"row2cell1\",\"row2cell2\"\n");
}
@Test
public void exportCsvWithQuote(){
CreateGrid(3,3);

View File

@ -0,0 +1,159 @@
package com.metaweb.gridworks.tests.exporters;
import java.io.IOException;
import java.io.StringWriter;
import java.util.Properties;
import static org.mockito.Mockito.mock;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import com.metaweb.gridworks.browsing.Engine;
import com.metaweb.gridworks.exporters.TsvExporter;
import com.metaweb.gridworks.model.Cell;
import com.metaweb.gridworks.model.Column;
import com.metaweb.gridworks.model.ModelException;
import com.metaweb.gridworks.model.Project;
import com.metaweb.gridworks.model.Row;
public class TsvExporterTests {
//dependencies
StringWriter writer;
Project project;
Engine engine;
Properties options;
//System Under Test
TsvExporter SUT;
@BeforeMethod
public void SetUp(){
SUT = new TsvExporter();
writer = new StringWriter();
project = new Project();
engine = new Engine(project);
options = mock(Properties.class);
}
@AfterMethod
public void TearDown(){
SUT = null;
writer = null;
project = null;
engine = null;
options = null;
}
@Test(groups={"broken"})
public void exportSimpleTsv(){
CreateGrid(2, 2);
try {
SUT.export(project, options, engine, writer);
} catch (IOException e) {
Assert.fail();
}
Assert.assertEquals(writer.toString(), "\"column0\"\t\"column1\"\n" +
"\"row0cell0\"\t\"row0cell1\"\n" +
"\"row1cell0\"\t\"row1cell1\"\n");
}
@Test(groups={"broken"})
public void exportTsvWithLineBreaks(){
CreateGrid(3,3);
project.rows.get(1).cells.set(1, new Cell("line\n\n\nbreak", null));
try {
SUT.export(project, options, engine, writer);
} catch (IOException e) {
Assert.fail();
}
Assert.assertEquals(writer.toString(), "\"column0\"\t\"column1\"\t\"column2\"\n" +
"\"row0cell0\"\t\"row0cell1\"\t\"row0cell2\"\n" +
"\"row1cell0\"\t\"line\n\n\nbreak\"\t\"row1cell2\"\n" +
"\"row2cell0\"\t\"row2cell1\"\t\"row2cell2\"\n");
}
@Test(groups={"broken"})
public void exportCsvWithComma(){
CreateGrid(3,3);
project.rows.get(1).cells.set(1, new Cell("with\t tab", null));
try {
SUT.export(project, options, engine, writer);
} catch (IOException e) {
Assert.fail();
}
Assert.assertEquals(writer.toString(), "\"column0\"\t\"column1\"\t\"column2\"\n" +
"\"row0cell0\"\t\"row0cell1\"\t\"row0cell2\"\n" +
"\"row1cell0\"\t\"with\t tab\"\t\"row1cell2\"\n" +
"\"row2cell0\"\t\"row2cell1\"\t\"row2cell2\"\n");
}
@Test(groups={"broken"})
public void exportTsvWithQuote(){
CreateGrid(3,3);
project.rows.get(1).cells.set(1, new Cell("line has \"quote\"", null));
try {
SUT.export(project, options, engine, writer);
} catch (IOException e) {
Assert.fail();
}
Assert.assertEquals(writer.toString(), "\"column0\"\t\"column1\"\t\"column2\"\n" +
"\"row0cell0\"\t\"row0cell1\"\t\"row0cell2\"\n" +
"\"row1cell0\"\t\"line has \"\"quote\"\"\"\t\"row1cell2\"\n" +
"\"row2cell0\"\t\"row2cell1\"\t\"row2cell2\"\n");
}
@Test(groups={"broken"})
public void exportTsvWithEmptyCells(){
CreateGrid(3,3);
project.rows.get(1).cells.set(1, null);
project.rows.get(2).cells.set(0, null);
try {
SUT.export(project, options, engine, writer);
} catch (IOException e) {
Assert.fail();
}
Assert.assertEquals(writer.toString(), "\"column0\"\t\"column1\"\t\"column2\"\n" +
"\"row0cell0\"\t\"row0cell1\"\t\"row0cell2\"\n" +
"\"row1cell0\"\t\t\"row1cell2\"\n" +
"\t\"row2cell1\"\t\"row2cell2\"\n");
}
//helper methods
protected void CreateColumns(int noOfColumns){
for(int i = 0; i < noOfColumns; i++){
try {
project.columnModel.addColumn(i, new Column(0, "column" + i), true);
} catch (ModelException e1) {
Assert.fail("Could not create column");
}
}
}
protected void CreateGrid(int noOfRows, int noOfColumns){
CreateColumns(noOfColumns);
for(int i = 0; i < noOfRows; i++){
Row row = new Row(noOfColumns);
for(int j = 0; j < noOfColumns; j++){
row.cells.add(new Cell("row" + i + "cell" + j, null));
}
project.rows.add(row);
}
}
}