Search in sources :

Example 1 with ScaleValidator

use of com.qcadoo.model.internal.validators.ScaleValidator in project qcadoo by qcadoo.

the class ValidatorTest method shouldHasErrorsIfBigDecimalScaleIsNotEqual.

@Test
public void shouldHasErrorsIfBigDecimalScaleIsNotEqual() throws Exception {
    // given
    Entity entity = new DefaultEntity(dataDefinition);
    entity.setField("money", new BigDecimal("123.45"));
    fieldDefinitionMoney.withValidator(new ScaleValidator(null, 4, null));
    // when
    entity = dataDefinition.save(entity);
    // then
    verify(session, never()).save(any(SampleSimpleDatabaseObject.class));
    assertFalse(entity.isValid());
}
Also used : Entity(com.qcadoo.model.api.Entity) DefaultEntity(com.qcadoo.model.internal.DefaultEntity) DefaultEntity(com.qcadoo.model.internal.DefaultEntity) ScaleValidator(com.qcadoo.model.internal.validators.ScaleValidator) SampleSimpleDatabaseObject(com.qcadoo.model.beans.sample.SampleSimpleDatabaseObject) BigDecimal(java.math.BigDecimal) DataAccessTest(com.qcadoo.model.internal.DataAccessTest) Test(org.junit.Test)

Example 2 with ScaleValidator

use of com.qcadoo.model.internal.validators.ScaleValidator in project qcadoo by qcadoo.

the class ValidatorTest method shouldHasErrorsIfBigDecimalPresicionAndScaleAreTooLong.

@Test
public void shouldHasErrorsIfBigDecimalPresicionAndScaleAreTooLong() throws Exception {
    // given
    Entity entity = new DefaultEntity(dataDefinition);
    entity.setField("money", new BigDecimal("123.456"));
    fieldDefinitionMoney.withValidator(new ScaleValidator(null, null, 2)).withValidator(new UnscaledValueValidator(null, null, 4));
    // when
    entity = dataDefinition.save(entity);
    // then
    verify(session, never()).save(any(SampleSimpleDatabaseObject.class));
    assertFalse(entity.isValid());
}
Also used : Entity(com.qcadoo.model.api.Entity) DefaultEntity(com.qcadoo.model.internal.DefaultEntity) DefaultEntity(com.qcadoo.model.internal.DefaultEntity) UnscaledValueValidator(com.qcadoo.model.internal.validators.UnscaledValueValidator) ScaleValidator(com.qcadoo.model.internal.validators.ScaleValidator) SampleSimpleDatabaseObject(com.qcadoo.model.beans.sample.SampleSimpleDatabaseObject) BigDecimal(java.math.BigDecimal) DataAccessTest(com.qcadoo.model.internal.DataAccessTest) Test(org.junit.Test)

Example 3 with ScaleValidator

use of com.qcadoo.model.internal.validators.ScaleValidator in project qcadoo by qcadoo.

the class ValidatorTest method shouldHasErrorsIfBigDecimalScaleAreTooShort.

@Test
public void shouldHasErrorsIfBigDecimalScaleAreTooShort() throws Exception {
    // given
    Entity entity = new DefaultEntity(dataDefinition);
    entity.setField("money", new BigDecimal("123.45"));
    fieldDefinitionMoney.withValidator(new ScaleValidator(3, null, null));
    // when
    entity = dataDefinition.save(entity);
    // then
    verify(session, never()).save(any(SampleSimpleDatabaseObject.class));
    assertFalse(entity.isValid());
}
Also used : Entity(com.qcadoo.model.api.Entity) DefaultEntity(com.qcadoo.model.internal.DefaultEntity) DefaultEntity(com.qcadoo.model.internal.DefaultEntity) ScaleValidator(com.qcadoo.model.internal.validators.ScaleValidator) SampleSimpleDatabaseObject(com.qcadoo.model.beans.sample.SampleSimpleDatabaseObject) BigDecimal(java.math.BigDecimal) DataAccessTest(com.qcadoo.model.internal.DataAccessTest) Test(org.junit.Test)

