Author: bleny Date: 2012-01-17 12:08:01 +0100 (Tue, 17 Jan 2012) New Revision: 2287 Url: http://nuiton.org/repositories/revision/nuiton-utils/2287 Log: add a test for Export in csv api Added: trunk/nuiton-csv/src/test/java/org/nuiton/util/csv/ExportTest.java trunk/nuiton-csv/src/test/java/org/nuiton/util/csv/RowBean.java trunk/nuiton-csv/src/test/java/org/nuiton/util/csv/RowBeanExportModel.java Modified: trunk/nuiton-csv/src/test/java/org/nuiton/util/csv/ImportTest.java Added: trunk/nuiton-csv/src/test/java/org/nuiton/util/csv/ExportTest.java =================================================================== --- trunk/nuiton-csv/src/test/java/org/nuiton/util/csv/ExportTest.java (rev 0) +++ trunk/nuiton-csv/src/test/java/org/nuiton/util/csv/ExportTest.java 2012-01-17 11:08:01 UTC (rev 2287) @@ -0,0 +1,67 @@ +package org.nuiton.util.csv; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.nuiton.util.DateUtil; + +import java.nio.charset.Charset; +import java.util.Arrays; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +public class ExportTest { + + private static final Log log = LogFactory.getLog(ExportTest.class); + + protected Set<RowBean> oneSizedSet = new HashSet<RowBean>(); + + protected Set<RowBean> twoSizedSet = new HashSet<RowBean>(); + + protected Set<RowBean> fiveSizedSet = new HashSet<RowBean>(); + + protected List<Set<RowBean>> sets; + + protected ExportModel<RowBean> model = new RowBeanExportModel(); + + @Before + public void setUp() { + + oneSizedSet.add(new RowBean(DateUtil.createDate(1, 12, 2011), "Batman", 1)); + + twoSizedSet.addAll(oneSizedSet); + twoSizedSet.add(new RowBean(DateUtil.createDate(2, 12, 2011), "", 7)); + + fiveSizedSet.addAll(twoSizedSet); + fiveSizedSet.add(new RowBean(DateUtil.createDate(7, 12, 2011), "", 9)); + fiveSizedSet.add(new RowBean(DateUtil.createDate(18, 12, 2011), "", 18)); + fiveSizedSet.add(new RowBean(DateUtil.createDate(23, 12, 2011), "", 4)); + + sets = Arrays.asList(new HashSet<RowBean>(), oneSizedSet, twoSizedSet, fiveSizedSet); + } + + @Test + public void testExportToString() throws Exception { + + for (Set<RowBean> set : sets) { + + String csv = Export.exportToString(model, set, Charset.forName("UTF-8")); + + if (log.isDebugEnabled()) { + log.debug("exported csv:\n" + csv); + } + + // 1 header line + one line per RowBean instance + int expectedLineCount = 1 + set.size(); + // number of '\n' in csv + int actualLineCount = csv.split("\n").length; + Assert.assertEquals("exported CSV must have all lines", + expectedLineCount, actualLineCount); + } + + } + +} Modified: trunk/nuiton-csv/src/test/java/org/nuiton/util/csv/ImportTest.java =================================================================== --- trunk/nuiton-csv/src/test/java/org/nuiton/util/csv/ImportTest.java 2012-01-17 11:07:34 UTC (rev 2286) +++ trunk/nuiton-csv/src/test/java/org/nuiton/util/csv/ImportTest.java 2012-01-17 11:08:01 UTC (rev 2287) @@ -55,7 +55,7 @@ public void testSimpleImportWithNoData() throws Exception { String content = "DATE;NUMBER;TITLE"; - List<Row> rows; + List<RowBean> rows; rows = importContent(new SimpleImportModel(), content); Assert.assertEquals(0, rows.size()); @@ -68,7 +68,7 @@ public void testSimpleImportWithOneLine() throws Exception { String content = "DATE;NUMBER;TITLE\n2011-12-05;18;\"1ère ligne\""; - List<Row> rows = importContent(new SimpleImportModel(), content); + List<RowBean> rows = importContent(new SimpleImportModel(), content); Assert.assertEquals(1, rows.size()); assertRowEquals(rows.get(0), DateUtil.createDate(5, 12, 2011), 18, "1ère ligne"); } @@ -84,7 +84,7 @@ "2011-12-05;18;\"1ère ligne\"\n" + "2011-12-06;19;\"2ème ligne\"\n" + "2011-12-07;21;\"3ème ligne\""; - List<Row> rows = importContent(new SimpleImportModel(), content); + List<RowBean> rows = importContent(new SimpleImportModel(), content); assertRowEquals(rows.get(0), DateUtil.createDate(5, 12, 2011), 18, "1ère ligne"); assertRowEquals(rows.get(1), DateUtil.createDate(6, 12, 2011), 19, "2ème ligne"); assertRowEquals(rows.get(2), DateUtil.createDate(7, 12, 2011), 21, "3ème ligne"); @@ -101,21 +101,21 @@ "2011-12-05;18;\"1ère ligne\"\n" + "2011-12-06;19;\"2ème ligne\"\n" + "2011-12-07;21;\"3ème ligne\""; - List<Row> rows = importContent(new ColumnImportModel(), content); + List<RowBean> rows = importContent(new ColumnImportModel(), content); assertRowEquals(rows.get(0), DateUtil.createDate(5, 12, 2011), 18, "1ère ligne"); assertRowEquals(rows.get(1), DateUtil.createDate(6, 12, 2011), 19, "2ème ligne"); assertRowEquals(rows.get(2), DateUtil.createDate(7, 12, 2011), 21, "3ème ligne"); } - protected List<Row> importContent(ImportModel<Row> model, + protected List<RowBean> importContent(ImportModel<RowBean> model, String content) throws IOException { Reader reader = new StringReader(content); try { - Import<Row> rowImport = Import.newImport(model, reader); + Import<RowBean> rowImport = Import.newImport(model, reader); try { - List<Row> result = new ArrayList<Row>(); - for (Row row : rowImport) { + List<RowBean> result = new ArrayList<RowBean>(); + for (RowBean row : rowImport) { result.add(row); } return result; @@ -127,78 +127,15 @@ } } - private void assertRowEquals(Row row, Date date, Integer number, String title) { + private void assertRowEquals(RowBean row, Date date, Integer number, String title) { Assert.assertEquals(date, row.getDate()); Assert.assertEquals(number, row.getNumber()); Assert.assertEquals(title, row.getTitle()); } - public static class Row { + private static class ColumnImportModel implements ImportModel<RowBean> { - private Date date; - - private String title; - - private Integer number; - - public Row() { - } - - public Row(Date date, String title, Integer number) { - this.date = date; - this.title = title; - this.number = number; - } - - public Date getDate() { - return date; - } - - public void setDate(Date date) { - this.date = date; - } - - public String getTitle() { - return title; - } - - public void setTitle(String title) { - this.title = title; - } - - public Integer getNumber() { - return number; - } - - public void setNumber(Integer number) { - this.number = number; - } - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (!(o instanceof Row)) return false; - - Row row = (Row) o; - - if (!date.equals(row.date)) return false; - if (!number.equals(row.number)) return false; - return title.equals(row.title); - - } - - @Override - public int hashCode() { - int result = date.hashCode(); - result = 31 * result + title.hashCode(); - result = 31 * result + number.hashCode(); - return result; - } - } - - private static class ColumnImportModel implements ImportModel<Row> { - - @Override public char getSeparator() { return ';'; } @@ -208,14 +145,14 @@ } @Override - public Row newEmptyInstance() { - return new Row(); + public RowBean newEmptyInstance() { + return new RowBean(); } @SuppressWarnings({"unchecked"}) @Override - public Collection<ImportableColumn<Row, Object>> getColumnsForImport() { - List<ImportableColumn<Row, Object>> result = new ArrayList<ImportableColumn<Row, Object>>(); + public Collection<ImportableColumn<RowBean, Object>> getColumnsForImport() { + List<ImportableColumn<RowBean, Object>> result = new ArrayList<ImportableColumn<RowBean, Object>>(); // Column types are not checked but safe because Object is necessary, and element type is always Row result.add(getTitleColumn()); result.add(getNumberColumn()); @@ -251,9 +188,9 @@ } @SuppressWarnings({"UnusedParameters"}) - private <T> ValueSetter<Row, T> withBeanSetter(String propertyName, Class<T> propertyClass) { + private <T> ValueSetter<RowBean, T> withBeanSetter(String propertyName, Class<T> propertyClass) { // propertyClass is not used but useful to check type - return new BeanProperty<Row, T>(propertyName); + return new BeanProperty<RowBean, T>(propertyName); } private ValueParser<Date> withDateParser() { @@ -268,7 +205,7 @@ } } - private static class SimpleImportModel implements ImportModel<Row> { + private static class SimpleImportModel implements ImportModel<RowBean> { private Log log = LogFactory.getLog(SimpleImportModel.class); @@ -285,21 +222,21 @@ } @Override - public Row newEmptyInstance() { - return new Row(); + public RowBean newEmptyInstance() { + return new RowBean(); } @Override - public Collection<ImportableColumn<Row, Object>> getColumnsForImport() { - List<ImportableColumn<Row, Object>> result = new ArrayList<ImportableColumn<Row, Object>>(); + public Collection<ImportableColumn<RowBean, Object>> getColumnsForImport() { + List<ImportableColumn<RowBean, Object>> result = new ArrayList<ImportableColumn<RowBean, Object>>(); result.add(getTitleImportable()); result.add(getNumberImportable()); result.add(getDateImportable()); return result; } - private ImportableColumn<Row, Object> getTitleImportable() { - return new AbstractImportableColumn<Row, String>() { + private ImportableColumn<RowBean, Object> getTitleImportable() { + return new AbstractImportableColumn<RowBean, String>() { @Override public String getHeaderName() { @@ -322,7 +259,7 @@ } @Override - public void update(Row object, + public void update(RowBean object, String value) throws Exception { object.setTitle(value); @@ -330,8 +267,8 @@ }; } - private ImportableColumn<Row, Object> getDateImportable() { - return new AbstractImportableColumn<Row, Date>() { + private ImportableColumn<RowBean, Object> getDateImportable() { + return new AbstractImportableColumn<RowBean, Date>() { @Override public String getHeaderName() { @@ -355,7 +292,7 @@ } @Override - public void update(Row object, + public void update(RowBean object, Date value) throws Exception { object.setDate(value); @@ -363,8 +300,8 @@ }; } - private ImportableColumn<Row, Object> getNumberImportable() { - return new AbstractImportableColumn<Row, Integer>() { + private ImportableColumn<RowBean, Object> getNumberImportable() { + return new AbstractImportableColumn<RowBean, Integer>() { @Override public String getHeaderName() { @@ -387,7 +324,7 @@ } @Override - public void update(Row object, + public void update(RowBean object, Integer value) throws Exception { object.setNumber(value); Added: trunk/nuiton-csv/src/test/java/org/nuiton/util/csv/RowBean.java =================================================================== --- trunk/nuiton-csv/src/test/java/org/nuiton/util/csv/RowBean.java (rev 0) +++ trunk/nuiton-csv/src/test/java/org/nuiton/util/csv/RowBean.java 2012-01-17 11:08:01 UTC (rev 2287) @@ -0,0 +1,66 @@ +package org.nuiton.util.csv; + +import java.util.Date; + +public class RowBean { + + private Date date; + + private String title; + + private Integer number; + + public RowBean() { + } + + public RowBean(Date date, String title, Integer number) { + this.date = date; + this.title = title; + this.number = number; + } + + public Date getDate() { + return date; + } + + public void setDate(Date date) { + this.date = date; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public Integer getNumber() { + return number; + } + + public void setNumber(Integer number) { + this.number = number; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof RowBean)) return false; + + RowBean row = (RowBean) o; + + if (!date.equals(row.date)) return false; + if (!number.equals(row.number)) return false; + return title.equals(row.title); + + } + + @Override + public int hashCode() { + int result = date.hashCode(); + result = 31 * result + title.hashCode(); + result = 31 * result + number.hashCode(); + return result; + } +} Added: trunk/nuiton-csv/src/test/java/org/nuiton/util/csv/RowBeanExportModel.java =================================================================== --- trunk/nuiton-csv/src/test/java/org/nuiton/util/csv/RowBeanExportModel.java (rev 0) +++ trunk/nuiton-csv/src/test/java/org/nuiton/util/csv/RowBeanExportModel.java 2012-01-17 11:08:01 UTC (rev 2287) @@ -0,0 +1,18 @@ +package org.nuiton.util.csv; + +class RowBeanExportModel implements ExportModel<RowBean> { + + @Override + public char getSeparator() { + return ';'; + } + + @Override + public Iterable<ExportableColumn<RowBean, Object>> getColumnsForExport() { + ModelBuilder<RowBean> modelBuilder = new ModelBuilder<RowBean>(); + modelBuilder.newColumnForExport("DATE", "date", Common.DAY); + modelBuilder.newColumnForExport("TITLE", "title"); + modelBuilder.newColumnForExport("NUMBER", "number", Common.INTEGER); + return (Iterable) modelBuilder.getColumnsForExport(); + } +}