opencsv-2.2.jar patched so that CsvWriter only selectively applies quotation marks around values when escape, line break, nested quotation marks or separator characters are encountered. Patch submitted to opencsv project as https://sourceforge.net/tracker/?func=detail&aid=3006635&group_id=148905&atid=773543

CsvExporter makes use of patched opencsv-2.2.jar. Quotation marks are only applied selectively.

CsvExporterTests fixed to correctly assign cellIndex when creating columns.

TsvExporterTests fixed to correctly assign cellIndex when creating columns.  Two broken tests now work.  Three tests are still marked as broken.

git-svn-id: http://google-refine.googlecode.com/svn/trunk@851 7d457c2a-affb-35e4-300a-418c747d4874
This commit is contained in:
Iain Sproat 2010-05-24 22:01:37 +00:00
parent c6827fe242
commit 3ac3feb01f
4 changed files with 55 additions and 54 deletions

Binary file not shown.

View File

@ -48,7 +48,7 @@ public class CsvExporter implements Exporter{
for(int i = 0; i < cols.length; i++){
cols[i] = project.columnModel.columns.get(i).getName();
}
csvWriter.writeNext(cols);
csvWriter.writeNext(cols,false);
columnHeader = false; //switch off flag
}
@ -60,7 +60,7 @@ public class CsvExporter implements Exporter{
}
}
csvWriter.writeNext(vals);
csvWriter.writeNext(vals,false);
return false;
}

View File

