Package javax.validation

Examples of javax.validation.Validator


            }
        };
       
        // 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


              !cons.getConstraintsForProperty("unconstraintField").hasConstraints());
        assertNull(cons.getConstraintsForProperty("unknownField"));
    }

    public void testValidateValue() {
        Validator validator = getValidator();
        assertTrue(
              validator.validateValue(Book.class, "subtitle", "123456789098765432").isEmpty());
        assertFalse(validator.validateValue(Book.class, "subtitle",
              "123456789098765432123412345678909876543212341234564567890987654321234",
              Second.class).isEmpty());
        // tests for issue 22: validation of a field without any constraints
        assertEquals(0, validator.validateValue(Book.class, "unconstraintField", 4).size());
        // tests for issue 22: validation of unknown field cause ValidationException
        try {
            validator.validateValue(Book.class, "unknownProperty", 4);
            fail("unknownProperty not detected");
        } catch (IllegalArgumentException ex) {
            // OK
            assertEquals(
                  "unknown property 'unknownProperty' in org.apache.bval.jsr303.example.Book",
View Full Code Here

                  ex.getMessage());
        }
    }

    public void testMetadataAPI_Book() {
        Validator validator = getValidator();
        Assert.assertNotNull(validator.getConstraintsForClass(Book.class));
        Assert.assertTrue(validator.getConstraintsForClass(Book.class) ==
              validator.getConstraintsForClass(Book.class));
        BeanDescriptor bc = validator.getConstraintsForClass(Book.class);
//        assertEquals(ElementType.TYPE, bc.getElementType());
        Assert.assertEquals(Book.class, bc.getElementClass());
//        assertEquals(false, bc.isCascaded());
//        assertEquals("", bc.getPropertyPath());
        Assert.assertTrue(bc.getConstraintDescriptors() != null);
View Full Code Here

//        assertEquals("", bc.getPropertyPath());
        Assert.assertTrue(bc.getConstraintDescriptors() != null);
    }

    public void testMetadataAPI_Engine() {
        Validator validator = getValidator();
        ElementDescriptor desc = validator.getConstraintsForClass(Engine.class)
              .getConstraintsForProperty("serialNumber");
        assertNotNull(desc);
//        assertEquals(ElementType.FIELD, desc.getElementType());
        Assert.assertEquals(String.class, desc.getElementClass());
    }
View Full Code Here

//        assertEquals(ElementType.FIELD, desc.getElementType());
        Assert.assertEquals(String.class, desc.getElementClass());
    }

    public void testMetadataAPI_Address() {
        Validator validator = getValidator();
        Assert.assertFalse(validator.getConstraintsForClass(Address.class)
              .getConstraintDescriptors().isEmpty());

        Set<PropertyDescriptor> props =
              validator.getConstraintsForClass(Address.class).getConstrainedProperties();
        Set<String> propNames = new HashSet(props.size());
        for (PropertyDescriptor each : props) {
            propNames.add(each.getPropertyName());
        }
        Assert.assertTrue(propNames.contains("addressline1")); // annotated at field level
        Assert.assertTrue(propNames.contains("addressline2"));
        Assert.assertTrue(propNames.contains("zipCode"));
        Assert.assertTrue(propNames.contains("country"));
        Assert.assertTrue(propNames.contains("city"));       // annotated at method level
        Assert.assertEquals(5, props.size());

        ElementDescriptor desc = validator.getConstraintsForClass(Address.class)
              .getConstraintsForProperty("addressline1");
        Assert.assertNotNull(desc);
        boolean found = false;
        for (ConstraintDescriptor each : desc.getConstraintDescriptors()) {
            if (each.getConstraintValidatorClasses().get(0)
View Full Code Here

        Assert.assertTrue(found);

    }

    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());
        for (String msg : new String[]{"must contain alphabetical characters only",
              "must match ....-....-...."}) {
            assertNotNull(TestUtils.getViolationWithMessage(violations, msg));
        }
View Full Code Here

        MaxTestEntity entity = new MaxTestEntity();
        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);
            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

   
    /**
     * JSR-303 Section 5.1.c, IllegalArgumentException should be thrown
     */
    public void testGetConstraintsForNullClass() {
        Validator validator = getValidator();
        try {
            validator.getConstraintsForClass(null);
            Assert.fail("No exception thrown on Validator.getConstraintsForClass(null)");
        } catch (IllegalArgumentException e) {
            // Correct
            return;
        }
View Full Code Here

            } catch (UnsupportedOperationException uoe) {
                logger.info("ActivationSpec does not support validate. Implementation of validate is optional");
            }
            // 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

        if (ve != null) {
            ELContext elContext = context.getELContext();
            ValueReference vr = ValueExpressionAnalyzer.getReference(elContext, ve);
           
            if (vr != null) {
                Validator validator = requestContext.getApplicationContext().getValidatorFactory().getValidator();
                Object base = vr.getBase();
                Object property = vr.getProperty();
               
                if (base != null && property != null) {
                    BeanDescriptor beanDescriptor = validator.getConstraintsForClass(base.getClass());
                   
                    if (beanDescriptor != null) {
                        return beanDescriptor.getConstraintsForProperty(property.toString());
                    }
                }
View Full Code Here

TOP

Related Classes of javax.validation.Validator

Copyright © 2018 www.massapicom. 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.