Preloader image

O objetivo deste exemplo é para demonstrar como os projetos maven podem ser organizados em um estilo mais real e como os testes com o OpenEJB podem se encaixar nessa estrutura.

Este exemplo pega o código moviefun básico que nós temos em muitos exemplos e o divide em dois módulos:

  • business-logic

  • business-model

Como os nomes sugerem, mantemos nossos beans @ Entity no módulo business-model e nossos beans de sessão no modelo business-logic Os testes localizados e executados a partir do módulo de lógica de negócios.

ear-testing
ear-testing/business-logic
ear-testing/business-logic/pom.xml
ear-testing/business-logic/src/main/java/org/superbiz/logic/Movies.java
ear-testing/business-logic/src/main/java/org/superbiz/logic/MoviesImpl.java
ear-testing/business-logic/src/main/resources
ear-testing/business-logic/src/main/resources/META-INF
ear-testing/business-logic/src/main/resources/META-INF/ejb-jar.xml
ear-testing/business-logic/src/test/java/org/superbiz/logic/MoviesTest.java
ear-testing/business-model
ear-testing/business-model/pom.xml
ear-testing/business-model/src/main/java/org/superbiz/model/Movie.java
ear-testing/business-model/src/main/resources/META-INF/persistence.xml
ear-testing/pom.xml

Configuração do projeto

O pom principal, aparado ao mínimo, tem a seguinte aparência:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

  <modelVersion>4.0.0</modelVersion>
  <groupId>org.superbiz</groupId>
  <artifactId>myear</artifactId>
  <version>1.1.0-SNAPSHOT</version>

  <packaging>pom</packaging>

  <modules>
    <module>business-model</module>
    <module>business-logic</module>
  </modules>

  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.apache.openejb</groupId>
        <artifactId>jakartaee-api</artifactId>
        <version>6.0-2</version>
      </dependency>
      <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.8.1</version>
      </dependency>
    </dependencies>
  </dependencyManagement>
</project>

O business-model/pom.xml é da seguinte maneira:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <parent>
    <groupId>org.superbiz</groupId>
    <artifactId>myear</artifactId>
    <version>1.1.0-SNAPSHOT</version>
  </parent>

  <modelVersion>4.0.0</modelVersion>

  <artifactId>business-model</artifactId>
  <packaging>jar</packaging>

  <dependencies>
    <dependency>
      <groupId>org.apache.openejb</groupId>
      <artifactId>jakartaee-api</artifactId>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <scope>test</scope>
    </dependency>

  </dependencies>

</project>

E finalmente, o business-logic/pom.xml que está configurado para suportar teste incorporado com o OpenEJB:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <parent>
    <groupId>org.superbiz</groupId>
    <artifactId>myear</artifactId>
    <version>1.1.0-SNAPSHOT</version>
  </parent>

  <modelVersion>4.0.0</modelVersion>

  <artifactId>business-logic</artifactId>
  <packaging>jar</packaging>

  <dependencies>
    <dependency>
      <groupId>org.superbiz</groupId>
      <artifactId>business-model</artifactId>
      <version>${project.version}</version>
    </dependency>
    <dependency>
      <groupId>org.apache.openejb</groupId>
      <artifactId>jakartaee-api</artifactId>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <scope>test</scope>
    </dependency>
    <!--
    O <scope>test</scope> garante que não seja do seu tempo de execução
     código depende de qualquer classe OpenEJB.
    -->
    <dependency>
      <groupId>org.apache.openejb</groupId>
      <artifactId>openejb-core</artifactId>
      <version>7.0.0-SNAPSHOT</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

Código de teste

O código de teste é o mesmo de sempre:

public class MoviesTest extends TestCase {

