Skip to content

Commit 7c81a29

Browse files
authored
Merge pull request #317 from ozlerhakan/4.5.0
bump 4.5.0
2 parents 0ee9a3f + 216ea45 commit 7c81a29

File tree

9 files changed

+106
-13
lines changed

9 files changed

+106
-13
lines changed

README.adoc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
:toclevels: 2
33

44
= Poiji
5-
:version: v4.4.0
6-
:branch: 4.4.0
5+
:version: v4.5.0
6+
:branch: 4.5.0
77

88
image:https://github.com/ozlerhakan/poiji/actions/workflows/maven.yml/badge.svg["Build Status"] image:https://app.codacy.com/project/badge/Grade/64f7e2cb9e604807b62334a4cfc3952d["Codacy code quality",link="https://www.codacy.com/gh/ozlerhakan/poiji/dashboard?utm_source=github.com&utm_medium=referral&utm_content=ozlerhakan/poiji&utm_campaign=Badge_Grade"]
99
image:https://codecov.io/gh/ozlerhakan/poiji/branch/{branch}/graph/badge.svg?token=MN6V6xOWBq["Codecov",link="https://codecov.io/gh/ozlerhakan/poiji"] image:https://img.shields.io/badge/apache.poi-5.2.3-brightgreen.svg[] image:https://app.fossa.com/api/projects/git%2Bgithub.com%2Fozlerhakan%2Fpoiji.svg?type=shield["FOSSA Status",link="https://app.fossa.com/projects/git%2Bgithub.com%2Fozlerhakan%2Fpoiji?ref=badge_shield"]
@@ -25,7 +25,7 @@ In your Maven/Gradle project, first add the corresponding dependency:
2525
<dependency>
2626
<groupId>com.github.ozlerhakan</groupId>
2727
<artifactId>poiji</artifactId>
28-
<version>4.4.0</version>
28+
<version>4.5.0</version>
2929
</dependency>
3030
----
3131

@@ -710,7 +710,7 @@ The `mandatoryHeader` field is compatible with XLS and XLSX files.
710710

711711
[NOTE]
712712
====
713-
The `mandatoryCell` field works **only** with XLS files.
713+
The `mandatoryCell` field works **only** with XLS files and `Sheet` instances. XLS workbooks are opened with `RETURN_BLANK_AS_NULL` missing cell policy. If passing a `Sheet` instance it is up for the caller to make sure the missing cell policy of the parent workbook is set accordingly.
714714
====
715715

716716
=== Debug Cells Formats

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
<groupId>com.github.ozlerhakan</groupId>
77
<artifactId>poiji</artifactId>
8-
<version>4.4.0</version>
8+
<version>4.5.0</version>
99
<packaging>jar</packaging>
1010

1111
<name>poiji</name>

src/main/java/com/poiji/bind/mapping/HSSFUnmarshallerFile.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.poiji.bind.PoijiFile;
44
import com.poiji.exception.PoijiException;
55
import com.poiji.option.PoijiOptions;
6+
import org.apache.poi.ss.usermodel.Row;
67
import org.apache.poi.ss.usermodel.Workbook;
78
import org.apache.poi.ss.usermodel.WorkbookFactory;
89

