branch feature/Add-a-list-parser created (now 5b922e2)
This is an automated email from the git hooks/post-receive script. New change to branch feature/Add-a-list-parser in repository nuiton-csv. See https://gitlab.nuiton.org/nuiton/nuiton-csv.git at 5b922e2 refs #43 : add a ValueParserFormatter for list element This branch includes the following new commits: new 5b922e2 refs #43 : add a ValueParserFormatter for list element The 1 revisions listed above as "new" are entirely new to this repository and will be described in separate emails. The revisions listed as "adds" were already present in the repository and have only been added to this reference. Detailed log of new commits: commit 5b922e22443099fb4ab03978cca07489bd6c5d2a Author: Yannick Martel <yannick.martel@gmail.com> Date: Mon Jun 5 15:16:19 2017 +0200 refs #43 : add a ValueParserFormatter for list element -- To stop receiving notification emails like this one, please contact nuiton.org SCM administrator <admin+scm@nuiton.org>.
This is an automated email from the git hooks/post-receive script. New commit to branch feature/Add-a-list-parser in repository nuiton-csv. See https://gitlab.nuiton.org/nuiton/nuiton-csv.git commit 5b922e22443099fb4ab03978cca07489bd6c5d2a Author: Yannick Martel <yannick.martel@gmail.com> Date: Mon Jun 5 15:16:19 2017 +0200 refs #43 : add a ValueParserFormatter for list element --- src/main/java/org/nuiton/csv/Common.java | 47 +++++++ src/test/java/org/nuiton/csv/ImportExportTest.java | 137 +++++++++++++++++++++ src/test/java/org/nuiton/csv/RowBean.java | 11 ++ 3 files changed, 195 insertions(+) diff --git a/src/main/java/org/nuiton/csv/Common.java b/src/main/java/org/nuiton/csv/Common.java index 703ca24..6356096 100644 --- a/src/main/java/org/nuiton/csv/Common.java +++ b/src/main/java/org/nuiton/csv/Common.java @@ -21,6 +21,9 @@ */ package org.nuiton.csv; +import com.google.common.base.Function; +import com.google.common.base.Joiner; +import com.google.common.collect.Lists; import org.apache.commons.beanutils.BeanUtilsBean; import org.apache.commons.beanutils.PropertyUtils; import org.apache.commons.lang3.StringUtils; @@ -122,6 +125,10 @@ public class Common { return new EnumByNameParserFormatter<E>(enumType); } + public static <E> ValueParserFormatter<List<E>> newListParserFormatter(String separator, ValueParserFormatter<E> valueParserFormatter) { + return new ListValueParser<E>(separator, valueParserFormatter); + } + public static <E extends Enum<E>> ValueParserFormatter<E> newEnumByOrdinalParserFormatter(Class<E> enumType) { return new EnumByOrdinalParserFormatter<E>(enumType); } @@ -375,6 +382,46 @@ public class Common { } } + public static class ListValueParser<E> implements ValueParserFormatter<List<E>> { + + protected String separator; + + protected ValueParserFormatter<E> valueParserFormater; + + public ListValueParser(String separator, ValueParserFormatter<E> valueParserFormatter) { + this.separator = separator; + this.valueParserFormater = valueParserFormatter; + } + + @Override + public List<E> parse(String value) throws ParseException { + List<E> result = null; + if (StringUtils.isNotBlank(value)) { + String[] values = value.split(separator); + result = Lists.newArrayListWithCapacity(values.length); + for (String soloValue : values) { + E data = this.valueParserFormater.parse(soloValue); + result.add(data); + } + } + return result; + } + + @Override + public String format(List<E> values) { + String result = null; + if (values != null) { + result = Joiner.on(separator).join(Lists.transform(values, new Function<E, String>() { + @Override + public String apply(E e) { + return valueParserFormater.format(e); + } + })); + } + return result; + } + } + public static abstract class NullableParserFormatter<O> implements ValueParserFormatter<O> { protected O defaultValue; diff --git a/src/test/java/org/nuiton/csv/ImportExportTest.java b/src/test/java/org/nuiton/csv/ImportExportTest.java new file mode 100644 index 0000000..91137b8 --- /dev/null +++ b/src/test/java/org/nuiton/csv/ImportExportTest.java @@ -0,0 +1,137 @@ +package org.nuiton.csv; + +/* + * #%L + * Nuiton CSV + * %% + * Copyright (C) 2011 - 2012 CodeLutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ + +import com.google.common.collect.Lists; +import com.google.common.collect.Sets; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.nuiton.csv.ext.AbstractImportExportModel; +import org.nuiton.util.DateUtil; + +import java.io.IOException; +import java.io.Reader; +import java.io.StringReader; +import java.nio.charset.Charset; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; +import java.util.Set; + +/** + * Simple test for import/export. + * + * @author Yannick Martel (martel@codelutin.com) + * @since 3.0 + */ +public class ImportExportTest { + + public static final Charset CHARSET = Charset.forName("UTF-8"); + + protected ImportExportModel<RowBean> importExportModel; + + @Before + public void setUp() { + AbstractImportExportModel<RowBean> importExportModel = new AbstractImportExportModel<RowBean>(';') { + @Override + public RowBean newEmptyInstance() { + return new RowBean(); + } + }; + importExportModel.newColumnForImportExport("NUMBER", "number", Common.INTEGER); + importExportModel.newColumnForImportExport("TITLE", "title", Common.STRING); + importExportModel.newColumnForImportExport("DATE", "date", new Common.DateValue("yyyy-MM-dd")); + importExportModel.newColumnForImportExport("ROWBEANENUM", "rowBeanEnum", Common.newEnumByNameParserFormatter(RowBeanEnum.class)); + importExportModel.newColumnForImportExport("BOOLS", "booleans", Common.newListParserFormatter(",", Common.BOOLEAN)); + + this.importExportModel = importExportModel; + } + + @Test + public void testSimpleImport() throws Exception { + String content = "DATE;NUMBER;TITLE;ROWBEANENUM;BOOLS\n" + + "2011-12-05;18;\"1ère ligne\";ZERO;Y,Y,N\n" + + "2011-12-06;19;\"2ème ligne\";ONE;N,N,N\n" + + "2011-12-07;21;\"3ème ligne\";TWO;Y,N,N"; + List<RowBean> rows = importContent(importExportModel, content); + assertRowEquals(rows.get(0), DateUtil.createDate(5, 12, 2011), 18, "1ère ligne", RowBeanEnum.ZERO, Lists.newArrayList(Boolean.TRUE, Boolean.TRUE, Boolean.FALSE)); + assertRowEquals(rows.get(1), DateUtil.createDate(6, 12, 2011), 19, "2ème ligne", RowBeanEnum.ONE, Lists.newArrayList(Boolean.FALSE, Boolean.FALSE, Boolean.FALSE)); + assertRowEquals(rows.get(2), DateUtil.createDate(7, 12, 2011), 21, "3ème ligne", RowBeanEnum.TWO, Lists.newArrayList(Boolean.TRUE, Boolean.FALSE, Boolean.FALSE)); + } + + @Test + public void testExportToString() throws Exception { + + RowBean one = new RowBean(DateUtil.createDate(30, 3, 1939), "Batman", 7, RowBeanEnum.ZERO); + one.setBooleans(Lists.newArrayList(Boolean.TRUE, Boolean.TRUE, Boolean.FALSE)); + + RowBean two = new RowBean(DateUtil.createDate(5, 7, 1984), "Nightwing", 17, RowBeanEnum.ONE); + two.setBooleans(Lists.newArrayList(Boolean.FALSE, Boolean.FALSE, Boolean.FALSE)); + + Set<RowBean> rowBeans = Sets.newHashSet(one, two); + + String csv = Export.exportToString(importExportModel, rowBeans, CHARSET); + + // 1 header line + one line per RowBean instance + int expectedLineCount = 1 + rowBeans.size(); + // number of '\n' in csv + int actualLineCount = csv.split("\n").length; + Assert.assertEquals("exported CSV must have all lines", + expectedLineCount, actualLineCount); + + Assert.assertTrue(csv.contains("Y,Y,N")); + Assert.assertTrue(csv.contains("N,N,N")); + + } + + protected List<RowBean> importContent(ImportModel<RowBean> model, + String content) throws IOException { + Reader reader = new StringReader(content); + try { + Import2<RowBean> rowImport = Import2.newImport(model, reader); + try { + List<RowBean> result = new ArrayList<RowBean>(); + for (ImportRow<RowBean> row : rowImport) { + result.add(row.getBean()); + } + return result; + } finally { + rowImport.close(); + } + } finally { + reader.close(); + } + } + + private void assertRowEquals(RowBean row, Date date, Integer number, String title, RowBeanEnum rowBeanEnum, List<Boolean> booleans) { + Assert.assertEquals(date, row.getDate()); + Assert.assertEquals(number, row.getNumber()); + Assert.assertEquals(title, row.getTitle()); + Assert.assertEquals(rowBeanEnum, row.getRowBeanEnum()); + Assert.assertEquals(booleans.size(), row.getBooleans().size()); + for (int i=0; i < booleans.size(); i++) { + Assert.assertEquals(booleans.get(i), row.getBooleans().get(i)); + } + } +} diff --git a/src/test/java/org/nuiton/csv/RowBean.java b/src/test/java/org/nuiton/csv/RowBean.java index e30d404..31accbd 100644 --- a/src/test/java/org/nuiton/csv/RowBean.java +++ b/src/test/java/org/nuiton/csv/RowBean.java @@ -22,6 +22,7 @@ package org.nuiton.csv; import java.util.Date; +import java.util.List; public class RowBean { @@ -33,6 +34,8 @@ public class RowBean { private RowBeanEnum rowBeanEnum; + private List<Boolean> booleans; + public RowBean() { } @@ -75,6 +78,14 @@ public class RowBean { this.rowBeanEnum = rowBeanEnum; } + public List<Boolean> getBooleans() { + return booleans; + } + + public void setBooleans(List<Boolean> booleans) { + this.booleans = booleans; + } + @Override public boolean equals(Object o) { if (this == o) return true; -- To stop receiving notification emails like this one, please contact nuiton.org SCM administrator <admin+scm@nuiton.org>.
participants (1)
-
nuiton.org scm