Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

4.5.0 release #317

Merged
merged 3 commits into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
:toclevels: 2

= Poiji
:version: v4.4.0
:branch: 4.4.0
:version: v4.5.0
:branch: 4.5.0

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"]
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"]
Expand All @@ -25,7 +25,7 @@ In your Maven/Gradle project, first add the corresponding dependency:
<dependency>
<groupId>com.github.ozlerhakan</groupId>
<artifactId>poiji</artifactId>
<version>4.4.0</version>
<version>4.5.0</version>
</dependency>
----

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

[NOTE]
====
The `mandatoryCell` field works **only** with XLS files.
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.
====

=== Debug Cells Formats
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>com.github.ozlerhakan</groupId>
<artifactId>poiji</artifactId>
<version>4.4.0</version>
<version>4.5.0</version>
<packaging>jar</packaging>

<name>poiji</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.poiji.bind.PoijiFile;
import com.poiji.exception.PoijiException;
import com.poiji.option.PoijiOptions;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;

Expand All @@ -24,7 +25,9 @@ final class HSSFUnmarshallerFile extends HSSFUnmarshaller {
@Override
protected Workbook workbook() {
try {
return WorkbookFactory.create(poijiFile.file(), options.getPassword(), true);
Workbook workbook = WorkbookFactory.create(poijiFile.file(), options.getPassword(), true);
workbook.setMissingCellPolicy(Row.MissingCellPolicy.RETURN_BLANK_AS_NULL);
return workbook;
} catch (IOException e) {
throw new PoijiException("Problem occurred while creating HSSFWorkbook", e);
}
Expand Down
11 changes: 7 additions & 4 deletions src/main/java/com/poiji/bind/mapping/HSSFUnmarshallerStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.poiji.bind.PoijiInputStream;
import com.poiji.exception.PoijiException;
import com.poiji.option.PoijiOptions;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;

Expand All @@ -23,12 +24,14 @@ final class HSSFUnmarshallerStream extends HSSFUnmarshaller {
@Override
protected Workbook workbook() {
try {

Workbook workbook;
if (options.getPassword() != null) {
return WorkbookFactory.create(poijiInputStream.stream(), options.getPassword());
workbook = WorkbookFactory.create(poijiInputStream.stream(), options.getPassword());
} else {
workbook = WorkbookFactory.create(poijiInputStream.stream());
}

return WorkbookFactory.create(poijiInputStream.stream());
workbook.setMissingCellPolicy(Row.MissingCellPolicy.RETURN_BLANK_AS_NULL);
return workbook;
} catch (IOException e) {
throw new PoijiException("Problem occurred while creating HSSFWorkbook", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@

import com.poiji.bind.Poiji;
import com.poiji.deserialize.model.byname.MandatoryMissingCells;
import com.poiji.exception.PoijiExcelType;
import com.poiji.exception.PoijiMultiRowException;
import com.poiji.exception.PoijiMultiRowException.PoijiRowSpecificException;
import com.poiji.option.PoijiOptions;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.Test;

import java.io.IOException;
import java.io.*;
import java.util.List;

import static org.junit.Assert.assertEquals;
Expand All @@ -22,7 +24,7 @@
public class MandatoryCellsExceptionTest {

@Test
public void testExcelMandatoryColumn() {
public void shouldThrowExceptionForMissingCell() {
try {
Poiji.fromExcel(createDummyExcel(), MandatoryMissingCells.class, PoijiOptions.PoijiOptionsBuilder
.settings()
Expand All @@ -38,6 +40,58 @@ public void testExcelMandatoryColumn() {
fail("Expected exception: " + PoijiMultiRowException.class.getName());
}

@Test
public void shouldThrowExceptionForBlankCellInSheet() throws IOException {
try (InputStream stream = new FileInputStream("src/test/resources/blank-cell.xlsx");
XSSFWorkbook workbook = new XSSFWorkbook(stream)) {
workbook.setMissingCellPolicy(Row.MissingCellPolicy.RETURN_BLANK_AS_NULL);
XSSFSheet sheet = workbook.getSheetAt(0);
Poiji.fromExcel(sheet, MandatoryMissingCells.class, PoijiOptions.PoijiOptionsBuilder
.settings()
.build());
} catch (PoijiMultiRowException e) {
List<PoijiRowSpecificException> errors = e.getErrors();
assertEquals(1, errors.size());
assertEquals("Address", errors.get(0).getColumnName());
assertEquals("address", errors.get(0).getFieldName());
assertEquals((Integer) 1, errors.get(0).getRowNum());
return;
}
fail("Expected exception: " + PoijiMultiRowException.class.getName());
}

@Test
public void shouldThrowExceptionForBlankCellInFile() {
try {
Poiji.fromExcel(new File("src/test/resources/blank-cell.xls"), MandatoryMissingCells.class,
PoijiOptions.PoijiOptionsBuilder.settings().build());
} catch (PoijiMultiRowException e) {
List<PoijiRowSpecificException> errors = e.getErrors();
assertEquals(1, errors.size());
assertEquals("Address", errors.get(0).getColumnName());
assertEquals("address", errors.get(0).getFieldName());
assertEquals((Integer) 1, errors.get(0).getRowNum());
return;
}
fail("Expected exception: " + PoijiMultiRowException.class.getName());
}

@Test
public void shouldThrowExceptionForBlankCellInStream() throws IOException {
try (InputStream stream = new FileInputStream("src/test/resources/blank-cell.xls")) {
Poiji.fromExcel(stream, PoijiExcelType.XLS, MandatoryMissingCells.class,
PoijiOptions.PoijiOptionsBuilder.settings().build());
} catch (PoijiMultiRowException e) {
List<PoijiRowSpecificException> errors = e.getErrors();
assertEquals(1, errors.size());
assertEquals("Address", errors.get(0).getColumnName());
assertEquals("address", errors.get(0).getFieldName());
assertEquals((Integer) 1, errors.get(0).getRowNum());
return;
}
fail("Expected exception: " + PoijiMultiRowException.class.getName());
}

private Sheet createDummyExcel() {

Workbook workbook = new HSSFWorkbook();
Expand Down
27 changes: 27 additions & 0 deletions src/test/java/com/poiji/parser/NumberParserTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.poiji.parser;

import org.junit.Test;

import com.poiji.option.PoijiOptions;
import com.poiji.option.PoijiOptions.PoijiOptionsBuilder;

import static org.junit.Assert.assertEquals;

import java.text.NumberFormat;

public class NumberParserTest {
@Test
public void parseNumber() {
PoijiOptions options = PoijiOptionsBuilder.settings().build();
NumberParser numParser = new NumberParser(NumberFormat.getInstance(options.getLocale()));
Number expectedNumber = numParser.parse("1").doubleValue();
assertEquals(expectedNumber.doubleValue(), 1.0, 0);
}

@Test(expected = NumberFormatException.class)
public void parseNullNumber() {
PoijiOptions options = PoijiOptionsBuilder.settings().build();
NumberParser numParser = new NumberParser(NumberFormat.getInstance(options.getLocale()));
numParser.parse(null).doubleValue();
}
}
6 changes: 6 additions & 0 deletions src/test/java/com/poiji/util/DefaultCastingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,12 @@ public void castBigDecimal() {
assertEquals(BigDecimal.valueOf(81.56891), testVal);
}

@Test
public void castEmptyString() {
Object testVal = casting.castValue(String.class, "", options);
assertEquals("", testVal);
}

static class MyConfig extends DefaultCasting {
Object castValue(Class<?> fieldType, String value, PoijiOptions options) {
return getValueObject(null, -1, -1, options, value, fieldType);
Expand Down
Binary file added src/test/resources/blank-cell.xls
Binary file not shown.
Binary file added src/test/resources/blank-cell.xlsx
Binary file not shown.
Loading