Preloader image

Bean Validation - Design por Contrato

O Bean Validation (também conhecida como JSR 303) contém um apêndice opcional que lida com validação de método.

Algumas implementações desta JSR implementam este apêndice (Apache bval, Hibernate validator por exemplo).

O OpenEJB fornece um interceptador na qual permite você usar esta feature para fazer o design por contrato.

Design por contrato

O objetivo é ser capaz de configurar com um grão mais fino seu contrato. No exemplo você pode especificar os centímetros mínimos de um homem do esporte que deve pular no salto com vara:

@Local
public interface PoleVaultingManager {
    int points(@Min(120) int centimeters);
}

Usando

TomEE e OpenEJB não fornecem mais BeanValidationAppendixInterceptor desde que o Bean Validation 1.1 o faz (com um uso ligeiramente diferente, mas exatamente o mesmo recurso).

Então basicamente você não necessita configurar qualquer coisa para usar isto.

Se um paramentro não é validado uma exceção é lançada, é um EJBException que envolve um ConstraintViolationException:

try {
    gamesManager.addSportMan("I lose", "EN");
    fail("no space should be in names");
} catch (EJBException wrappingException) {
    assertTrue(wrappingException.getCause() instanceof ConstraintViolationException);
    ConstraintViolationException exception = ConstraintViolationException.class.cast(wrappingException.getCausedByException());
    assertEquals(1, exception.getConstraintViolations().size());
}

Exemplo

OlympicGamesManager

package org.superbiz.designbycontract;

import jakarta.ejb.Stateless;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Pattern;
import jakarta.validation.constraints.Size;

@Stateless
public class OlympicGamesManager {
    public String addSportMan(@Pattern(regexp = "^[A-Za-z]+$") String name, @Size(min = 2, max = 4) String country) {
        if (country.equals("USA")) {
            return null;
        }
        return new StringBuilder(name).append(" [").append(country).append("]").toString();
    }
}

PoleVaultingManager

package org.superbiz.designbycontract;

import jakarta.ejb.Local;
import jakarta.validation.constraints.Min;

@Local
public interface PoleVaultingManager {
    int points(@Min(120) int centimeters);
}

PoleVaultingManagerBean

package org.superbiz.designbycontract;

import jakarta.ejb.Stateless;

@Stateless
public class PoleVaultingManagerBean implements PoleVaultingManager {
    @Override
    public int points(int centimeters) {
        return centimeters - 120;
    }
}

OlympicGamesTest

public class OlympicGamesTest {
    private static Context context;

    @EJB
    private OlympicGamesManager gamesManager;

    @EJB
    private PoleVaultingManager poleVaultingManager;

    @BeforeClass
    public static void start() {
        Properties properties = new Properties();
        properties.setProperty(BeanContext.USER_INTERCEPTOR_KEY, BeanValidationAppendixInterceptor.class.getName());
        context = EJBContainer.createEJBContainer(properties).getContext();
    }

    @Before
    public void inject() throws Exception {
        context.bind("inject", this);
    }

    @AfterClass
    public static void stop() throws Exception {
        if (context != null) {
            context.close();
        }
    }

    @Test
    public void sportMenOk() throws Exception {
        assertEquals("IWin [FR]", gamesManager.addSportMan("IWin", "FR"));
    }

    @Test
    public void sportMenKoBecauseOfName() throws Exception {
        try {
            gamesManager.addSportMan("I lose", "EN");
            fail("no space should be in names");
        } catch (EJBException wrappingException) {
            assertTrue(wrappingException.getCause() instanceof ConstraintViolationException);
            ConstraintViolationException exception = ConstraintViolationException.class.cast(wrappingException.getCausedByException());
            assertEquals(1, exception.getConstraintViolations().size());
        }
    }

    @Test
    public void sportMenKoBecauseOfCountry() throws Exception {
        try {
            gamesManager.addSportMan("ILoseTwo", "TOO-LONG");
            fail("country should be between 2 and 4 characters");
        } catch (EJBException wrappingException) {
            assertTrue(wrappingException.getCause() instanceof ConstraintViolationException);
            ConstraintViolationException exception = ConstraintViolationException.class.cast(wrappingException.getCausedByException());
            assertEquals(1, exception.getConstraintViolations().size());
        }
    }

