Search in sources :

Example 1 with ConstraintDeclarationException

use of javax.validation.ConstraintDeclarationException in project validators by xlate.

the class ExpressionValidatorTest method testNonBooleanInWhenCondition.

@Test
void testNonBooleanInWhenCondition() {
    Mockito.when(annotation.value()).thenReturn("self.earlier lt self.later");
    Mockito.when(annotation.when()).thenReturn("'0'");
    Map<String, Date> data = new HashMap<>();
    data.put("earlier", new Date(1));
    data.put("later", new Date());
    target.initialize(annotation);
    ConstraintDeclarationException ex = assertThrows(ConstraintDeclarationException.class, () -> {
        target.isValid(data, context);
    });
    Assertions.assertTrue(ex.getMessage().contains("`'0'` does not evaluate to Boolean"));
}
Also used : HashMap(java.util.HashMap) ConstraintDeclarationException(javax.validation.ConstraintDeclarationException) Date(java.util.Date) Test(org.junit.jupiter.api.Test)

Example 2 with ConstraintDeclarationException

use of javax.validation.ConstraintDeclarationException in project validators by xlate.

the class JdbcStatementValidatorTest method testSetParametersSQLException.

@Test
void testSetParametersSQLException() throws SQLException {
    TestSetParametersSQLException self = new TestSetParametersSQLException();
    String[] parameters = { "self" };
    PreparedStatement statement = Mockito.mock(PreparedStatement.class);
    Mockito.doThrow(java.sql.SQLException.class).when(statement).setObject(1, self);
    ELProcessor processor = new ELProcessor();
    processor.defineBean("self", self);
    ConstraintDeclarationException ex = assertThrows(ConstraintDeclarationException.class, () -> {
        target.setParameters(processor, parameters, statement);
    });
    Throwable cause = ex.getCause();
    assertNotNull(cause);
    assertEquals(java.sql.SQLException.class, cause.getClass());
}
Also used : ConstraintDeclarationException(javax.validation.ConstraintDeclarationException) ELProcessor(javax.el.ELProcessor) PreparedStatement(java.sql.PreparedStatement) Test(org.junit.jupiter.api.Test)

Example 3 with ConstraintDeclarationException

use of javax.validation.ConstraintDeclarationException in project validators by xlate.

the class DateTimeValidator method initialize.

@Override
public void initialize(DateTime constraintAnnotation) {
    formatterType = constraintAnnotation.parserType();
    final DateTime annotation = constraintAnnotation;
    final String[] patterns = annotation.patterns();
    if (patterns.length == 0) {
        throw new ConstraintDeclarationException("At least one DateFormat pattern must be provided.");
    }
    if (formatterType == ParserType.JAVA_TEXT) {
        formatters = Arrays.stream(patterns).map(p -> toJavaTextDateFormat(p, annotation)).collect(Collectors.toList());
    } else {
        formatters = Arrays.stream(patterns).map(p -> toJavaTimeDateTimeFormatter(p, annotation)).collect(Collectors.toList());
    }
}
Also used : ConstraintDeclarationException(javax.validation.ConstraintDeclarationException) DateTime(io.xlate.validation.constraints.DateTime)

Example 4 with ConstraintDeclarationException

use of javax.validation.ConstraintDeclarationException in project validators by xlate.

the class DateTimeValidatorTest method testMissingPatterns.

@Test
void testMissingPatterns() {
    Mockito.when(annotation.patterns()).thenReturn(new String[0]);
    ConstraintDeclarationException ex = assertThrows(ConstraintDeclarationException.class, () -> {
        target.initialize(annotation);
    });
    assertEquals("At least one DateFormat pattern must be provided.", ex.getMessage());
}
Also used : ConstraintDeclarationException(javax.validation.ConstraintDeclarationException) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 5 with ConstraintDeclarationException

use of javax.validation.ConstraintDeclarationException in project validators by xlate.

the class DateTimeValidatorTest method testInvalidPattern.

@ParameterizedTest
@ValueSource(strings = { "JAVA_TEXT", "JAVA_TIME" })
void testInvalidPattern(DateTime.ParserType type) {
    Mockito.when(annotation.parserType()).thenReturn(type);
    Mockito.when(annotation.patterns()).thenReturn(new String[] { " NOT A VALID DATE PATTERN " });
    ConstraintDeclarationException ex = assertThrows(ConstraintDeclarationException.class, () -> {
        target.initialize(annotation);
    });
    assertTrue(ex.getMessage().startsWith("Invalid format pattern "));
}
Also used : ConstraintDeclarationException(javax.validation.ConstraintDeclarationException) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

ConstraintDeclarationException (javax.validation.ConstraintDeclarationException)7 Test (org.junit.jupiter.api.Test)4 PreparedStatement (java.sql.PreparedStatement)2 ELProcessor (javax.el.ELProcessor)2 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)2 DateTime (io.xlate.validation.constraints.DateTime)1 DateFormat (java.text.DateFormat)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 ValueSource (org.junit.jupiter.params.provider.ValueSource)1