Search in sources :

Example 26 with ConstraintViolationException

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

the class DDLWithoutCallbackTest method assertDatabaseConstraintViolationThrown.

private void assertDatabaseConstraintViolationThrown(Object o) {
    Session s = openSession();
    Transaction tx = s.beginTransaction();
    try {
        s.persist(o);
        s.flush();
        fail("expecting SQL constraint violation");
    } catch (PersistenceException pe) {
        final Throwable cause = pe.getCause();
        if (cause instanceof ConstraintViolationException) {
            fail("invalid object should not be validated");
        } else if (cause instanceof org.hibernate.exception.ConstraintViolationException) {
            if (getDialect().supportsColumnCheck()) {
            // expected
            } else {
                org.hibernate.exception.ConstraintViolationException cve = (org.hibernate.exception.ConstraintViolationException) cause;
                fail("Unexpected SQL constraint violation [" + cve.getConstraintName() + "] : " + cve.getSQLException());
            }
        }
    }
    tx.rollback();
    s.close();
}
Also used : Transaction(org.hibernate.Transaction) PersistenceException(javax.persistence.PersistenceException) ConstraintViolationException(javax.validation.ConstraintViolationException) Session(org.hibernate.Session)

Example 27 with ConstraintViolationException

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

the class HibernateTraversableResolverTest method testToOneAssocNotValidated.

@Test
public void testToOneAssocNotValidated() {
    Session s = openSession();
    Transaction tx = s.beginTransaction();
    Screen screen = new Screen();
    PowerSupply ps = new PowerSupply();
    ps.setPosition("1");
    ps.setPower(new BigDecimal(350));
    screen.setPowerSupply(ps);
    try {
        s.persist(screen);
        s.flush();
        fail("Associated objects should not be validated");
    } catch (ConstraintViolationException e) {
        assertEquals(1, e.getConstraintViolations().size());
        final ConstraintViolation constraintViolation = e.getConstraintViolations().iterator().next();
        assertEquals(PowerSupply.class, constraintViolation.getRootBeanClass());
    }
    tx.rollback();
    s.close();
}
Also used : Transaction(org.hibernate.Transaction) ConstraintViolation(javax.validation.ConstraintViolation) ConstraintViolationException(javax.validation.ConstraintViolationException) BigDecimal(java.math.BigDecimal) Session(org.hibernate.Session) Test(org.junit.Test)

Example 28 with ConstraintViolationException

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

the class HibernateTraversableResolverTest method testAssocInEmbeddedNotValidated.

@Test
public void testAssocInEmbeddedNotValidated() {
    Session s = openSession();
    Transaction tx = s.beginTransaction();
    Screen screen = new Screen();
    screen.setStopButton(new Button());
    screen.getStopButton().setName("STOOOOOP");
    PowerSupply ps = new PowerSupply();
    screen.setPowerSupply(ps);
    DisplayConnector conn = new DisplayConnector();
    conn.setNumber(1);
    screen.getConnectors().add(conn);
    final Display display = new Display();
    display.setBrand("dell");
    conn.setDisplay(display);
    s.persist(display);
    s.flush();
    try {
        display.setBrand(null);
        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(Display.class, constraintViolation.getRootBeanClass());
    }
    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 29 with ConstraintViolationException

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

the class BeanValidationDisabledTest 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");
    }
    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 30 with ConstraintViolationException

use of javax.validation.ConstraintViolationException in project jersey by jersey.

the class DefaultConfiguredValidator method onValidate.

// Invoked as the last validation interceptor method in the chain.
@Override
public void onValidate(final ValidationInterceptorContext ctx) {
    final Object resource = ctx.getResource();
    final Invocable resourceMethod = ctx.getInvocable();
    final Object[] args = ctx.getArgs();
    final Set<ConstraintViolation<Object>> constraintViolations = new HashSet<>();
    final BeanDescriptor beanDescriptor = getConstraintsForClass(resource.getClass());
    // Resource validation.
    if (beanDescriptor.isBeanConstrained()) {
        constraintViolations.addAll(validate(resource));
    }
    if (resourceMethod != null && configuration.getBootstrapConfiguration().isExecutableValidationEnabled()) {
        final Method handlingMethod = resourceMethod.getHandlingMethod();
        // Resource method validation - input parameters.
        final MethodDescriptor methodDescriptor = beanDescriptor.getConstraintsForMethod(handlingMethod.getName(), handlingMethod.getParameterTypes());
        if (methodDescriptor != null && methodDescriptor.hasConstrainedParameters()) {
            constraintViolations.addAll(forExecutables().validateParameters(resource, handlingMethod, args));
        }
    }
    if (!constraintViolations.isEmpty()) {
        throw new ConstraintViolationException(constraintViolations);
    }
}
Also used : Invocable(org.glassfish.jersey.server.model.Invocable) BeanDescriptor(javax.validation.metadata.BeanDescriptor) ConstraintViolation(javax.validation.ConstraintViolation) ConstraintViolationException(javax.validation.ConstraintViolationException) Method(java.lang.reflect.Method) MethodDescriptor(javax.validation.metadata.MethodDescriptor) HashSet(java.util.HashSet)

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