Package javax.validation

Examples of javax.validation.Validator.validate()


        // Create a validator with this factory
        ApacheValidatorConfiguration customConfig = Validation.byProvider(ApacheValidationProvider.class).configure().constraintValidatorFactory(customFactory);
        Validator validator = customConfig.buildValidatorFactory().getValidator();
       
        try {
            validator.validate(new Person());
            fail("ValidationException must be thrown when factory returns a null constraint validator.");
        } catch (ValidationException e) {
            // correct
        }
    }
View Full Code Here


    public void testValidateMultiValuedConstraints() {
        Validator validator = getValidator();
        Engine engine = new Engine();
        engine.serialNumber = "abcd-defg-0123";
        Set<ConstraintViolation<Engine>> violations;
        violations = validator.validate(engine);
        assertEquals(0, violations.size());

        engine.serialNumber = "!)/(/()";
        violations = validator.validate(engine);
        assertEquals(2, violations.size());
View Full Code Here

        Set<ConstraintViolation<Engine>> violations;
        violations = validator.validate(engine);
        assertEquals(0, violations.size());

        engine.serialNumber = "!)/(/()";
        violations = validator.validate(engine);
        assertEquals(2, violations.size());
        for (String msg : new String[]{"must contain alphabetical characters only",
              "must match ....-....-...."}) {
            assertNotNull(TestUtils.getViolationWithMessage(violations, msg));
        }
View Full Code Here

        entity.setText("101");
        entity.setProperty("201");
        entity.setLongValue(301);
        entity.setDecimalValue(new BigDecimal(401));
        Validator validator = getValidator();
        Set<ConstraintViolation<MaxTestEntity>> violations = validator.validate(entity);
        assertEquals(4, violations.size());

        NoValidatorTestEntity entity2 = new NoValidatorTestEntity();
        try {
            validator.validate(entity2);
View Full Code Here

        Set<ConstraintViolation<MaxTestEntity>> violations = validator.validate(entity);
        assertEquals(4, violations.size());

        NoValidatorTestEntity entity2 = new NoValidatorTestEntity();
        try {
            validator.validate(entity2);
            fail("UnexpectedTypeException expected but not thrown");
        } catch (UnexpectedTypeException ex) {
            // we expected this
            assertEquals("No validator could be found for type java.lang.Object. " +
                  "See: @Max at private java.lang.Object " +
View Full Code Here

            }
            // also try validating using Bean Validation if there is a Validator available in the context.
            try {
                Validator validator = (Validator)beanContext.getJndiContext().lookup("comp/Validator");

                Set generalSet = validator.validate(activationSpec);
                if (!generalSet.isEmpty()) {
                    throw new ConstraintViolationException("Constraint violation for ActivationSpec " + activationSpecClass.getName(), generalSet);
                }
            } catch (NamingException e) {
                logger.debug("No Validator bound to JNDI context");
View Full Code Here

  public final void testEntityValidation() throws Exception {
    final TestEntity e = getTestEntity();
    final ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
    final Validator validator = factory.getValidator();
    assert validator != null;
    final Set<ConstraintViolation<TestEntity>> invalids = validator.validate(e);
    for(final ConstraintViolation<TestEntity> em : invalids) {
      logger.debug("prop: " + em.getPropertyPath() + ", msg: " + em.getMessage());
    }
  }
View Full Code Here

  public final void testEntityValidation() throws Exception {
    final TestEntity e = getTestEntity();
    final ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
    final Validator validator = factory.getValidator();
    assert validator != null;
    final Set<ConstraintViolation<TestEntity>> invalids = validator.validate(e);
    for(final ConstraintViolation<TestEntity> em : invalids) {
      logger.debug("prop: " + em.getPropertyPath() + ", msg: " + em.getMessage());
    }
  }
View Full Code Here

    }

    @Test
    public void testValidationUsingExpressionLanguage() {
        Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
        Set<ConstraintViolation<TestBean>> violations = validator.validate(new TestBean());

        assertEquals(1, violations.size());
        assertEquals("'Bob' is too short, it should at least be 5 characters long.", violations.iterator().next().getMessage());
    }
View Full Code Here


    @Test
    public void testScriptAssert() throws NamingException, SQLException {
        Validator validator = (Validator) new InitialContext().lookup("java:comp/Validator");
        final Set<ConstraintViolation<ScriptAssertBean>> result = validator.validate(new ScriptAssertBean());
        Assert.assertEquals(1, result.size());
    }

    /**
     * Returns true if this testcase is expected to pass against the current Java runtime. Else returns false.
View Full Code Here

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.