    @Test
    public void polVaulting() throws Exception {
        assertEquals(100, poleVaultingManager.points(220));
    }

    @Test
    public void tooShortPolVaulting() throws Exception {
        try {
            poleVaultingManager.points(119);
            fail("the jump is too short");
        } catch (EJBException wrappingException) {
            assertTrue(wrappingException.getCause() instanceof ConstraintViolationException);
            ConstraintViolationException exception = ConstraintViolationException.class.cast(wrappingException.getCausedByException());
            assertEquals(1, exception.getConstraintViolations().size());
        }
    }
}

Executando

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running OlympicGamesTest
Apache OpenEJB 4.0.0-beta-1    build: 20111002-04:06
http://tomee.apache.org/
INFO - openejb.home = /Users/dblevins/examples/bean-validation-design-by-contract
INFO - openejb.base = /Users/dblevins/examples/bean-validation-design-by-contract
INFO - Using 'jakarta.ejb.embeddable.EJBContainer=true'
INFO - Configuring Service(id=Default Security Service, type=SecurityService, provider-id=Default Security Service)
INFO - Configuring Service(id=Default Transaction Manager, type=TransactionManager, provider-id=Default Transaction Manager)
INFO - Found EjbModule in classpath: /Users/dblevins/examples/bean-validation-design-by-contract/target/classes
INFO - Beginning load: /Users/dblevins/examples/bean-validation-design-by-contract/target/classes
INFO - Configuring enterprise application: /Users/dblevins/examples/bean-validation-design-by-contract
INFO - Configuring Service(id=Default Stateless Container, type=Container, provider-id=Default Stateless Container)
INFO - Auto-creating a container for bean PoleVaultingManagerBean: Container(type=STATELESS, id=Default Stateless Container)
INFO - Configuring Service(id=Default Managed Container, type=Container, provider-id=Default Managed Container)
INFO - Auto-creating a container for bean OlympicGamesTest: Container(type=MANAGED, id=Default Managed Container)
INFO - Enterprise application "/Users/dblevins/examples/bean-validation-design-by-contract" loaded.
INFO - Assembling app: /Users/dblevins/examples/bean-validation-design-by-contract
INFO - Jndi(name="java:global/bean-validation-design-by-contract/PoleVaultingManagerBean!org.superbiz.designbycontract.PoleVaultingManager")
INFO - Jndi(name="java:global/bean-validation-design-by-contract/PoleVaultingManagerBean")
INFO - Jndi(name="java:global/bean-validation-design-by-contract/OlympicGamesManager!org.superbiz.designbycontract.OlympicGamesManager")
INFO - Jndi(name="java:global/bean-validation-design-by-contract/OlympicGamesManager")
INFO - Jndi(name="java:global/EjbModule236054577/OlympicGamesTest!OlympicGamesTest")
INFO - Jndi(name="java:global/EjbModule236054577/OlympicGamesTest")
INFO - Created Ejb(deployment-id=OlympicGamesManager, ejb-name=OlympicGamesManager, container=Default Stateless Container)
INFO - Created Ejb(deployment-id=PoleVaultingManagerBean, ejb-name=PoleVaultingManagerBean, container=Default Stateless Container)
INFO - Created Ejb(deployment-id=OlympicGamesTest, ejb-name=OlympicGamesTest, container=Default Managed Container)
INFO - Started Ejb(deployment-id=OlympicGamesManager, ejb-name=OlympicGamesManager, container=Default Stateless Container)
INFO - Started Ejb(deployment-id=PoleVaultingManagerBean, ejb-name=PoleVaultingManagerBean, container=Default Stateless Container)
INFO - Started Ejb(deployment-id=OlympicGamesTest, ejb-name=OlympicGamesTest, container=Default Managed Container)
INFO - Deployed Application(path=/Users/dblevins/examples/bean-validation-design-by-contract)
Tests run: 5, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.245 sec

Results :

Tests run: 5, Failures: 0, Errors: 0, Skipped: 0