// Null TableDescription.
reader = new StringReader("\n\n\n1,2,3\n\n4,5,6\n\n\n\n\n");
dataTable = CsvDataSourceHelper.read(reader, null, false);
assertEquals(2, dataTable.getNumberOfRows());
assertEquals(3, dataTable.getNumberOfColumns());
assertEquals(new TextValue("1"), dataTable.getRow(0).getCell(0).getValue());
assertEquals(new TextValue("2"), dataTable.getRow(0).getCell(1).getValue());
assertEquals(new TextValue("3"), dataTable.getRow(0).getCell(2).getValue());
assertEquals(new TextValue("4"), dataTable.getRow(1).getCell(0).getValue());
assertEquals(new TextValue("5"), dataTable.getRow(1).getCell(1).getValue());
assertEquals(new TextValue("6"), dataTable.getRow(1).getCell(2).getValue());
// Different column numbers.
boolean catched = false;
try {
reader = new StringReader("1,2\na, b, c");
dataTable = CsvDataSourceHelper.read(reader, null, false);
} catch (CsvDataSourceException e) {
catched = true;
assertEquals(ReasonType.INTERNAL_ERROR, e.getReasonType());
assertEquals(
"Wrong number of columns in the data.",
e.getMessageToUser());
}
assertTrue(catched);
// Working example with a table description filled only with types.
reader = new StringReader("1,a,true\n4,x13a,false\n1400,4,true");
List<ColumnDescription> columnDescriptions = Lists.newArrayList();
columnDescriptions.add(new ColumnDescription("i1", ValueType.NUMBER, null));
columnDescriptions.add(new ColumnDescription("i2", ValueType.TEXT, null));
columnDescriptions.add(new ColumnDescription("i3", ValueType.DATE, null));
dataTable = CsvDataSourceHelper.read(reader, columnDescriptions, false);
assertEquals(3, dataTable.getNumberOfRows());
assertEquals(3, dataTable.getNumberOfColumns());
assertEquals(new NumberValue(1), dataTable.getRow(0).getCell(0).getValue());
assertEquals(new TextValue("a"), dataTable.getRow(0).getCell(1).getValue());
assertEquals(DateValue.getNullValue(), dataTable.getRow(0).getCell(2).getValue());
assertEquals(new NumberValue(4), dataTable.getRow(1).getCell(0).getValue());
assertEquals(new TextValue("x13a"), dataTable.getRow(1).getCell(1).getValue());
assertEquals(DateValue.getNullValue(), dataTable.getRow(1).getCell(2).getValue());
assertEquals(new NumberValue(1400), dataTable.getRow(2).getCell(0).getValue());
assertEquals(new TextValue("4"), dataTable.getRow(2).getCell(1).getValue());
assertEquals(DateValue.getNullValue(), dataTable.getRow(2).getCell(2).getValue());
// Working example with a table description filled only with types.
reader = new StringReader("1,a,2004-03-01\n4,x13a,2005-04-02\n1400,4,2006-05-03");
columnDescriptions = Lists.newArrayList();
columnDescriptions.add(new ColumnDescription("i1", ValueType.NUMBER, null));
columnDescriptions.add(new ColumnDescription("i2", ValueType.TEXT, null));
columnDescriptions.add(new ColumnDescription("i3", ValueType.DATE, null));
dataTable = CsvDataSourceHelper.read(reader, columnDescriptions, false);
assertEquals(3, dataTable.getNumberOfRows());
assertEquals(3, dataTable.getNumberOfColumns());
assertEquals(new NumberValue(1), dataTable.getRow(0).getCell(0).getValue());
assertEquals(new TextValue("a"), dataTable.getRow(0).getCell(1).getValue());
assertEquals(new DateValue(2004, 2, 1), dataTable.getRow(0).getCell(2).getValue());
assertEquals(new NumberValue(4), dataTable.getRow(1).getCell(0).getValue());
assertEquals(new TextValue("x13a"), dataTable.getRow(1).getCell(1).getValue());
assertEquals(new DateValue(2005, 3, 2), dataTable.getRow(1).getCell(2).getValue());
assertEquals(new NumberValue(1400), dataTable.getRow(2).getCell(0).getValue());
assertEquals(new TextValue("4"), dataTable.getRow(2).getCell(1).getValue());
assertEquals(new DateValue(2006, 4, 3), dataTable.getRow(2).getCell(2).getValue());
assertEquals("i1", dataTable.getColumnDescription(0).getId());
assertEquals("Column0", dataTable.getColumnDescription(0).getLabel());
assertEquals("i2", dataTable.getColumnDescription(1).getId());
assertEquals("Column1", dataTable.getColumnDescription(1).getLabel());
assertEquals("i3", dataTable.getColumnDescription(2).getId());
assertEquals("Column2", dataTable.getColumnDescription(2).getLabel());
// Working example with header rows.
reader = new StringReader("1,a,2004-03-01\n4,x13a,2005-04-02\n1400,4,2006-05-03");
columnDescriptions = Lists.newArrayList();
columnDescriptions.add(new ColumnDescription("i1", ValueType.NUMBER, null));
columnDescriptions.add(new ColumnDescription("i2", ValueType.TEXT, null));
columnDescriptions.add(new ColumnDescription("i3", ValueType.DATE, null));
dataTable = CsvDataSourceHelper.read(reader, columnDescriptions, true);
assertEquals(2, dataTable.getNumberOfRows());
assertEquals(3, dataTable.getNumberOfColumns());
assertEquals(new NumberValue(4), dataTable.getRow(0).getCell(0).getValue());
assertEquals(new TextValue("x13a"), dataTable.getRow(0).getCell(1).getValue());
assertEquals(new DateValue(2005, 3, 2), dataTable.getRow(0).getCell(2).getValue());
assertEquals(new NumberValue(1400), dataTable.getRow(1).getCell(0).getValue());
assertEquals(new TextValue("4"), dataTable.getRow(1).getCell(1).getValue());
assertEquals(new DateValue(2006, 4, 3), dataTable.getRow(1).getCell(2).getValue());
assertEquals("i1", dataTable.getColumnDescription(0).getId());
assertEquals("1", dataTable.getColumnDescription(0).getLabel());
assertEquals("i2", dataTable.getColumnDescription(1).getId());
assertEquals("a", dataTable.getColumnDescription(1).getLabel());
assertEquals("i3", dataTable.getColumnDescription(2).getId());
assertEquals("2004-03-01", dataTable.getColumnDescription(2).getLabel());
// Bad table description for that data.
catched = false;
try {
reader = new StringReader("true\nfalse\nfalse");
columnDescriptions = Lists.newArrayList();
dataTable = CsvDataSourceHelper.read(reader, columnDescriptions, false);
} catch (CsvDataSourceException e) {
catched = true;
assertEquals(ReasonType.INTERNAL_ERROR, e.getReasonType());
assertEquals(
"Wrong number of columns in the data.",
e.getMessageToUser());
}
assertTrue(catched);
// Working example with a null table description.
reader = new StringReader("true,false\ntrue,false\ntrue,false\nfalse,false");
columnDescriptions = null;
dataTable = CsvDataSourceHelper.read(reader, columnDescriptions, false);
assertEquals(4, dataTable.getNumberOfRows());
assertEquals(2, dataTable.getNumberOfColumns());
assertEquals(new TextValue("true"), dataTable.getRow(0).getCell(0).getValue());
assertEquals(new TextValue("false"), dataTable.getRow(0).getCell(1).getValue());
assertEquals(new TextValue("true"), dataTable.getRow(1).getCell(0).getValue());
assertEquals(new TextValue("false"), dataTable.getRow(1).getCell(1).getValue());
assertEquals(new TextValue("true"), dataTable.getRow(2).getCell(0).getValue());
assertEquals(new TextValue("false"), dataTable.getRow(2).getCell(1).getValue());
assertEquals(new TextValue("false"), dataTable.getRow(3).getCell(0).getValue());
assertEquals(new TextValue("false"), dataTable.getRow(3).getCell(1).getValue());
// Working example with a table description filled with types.
reader = new StringReader("true,false\ntrue,false\ntrue,false\nfalse,false");
columnDescriptions = Lists.newArrayList();
columnDescriptions.add(new ColumnDescription("1", ValueType.BOOLEAN, "123"));