@@ -24,7 +25,9 @@ final class HSSFUnmarshallerFile extends HSSFUnmarshaller {
2425
@Override
2526
protected Workbook workbook() {
2627
try {
27-
return WorkbookFactory.create(poijiFile.file(), options.getPassword(), true);
28+
Workbook workbook = WorkbookFactory.create(poijiFile.file(), options.getPassword(), true);
29+
workbook.setMissingCellPolicy(Row.MissingCellPolicy.RETURN_BLANK_AS_NULL);
30+
return workbook;
2831
} catch (IOException e) {
2932
throw new PoijiException("Problem occurred while creating HSSFWorkbook", e);
3033
}

src/main/java/com/poiji/bind/mapping/HSSFUnmarshallerStream.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.poiji.bind.PoijiInputStream;
44
import com.poiji.exception.PoijiException;
55
import com.poiji.option.PoijiOptions;
6+
import org.apache.poi.ss.usermodel.Row;
67
import org.apache.poi.ss.usermodel.Workbook;
78
import org.apache.poi.ss.usermodel.WorkbookFactory;
89

@@ -23,12 +24,14 @@ final class HSSFUnmarshallerStream extends HSSFUnmarshaller {
2324
@Override
2425
protected Workbook workbook() {
2526
try {
26-
27+
Workbook workbook;
2728
if (options.getPassword() != null) {
28-
return WorkbookFactory.create(poijiInputStream.stream(), options.getPassword());
29+
workbook = WorkbookFactory.create(poijiInputStream.stream(), options.getPassword());
30+
} else {
31+
workbook = WorkbookFactory.create(poijiInputStream.stream());
2932
}
30-
31-
return WorkbookFactory.create(poijiInputStream.stream());
33+
workbook.setMissingCellPolicy(Row.MissingCellPolicy.RETURN_BLANK_AS_NULL);
34+
return workbook;
3235
} catch (IOException e) {
3336
throw new PoijiException("Problem occurred while creating HSSFWorkbook", e);
3437
}

src/test/java/com/poiji/deserialize/MandatoryCellsExceptionTest.java

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,20 @@
22

33
import com.poiji.bind.Poiji;
44
import com.poiji.deserialize.model.byname.MandatoryMissingCells;
5+
import com.poiji.exception.PoijiExcelType;
56
import com.poiji.exception.PoijiMultiRowException;
67
import com.poiji.exception.PoijiMultiRowException.PoijiRowSpecificException;
78
import com.poiji.option.PoijiOptions;
8-
99
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
1010
import org.apache.poi.ss.usermodel.Cell;
1111
import org.apache.poi.ss.usermodel.Row;
1212
import org.apache.poi.ss.usermodel.Sheet;
1313
import org.apache.poi.ss.usermodel.Workbook;
14+
import org.apache.poi.xssf.usermodel.XSSFSheet;
15+
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
1416
import org.junit.Test;
1517

16-
import java.io.IOException;
18+
import java.io.*;
1719
import java.util.List;
1820

1921
import static org.junit.Assert.assertEquals;
@@ -22,7 +24,7 @@
2224
public class MandatoryCellsExceptionTest {
2325

2426
@Test
25-
public void testExcelMandatoryColumn() {
27+
public void shouldThrowExceptionForMissingCell() {
2628
try {
2729
Poiji.fromExcel(createDummyExcel(), MandatoryMissingCells.class, PoijiOptions.PoijiOptionsBuilder
2830
.settings()
@@ -38,6 +40,58 @@ public void testExcelMandatoryColumn() {
3840
fail("Expected exception: " + PoijiMultiRowException.class.getName());
3941
}
4042

43+
@Test
44+
public void shouldThrowExceptionForBlankCellInSheet() throws IOException {
45+
try (InputStream stream = new FileInputStream("src/test/resources/blank-cell.xlsx");
46+
XSSFWorkbook workbook = new XSSFWorkbook(stream)) {
47+
workbook.setMissingCellPolicy(Row.MissingCellPolicy.RETURN_BLANK_AS_NULL);
48+
XSSFSheet sheet = workbook.getSheetAt(0);
49+
Poiji.fromExcel(sheet, MandatoryMissingCells.class, PoijiOptions.PoijiOptionsBuilder
50+
.settings()
51+
.build());
52+
} catch (PoijiMultiRowException e) {
53+
List<PoijiRowSpecificException> errors = e.getErrors();
54+
assertEquals(1, errors.size());
55+
assertEquals("Address", errors.get(0).getColumnName());
56+
assertEquals("address", errors.get(0).getFieldName());
57+
assertEquals((Integer) 1, errors.get(0).getRowNum());
58+
return;
59+
}
60+
fail("Expected exception: " + PoijiMultiRowException.class.getName());
61+
}
62+
63+
@Test
64+
public void shouldThrowExceptionForBlankCellInFile() {
65+
try {
66+
Poiji.fromExcel(new File("src/test/resources/blank-cell.xls"), MandatoryMissingCells.class,
67+
PoijiOptions.PoijiOptionsBuilder.settings().build());
68+
} catch (PoijiMultiRowException e) {
69+
List<PoijiRowSpecificException> errors = e.getErrors();
70+
assertEquals(1, errors.size());
71+
assertEquals("Address", errors.get(0).getColumnName());
72+
assertEquals("address", errors.get(0).getFieldName());
73+
assertEquals((Integer) 1, errors.get(0).getRowNum());
74+
return;
75+
}
76+
fail("Expected exception: " + PoijiMultiRowException.class.getName());
77+
}
78+
79+
@Test
80+
public void shouldThrowExceptionForBlankCellInStream() throws IOException {
81+
try (InputStream stream = new FileInputStream("src/test/resources/blank-cell.xls")) {
82+
Poiji.fromExcel(stream, PoijiExcelType.XLS, MandatoryMissingCells.class,
83+
PoijiOptions.PoijiOptionsBuilder.settings().build());
84+
} catch (PoijiMultiRowException e) {
85+
List<PoijiRowSpecificException> errors = e.getErrors();
86+
assertEquals(1, errors.size());
87+
assertEquals("Address", errors.get(0).getColumnName());
88+
assertEquals("address", errors.get(0).getFieldName());
89+
assertEquals((Integer) 1, errors.get(0).getRowNum());
90+
return;
91+
}
92+
fail("Expected exception: " + PoijiMultiRowException.class.getName());
93+
}
94+
4195
private Sheet createDummyExcel() {
4296

4397
Workbook workbook = new HSSFWorkbook();
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.poiji.parser;
2+
3+
import org.junit.Test;
4+
5+
import com.poiji.option.PoijiOptions;
6+
import com.poiji.option.PoijiOptions.PoijiOptionsBuilder;
7+
8+
import static org.junit.Assert.assertEquals;
9+
10+
import java.text.NumberFormat;
11+
12+
public class NumberParserTest {
13+
@Test
14+
public void parseNumber() {
15+
PoijiOptions options = PoijiOptionsBuilder.settings().build();
16+
NumberParser numParser = new NumberParser(NumberFormat.getInstance(options.getLocale()));
17+
Number expectedNumber = numParser.parse("1").doubleValue();
18+
assertEquals(expectedNumber.doubleValue(), 1.0, 0);
19+
}
20+
21+
@Test(expected = NumberFormatException.class)
22+
public void parseNullNumber() {
23+
PoijiOptions options = PoijiOptionsBuilder.settings().build();
24+
NumberParser numParser = new NumberParser(NumberFormat.getInstance(options.getLocale()));
25+
numParser.parse(null).doubleValue();
26+
}
27+
}

src/test/java/com/poiji/util/DefaultCastingTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,12 @@ public void castBigDecimal() {
266266
assertEquals(BigDecimal.valueOf(81.56891), testVal);
267267
}
268268

269+
@Test
270+
public void castEmptyString() {
271+
Object testVal = casting.castValue(String.class, "", options);
272+
assertEquals("", testVal);
273+
}
274+
269275
static class MyConfig extends DefaultCasting {
270276
Object castValue(Class<?> fieldType, String value, PoijiOptions options) {
271277
return getValueObject(null, -1, -1, options, value, fieldType);

src/test/resources/blank-cell.xls

5 KB
Binary file not shown.

src/test/resources/blank-cell.xlsx

7 KB
Binary file not shown.

0 commit comments

Comments
 (0)