Package javax.validation

Examples of javax.validation.Validator


        errors = validator.validate(o1);
        assertEquals(1, errors.size());
    }

    public void testBook() {
        Validator validator = getValidator();
        Author author = new Author();
        author.setLastName("Baudelaire");
        author.setFirstName("");
        Book book = new Book();
        book.setAuthor(author);
        book.setSubtitle("12345678900125678901234578901234567890");

        // NotEmpty failure on the title field
        Set<ConstraintViolation<Book>> errors = validator.validate(book, Book.All.class);
        Assert.assertTrue(!errors.isEmpty());

        book.setTitle("Les fleurs du mal");
        author.setCompany("Some random publisher with a very very very long name");
View Full Code Here


        a.getAddresses().add(adr);

        a.setFirstName("Karl");
        a.setLastName("May");

        Validator v = getValidator();
        Set found = v.validate(a, Default.class, First.class, Last.class);
        Assert.assertTrue(!found.isEmpty());
        Assert.assertEquals(4, found.size());

        adr.setCity("Berlin");
        adr.setZipCode("12345");
        adr.setCompany("apache");
        found = v.validate(a, Default.class, First.class, Last.class);
        Assert.assertEquals(1, found.size());
        ConstraintViolation ic = (ConstraintViolation) found.iterator().next();
        Assert.assertEquals("addresses[0].country.name", ic.getPropertyPath().toString());
    }
View Full Code Here

        Assert.assertTrue(!getValidator().validate(foo).isEmpty());
        // check that no nullpointer exception gets thrown
    }

    public void testGroups() {
        Validator validator = getValidator();
        Author author = new Author();
        author.setCompany("ACME");
        Book book = new Book();
        book.setTitle("");
        book.setAuthor(author);
        boolean foundTitleConstraint = false;
        Set<ConstraintViolation<Book>> constraintViolations =
              validator.validate(book, Book.All.class);
        assertEquals(1, constraintViolations.size());
        //assuming an english locale, the interpolated message is returned
        for (ConstraintViolation constraintViolation : constraintViolations) {
            if (constraintViolation.getRootBean().getClass() == Book.class) {
                Assert.assertEquals(
View Full Code Here

        ad.setCity("error");
        ad.setZipCode("error");
        ad.setAddressline1("something");
        ad.setCountry(new Country());
        ad.getCountry().setName("something");
        Validator v = getValidator();
        Set<ConstraintViolation<Address>> violations = v.validate(ad);
        Assert.assertEquals(2, violations.size());
        for (ConstraintViolation each : violations) {
            Assert.assertTrue(each.getMessage().endsWith(" not OK"));
        }
        assertNotNull(TestUtils.getViolation(violations, "city"));
View Full Code Here

    public void testValidateNestedPropertyPath() throws InvocationTargetException,
          NoSuchMethodException, IllegalAccessException {
        final String propPath = "addresses[0].country.ISO2Code";

        Validator v = getValidator();
        Author author = new Author();
        author.setAddresses(new ArrayList());
        Address adr = new Address();
        author.getAddresses().add(adr);
        Country country = new Country();
        adr.setCountry(country);
        country.setISO2Code("too_long");

        Set<ConstraintViolation<Author>> iv = v.validateProperty(author, propPath);
        Assert.assertEquals(1, iv.size());
        country.setISO2Code("23");
        iv = v.validateProperty(author, propPath);
        Assert.assertEquals(0, iv.size());
        iv = v.validateValue(Author.class, propPath, "345");
        Assert.assertEquals(1, iv.size());
        iv = v.validateValue(Author.class, propPath, "34");
        Assert.assertEquals(0, iv.size());
    }
View Full Code Here

        iv = v.validateValue(Author.class, propPath, "34");
        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());
View Full Code Here

    /**
     * Checks that the correct exception is thrown when validating a bean whose
     * getter throws an exception.
     */
    public void testExceptionThrowingBean() {
        Validator validator = getValidator();
        try {
            validator.validate(new ExceptionThrowingBean());
            Assert.fail("No exception thrown when validating a bean whose getter throws a RTE");
        } catch (ValidationException e) {
            // Correct
        }
    }
View Full Code Here

    /**
     * Checks that an {@link IllegalArgumentException} is thrown when passing
     * <code>null</code> as group array.
     */
    public void testValidateNullGroup() {
        Validator validator = getValidator();
        try {
            Class<?>[] groups = null;
            validator.validate(new String(), groups);
            Assert.fail("No exception thrown when passing null as group array");
        } catch (IllegalArgumentException e) {
            // Correct
        }
    }
View Full Code Here

    /**
     * Checks that an {@link IllegalArgumentException} is thrown when passing an
     * invalid property name.
     */
    public void testValidateInvalidPropertyName() {
        Validator validator = getValidator();

        // Null propertyName
        try {
            validator.validateProperty(new Person(), null);
        } catch (IllegalArgumentException e) {
            // Correct
        }

        // Empty propertyName
        try {
            validator.validateProperty(new Person(), "");
        } catch (IllegalArgumentException e) {
            // Correct
        }

        // Invalid propertyName
        try {
            validator.validateProperty(new Person(), "surname");
        } catch (IllegalArgumentException e) {
            // Correct
        }

    }
View Full Code Here

    /**
     * Checks that an {@link IllegalArgumentException} is thrown when trying to
     * validate a property on a null object.
     */
    public void testValidatePropertyOnNullBean() {
        Validator validator = getValidator();
        try {
            validator.validateProperty(null, "class");
        } catch (IllegalArgumentException e) {
            // Correct
        }
    }
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.