diff --git a/common/sourceTest/java/ch/systemsx/cisd/common/parser/ExcelFileLoaderTest.java b/common/sourceTest/java/ch/systemsx/cisd/common/parser/ExcelFileLoaderTest.java new file mode 100644 index 0000000000000000000000000000000000000000..810f3ea35bc7e0023b9b8f5aa53a247d6e607cdf --- /dev/null +++ b/common/sourceTest/java/ch/systemsx/cisd/common/parser/ExcelFileLoaderTest.java @@ -0,0 +1,246 @@ +/* + * Copyright 2013 ETH Zuerich, CISD + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ch.systemsx.cisd.common.parser; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.apache.commons.lang.builder.EqualsBuilder; +import org.apache.commons.lang.builder.ToStringBuilder; +import org.apache.poi.ss.usermodel.Sheet; +import org.testng.Assert; +import org.testng.annotations.Test; + +/** + * @author pkupczyk + */ +public class ExcelFileLoaderTest +{ + + private static final TestBean ROW_1 = new TestBean("row1column1", "row1column2", + "column3default"); + + private static final TestBean ROW_1_WITH_COLLUMN_2_DEFAULT = new TestBean("row1column1", + "column2default", "column3default"); + + private static final TestBean ROW_2 = new TestBean("row2column1", "row2column2", + "column3default"); + + private static final TestBean ROW_2_WITH_COLUMN_2_DEFAULT = new TestBean("row2column1", + "column2default", "column3default"); + + /** + * Tested file format: + * + * <pre> + * column1 column2 + * value1 value2 + * </pre> + */ + @Test + public void testFormatWithColumnHeadersInTheFirstLine() throws Exception + { + List<TestBean> beans = loadBeans("excel-with-column-headers-in-the-first-line.xls"); + assertBeans(beans, Arrays.asList(ROW_1, ROW_2)); + } + + /** + * Tested file format: + * + * <pre> + * [DEFAULT] + * column2 value2 + * [DEFAULT] + * column1 + * value1 + * </pre> + */ + @Test + public void testFormatWithColumnHeadersInTheFirstLineWithDefaultSection() throws Exception + { + List<TestBean> beans = + loadBeans("excel-with-column-headers-in-the-first-line-with-default-section.xls"); + assertBeans(beans, Arrays.asList(ROW_1_WITH_COLLUMN_2_DEFAULT, ROW_2_WITH_COLUMN_2_DEFAULT)); + } + + /** + * Tested file format: + * + * <pre> + * # 1. line of comment + * # 2. line of comment + * # ... + * column1 column2 + * value1 value2 + * </pre> + */ + @Test(enabled = false) + public void testFormatWithColumnHeadersInALineAfterComments() throws Exception + { + List<TestBean> beans = loadBeans("excel-with-column-headers-in-a-line-after-comments.xls"); + assertBeans(beans, Arrays.asList(ROW_1, ROW_2)); + } + + /** + * Tested file format: + * + * <pre> + * # 1. line of comment + * # 2. line of comment + * # ... + * [DEFAULT] + * column2 value2 + * [DEFAULT] + * column1 + * value1 + * </pre> + */ + @Test(enabled = false) + public void testFormatWithColumnHeadersInALineAfterCommentsWithDefaultSection() + throws Exception + { + List<TestBean> beans = + loadBeans("excel-with-column-headers-in-a-line-after-comments-with-default-section.xls"); + assertBeans(beans, Arrays.asList(ROW_1_WITH_COLLUMN_2_DEFAULT, ROW_2_WITH_COLUMN_2_DEFAULT)); + } + + /** + * Tested file format: + * + * <pre> + * # 1. line of comment + * # 2. line of comment + * # ... + * # + * #column1 column2 + * </pre> + */ + @Test(enabled = false) + public void testFormatWithColumnHeadersInTheLastCommentLine() throws Exception + { + List<TestBean> beans = loadBeans("excel-with-column-headers-in-the-last-comment-line.xls"); + assertBeans(beans, Arrays.asList(ROW_1, ROW_2)); + } + + /** + * Tested file format: + * + * <pre> + * # 1. line of comment + * # 2. line of comment + * # ... + * # + * #column1 + * [DEFAULT] + * column2 value2 + * [DEFAULT] + * value1 + * </pre> + */ + @Test(enabled = false) + public void testFormatWithColumnHeadersInTheLastCommentLineWithDefaultSection() + throws Exception + { + List<TestBean> beans = + loadBeans("excel-with-column-headers-in-the-last-comment-line-with-default-section.xls"); + assertBeans(beans, Arrays.asList(ROW_1_WITH_COLLUMN_2_DEFAULT, ROW_2_WITH_COLUMN_2_DEFAULT)); + } + + private List<TestBean> loadBeans(String fileName) throws Exception + { + Sheet sheet = ExcelTestUtil.getSheet(getClass(), fileName); + ExcelFileLoader<TestBean> loader = new ExcelFileLoader<TestBean>(TestBean.class); + Map<String, String> defaults = new HashMap<String, String>(); + defaults.put("column3", "column3default"); + return loader.load(sheet, sheet.getFirstRowNum(), sheet.getLastRowNum(), + defaults); + } + + private void assertBeans(List<TestBean> actualBeans, List<TestBean> expectedBeans) + { + Assert.assertEquals(actualBeans, expectedBeans); + } + + public static class TestBean + { + + private String col1; + + private String col2; + + private String col3; + + public TestBean() + { + } + + public TestBean(String col1, String col2, String col3) + { + this.col1 = col1; + this.col2 = col2; + this.col3 = col3; + } + + @BeanProperty(label = "column1", optional = true) + public void setCol1(String col1) + { + this.col1 = col1; + } + + public String getCol1() + { + return col1; + } + + @BeanProperty(label = "column2", optional = true) + public void setCol2(String col2) + { + this.col2 = col2; + } + + public String getCol2() + { + return col2; + } + + @BeanProperty(label = "column3", optional = true) + public void setCol3(String col3) + { + this.col3 = col3; + } + + public String getCol3() + { + return col3; + } + + @Override + public boolean equals(Object obj) + { + return EqualsBuilder.reflectionEquals(this, obj); + } + + @Override + public String toString() + { + return ToStringBuilder.reflectionToString(this); + } + } + +} diff --git a/common/sourceTest/java/ch/systemsx/cisd/common/parser/ExcelRowTokenizerTest.java b/common/sourceTest/java/ch/systemsx/cisd/common/parser/ExcelRowTokenizerTest.java index 94ef6469c2f9a2eea531d02eefecbe175e00626e..75533fcc4e38576232cee52230e627c8e38d4bc0 100644 --- a/common/sourceTest/java/ch/systemsx/cisd/common/parser/ExcelRowTokenizerTest.java +++ b/common/sourceTest/java/ch/systemsx/cisd/common/parser/ExcelRowTokenizerTest.java @@ -16,17 +16,11 @@ package ch.systemsx.cisd.common.parser; -import java.io.File; -import java.io.FileInputStream; -import java.io.InputStream; import java.util.Arrays; import java.util.List; -import org.apache.commons.io.IOUtils; -import org.apache.poi.hssf.usermodel.HSSFSheet; -import org.apache.poi.hssf.usermodel.HSSFWorkbook; -import org.apache.poi.poifs.filesystem.POIFSFileSystem; import org.apache.poi.ss.usermodel.Row; +import org.apache.poi.ss.usermodel.Sheet; import org.testng.AssertJUnit; import org.testng.annotations.Test; @@ -35,7 +29,6 @@ import org.testng.annotations.Test; */ public class ExcelRowTokenizerTest extends AssertJUnit { - private static final String TEST_FOLDER = "../common/sourceTest/java/"; @Test public void testIntegerValuesParsedCorrectly() throws Exception @@ -64,20 +57,8 @@ public class ExcelRowTokenizerTest extends AssertJUnit private List<Row> getRows() throws Exception { - - File excelDir = new File(TEST_FOLDER + getClass().getPackage().getName().replace('.', '/')); - File excelFile = new File(excelDir, "excel-row-tokenizer-test.xls"); - final InputStream stream = new FileInputStream(excelFile); - try - { - POIFSFileSystem poifsFileSystem = new POIFSFileSystem(stream); - HSSFWorkbook workbook = new HSSFWorkbook(poifsFileSystem); - final HSSFSheet sheet = workbook.getSheetAt(0); - return Arrays.<Row> asList(sheet.getRow(0), sheet.getRow(1), sheet.getRow(2)); - } finally - { - IOUtils.closeQuietly(stream); - } + Sheet sheet = ExcelTestUtil.getSheet(getClass(), "excel-row-tokenizer-test.xls"); + return Arrays.<Row> asList(sheet.getRow(0), sheet.getRow(1), sheet.getRow(2)); } } diff --git a/common/sourceTest/java/ch/systemsx/cisd/common/parser/ExcelTestUtil.java b/common/sourceTest/java/ch/systemsx/cisd/common/parser/ExcelTestUtil.java new file mode 100644 index 0000000000000000000000000000000000000000..f3bf53ae7a1f342737aba3bf34ac3814e81865b5 --- /dev/null +++ b/common/sourceTest/java/ch/systemsx/cisd/common/parser/ExcelTestUtil.java @@ -0,0 +1,55 @@ +/* + * Copyright 2013 ETH Zuerich, CISD + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ch.systemsx.cisd.common.parser; + +import java.io.File; +import java.io.FileInputStream; +import java.io.InputStream; + +import org.apache.commons.io.IOUtils; +import org.apache.poi.hssf.usermodel.HSSFWorkbook; +import org.apache.poi.poifs.filesystem.POIFSFileSystem; +import org.apache.poi.ss.usermodel.Sheet; + +/** + * @author pkupczyk + */ +public class ExcelTestUtil +{ + + private static final String TEST_FOLDER = "../common/sourceTest/java/"; + + public static final Sheet getSheet(Class<?> excelTestClass, String excelFileName) + throws Exception + { + File excelDir = + new File(TEST_FOLDER + + excelTestClass.getPackage().getName().replace('.', '/')); + File excelFile = new File(excelDir, excelFileName); + final InputStream stream = new FileInputStream(excelFile); + try + { + POIFSFileSystem poifsFileSystem = new POIFSFileSystem(stream); + HSSFWorkbook workbook = new HSSFWorkbook(poifsFileSystem); + return workbook.getSheetAt(0); + } finally + { + IOUtils.closeQuietly(stream); + } + } + +} diff --git a/common/sourceTest/java/ch/systemsx/cisd/common/parser/excel-with-column-headers-in-a-line-after-comments-with-default-section.xls b/common/sourceTest/java/ch/systemsx/cisd/common/parser/excel-with-column-headers-in-a-line-after-comments-with-default-section.xls new file mode 100644 index 0000000000000000000000000000000000000000..8a102a76b03ca65cab0820a265cec91aebdd6eb1 Binary files /dev/null and b/common/sourceTest/java/ch/systemsx/cisd/common/parser/excel-with-column-headers-in-a-line-after-comments-with-default-section.xls differ diff --git a/common/sourceTest/java/ch/systemsx/cisd/common/parser/excel-with-column-headers-in-a-line-after-comments.xls b/common/sourceTest/java/ch/systemsx/cisd/common/parser/excel-with-column-headers-in-a-line-after-comments.xls new file mode 100644 index 0000000000000000000000000000000000000000..0dc1cd17e9ca511f996c44023e7d099c9753e6c6 Binary files /dev/null and b/common/sourceTest/java/ch/systemsx/cisd/common/parser/excel-with-column-headers-in-a-line-after-comments.xls differ diff --git a/common/sourceTest/java/ch/systemsx/cisd/common/parser/excel-with-column-headers-in-the-first-line-with-default-section.xls b/common/sourceTest/java/ch/systemsx/cisd/common/parser/excel-with-column-headers-in-the-first-line-with-default-section.xls new file mode 100644 index 0000000000000000000000000000000000000000..cdd42f2ee17c083a24caadaaf3198f0bb63a685a Binary files /dev/null and b/common/sourceTest/java/ch/systemsx/cisd/common/parser/excel-with-column-headers-in-the-first-line-with-default-section.xls differ diff --git a/common/sourceTest/java/ch/systemsx/cisd/common/parser/excel-with-column-headers-in-the-first-line.xls b/common/sourceTest/java/ch/systemsx/cisd/common/parser/excel-with-column-headers-in-the-first-line.xls new file mode 100644 index 0000000000000000000000000000000000000000..6662b2b02387905b3e5dc2d2aab4844b82f82208 Binary files /dev/null and b/common/sourceTest/java/ch/systemsx/cisd/common/parser/excel-with-column-headers-in-the-first-line.xls differ diff --git a/common/sourceTest/java/ch/systemsx/cisd/common/parser/excel-with-column-headers-in-the-last-comment-line-with-default-section.xls b/common/sourceTest/java/ch/systemsx/cisd/common/parser/excel-with-column-headers-in-the-last-comment-line-with-default-section.xls new file mode 100644 index 0000000000000000000000000000000000000000..d5f05a76fd200b08bc98e0c82cc684d51b9fb285 Binary files /dev/null and b/common/sourceTest/java/ch/systemsx/cisd/common/parser/excel-with-column-headers-in-the-last-comment-line-with-default-section.xls differ diff --git a/common/sourceTest/java/ch/systemsx/cisd/common/parser/excel-with-column-headers-in-the-last-comment-line.xls b/common/sourceTest/java/ch/systemsx/cisd/common/parser/excel-with-column-headers-in-the-last-comment-line.xls new file mode 100644 index 0000000000000000000000000000000000000000..88d624e8fcffa657b501deb1dd6ec6f4c835881d Binary files /dev/null and b/common/sourceTest/java/ch/systemsx/cisd/common/parser/excel-with-column-headers-in-the-last-comment-line.xls differ