    public void test() throws Exception {
        Properties p = new Properties();
        p.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.openejb.core.LocalInitialContextFactory");

        p.put("openejb.deployments.classpath.ear", "true");

        p.put("movieDatabase", "new://Resource?type=DataSource");
        p.put("movieDatabase.JdbcDriver", "org.hsqldb.jdbcDriver");
        p.put("movieDatabase.JdbcUrl", "jdbc:hsqldb:mem:moviedb");

        p.put("movieDatabaseUnmanaged", "new://Resource?type=DataSource");
        p.put("movieDatabaseUnmanaged.JdbcDriver", "org.hsqldb.jdbcDriver");
        p.put("movieDatabaseUnmanaged.JdbcUrl", "jdbc:hsqldb:mem:moviedb");
        p.put("movieDatabaseUnmanaged.JtaManaged", "false");

        Context context = new InitialContext(p);

        Movies movies = (Movies) context.lookup("MoviesLocal");

        movies.addMovie(new Movie("Quentin Tarantino", "Reservoir Dogs", 1992));
        movies.addMovie(new Movie("Joel Coen", "Fargo", 1996));
        movies.addMovie(new Movie("Joel Coen", "The Big Lebowski", 1998));

        List<Movie> list = movies.getMovies();
        assertEquals("List.size()", 3, list.size());

        for (Movie movie : list) {
            movies.deleteMovie(movie);
        }

        assertEquals("Movies.getMovies()", 0, movies.getMovies().size());
    }
}

Executando

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running org.superbiz.logic.MoviesTest
Apache OpenEJB 7.0.0-SNAPSHOT    build: 20111002-04:06
http://tomee.apache.org/
INFO - openejb.home = /Users/dblevins/examples/ear-testing/business-logic
INFO - openejb.base = /Users/dblevins/examples/ear-testing/business-logic
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 - Configuring Service(id=movieDatabaseUnmanaged, type=Resource, provider-id=Default JDBC Database)
INFO - Configuring Service(id=movieDatabase, type=Resource, provider-id=Default JDBC Database)
INFO - Found PersistenceModule in classpath: /Users/dblevins/examples/ear-testing/business-model/target/business-model-1.0.jar
INFO - Found EjbModule in classpath: /Users/dblevins/examples/ear-testing/business-logic/target/classes
INFO - Using 'openejb.deployments.classpath.ear=true'
INFO - Beginning load: /Users/dblevins/examples/ear-testing/business-model/target/business-model-1.0.jar
INFO - Beginning load: /Users/dblevins/examples/ear-testing/business-logic/target/classes
INFO - Configuring enterprise application: /Users/dblevins/examples/ear-testing/business-logic/classpath.ear
INFO - Configuring Service(id=Default Stateful Container, type=Container, provider-id=Default Stateful Container)
INFO - Auto-creating a container for bean Movies: Container(type=STATEFUL, id=Default Stateful Container)
INFO - Configuring PersistenceUnit(name=movie-unit)
INFO - Enterprise application "/Users/dblevins/examples/ear-testing/business-logic/classpath.ear" loaded.
INFO - Assembling app: /Users/dblevins/examples/ear-testing/business-logic/classpath.ear
INFO - PersistenceUnit(name=movie-unit, provider=org.apache.openjpa.persistence.PersistenceProviderImpl) - provider time 415ms
INFO - Jndi(name=MoviesLocal) --> Ejb(deployment-id=Movies)
INFO - Jndi(name=global/classpath.ear/business-logic/Movies!org.superbiz.logic.Movies) --> Ejb(deployment-id=Movies)
INFO - Jndi(name=global/classpath.ear/business-logic/Movies) --> Ejb(deployment-id=Movies)
INFO - Created Ejb(deployment-id=Movies, ejb-name=Movies, container=Default Stateful Container)
INFO - Started Ejb(deployment-id=Movies, ejb-name=Movies, container=Default Stateful Container)
INFO - Deployed Application(path=/Users/dblevins/examples/ear-testing/business-logic/classpath.ear)
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 2.393 sec

Results :

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