Package javax.validation

Examples of javax.validation.Validator


        interpolator = new ResourceBundleMessageInterpolator(clientDefaultMessages);
      }
      localesMap.put(clientLocale.toString(), interpolator);
    }

    Validator validator = factory.usingContext().messageInterpolator(interpolator).getValidator();
   
    Set<ConstraintViolation<ActionSupport>> constraintViolations = validator.validate(actionAs);
   
    List<Path> invalidFieldNames = new ArrayList<Path>();
    Map parameters = ActionContext.getContext().getParameters();
    for (ConstraintViolation<ActionSupport> constrantViolation : constraintViolations) {
      Path fieldPath = constrantViolation.getPropertyPath();
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

    /**
     * Check that a {@link ConstraintDefinitionException} is thrown when the
     * only available validator is associated with a different annotation type.
     */
    public void testInvalidValidator() {
        Validator validator = getValidator();
        try {
            validator.validate(new Person());
            fail("No exception thrown, but no valid validator available.");
        } catch (ConstraintDefinitionException e) {
            // correct
        }
    }
View Full Code Here

        Assert.assertEquals(Group.DEFAULT, gseq.get(0));
        Assert.assertEquals(new Group(GClass1.class), gseq.get(1));
    }

    public void testGroups() {
        Validator validator = getValidator();

        Author author = new Author();
        author.setLastName("");
        author.setFirstName("");
        Book book = new Book();
        book.setTitle("");
        book.setAuthor(author);

        Set<ConstraintViolation<Book>> constraintViolations =
              validator.validate(book, First.class, Second.class, Last.class);
        assertEquals("Wrong number of constraints", 3, constraintViolations.size());
        assertNotNull(TestUtils.getViolation(constraintViolations, "title"));
        assertNotNull(TestUtils.getViolation(constraintViolations, "author.firstName"));
        assertNotNull(TestUtils.getViolation(constraintViolations, "author.lastName"));

        author.setFirstName("Gavin");
        author.setLastName("King");

        constraintViolations = validator.validate(book, First.class, Second.class, Last.class);
        ConstraintViolation constraintViolation = constraintViolations.iterator().next();
        assertEquals(1, constraintViolations.size());
        assertEquals("may not be empty", constraintViolation.getMessage());
        assertEquals(book, constraintViolation.getRootBean());
        assertEquals(book.getTitle(), constraintViolation.getInvalidValue());
        assertEquals("title", constraintViolation.getPropertyPath().toString());

        book.setTitle("My fault");
        book.setSubtitle("confessions of a president - a book for a nice price");

        constraintViolations = validator.validate(book, First.class, Second.class, Last.class);
        assertEquals(1, constraintViolations.size());
        constraintViolation = constraintViolations.iterator().next();
        assertEquals("size must be between 0 and 30", constraintViolation.getMessage());
        assertEquals(book, constraintViolation.getRootBean());
        assertEquals(book.getSubtitle(), constraintViolation.getInvalidValue());
        assertEquals("subtitle", constraintViolation.getPropertyPath().toString());

        book.setSubtitle("Capitalism in crisis");
        author.setCompany("1234567890ß9876543212578909876542245678987432");

        constraintViolations = validator.validate(book);
        constraintViolation = constraintViolations.iterator().next();
        assertEquals(1, constraintViolations.size());
        assertEquals("size must be between 0 and 40", constraintViolation.getMessage());
        assertEquals(book, constraintViolation.getRootBean());
        assertEquals(author.getCompany(), constraintViolation.getInvalidValue());
        assertEquals("author.company", constraintViolation.getPropertyPath().toString());

        author.setCompany("apache");

        constraintViolations = validator.validate(book, First.class, Second.class, Last.class);
        assertEquals(0, constraintViolations.size());
    }
