Package javax.validation.metadata

Examples of javax.validation.metadata.BeanDescriptor


            ValueExpression valueExpression = ((UIComponent) input).getValueExpression("value");
            if (valueExpression != null) {
                ValueExpressionAnalyzer valueExpressionAnalyzer = new ValueExpressionAnalyzer(valueExpression);
                ValueReference vref = valueExpressionAnalyzer.getValueReference(context.getELContext());
                if (vref != null) { // valueExpressionAnalyzer can return a null value. The condition prevents a NPE
                    BeanDescriptor constraintsForClass = validator.getConstraintsForClass(vref.getBase().getClass());
                    PropertyDescriptor d = constraintsForClass.getConstraintsForProperty((String) vref.getProperty());
                    return (d != null) && d.hasConstraints();
                }
            }
            return false;
        }
View Full Code Here


        Assert.assertTrue(!validator.validateProperty(object, "title").isEmpty());
    }*/

    public void testPropertyDescriptorHasConstraints() {
        BeanDescriptor cons = getValidator().getConstraintsForClass(Book.class);
        assertTrue(cons.getConstraintsForProperty("author").hasConstraints());
        assertTrue(cons.getConstraintsForProperty("title").hasConstraints());
        assertTrue(cons.getConstraintsForProperty("uselessField").hasConstraints());
        // cons.getConstraintsForProperty("unconstraintField") == null without Introspector
        // cons.getConstraintsForProperty("unconstraintField") != null with Introspector
        assertTrue(cons.getConstraintsForProperty("unconstraintField") == null ||
              !cons.getConstraintsForProperty("unconstraintField").hasConstraints());
        assertNull(cons.getConstraintsForProperty("unknownField"));
    }
View Full Code Here

    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);
        TestUtils.failOnModifiable(bc.getConstraintDescriptors(), "beanDescriptor constraintDescriptors");
    }
View Full Code Here

     * Checks the correct parsing of a constraint with an array of constraints
     * as attributes.
     */
    public void testCustomAttributes() {
        Validator validator = getValidator();
        BeanDescriptor constraints = validator.getConstraintsForClass(Person.class);
        Set<ConstraintDescriptor<?>> ageConstraints = constraints.getConstraintsForProperty("age").getConstraintDescriptors();
       
        Assert.assertEquals("There should be 2 constraints in 'age'", ageConstraints.size(), 2);
        for ( ConstraintDescriptor<?> cd : ageConstraints ) {
            Assert.assertEquals("Annotation should be @Min", cd.getAnnotation().annotationType().getName(), Min.class.getName());
        }
View Full Code Here

        Assert.assertEquals(0, iv.size());
    }

    public void testMetadataAPI() {
        Validator bookValidator = getValidator();
        BeanDescriptor bookBeanDescriptor =
              bookValidator.getConstraintsForClass(Book.class);

        // expect no constraints on Book's Class-Level
        Assert.assertFalse(bookBeanDescriptor.hasConstraints());
        // but there are constraints on Book's Property-Level
        Assert.assertTrue(bookBeanDescriptor.isBeanConstrained());
        Assert.assertTrue(
              bookBeanDescriptor.getConstraintDescriptors().size() == 0); //no constraint
        //more specifically "author" and "title"
        Assert.assertEquals(4, bookBeanDescriptor.getConstrainedProperties().size());
        //not a property
        Assert.assertTrue(
              bookBeanDescriptor.getConstraintsForProperty("doesNotExist") == null);
        //property with no constraint
        Assert.assertTrue(
              bookBeanDescriptor.getConstraintsForProperty("description") == null);
        PropertyDescriptor propertyDescriptor =
              bookBeanDescriptor.getConstraintsForProperty("title");
        Assert.assertEquals(2, propertyDescriptor.getConstraintDescriptors().size());
        Assert.assertTrue("title".equals(propertyDescriptor.getPropertyName()));
        //assuming the implementation returns the NotEmpty constraint first
        Iterator<ConstraintDescriptor<?>> iter =
              propertyDescriptor.getConstraintDescriptors().iterator();
        ConstraintDescriptor constraintDescriptor = null;
        while (iter.hasNext()) {
            constraintDescriptor = iter.next();
            if (constraintDescriptor.getAnnotation().annotationType()
                  .equals(NotNull.class)) {
                break;
            }

        }
        Assert.assertTrue(constraintDescriptor != null);
        Assert.assertTrue(constraintDescriptor.getGroups().size() == 1); //"first"
        Assert.assertEquals(NotNullValidator.class,
              constraintDescriptor.getConstraintValidatorClasses().get(0));
        //assuming the implementation returns the Size constraint first
        propertyDescriptor = bookBeanDescriptor.getConstraintsForProperty("subtitle");
        Iterator<ConstraintDescriptor<?>> iterator =
              propertyDescriptor.getConstraintDescriptors().iterator();
        constraintDescriptor = iterator.next();
        Assert.assertTrue(
              constraintDescriptor.getAnnotation().annotationType().equals(Size.class));
        Assert.assertTrue(
              ((Integer) constraintDescriptor.getAttributes().get("max")) == 30);
        Assert.assertTrue(constraintDescriptor.getGroups().size() == 1);
        propertyDescriptor = bookBeanDescriptor.getConstraintsForProperty("author");
        Assert.assertTrue(propertyDescriptor.getConstraintDescriptors().size() == 1);
        Assert.assertTrue(propertyDescriptor.isCascaded());
    }