@ -57,9 +57,9 @@ public class CsvExporterTests {
Assert.fail();
}
Assert.assertEquals(writer.toString(), "\"column0\",\"column1\"\n" +
"\"row0cell0\",\"row0cell1\"\n" +
"\"row1cell0\",\"row1cell1\"\n");
Assert.assertEquals(writer.toString(), "column0,column1\n" +
"row0cell0,row0cell1\n" +
"row1cell0,row1cell1\n");
}
@ -74,10 +74,10 @@ public class CsvExporterTests {
Assert.fail();
}
Assert.assertEquals(writer.toString(), "\"column0\",\"column1\",\"column2\"\n" +
"\"row0cell0\",\"row0cell1\",\"row0cell2\"\n" +
"\"row1cell0\",\"line\n\n\nbreak\",\"row1cell2\"\n" +
"\"row2cell0\",\"row2cell1\",\"row2cell2\"\n");
Assert.assertEquals(writer.toString(), "column0,column1,column2\n" +
"row0cell0,row0cell1,row0cell2\n" +
"row1cell0,\"line\n\n\nbreak\",row1cell2\n" +
"row2cell0,row2cell1,row2cell2\n");
}
@Test
@ -91,10 +91,10 @@ public class CsvExporterTests {
Assert.fail();
}
Assert.assertEquals(writer.toString(), "\"column0\",\"column1\",\"column2\"\n" +
"\"row0cell0\",\"row0cell1\",\"row0cell2\"\n" +
"\"row1cell0\",\"with, comma\",\"row1cell2\"\n" +
"\"row2cell0\",\"row2cell1\",\"row2cell2\"\n");
Assert.assertEquals(writer.toString(), "column0,column1,column2\n" +
"row0cell0,row0cell1,row0cell2\n" +
"row1cell0,\"with, comma\",row1cell2\n" +
"row2cell0,row2cell1,row2cell2\n");
}
@Test
@ -108,10 +108,10 @@ public class CsvExporterTests {
Assert.fail();
}
Assert.assertEquals(writer.toString(), "\"column0\",\"column1\",\"column2\"\n" +
"\"row0cell0\",\"row0cell1\",\"row0cell2\"\n" +
"\"row1cell0\",\"line has \"\"quote\"\"\",\"row1cell2\"\n" +
"\"row2cell0\",\"row2cell1\",\"row2cell2\"\n");
Assert.assertEquals(writer.toString(), "column0,column1,column2\n" +
"row0cell0,row0cell1,row0cell2\n" +
"row1cell0,\"line has \"\"quote\"\"\",row1cell2\n" +
"row2cell0,row2cell1,row2cell2\n");
}
@Test
@ -126,10 +126,10 @@ public class CsvExporterTests {
Assert.fail();
}
Assert.assertEquals(writer.toString(), "\"column0\",\"column1\",\"column2\"\n" +
"\"row0cell0\",\"row0cell1\",\"row0cell2\"\n" +
"\"row1cell0\",,\"row1cell2\"\n" +
",\"row2cell1\",\"row2cell2\"\n");
Assert.assertEquals(writer.toString(), "column0,column1,column2\n" +
"row0cell0,row0cell1,row0cell2\n" +
"row1cell0,,row1cell2\n" +
",row2cell1,row2cell2\n");
}
//helper methods
@ -137,7 +137,7 @@ public class CsvExporterTests {
protected void CreateColumns(int noOfColumns){
for(int i = 0; i < noOfColumns; i++){
try {
project.columnModel.addColumn(i, new Column(0, "column" + i), true);
project.columnModel.addColumn(i, new Column(i, "column" + i), true);
} catch (ModelException e1) {
Assert.fail("Could not create column");
}

View File

@ -47,7 +47,7 @@ public class TsvExporterTests {
options = null;
}
@Test(groups={"broken"})
@Test
public void exportSimpleTsv(){
CreateGrid(2, 2);
@ -57,16 +57,16 @@ public class TsvExporterTests {
Assert.fail();
}
Assert.assertEquals(writer.toString(), "\"column0\"\t\"column1\"\n" +
"\"row0cell0\"\t\"row0cell1\"\n" +
"\"row1cell0\"\t\"row1cell1\"\n");
Assert.assertEquals(writer.toString(), "column0\tcolumn1\n" +
"row0cell0\trow0cell1\n" +
"row1cell0\trow1cell1\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);
@ -74,14 +74,14 @@ public class TsvExporterTests {
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");
Assert.assertEquals(writer.toString(), "column0\tcolumn1\tcolumn2\n" +
"row0cell0\trow0cell1\trow0cell2\n" +
"row1cell0\t\"line\n\n\nbreak\"\trow1cell2\n" +
"row2cell0\trow2cell1\trow2cell2\n");
}
@Test(groups={"broken"})
public void exportCsvWithComma(){
public void exportTsvWithComma(){
CreateGrid(3,3);
project.rows.get(1).cells.set(1, new Cell("with\t tab", null));
@ -91,16 +91,16 @@ public class TsvExporterTests {
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");
Assert.assertEquals(writer.toString(), "column0\tcolumn1\tcolumn2\n" +
"row0cell0\trow0cell1\trow0cell2\n" +
"row1cell0\t\"with\t tab\"\trow1cell2\n" +
"row2cell0\trow2cell1\trow2cell2\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);
@ -108,16 +108,16 @@ public class TsvExporterTests {
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");
Assert.assertEquals(writer.toString(), "column0\tcolumn1\tcolumn2\n" +
"row0cell0\trow0cell1\trow0cell2\n" +
"row1cell0\t\"line has \"\"quote\"\"\"\trow1cell2\n" +
"row2cell0\trow2cell1\trow2cell2\n");
}
@Test(groups={"broken"})
@Test
public void exportTsvWithEmptyCells(){
CreateGrid(3,3);
project.rows.get(1).cells.set(1, null);
project.rows.get(2).cells.set(0, null);
try {
@ -126,18 +126,19 @@ public class TsvExporterTests {
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");
Assert.assertEquals(writer.toString(), "column0\tcolumn1\tcolumn2\n" +
"row0cell0\trow0cell1\trow0cell2\n" +
"row1cell0\t\trow1cell2\n" +
"\trow2cell1\trow2cell2\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);
project.columnModel.addColumn(i, new Column(i, "column" + i), true);
project.columnModel.columns.get(i).getCellIndex();
} catch (ModelException e1) {
Assert.fail("Could not create column");
}
@ -146,7 +147,7 @@ public class TsvExporterTests {
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++){