Skip to content

Commit

Permalink
Merge e7cb43c into eeeb5da
Browse files Browse the repository at this point in the history
  • Loading branch information
Brutus5000 committed Jun 8, 2020
2 parents eeeb5da + e7cb43c commit 32c22e7
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 7 deletions.
26 changes: 23 additions & 3 deletions src/main/java/com/poiji/config/DefaultCasting.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.poiji.config;

import com.poiji.option.PoijiOptions;
import com.poiji.parser.BooleanParser;
import com.poiji.parser.Parsers;

import java.math.BigDecimal;
Expand Down Expand Up @@ -43,6 +44,22 @@ private void logError(String value, Object defaultValue, String sheetName, int r
}
}

private Boolean primitiveBooleanValue(String value, String sheetName, int row, int col) {
try {
return Parsers.booleans().parse(value);
} catch (BooleanParser.BooleanParseException bpe) {
return onError(value, sheetName, row, col, bpe, false);
}
}

private Boolean booleanValue(String value, String sheetName, int row, int col, PoijiOptions options) {
try {
return Parsers.booleans().parse(value);
} catch (BooleanParser.BooleanParseException bpe) {
return onError(value, sheetName, row, col, bpe, options.preferNullOverDefault() ? null : false);
}
}

private int primitiveIntegerValue(String value, String sheetName, int row, int col) {
try {
return Parsers.integers().parse(value).intValue();
Expand Down Expand Up @@ -216,15 +233,18 @@ public Object castValue(Class<?> fieldType, String rawValue, int row, int col, P
} else if (fieldType == Double.class) {
o = doubleValue(value, sheetName, row, col, options);

} else if (fieldType == boolean.class) {
o = primitiveBooleanValue(value, sheetName, row, col);

} else if (fieldType == Boolean.class) {
o = booleanValue(value, sheetName, row, col, options);

} else if (fieldType == float.class) {
o = primitiveFloatValue(value, sheetName, row, col);

} else if (fieldType == Float.class) {
o = floatValue(value, sheetName, row, col, options);

} else if (fieldType == boolean.class || fieldType == Boolean.class) {
o = Boolean.valueOf(value);

} else if (fieldType == Date.class) {
o = dateValue(value, sheetName, row, col, options);

Expand Down
34 changes: 34 additions & 0 deletions src/main/java/com/poiji/parser/BooleanParser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.poiji.parser;

public class BooleanParser implements Parser<Boolean> {
@Override
public Boolean parse(String value) {
if ("true".equalsIgnoreCase(value.trim())) {
return true;
}

if ("false".equalsIgnoreCase(value.trim())) {
return false;
}

/* LibreOffice compatibility:
*
* LibreOffice booleans are stored as formula =TRUE() or =FALSE() which is parsed by POI to 1 or 0.
*/
if ("1".equals(value.trim())) {
return true;
}

if ("0".equals(value.trim())) {
return false;
}

throw new BooleanParseException(value);
}

public static class BooleanParseException extends RuntimeException {
public BooleanParseException(String value) {
super("Can't parse value to Boolean: " + value);
}
}
}
3 changes: 3 additions & 0 deletions src/main/java/com/poiji/parser/Parsers.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,7 @@ public static NumberParser numbers() {
return new NumberParser(NumberFormat.getInstance());
}

public static BooleanParser booleans() {
return new BooleanParser();
}
}
26 changes: 25 additions & 1 deletion src/test/java/com/poiji/util/DefaultCastingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,37 @@ public void castTextException() {
}

@Test
public void castBoolean() {
public void castBooleanTrue() {

Boolean testVal = (Boolean) casting.castValue(boolean.class, "True", options);

assertEquals(true, testVal);
}

@Test
public void castBooleanLibreOfficeTrue() {

Boolean testVal = (Boolean) casting.castValue(boolean.class, "1 ", options);

assertEquals(true, testVal);
}

@Test
public void castBooleanFalse() {

Boolean testVal = (Boolean) casting.castValue(boolean.class, " False", options);

assertEquals(false, testVal);
}

@Test
public void castBooleanLibreOfficeFalse() {

Boolean testVal = (Boolean) casting.castValue(boolean.class, "0", options);

assertEquals(false, testVal);
}

@Test
public void castFloat() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
import com.poiji.config.DefaultCastingError;
import com.poiji.option.PoijiOptions;
import com.poiji.option.PoijiOptions.PoijiOptionsBuilder;
import com.poiji.parser.BooleanParser;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import java.math.BigDecimal;
import java.text.ParseException;
import java.time.LocalDate;
import java.time.format.DateTimeParseException;
import java.util.Arrays;
Expand Down Expand Up @@ -367,6 +367,56 @@ public void castDoubleNullExceptionWithLogging() {
assertSingleCastingErrorPresent(sheetName, row, col, value, null, NumberFormatException.class);
}

@Test
public void castPrimitiveBooleanExceptionWithLogging() {

PoijiOptions options = PoijiOptionsBuilder.settings()
.sheetName(sheetName)
.build();

String value = "not an boolean";

Boolean expectedDefault = false;

Boolean testVal = (Boolean) casting.castValue(boolean.class, value, row, col, options);

assertEquals(expectedDefault, testVal);
assertSingleCastingErrorPresent(sheetName, row, col, value, expectedDefault, BooleanParser.BooleanParseException.class);
}

@Test
public void castBooleanDefaultExceptionWithLogging() {

PoijiOptions options = PoijiOptionsBuilder.settings()
.sheetName(sheetName)
.build();

String value = "not an Boolean";

Boolean expectedDefault = false;

Boolean testVal = (Boolean) casting.castValue(Boolean.class, value, row, col, options);

assertEquals(expectedDefault, testVal);
assertSingleCastingErrorPresent(sheetName, row, col, value, expectedDefault, BooleanParser.BooleanParseException.class);
}

@Test
public void castBooleanNullExceptionWithLogging() {

PoijiOptions options = PoijiOptionsBuilder.settings()
.sheetName(sheetName)
.preferNullOverDefault(true)
.build();

String value = "not a Boolean";

Boolean testVal = (Boolean) casting.castValue(Boolean.class, value, row, col, options);

assertNull(testVal);
assertSingleCastingErrorPresent(sheetName, row, col, value, null, BooleanParser.BooleanParseException.class);
}

// Float
@Test
public void castPrimitiveFloatExceptionWithoutLogging() {
Expand Down Expand Up @@ -551,7 +601,7 @@ public void castDateNullExceptionWithoutLogging() {
Date testVal = (Date) casting.castValue(Date.class, value, options);

assertNull(testVal);
assertSingleCastingErrorPresent(sheetName, EMPTY_ROW, EMPTY_COL, value, null, ParseException.class);
assertSingleCastingErrorPresent(sheetName, EMPTY_ROW, EMPTY_COL, value, null, java.text.ParseException.class);
}

@Test
Expand All @@ -567,7 +617,7 @@ public void castDateNullExceptionWithLogging() {
Date testVal = (Date) casting.castValue(Date.class, value, row, col, options);

assertNull(testVal);
assertSingleCastingErrorPresent(sheetName, row, col, value, null, ParseException.class);
assertSingleCastingErrorPresent(sheetName, row, col, value, null, java.text.ParseException.class);
}

// LocalDate
Expand Down

0 comments on commit 32c22e7

Please sign in to comment.