View Full Code Here

     * {@link ElementDescriptor#getElementClass()} work as defined in the spec.
     */
    public void testElementDescriptorGetElementClass() {
        Validator validator = getValidator();
       
        BeanDescriptor beanDescriptor = validator.getConstraintsForClass( Person.class );
        Assert.assertEquals("Incorrect class returned", Person.class, beanDescriptor.getElementClass());
       
        PropertyDescriptor nameDescriptor = beanDescriptor.getConstraintsForProperty("name");
        Assert.assertEquals("Incorrect class returned", String.class, nameDescriptor.getElementClass());
    }
View Full Code Here

     * {@link BeanDescriptor#getConstraintsForProperty(String)} with an invalid
     * property name.
     */
    public void testGetConstraintsForInvalidProperty() {
        Validator validator = getValidator();
        BeanDescriptor personDescriptor = validator.getConstraintsForClass(Person.class);
       
        try {
            personDescriptor.getConstraintsForProperty(null);
            fail("No exception thrown when calling getConstraintsForProperty with null property");
        } catch (IllegalArgumentException e) {
            // Correct
        }
       
        try {
            personDescriptor.getConstraintsForProperty("");
            fail("No exception thrown when calling getConstraintsForProperty with empty property");
        } catch (IllegalArgumentException e) {
            // Correct
        }
    }
View Full Code Here

        }

        // Initialize Bean Validation.
        final ValidatorFactory validatorFactory = createValidatorFactory(context);
        final javax.validation.Validator validator = createValidator(validatorFactory);
        final BeanDescriptor beanDescriptor = validator.getConstraintsForClass(valueBaseClass);
        if (!beanDescriptor.isBeanConstrained())
        {
            return;
        }
       
        // Note that validationGroupsArray was initialized when createValidator was called
View Full Code Here

        String valueProperty = (String) referenceProperty;

        // Initialize Bean Validation.
        ValidatorFactory validatorFactory = createValidatorFactory(context);
        javax.validation.Validator validator = createValidator(validatorFactory);
        BeanDescriptor beanDescriptor = validator.getConstraintsForClass(valueBaseClass);
        if (!beanDescriptor.isBeanConstrained())
        {
            return;
        }
       
        // Note that validationGroupsArray was initialized when createValidator was called
View Full Code Here

        return Collections.emptySet();
    }

    public static ElementDescriptor getElementDescriptor(Class targetClass, String property)
    {
        BeanDescriptor beanDescriptor = ExtValBeanValidationContext.getCurrentInstance().getValidatorFactory()
                .getValidator().getConstraintsForClass(targetClass);

        return beanDescriptor.getConstraintsForProperty(property);
    }
View Full Code Here

TOP

Related Classes of javax.validation.metadata.BeanDescriptor

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.