CsvExporter uses columnIndex when accessing cells. Feature added to omit column header from first line of exported CSV. CsvExporterTests tests for this new feature.
The same unit test is available in TsvExporterTests. Test is broken, and marked as such. git-svn-id: http://google-refine.googlecode.com/svn/trunk@857 7d457c2a-affb-35e4-300a-418c747d4874
This commit is contained in:
parent
ffd6e265a9
commit
6779707399
@ -12,6 +12,7 @@ import com.metaweb.gridworks.browsing.Engine;
|
||||
import com.metaweb.gridworks.browsing.FilteredRows;
|
||||
import com.metaweb.gridworks.browsing.RowVisitor;
|
||||
import com.metaweb.gridworks.model.Cell;
|
||||
import com.metaweb.gridworks.model.Column;
|
||||
import com.metaweb.gridworks.model.Project;
|
||||
import com.metaweb.gridworks.model.Row;
|
||||
|
||||
@ -31,36 +32,48 @@ public class CsvExporter implements Exporter{
|
||||
@Override
|
||||
public void export(Project project, Properties options, Engine engine, Writer writer) throws IOException {
|
||||
{
|
||||
boolean printColumnHeader = true;
|
||||
if(options != null)
|
||||
printColumnHeader = options.getProperty("printColumnHeader")=="false"?false:true;
|
||||
|
||||
RowVisitor visitor = new RowVisitor() {
|
||||
CSVWriter csvWriter;
|
||||
boolean columnHeader = true; //the first row should also add the column headers
|
||||
boolean printColumnHeader = true;
|
||||
boolean isFirstRow = true; //the first row should also add the column headers
|
||||
|
||||
public RowVisitor init(CSVWriter writer) {
|
||||
this.csvWriter = writer;
|
||||
return this;
|
||||
}
|
||||
|
||||
public RowVisitor init(CSVWriter writer, boolean printColumnHeader){
|
||||
this.csvWriter = writer;
|
||||
this.printColumnHeader = printColumnHeader;
|
||||
return this;
|
||||
}
|
||||
|
||||
public boolean visit(Project project, int rowIndex, Row row) {
|
||||
String[] vals = null;
|
||||
String[] cols = new String[project.columnModel.columns.size()];
|
||||
String[] vals = new String[row.cells.size()];
|
||||
|
||||
if( columnHeader ){
|
||||
String[] cols = new String[project.columnModel.columns.size()];
|
||||
for(int i = 0; i < cols.length; i++){
|
||||
cols[i] = project.columnModel.columns.get(i).getName();
|
||||
}
|
||||
csvWriter.writeNext(cols,false);
|
||||
columnHeader = false; //switch off flag
|
||||
}
|
||||
int i = 0;
|
||||
for(Column col : project.columnModel.columns){
|
||||
int cellIndex = col.getCellIndex();
|
||||
cols[i] = col.getName();
|
||||
|
||||
vals = new String[row.cells.size()];
|
||||
for(int i = 0; i < vals.length; i++){
|
||||
Cell cell = row.cells.get(i);
|
||||
Cell cell = row.cells.get(cellIndex);
|
||||
if(cell != null){
|
||||
vals[i] = row.cells.get(i).value.toString();
|
||||
vals[i] = cell.value.toString();
|
||||
}
|
||||
i++;
|
||||
}
|
||||
|
||||
if( printColumnHeader && isFirstRow ){
|
||||
csvWriter.writeNext(cols,false);
|
||||
isFirstRow = false; //switch off flag
|
||||
}
|
||||
csvWriter.writeNext(vals,false);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -78,7 +91,7 @@ public class CsvExporter implements Exporter{
|
||||
}
|
||||
}
|
||||
|
||||
}.init(new CSVWriter(writer));
|
||||
}.init(new CSVWriter(writer), printColumnHeader);
|
||||
|
||||
FilteredRows filteredRows = engine.getAllFilteredRows();
|
||||
filteredRows.accept(project, visitor);
|
||||
|
@ -5,6 +5,9 @@ import java.io.StringWriter;
|
||||
import java.util.Properties;
|
||||
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.times;
|
||||
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.AfterMethod;
|
||||
@ -62,11 +65,27 @@ public class CsvExporterTests {
|
||||
"row1cell0,row1cell1\n");
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void exportSimpleCsvNoHeader(){
|
||||
CreateGrid(2, 2);
|
||||
when(options.getProperty("printColumnHeader")).thenReturn("false");
|
||||
try {
|
||||
SUT.export(project, options, engine, writer);
|
||||
} catch (IOException e) {
|
||||
Assert.fail();
|
||||
}
|
||||
|
||||
Assert.assertEquals(writer.toString(), "row0cell0,row0cell1\n" +
|
||||
"row1cell0,row1cell1\n");
|
||||
|
||||
verify(options,times(1)).getProperty("printColumnHeader");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void exportCsvWithLineBreaks(){
|
||||
CreateGrid(3,3);
|
||||
|
||||
|
||||
project.rows.get(1).cells.set(1, new Cell("line\n\n\nbreak", null));
|
||||
try {
|
||||
SUT.export(project, options, engine, writer);
|
||||
@ -79,11 +98,11 @@ public class CsvExporterTests {
|
||||
"row1cell0,\"line\n\n\nbreak\",row1cell2\n" +
|
||||
"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);
|
||||
@ -96,11 +115,11 @@ public class CsvExporterTests {
|
||||
"row1cell0,\"with, comma\",row1cell2\n" +
|
||||
"row2cell0,row2cell1,row2cell2\n");
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void exportCsvWithQuote(){
|
||||
CreateGrid(3,3);
|
||||
|
||||
|
||||
project.rows.get(1).cells.set(1, new Cell("line has \"quote\"", null));
|
||||
try {
|
||||
SUT.export(project, options, engine, writer);
|
||||
@ -113,11 +132,11 @@ public class CsvExporterTests {
|
||||
"row1cell0,\"line has \"\"quote\"\"\",row1cell2\n" +
|
||||
"row2cell0,row2cell1,row2cell2\n");
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void exportCsvWithEmptyCells(){
|
||||
CreateGrid(3,3);
|
||||
|
||||
|
||||
project.rows.get(1).cells.set(1, null);
|
||||
project.rows.get(2).cells.set(0, null);
|
||||
try {
|
||||
@ -131,7 +150,7 @@ public class CsvExporterTests {
|
||||
"row1cell0,,row1cell2\n" +
|
||||
",row2cell1,row2cell2\n");
|
||||
}
|
||||
|
||||
|
||||
//helper methods
|
||||
|
||||
protected void CreateColumns(int noOfColumns){
|
||||
@ -146,7 +165,7 @@ public class CsvExporterTests {
|
||||
|
||||
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++){
|
||||
|
@ -5,6 +5,9 @@ import java.io.StringWriter;
|
||||
import java.util.Properties;
|
||||
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.AfterMethod;
|
||||
@ -62,6 +65,22 @@ public class TsvExporterTests {
|
||||
"row1cell0\trow1cell1\n");
|
||||
|
||||
}
|
||||
|
||||
@Test(groups={"broken"})
|
||||
public void exportSimpleTsvNoHeader(){
|
||||
CreateGrid(2, 2);
|
||||
when(options.getProperty("printColumnHeader")).thenReturn("false");
|
||||
try {
|
||||
SUT.export(project, options, engine, writer);
|
||||
} catch (IOException e) {
|
||||
Assert.fail();
|
||||
}
|
||||
|
||||
Assert.assertEquals(writer.toString(), "row0cell0\trow0cell1\n" +
|
||||
"row1cell0\trow1cell1\n");
|
||||
|
||||
verify(options,times(1)).getProperty("printColumnHeader");
|
||||
}
|
||||
|
||||
@Test(groups={"broken"})
|
||||
public void exportTsvWithLineBreaks(){
|
||||
|
Loading…
Reference in New Issue
Block a user