Package org.springmodules.validation.bean

Examples of org.springmodules.validation.bean.BeanValidator


        person.setEmail("uri@b");
        person.setPassword("pa");
        person.setConfirmPassword("pa1");

        BindException errors = new BindException(person, "person");
        BeanValidator validator = new BeanValidator(loader);
        validator.validate(person, errors);

        assertEquals(1, errors.getGlobalErrorCount());

    }
View Full Code Here


        person.setHomeless(false);
        person.setAddress(new Address(null, "Amsterdam"));
        person.setNullableInteger(new Integer(3));

        AnnotationBeanValidationConfigurationLoader loader = new AnnotationBeanValidationConfigurationLoader();
        BeanValidator validator = new BeanValidator(loader);

        BindException errors = new BindException(person, "person");

        validator.validate(person, errors);

        assertTrue(errors.hasGlobalErrors());
        assertEquals(2, errors.getGlobalErrorCount());
        assertTrue(errors.hasFieldErrors());
        assertTrue(errors.hasFieldErrors("firstName"));
View Full Code Here

        person.setHomeless(false);
        person.setAddress(new Address(null, "Amsterdam"));
        person.setNullableInteger(new Integer(3));

        AnnotationBeanValidationConfigurationLoader loader = new AnnotationBeanValidationConfigurationLoader();
        BeanValidator validator = new BeanValidator(loader);

        BindException errors = new BindException(person, "person");

        validator.validate(person, errors);

        assertTrue(errors.hasGlobalErrors());
        assertEquals(3, errors.getGlobalErrorCount());
        assertTrue(errors.hasFieldErrors());
        assertTrue(errors.hasFieldErrors("firstName"));
View Full Code Here

        person.setHomeless(false);
        person.setAddress(new Address(null, "Amsterdam")); // (all contexts)
        person.setNullableInteger(new Integer(3)); // invalid (ctx2) - min value is 10

        AnnotationBeanValidationConfigurationLoader loader = new AnnotationBeanValidationConfigurationLoader();
        BeanValidator validator = new BeanValidator(loader);

        BindException errors = new BindException(person, "person");

        setContext("ctx1");

        validator.validate(person, errors);

        assertTrue(errors.hasGlobalErrors());
        assertEquals(2, errors.getGlobalErrorCount());
        assertTrue(errors.hasFieldErrors());
        assertTrue(errors.hasFieldErrors("firstName"));
        assertTrue(errors.hasFieldErrors("lastName"));
        assertEquals(1, errors.getFieldErrorCount("lastName"));
        assertFalse(errors.hasFieldErrors("birthday"));
        assertEquals(0, errors.getFieldErrorCount("age"));
        assertTrue(errors.hasFieldErrors("father"));
        assertTrue(errors.hasFieldErrors("mother.*"));
        assertTrue(errors.hasFieldErrors("friends"));
        assertTrue(errors.hasFieldErrors("address.street"));
        assertFalse(errors.hasFieldErrors("nullableString"));
        assertFalse(errors.hasFieldErrors("nullableInteger"));
        assertEquals("Address.street[not.null]", errors.getFieldError("address.street").getCode());

        clearContext();

        setContext("ctx2");

        errors = new BindException(person, "person");
        validator.validate(person, errors);

        assertFalse(errors.hasGlobalErrors());
        assertTrue(errors.hasFieldErrors());
        assertFalse(errors.hasFieldErrors("firstName"));
        assertFalse(errors.hasFieldErrors("lastName"));
View Full Code Here

    }

    public void testValidatorRef_WhenNotDeployedInApplicationContext() throws Exception {

        BeanValidator validator = new BeanValidator();
        validator.setConfigurationLoader(new AnnotationBeanValidationConfigurationLoader());

        TeztBean1 bean = new TeztBean1();
        BindException errors = new BindException(bean, "bean");

        try {
            validator.validate(bean, errors);
            fail("Expecting an UnsupportedOperationException for the validator was not deployed within an application context");
        } catch (UnsupportedOperationException uso) {
            // expected
        }
    }
