Skip to content

Commit

Permalink
BigInteger scale limit counts absolute value now.
Browse files Browse the repository at this point in the history
Signed-off-by: Tomáš Kraus <[email protected]>
  • Loading branch information
Tomas-Kraus committed Jul 17, 2023
1 parent 64e9f59 commit 85fe45c
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 13 deletions.
15 changes: 7 additions & 8 deletions impl/src/main/java/org/eclipse/parsson/JsonNumberImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
*/
abstract class JsonNumberImpl implements JsonNumber {

private static final String SCALE_LIMIT_EXCEPTION_MESSAGE
= "Scale value %d of this BigInteger exceeded maximal allowed absolute value of %d";

private int hashCode;
private final int bigIntegerScaleLimit;

Expand Down Expand Up @@ -287,25 +290,21 @@ public double doubleValue() {
@Override
public BigInteger bigIntegerValue() {
BigDecimal bd = bigDecimalValue();
if (bd.scale() <= bigIntegerScaleLimit) {
if (Math.abs(bd.scale()) <= bigIntegerScaleLimit) {
return bd.toBigInteger();
}
throw new UnsupportedOperationException(
String.format(
"Scale value %d of this BigInteger exceeded maximal allowed value of %d",
bd.scale(), bigIntegerScaleLimit));
String.format(SCALE_LIMIT_EXCEPTION_MESSAGE, bd.scale(), bigIntegerScaleLimit));
}

@Override
public BigInteger bigIntegerValueExact() {
BigDecimal bd = bigDecimalValue();
if (bd.scale() <= bigIntegerScaleLimit) {
if (Math.abs(bd.scale()) <= bigIntegerScaleLimit) {
return bd.toBigIntegerExact();
}
throw new UnsupportedOperationException(
String.format(
"Scale value %d of this BigInteger exceeded maximal allowed value of %d",
bd.scale(), bigIntegerScaleLimit));
String.format(SCALE_LIMIT_EXCEPTION_MESSAGE, bd.scale(), bigIntegerScaleLimit));
}

@Override
Expand Down
4 changes: 2 additions & 2 deletions impl/src/main/java/org/eclipse/parsson/api/JsonConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
public interface JsonConfig {

/**
* Configuration property to limit maximum value of BigInteger scale value.
* This property limits maximum value of scale value to be allowed
* Configuration property to limit maximum absolute value of BigInteger scale.
* This property limits maximum absolute value of scale to be allowed
* in {@link jakarta.json.JsonNumber#bigIntegerValue()}
* and {@link jakarta.json.JsonNumber#bigIntegerValueExact()} implemented methods.
* Default value is set to {@code 100000}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,26 @@ public void testSystemPropertyBigIntegerScaleAboveLimit() {
} catch (UnsupportedOperationException e) {
// UnsupportedOperationException is expected to be thrown
assertEquals(
"Scale value 50001 of this BigInteger exceeded maximal allowed value of 50000",
"Scale value 50001 of this BigInteger exceeded maximal allowed absolute value of 50000",
e.getMessage());
}
System.clearProperty("org.eclipse.parsson.maxBigIntegerScale");
}

// Test BigInteger scale value limit set from system property using value above limit.
// Call shall throw specific UnsupportedOperationException exception.
// Default value is 100000 and system property lowered it to 50000 so value with scale -50001
// test shall fail with exception message matching modified limits.
public void testSystemPropertyBigIntegerNegScaleAboveLimit() {
BigDecimal value = new BigDecimal("3.1415926535897932384626433")
.setScale(-50001, RoundingMode.HALF_UP);
try {
Json.createValue(value).bigIntegerValue();
fail("No exception was thrown from bigIntegerValue with scale over limit");
} catch (UnsupportedOperationException e) {
// UnsupportedOperationException is expected to be thrown
assertEquals(
"Scale value -50001 of this BigInteger exceeded maximal allowed absolute value of 50000",
e.getMessage());
}
System.clearProperty("org.eclipse.parsson.maxBigIntegerScale");
Expand Down
44 changes: 42 additions & 2 deletions impl/src/test/java/org/eclipse/parsson/tests/JsonNumberTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,23 @@ public void testDefaultBigIntegerScaleAboveLimit() {
} catch (UnsupportedOperationException e) {
// UnsupportedOperationException is expected to be thrown
assertEquals(
"Scale value 100001 of this BigInteger exceeded maximal allowed value of 100000",
"Scale value 100001 of this BigInteger exceeded maximal allowed absolute value of 100000",
e.getMessage());
}
}

// Test default BigInteger scale value limit using value above limit.
// Call shall throw specific UnsupportedOperationException exception.
public void testDefaultBigIntegerNegScaleAboveLimit() {
BigDecimal value = new BigDecimal("3.1415926535897932384626433")
.setScale(-100001, RoundingMode.HALF_UP);
try {
Json.createValue(value).bigIntegerValue();
fail("No exception was thrown from bigIntegerValue with scale over limit");
} catch (UnsupportedOperationException e) {
// UnsupportedOperationException is expected to be thrown
assertEquals(
"Scale value -100001 of this BigInteger exceeded maximal allowed absolute value of 100000",
e.getMessage());
}
}
Expand All @@ -308,7 +324,31 @@ public void testConfigBigIntegerScaleAboveLimit() {
} catch (UnsupportedOperationException e) {
// UnsupportedOperationException is expected to be thrown
assertEquals(
"Scale value 50001 of this BigInteger exceeded maximal allowed value of 50000",
"Scale value 50001 of this BigInteger exceeded maximal allowed absolute value of 50000",
e.getMessage());
}
}

// Test BigInteger scale value limit set from config Map using value above limit.
// Call shall throw specific UnsupportedOperationException exception.
// Config Map limit is stored in target JsonObject and shall be present for later value manipulation.
// Default value is 100000 and config Map property lowered it to 50000 so value with scale -50001
// test shall fail with exception message matching modified limits.
public void testConfigBigIntegerNegScaleAboveLimit() {
BigDecimal value = new BigDecimal("3.1415926535897932384626433")
.setScale(-50001, RoundingMode.HALF_UP);
Map<String, ?> config = Map.of(JsonConfig.MAX_BIGINTEGER_SCALE, "50000");
try {
JsonObject jsonObject = Json.createBuilderFactory(config)
.createObjectBuilder()
.add("bigDecimal", value)
.build();
jsonObject.getJsonNumber("bigDecimal").bigIntegerValue();
fail("No exception was thrown from bigIntegerValue with scale over limit");
} catch (UnsupportedOperationException e) {
// UnsupportedOperationException is expected to be thrown
assertEquals(
"Scale value -50001 of this BigInteger exceeded maximal allowed absolute value of 50000",
e.getMessage());
}
}
Expand Down

0 comments on commit 85fe45c

Please sign in to comment.