View Full Code Here

        constraintViolations = validator.validate(book, First.class, Second.class, Last.class);
        assertEquals(0, constraintViolations.size());
    }

    public void testGroupSequence() {
        Validator validator = getValidator();

        Author author = new Author();
        author.setLastName("");
        author.setFirstName("");
        Book book = new Book();
        book.setAuthor(author);

        Set<ConstraintViolation<Book>> constraintViolations =
              validator.validate(book, Book.All.class);
        assertEquals(2, constraintViolations.size());

        author.setFirstName("Kelvin");
        author.setLastName("Cline");

        constraintViolations = validator.validate(book, Book.All.class);
        ConstraintViolation constraintViolation = constraintViolations.iterator().next();
        assertEquals(1, constraintViolations.size());
        assertEquals("may not be null", constraintViolation.getMessage());
        assertEquals(book, constraintViolation.getRootBean());
        assertEquals(book.getTitle(), constraintViolation.getInvalidValue());
        assertEquals("title", constraintViolation.getPropertyPath().toString());

        book.setTitle("247307892430798789024389798789");
        book.setSubtitle("f43u rlök fjöq3liu opiur ölw3kj rölkj d");

        constraintViolations = validator.validate(book, Book.All.class);
        assertEquals(1, constraintViolations.size());
    }
View Full Code Here

    }

    public void testAccessStrategies_field_method() {
        AccessTestBusinessObject o1 = new AccessTestBusinessObject("1");
        AccessTestBusinessObjectSub o2 = new AccessTestBusinessObjectSub("3");
        Validator validator = getValidator();
        Set<ConstraintViolation<AccessTestBusinessObject>> errors =
              validator.validate(o1);
        assertTrue(errors.isEmpty());
        Set<ConstraintViolation<AccessTestBusinessObjectSub>> errors2 =
              validator.validate(o2);
        assertTrue(errors2.isEmpty());

        o2 = new AccessTestBusinessObjectSub("1");
        errors2 = validator.validate(o2);
        assertEquals(1, errors2.size());

        // assert, that getvar2() and getVar2() are both validated with their getter method
        o2 = new AccessTestBusinessObjectSub("3");
        o2.setVar2("1");
        o2.setvar2("2");
        errors2 = validator.validate(o2);
        assertEquals(2, errors2.size());

        o2.setvar2("5");
        o2.setVar2("6");
        errors2 = validator.validate(o2);
        assertEquals(0, errors2.size());

        o2.setvar2("5");
        o2.setVar2("-1");
        errors2 = validator.validate(o2);
        assertEquals(1, errors2.size());
    }
View Full Code Here

     * Check that when there is one constraint failure in one of the groups in
     * a sequence, validation stops.
     * JSR-303: 3.4.3
     */
    public void testValidationStopsWhenFailuresOnGroup() {
        Validator validator = getValidator();
       
        // Validate Dummy with its redefined Default group
        Set<ConstraintViolation<Dummy>> violations = validator.validate(new Dummy());
        assertEquals("Only 1 violation expected", 1, violations.size());
        ConstraintViolation<Dummy> violation = violations.iterator().next();
        assertEquals("Group1 should be evaluated first", "field1", violation.getPropertyPath().toString());
    }
View Full Code Here

    public void testAccessStrategies_on_children() {
        AccessTestBusinessObject o1 = new AccessTestBusinessObject("1");
        AccessTestBusinessObject o2 = new AccessTestBusinessObject("2");
        o1.next(o2);
        Validator validator = getValidator();
        Set<ConstraintViolation<AccessTestBusinessObject>> errors =
              validator.validate(o1);
        // assert, that field access 'next' is used and not getNext() is called!!!
        assertEquals(1, errors.size());
        o2 = new AccessTestBusinessObject("1");
        o1.next(o2);
        errors = validator.validate(o1);
        assertEquals(0, errors.size());

        // assert that toBeIgnored not validated, because not annotated with @Valid
        o1.setToBeIgnored(new AccessTestBusinessObject("99"));
        errors = validator.validate(o1);
        assertEquals(0, errors.size());

        o1.setNext(new AccessTestBusinessObject("99"));
        errors = validator.validate(o1);
        assertEquals(1, errors.size());
    }
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.