View Full Code Here

    }

    public void testConditionRef_WhenNotDeployedInApplicationContext() throws Exception {

        BeanValidator validator = new BeanValidator();
        validator.setConfigurationLoader(new AnnotationBeanValidationConfigurationLoader());

        TeztBean2 bean = new TeztBean2();
        BindException errors = new BindException(bean, "bean");

        try {
            validator.validate(bean, errors);
            fail("Expecting an UnsupportedOperationException for the validator was not deployed within an application context");
        } catch (UnsupportedOperationException uso) {
            // expected
        }
    }
View Full Code Here

        registry.registerPropertyHandler(new JpaManyToOneAnnotationHandler());
        registry.registerPropertyHandler(new JpaOneToOneAnnotationHandler());

        AnnotationBeanValidationConfigurationLoader loader = new AnnotationBeanValidationConfigurationLoader();
        loader.setHandlerRegistry(registry);
        BeanValidator validator = new BeanValidator(loader);

        BindException errors = new BindException(entity, "entity");

        validator.validate(entity, errors);

        assertTrue(errors.hasFieldErrors());
        assertTrue(errors.hasFieldErrors("name"));
        assertTrue(errors.hasFieldErrors("text"));
        assertTrue(errors.hasFieldErrors("oneToOne"));
View Full Code Here

        person.setEmail("uri@b");
        person.setPassword("pa");
        person.setConfirmPassword("pa1");

        BindException errors = new BindException(person, "person");
        BeanValidator validator = new BeanValidator(loader);
        validator.validate(person, errors);

        assertEquals(2, errors.getGlobalErrorCount());
        assertEquals(1, errors.getFieldErrorCount("smallInteger"));
        assertEquals(1, errors.getFieldErrorCount("lastName"));
        assertEquals("Person.lastName[validateLastNameIsLongerThanTen()]", errors.getFieldError("lastName").getCode());
View Full Code Here

    public void testValidateWithContext() throws Exception {
        DefaultXmlBeanValidationConfigurationLoader loader = createLoader("PersonWithContext.vld.xml");
        BeanValidationConfiguration config = loader.loadConfiguration(Person.class);
        assertEquals(2, config.getGlobalRules().length);

        BeanValidator validator = new BeanValidator(loader);

        Person person = new Person();
        person.setFirstName("Uri");
        person.setLastName("Boness");
        person.setAge(-1);
        person.setEmail("uri@b");
        person.setPassword("pa");
        person.setConfirmPassword("pa1");

        ValidationContextUtils.setContext("ctx1");

        BindException errors = new BindException(person, "person");
        validator.validate(person, errors);

        assertEquals(1, errors.getGlobalErrorCount());
        assertFalse(errors.hasFieldErrors("smallInteger"));
        assertEquals(1, errors.getFieldErrorCount("lastName"));

        ValidationContextUtils.clearContext();

        ValidationContextUtils.setContext("ctx2");

        errors = new BindException(person, "person");
        validator.validate(person, errors);

        assertEquals(1, errors.getGlobalErrorCount());
        assertEquals(1, errors.getFieldErrorCount("smallInteger"));
        assertEquals(1, errors.getFieldErrorCount("lastName"));
        assertEquals("Person.lastName[validateLastNameIsLongerThanTen()]", errors.getFieldError("lastName").getCode());
View Full Code Here

    public void testValidationBean_WhenDeployedInApplicationContext() throws Exception {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("appCtxt.xml", getClass());
        DefaultXmlBeanValidationConfigurationLoader loader = createLoader("TestBean1.vld.xml", context);

        BeanValidator validator = new BeanValidator(loader);
       
        TestBean bean = new TestBean();
        BindException errors = new BindException(bean, "bean");
        validator.validate(bean, errors);
        assertTrue(errors.hasErrors());
        assertTrue(errors.hasFieldErrors("name"));

        bean = new TestBean("name");
        errors = new BindException(bean, "bean");
        validator.validate(bean, errors);
        assertFalse(errors.hasErrors());
    }
View Full Code Here

TOP

Related Classes of org.springmodules.validation.bean.BeanValidator

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.