diff --git a/superfields/src/main/java/org/vaadin/miki/superfields/numbers/AbstractSuperNumberField.java b/superfields/src/main/java/org/vaadin/miki/superfields/numbers/AbstractSuperNumberField.java index bfc4fe5f..6a9c91d6 100644 --- a/superfields/src/main/java/org/vaadin/miki/superfields/numbers/AbstractSuperNumberField.java +++ b/superfields/src/main/java/org/vaadin/miki/superfields/numbers/AbstractSuperNumberField.java @@ -291,7 +291,7 @@ private void updateRegularExpression() { this.field.setPattern(this.regexp); LOGGER.debug("pattern updated to {}", this.regexp); - if(!this.isNegativeValueAllowed() && this.negativityPredicate.test(value)) { + if(!this.isNegativeValueAllowed() && value != null && this.negativityPredicate.test(value)) { LOGGER.debug("negative values are not allowed, so turning into positive value {}", value); this.setValue(this.turnToPositiveOperator.apply(value)); } diff --git a/superfields/src/test/java/org/vaadin/miki/superfields/numbers/BaseTestsForIntegerNumbers.java b/superfields/src/test/java/org/vaadin/miki/superfields/numbers/BaseTestsForIntegerNumbers.java index efd1f472..fe95777f 100644 --- a/superfields/src/test/java/org/vaadin/miki/superfields/numbers/BaseTestsForIntegerNumbers.java +++ b/superfields/src/test/java/org/vaadin/miki/superfields/numbers/BaseTestsForIntegerNumbers.java @@ -200,6 +200,37 @@ public void testNullWithNullAllowed() { Assert.assertTrue("null representation must be an empty string", this.getField().getRawValue().isEmpty()); } + @Test + public void testNullWithNullAllowedAndNegativeNotAllowed() { + this.getField().setNullValueAllowed(true); + this.getField().setNegativeValueAllowed(false); + this.getField().setValue(null); + T value = this.getField().getValue(); + Assert.assertNull("value was set to null and it must be null", value); + Assert.assertTrue("null representation must be an empty string", this.getField().getRawValue().isEmpty()); + } + + @Test + public void testNullWithNullAllowedAndNegativeAllowed() { + this.getField().setNullValueAllowed(true); + this.getField().setNegativeValueAllowed(true); + this.getField().setValue(null); + T value = this.getField().getValue(); + Assert.assertNull("value was set to null and it must be null", value); + Assert.assertTrue("null representation must be an empty string", this.getField().getRawValue().isEmpty()); + } + + @Test + public void testNullAllowedSetNullThenChangeRegexp() { + this.getField().setNullValueAllowed(true); + this.getField().setValue(null); + this.getField().setNegativeValueAllowed(!this.getField().isNegativeValueAllowed()); + T value = this.getField().getValue(); + Assert.assertNull("value was set to null and it must be null", value); + Assert.assertTrue("null representation must be an empty string", this.getField().getRawValue().isEmpty()); + } + + @Test(expected = IllegalArgumentException.class) public void testNullWithNoNullAllowedThrowsException() { // by default, there should be no allowance for null values