Fixed issue 294: "Exporting date type column to TSV/CSV shows java debugging information instead of value" with help from Gabriel Sjoberg.

git-svn-id: http://google-refine.googlecode.com/svn/trunk@1967 7d457c2a-affb-35e4-300a-418c747d4874
This commit is contained in:
David Huynh 2010-12-28 15:54:24 +00:00
parent ca8f64ddc4
commit 6fb2b05739
2 changed files with 37 additions and 1 deletions

View File

@ -35,6 +35,8 @@ package com.google.refine.exporters;
import java.io.IOException; import java.io.IOException;
import java.io.Writer; import java.io.Writer;
import java.util.Calendar;
import java.util.Date;
import java.util.Properties; import java.util.Properties;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -48,6 +50,7 @@ import com.google.refine.browsing.RowVisitor;
import com.google.refine.model.Column; import com.google.refine.model.Column;
import com.google.refine.model.Project; import com.google.refine.model.Project;
import com.google.refine.model.Row; import com.google.refine.model.Row;
import com.google.refine.util.ParsingUtilities;
public class CsvExporter implements WriterExporter{ public class CsvExporter implements WriterExporter{
@ -94,7 +97,15 @@ public class CsvExporter implements WriterExporter{
Object value = row.getCellValue(cellIndex); Object value = row.getCellValue(cellIndex);
if (value != null) { if (value != null) {
vals[i] = value instanceof String ? (String) value : value.toString(); if (value instanceof String) {
vals[i] = (String) value;
} else if (value instanceof Calendar) {
vals[i] = ParsingUtilities.dateToString(((Calendar) value).getTime());
} else if (value instanceof Date) {
vals[i] = ParsingUtilities.dateToString((Date) value);
} else {
vals[i] = value.toString();
}
} }
i++; i++;
} }

View File

@ -35,6 +35,8 @@ package com.google.refine.tests.exporters;
import java.io.IOException; import java.io.IOException;
import java.io.StringWriter; import java.io.StringWriter;
import java.util.Calendar;
import java.util.Date;
import java.util.Properties; import java.util.Properties;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
@ -57,6 +59,7 @@ import com.google.refine.model.ModelException;
import com.google.refine.model.Project; import com.google.refine.model.Project;
import com.google.refine.model.Row; import com.google.refine.model.Row;
import com.google.refine.tests.RefineTest; import com.google.refine.tests.RefineTest;
import com.google.refine.util.ParsingUtilities;
public class CsvExporterTests extends RefineTest { public class CsvExporterTests extends RefineTest {
@ -192,6 +195,28 @@ public class CsvExporterTests extends RefineTest {
"row1cell0,,row1cell2\n" + "row1cell0,,row1cell2\n" +
",row2cell1,row2cell2\n"); ",row2cell1,row2cell2\n");
} }
@Test
public void exportDateColumns(){
CreateGrid(1,1);
Calendar calendar = Calendar.getInstance();
Date date = new Date();
when(options.getProperty("printColumnHeader")).thenReturn("false");
project.rows.get(0).cells.set(0, new Cell(calendar, null));
project.rows.get(0).cells.set(1, new Cell(date, null));
try {
SUT.export(project, options, engine, writer);
} catch (IOException e) {
Assert.fail();
}
String expectedOutput = ParsingUtilities.dateToString(calendar.getTime()) + "," +
ParsingUtilities.dateToString(date) + "\n";
Assert.assertEquals(writer.toString(), expectedOutput);
}
//helper methods //helper methods