Search in sources :

Example 11 with ConstraintViolationException

use of javax.validation.ConstraintViolationException in project hibernate-orm by hibernate.

the class HibernateTraversableResolverTest method testEmbeddedCollection.

@Test
public void testEmbeddedCollection() {
    Session s = openSession();
    Transaction tx = s.beginTransaction();
    Screen screen = new Screen();
    PowerSupply ps = new PowerSupply();
    screen.setPowerSupply(ps);
    DisplayConnector conn = new DisplayConnector();
    conn.setNumber(0);
    screen.getConnectors().add(conn);
    try {
        s.persist(screen);
        s.flush();
        fail("Collection of embedded objects should be validated");
    } catch (ConstraintViolationException e) {
        assertEquals(1, e.getConstraintViolations().size());
        final ConstraintViolation constraintViolation = e.getConstraintViolations().iterator().next();
        assertEquals(Screen.class, constraintViolation.getRootBeanClass());
        // toString works since hibernate validator's Path implementation works accordingly. Should do a Path comparison though
        assertEquals("connectors[].number", constraintViolation.getPropertyPath().toString());
    }
    tx.rollback();
    s.close();
}
Also used : Transaction(org.hibernate.Transaction) ConstraintViolation(javax.validation.ConstraintViolation) ConstraintViolationException(javax.validation.ConstraintViolationException) Session(org.hibernate.Session) Test(org.junit.Test)

Example 12 with ConstraintViolationException

use of javax.validation.ConstraintViolationException in project hibernate-orm by hibernate.

the class BeanValidationAutoTest method testListeners.

@Test
public void testListeners() {
    CupHolder ch = new CupHolder();
    ch.setRadius(new BigDecimal("12"));
    Session s = openSession();
    Transaction tx = s.beginTransaction();
    try {
        s.persist(ch);
        s.flush();
        fail("invalid object should not be persisted");
    } catch (ConstraintViolationException e) {
        assertEquals(1, e.getConstraintViolations().size());
    }
    tx.rollback();
    s.close();
}
Also used : Transaction(org.hibernate.Transaction) ConstraintViolationException(javax.validation.ConstraintViolationException) BigDecimal(java.math.BigDecimal) Session(org.hibernate.Session) Test(org.junit.Test)

Example 13 with ConstraintViolationException

use of javax.validation.ConstraintViolationException in project hibernate-orm by hibernate.

the class BeanValidationGroupsTest method testListeners.

@Test
public void testListeners() {
    CupHolder ch = new CupHolder();
    ch.setRadius(new BigDecimal("12"));
    Session s = openSession();
    Transaction tx = s.beginTransaction();
    try {
        s.persist(ch);
        s.flush();
    } catch (ConstraintViolationException e) {
        fail("invalid object should not be validated");
    }
    try {
        ch.setRadius(null);
        s.flush();
    } catch (ConstraintViolationException e) {
        fail("invalid object should not be validated");
    }
    try {
        s.delete(ch);
        s.flush();
        fail("invalid object should not be persisted");
    } catch (ConstraintViolationException e) {
        assertEquals(1, e.getConstraintViolations().size());
        // TODO - seems this explicit case is necessary with JDK 5 (at least on Mac). With Java 6 there is no problem
        Annotation annotation = e.getConstraintViolations().iterator().next().getConstraintDescriptor().getAnnotation();
        assertEquals(NotNull.class, annotation.annotationType());
    }
    tx.rollback();
    s.close();
}
Also used : Transaction(org.hibernate.Transaction) ConstraintViolationException(javax.validation.ConstraintViolationException) NotNull(javax.validation.constraints.NotNull) BigDecimal(java.math.BigDecimal) Annotation(java.lang.annotation.Annotation) Session(org.hibernate.Session) Test(org.junit.Test)

Example 14 with ConstraintViolationException

use of javax.validation.ConstraintViolationException in project hibernate-orm by hibernate.

the class HibernateTraversableResolverTest method testNonLazyAssocFieldWithConstraintsFailureExpected.

@Test
public void testNonLazyAssocFieldWithConstraintsFailureExpected() {
    Session s = openSession();
    Transaction tx = s.beginTransaction();
    Screen screen = new Screen();
    screen.setPowerSupply(null);
    try {
        s.persist(screen);
        s.flush();
        fail("@NotNull on a non lazy association is not evaluated");
    } catch (ConstraintViolationException e) {
        assertEquals(1, e.getConstraintViolations().size());
    }
    tx.rollback();
    s.close();
}
Also used : Transaction(org.hibernate.Transaction) ConstraintViolationException(javax.validation.ConstraintViolationException) Session(org.hibernate.Session) Test(org.junit.Test)

Example 15 with ConstraintViolationException

use of javax.validation.ConstraintViolationException in project apex-core by apache.

the class CustomControlTupleTest method testApp.

public void testApp(StreamingApplication app) throws Exception {
    try {
        LocalMode lma = LocalMode.newInstance();
        Configuration conf = new Configuration(false);
        lma.prepareDAG(app, conf);
        LocalMode.Controller lc = lma.getController();
        ((StramLocalCluster) lc).setExitCondition(new Callable<Boolean>() {

            @Override
            public Boolean call() throws Exception {
                return endApp;
            }
        });
        // runs for 20 seconds and quits if terminating condition not reached
        lc.run(200000);
        LOG.info("Control Tuples received {} expected {}", numControlTuples, controlIndex);
        Assert.assertTrue("Incorrect Control Tuples", numControlTuples == controlIndex);
    } catch (ConstraintViolationException e) {
        Assert.fail("constraint violations: " + e.getConstraintViolations());
    }
}
Also used : Configuration(org.apache.hadoop.conf.Configuration) LocalMode(com.datatorrent.api.LocalMode) ConstraintViolationException(javax.validation.ConstraintViolationException) ConstraintViolationException(javax.validation.ConstraintViolationException)

Aggregations

ConstraintViolationException (javax.validation.ConstraintViolationException)37 Test (org.junit.Test)26 ConstraintViolation (javax.validation.ConstraintViolation)12 Session (org.hibernate.Session)11 Transaction (org.hibernate.Transaction)11 LogicalPlan (com.datatorrent.stram.plan.logical.LogicalPlan)9 BigDecimal (java.math.BigDecimal)7 HashSet (java.util.HashSet)5 Method (java.lang.reflect.Method)4 Set (java.util.Set)4 ValidationException (javax.validation.ValidationException)4 Validator (javax.validation.Validator)4 RpcException (com.alibaba.dubbo.rpc.RpcException)3 RecoverableInputOperator (com.datatorrent.stram.engine.RecoverableInputOperator)3 Date (java.util.Date)3 ValidationParameter (com.alibaba.dubbo.examples.validation.api.ValidationParameter)2 ValidationService (com.alibaba.dubbo.examples.validation.api.ValidationService)2 LocalMode (com.datatorrent.api.LocalMode)2 GenericOperator (com.datatorrent.stram.engine.GenericNodeTest.GenericOperator)2 List (java.util.List)2