Example 4 with ScaleValidator

use of com.qcadoo.model.internal.validators.ScaleValidator in project qcadoo by qcadoo.

the class DecimalType method getMissingValidators.

@Override
public Collection<FieldHookDefinition> getMissingValidators(final Iterable<FieldHookDefinition> validators) {
    FieldHookDefinition defaultScaleValidator = new ScaleValidator(null, null, NumberService.DEFAULT_MAX_FRACTION_DIGITS_IN_DECIMAL);
    FieldHookDefinition defaultUnscaledValueValidator = new UnscaledValueValidator(null, null, NumberService.DEFAULT_MAX_INTEGER_DIGITS_IN_DECIMAL);
    Set<FieldHookDefinition> missingValidators = Sets.<FieldHookDefinition>newHashSet(defaultScaleValidator, defaultUnscaledValueValidator);
    for (FieldHookDefinition validator : validators) {
        if (validator instanceof UnscaledValueValidator && ((UnscaledValueValidator) validator).hasUppuerBoundDefined()) {
            missingValidators.remove(defaultUnscaledValueValidator);
        }
        if (validator instanceof ScaleValidator && ((ScaleValidator) validator).hasUppuerBoundDefined()) {
            missingValidators.remove(defaultScaleValidator);
        }
    }
    return missingValidators;
}
Also used : FieldHookDefinition(com.qcadoo.model.internal.api.FieldHookDefinition) UnscaledValueValidator(com.qcadoo.model.internal.validators.UnscaledValueValidator) ScaleValidator(com.qcadoo.model.internal.validators.ScaleValidator)

Example 5 with ScaleValidator

use of com.qcadoo.model.internal.validators.ScaleValidator in project qcadoo by qcadoo.

the class ValidatorTest method shouldHasNoErrorsIfBigDecimalValuePresicionAndScaleIsOk.

@Test
public void shouldHasNoErrorsIfBigDecimalValuePresicionAndScaleIsOk() throws Exception {
    // given
    Entity entity = new DefaultEntity(dataDefinition);
    entity.setField("money", new BigDecimal("123.4"));
    fieldDefinitionMoney.withValidator(new UnscaledValueValidator(null, null, 3)).withValidator(new ScaleValidator(null, null, 1));
    // when
    entity = dataDefinition.save(entity);
    // then
    verify(session).save(any(SampleSimpleDatabaseObject.class));
    assertTrue(entity.isValid());
}
Also used : Entity(com.qcadoo.model.api.Entity) DefaultEntity(com.qcadoo.model.internal.DefaultEntity) DefaultEntity(com.qcadoo.model.internal.DefaultEntity) UnscaledValueValidator(com.qcadoo.model.internal.validators.UnscaledValueValidator) ScaleValidator(com.qcadoo.model.internal.validators.ScaleValidator) SampleSimpleDatabaseObject(com.qcadoo.model.beans.sample.SampleSimpleDatabaseObject) BigDecimal(java.math.BigDecimal) DataAccessTest(com.qcadoo.model.internal.DataAccessTest) Test(org.junit.Test)

Aggregations

ScaleValidator (com.qcadoo.model.internal.validators.ScaleValidator)6 Entity (com.qcadoo.model.api.Entity)5 SampleSimpleDatabaseObject (com.qcadoo.model.beans.sample.SampleSimpleDatabaseObject)5 DataAccessTest (com.qcadoo.model.internal.DataAccessTest)5 DefaultEntity (com.qcadoo.model.internal.DefaultEntity)5 Test (org.junit.Test)5 BigDecimal (java.math.BigDecimal)4 UnscaledValueValidator (com.qcadoo.model.internal.validators.UnscaledValueValidator)3 FieldHookDefinition (com.qcadoo.model.internal.api.FieldHookDefinition)1