[jboss-cvs] JBossAS SVN: r104900 - in projects/ejb-book/trunk: chxx-employeeregistry and 20 other directories.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Mon May 17 23:52:59 EDT 2010
Author: ALRubinger
Date: 2010-05-17 23:52:58 -0400 (Mon, 17 May 2010)
New Revision: 104900
Added:
projects/ejb-book/trunk/chxx-employeeregistry/
projects/ejb-book/trunk/chxx-employeeregistry/pom.xml
projects/ejb-book/trunk/chxx-employeeregistry/src/
projects/ejb-book/trunk/chxx-employeeregistry/src/main/
projects/ejb-book/trunk/chxx-employeeregistry/src/main/java/
projects/ejb-book/trunk/chxx-employeeregistry/src/main/java/org/
projects/ejb-book/trunk/chxx-employeeregistry/src/main/java/org/jboss/
projects/ejb-book/trunk/chxx-employeeregistry/src/main/java/org/jboss/ejb3/
projects/ejb-book/trunk/chxx-employeeregistry/src/main/java/org/jboss/ejb3/examples/
projects/ejb-book/trunk/chxx-employeeregistry/src/main/java/org/jboss/ejb3/examples/employeeregistry/
projects/ejb-book/trunk/chxx-employeeregistry/src/main/java/org/jboss/ejb3/examples/employeeregistry/entity/
projects/ejb-book/trunk/chxx-employeeregistry/src/main/java/org/jboss/ejb3/examples/employeeregistry/entity/Employee.java
projects/ejb-book/trunk/chxx-employeeregistry/src/main/resources/
projects/ejb-book/trunk/chxx-employeeregistry/src/main/resources/persistence.xml
projects/ejb-book/trunk/chxx-employeeregistry/src/test/
projects/ejb-book/trunk/chxx-employeeregistry/src/test/java/
projects/ejb-book/trunk/chxx-employeeregistry/src/test/java/org/
projects/ejb-book/trunk/chxx-employeeregistry/src/test/java/org/jboss/
projects/ejb-book/trunk/chxx-employeeregistry/src/test/java/org/jboss/ejb3/
projects/ejb-book/trunk/chxx-employeeregistry/src/test/java/org/jboss/ejb3/examples/
projects/ejb-book/trunk/chxx-employeeregistry/src/test/java/org/jboss/ejb3/examples/employeeregistry/
projects/ejb-book/trunk/chxx-employeeregistry/src/test/java/org/jboss/ejb3/examples/employeeregistry/EmployeeIntegrationTest.java
projects/ejb-book/trunk/chxx-employeeregistry/src/test/resources/
projects/ejb-book/trunk/testsupport/src/main/java/org/jboss/ejb3/examples/testsupport/dbquery/EntityManagerExposingBean.java
projects/ejb-book/trunk/testsupport/src/main/java/org/jboss/ejb3/examples/testsupport/dbquery/EntityManagerExposingLocalBusiness.java
Removed:
projects/ejb-book/trunk/testsupport/src/main/java/org/jboss/ejb3/examples/testsupport/dbquery/DbQueryBean.java
projects/ejb-book/trunk/testsupport/src/main/java/org/jboss/ejb3/examples/testsupport/dbquery/DbQueryLocalBusiness.java
Modified:
projects/ejb-book/trunk/chxx-poker/pom.xml
projects/ejb-book/trunk/chxx-poker/src/test/java/org/jboss/ejb3/examples/chxx/transactions/TransactionalPokerGameIntegrationTest.java
projects/ejb-book/trunk/pom.xml
Log:
[EJBBOOK-27] Added EntityManager integration tests to show CRUD operations on a POJO view
Property changes on: projects/ejb-book/trunk/chxx-employeeregistry
___________________________________________________________________
Name: svn:ignore
+ target
.settings
.classpath
.project
bin
Added: projects/ejb-book/trunk/chxx-employeeregistry/pom.xml
===================================================================
--- projects/ejb-book/trunk/chxx-employeeregistry/pom.xml (rev 0)
+++ projects/ejb-book/trunk/chxx-employeeregistry/pom.xml 2010-05-18 03:52:58 UTC (rev 104900)
@@ -0,0 +1,197 @@
+<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 Information -->
+ <parent>
+ <groupId>org.jboss.ejb3.examples</groupId>
+ <artifactId>jboss-ejb3-examples-build</artifactId>
+ <version>0.0.1-SNAPSHOT</version>
+ <relativePath>../pom.xml</relativePath>
+ </parent>
+
+ <!-- Model Version -->
+ <modelVersion>4.0.0</modelVersion>
+
+ <!-- Artifact Information -->
+ <artifactId>jboss-ejb3-examples-chxx-employeeregistry</artifactId>
+ <name>JBoss EJB 3.x Examples - Chapter XX: Employee Registry EJBs</name>
+ <description>Example to accompany O'Reilly "Enterprise Java Beans 6th Edition" Chapter XX</description>
+
+ <!-- Build -->
+ <build>
+ </build>
+
+
+ <!-- Properties -->
+ <properties>
+
+ <!-- Versioning -->
+ <!-- JBOSS_HOME (We'll unpack into here -->
+ <JBOSS_HOME>${project.build.directory}/jboss-${version.org.jboss.jbossas}</JBOSS_HOME>
+
+ </properties>
+
+ <!-- Dependencies -->
+ <dependencies>
+
+ <!--
+ <dependency>
+ <groupId>org.jboss.spec.javax.ejb</groupId>
+ <artifactId>jboss-ejb-api_3.1_spec</artifactId>
+ <scope>provided</scope>
+ </dependency>
+ -->
+
+ <!-- Examples Support -->
+ <dependency>
+ <groupId>${project.groupId}</groupId>
+ <artifactId>jboss-ejb3-examples-testsupport</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+
+
+ <!--
+
+ Test Dependencies
+ -->
+
+
+
+ <!-- Arquillian Test Harness -->
+ <dependency>
+ <groupId>org.jboss.arquillian.container</groupId>
+ <artifactId>arquillian-jbossas-embedded-60</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.jboss.arquillian</groupId>
+ <artifactId>arquillian-junit</artifactId>
+ </dependency>
+
+ <!-- JUnit -->
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ </dependency>
+
+ <!-- JBossAS -->
+ <dependency>
+ <groupId>org.jboss.jbossas</groupId>
+ <artifactId>jboss-as-depchain</artifactId>
+ <version>${version.org.jboss.jbossas}</version>
+ <type>pom</type>
+ <scope>compile</scope> <!-- Should be "test", but I can't find the GAV for JPA APIs -->
+ </dependency>
+
+ </dependencies>
+
+ <!--
+ We also need to place the AS depchain into
+ the "dependencyManagement" section in import scope
+ so that Maven respects the "exclusion" elements
+ configured
+ -->
+ <dependencyManagement>
+ <dependencies>
+
+ <!-- org.jboss.jbossas -->
+ <dependency>
+ <groupId>org.jboss.jbossas</groupId>
+ <artifactId>jboss-as-depchain</artifactId>
+ <version>${version.org.jboss.jbossas}</version>
+ <type>pom</type>
+ <scope>import</scope>
+ </dependency>
+ </dependencies>
+
+ </dependencyManagement>
+
+ <profiles>
+
+ <profile>
+
+ <!-- Declare the "Integration Test" Profile -->
+ <id>it</id>
+ <activation>
+ <activeByDefault>true</activeByDefault>
+ </activation>
+
+ <build>
+
+ <plugins>
+
+ <!-- Get AS and put into "target" -->
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-dependency-plugin</artifactId>
+ <executions>
+ <execution>
+ <id>unpack</id>
+ <phase>pre-integration-test</phase> <!-- So run before testing -->
+ <goals>
+ <goal>unpack</goal>
+ </goals>
+ <configuration>
+ <artifactItems>
+ <artifactItem>
+ <groupId>org.jboss.jbossas</groupId>
+ <artifactId>jboss-as-distribution</artifactId>
+ <version>${version.org.jboss.jbossas}</version>
+ <type>zip</type>
+ <overWrite>false</overWrite>
+ <outputDirectory>${project.build.directory}</outputDirectory>
+ </artifactItem>
+ </artifactItems>
+ </configuration>
+ </execution>
+ </executions>
+ </plugin>
+
+
+ <!-- Surefire -->
+ <plugin>
+ <artifactId>maven-surefire-plugin</artifactId>
+ <executions>
+ <execution>
+ <id>integration-test</id>
+ <phase>integration-test</phase>
+ <goals>
+ <goal>test</goal>
+ </goals>
+ <configuration>
+ <additionalClasspathElements>
+ <additionalClasspathElement>${JBOSS_HOME}/client/jbossws-native-client.jar</additionalClasspathElement>
+ </additionalClasspathElements>
+
+ <redirectTestOutputToFile>true</redirectTestOutputToFile>
+ <trimStackTrace>false</trimStackTrace>
+ <printSummary>true</printSummary>
+ <includes>
+ <include>**/*IntegrationTest.java</include>
+ </includes>
+ <forkMode>always</forkMode>
+ <!--
+ MaxPermSize Required to bump the space for relective
+ data like classes, methods, etc. EMB-41, EMB-74. Endorsed
+ required for things like WS support (EMB-61)
+
+ Disable assertions because something is not playing nicely in Hibernate:
+ (19:58:01,172 ERROR [org.jboss.kernel.plugins.dependency.AbstractKernelController]
+ Error installing to Start: name=persistence.unit:unitName=#tempdb state=Create: java.lang.AssertionError
+ at org.hibernate.cfg.InheritanceState.getElementsToProcess(
+ InheritanceState.java:222) [hibernate-annotations-3.5.0-CR-1.jar:3.5.0-CR-1])
+ -->
+ <argLine>-disableassertions -Xmx512m -XX:MaxPermSize=256m -Djava.net.preferIPv4Stack=true -Djava.util.logging.manager=org.jboss.logmanager.LogManager -Djava.endorsed.dirs=${JBOSS_HOME}/lib/endorsed -Djboss.home=${JBOSS_HOME} -Djboss.boot.server.log.dir=${JBOSS_HOME}</argLine>
+ </configuration>
+ </execution>
+ </executions>
+ </plugin>
+
+ </plugins>
+
+ </build>
+
+ </profile>
+
+ </profiles>
+
+</project>
Property changes on: projects/ejb-book/trunk/chxx-employeeregistry/pom.xml
___________________________________________________________________
Name: svn:executable
+ *
Added: projects/ejb-book/trunk/chxx-employeeregistry/src/main/java/org/jboss/ejb3/examples/employeeregistry/entity/Employee.java
===================================================================
--- projects/ejb-book/trunk/chxx-employeeregistry/src/main/java/org/jboss/ejb3/examples/employeeregistry/entity/Employee.java (rev 0)
+++ projects/ejb-book/trunk/chxx-employeeregistry/src/main/java/org/jboss/ejb3/examples/employeeregistry/entity/Employee.java 2010-05-18 03:52:58 UTC (rev 104900)
@@ -0,0 +1,123 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2010, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.ejb3.examples.employeeregistry.entity;
+
+import javax.persistence.Entity;
+import javax.persistence.Id;
+
+/**
+ * Represents an Employee in the system. Modeled as a simple
+ * value object with some additional EJB and JPA annotations.
+ *
+ * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
+ * @version $Revision: $
+ */
+ at Entity
+// Mark that we're an Entity Bean, EJB's integration point
+// with Java Persistence
+public class Employee
+{
+
+ //-------------------------------------------------------------------------------------||
+ // Instance Members -------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * Primary key of this entity
+ */
+ @Id
+ // Mark that this field is the primary key
+ private Long id;
+
+ /**
+ * Name of the employee
+ */
+ private String name;
+
+ //-------------------------------------------------------------------------------------||
+ // Constructor ------------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * Default constructor, required by JPA
+ */
+ public Employee()
+ {
+
+ }
+
+ /**
+ * Convenience constructor
+ */
+ public Employee(final long id, final String name)
+ {
+ // Set
+ this.id = id;
+ this.name = name;
+ }
+
+ //-------------------------------------------------------------------------------------||
+ // Accessors / Mutators ---------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * @return the id
+ */
+ public Long getId()
+ {
+ return id;
+ }
+
+ /**
+ * @param id the id to set
+ */
+ public void setId(final Long id)
+ {
+ this.id = id;
+ }
+
+ /**
+ * @return the name
+ */
+ public String getName()
+ {
+ return name;
+ }
+
+ /**
+ * @param name the name to set
+ */
+ public void setName(final String name)
+ {
+ this.name = name;
+ }
+
+ /**
+ * {@inheritDoc}
+ * @see java.lang.Object#toString()
+ */
+ @Override
+ public String toString()
+ {
+ return "Employee [id=" + id + ", name=" + name + "]";
+ }
+}
Added: projects/ejb-book/trunk/chxx-employeeregistry/src/main/resources/persistence.xml
===================================================================
--- projects/ejb-book/trunk/chxx-employeeregistry/src/main/resources/persistence.xml (rev 0)
+++ projects/ejb-book/trunk/chxx-employeeregistry/src/main/resources/persistence.xml 2010-05-18 03:52:58 UTC (rev 104900)
@@ -0,0 +1,19 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<persistence xmlns="http://java.sun.com/xml/ns/persistence"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
+ http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
+ version="1.0">
+ <persistence-unit name="tempdb">
+ <jta-data-source>java:/DefaultDS</jta-data-source>
+ <properties>
+ <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
+ <!--
+
+ You can enable this for Hibernate to dump SQL output to STDOUT
+ <property name="hibernate.show_sql" value="true"/>
+
+ -->
+ </properties>
+ </persistence-unit>
+</persistence>
Added: projects/ejb-book/trunk/chxx-employeeregistry/src/test/java/org/jboss/ejb3/examples/employeeregistry/EmployeeIntegrationTest.java
===================================================================
--- projects/ejb-book/trunk/chxx-employeeregistry/src/test/java/org/jboss/ejb3/examples/employeeregistry/EmployeeIntegrationTest.java (rev 0)
+++ projects/ejb-book/trunk/chxx-employeeregistry/src/test/java/org/jboss/ejb3/examples/employeeregistry/EmployeeIntegrationTest.java 2010-05-18 03:52:58 UTC (rev 104900)
@@ -0,0 +1,374 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2010, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.ejb3.examples.employeeregistry;
+
+import java.util.concurrent.Callable;
+import java.util.logging.Logger;
+
+import javax.naming.Context;
+import javax.naming.InitialContext;
+import javax.persistence.EntityManager;
+
+import org.jboss.arquillian.api.Deployment;
+import org.jboss.arquillian.api.RunMode;
+import org.jboss.arquillian.api.RunModeType;
+import org.jboss.arquillian.junit.Arquillian;
+import org.jboss.ejb3.examples.employeeregistry.entity.Employee;
+import org.jboss.ejb3.examples.testsupport.dbquery.EntityManagerExposingBean;
+import org.jboss.ejb3.examples.testsupport.dbquery.EntityManagerExposingLocalBusiness;
+import org.jboss.ejb3.examples.testsupport.txwrap.TaskExecutionException;
+import org.jboss.ejb3.examples.testsupport.txwrap.TxWrappingBean;
+import org.jboss.ejb3.examples.testsupport.txwrap.TxWrappingLocalBusiness;
+import org.jboss.shrinkwrap.api.ShrinkWrap;
+import org.jboss.shrinkwrap.api.spec.JavaArchive;
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+/**
+ *
+ *
+ * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
+ * @version $Revision: $
+ */
+ at RunWith(Arquillian.class)
+ at RunMode(RunModeType.LOCAL)
+public class EmployeeIntegrationTest
+{
+
+ //-------------------------------------------------------------------------------------||
+ // Class Members ----------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+ // Class Members ----------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * Logger
+ */
+ private static final Logger log = Logger.getLogger(EmployeeIntegrationTest.class.getName());
+
+ /**
+ * Naming Context
+ * @deprecated Remove when Arquillian will inject the EJB proxies
+ */
+ @Deprecated
+ private static Context jndiContext;
+
+ /**
+ * The Deployment into the EJB Container
+ */
+ @Deployment
+ public static JavaArchive getDeployment()
+ {
+ final JavaArchive archive = ShrinkWrap.create("test.jar", JavaArchive.class).addPackages(true,
+ Employee.class.getPackage()).addManifestResource("persistence.xml").addPackages(false,
+ TxWrappingLocalBusiness.class.getPackage(), EntityManagerExposingBean.class.getPackage());
+ log.info(archive.toString(true));
+ return archive;
+ }
+
+ /*
+ * Data for our tests
+ */
+
+ private static final long ID_DAVE = 1L;
+
+ private static final long ID_JOSH = 2L;
+
+ private static final long ID_RICK = 3L;
+
+ private static final String NAME_DAVE = "Dave";
+
+ private static final String NAME_DAVE_NEW = "Dave - The Good Doctor";
+
+ private static final String NAME_JOSH = "Josh";
+
+ private static final String NAME_RICK = "Rick, Jr.";
+
+ //-------------------------------------------------------------------------------------||
+ // Instance Members -------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * EJB which wraps supplied {@link Callable} instances inside of a new Tx
+ */
+ // TODO: Support Injection of @EJB here when Arquillian for Embedded JBossAS will support it
+ private TxWrappingLocalBusiness txWrapper;
+
+ /**
+ * EJB which provides direct access to an {@link EntityManager}'s method for use in testing.
+ * Must be called inside an existing Tx so that returned entities are not detached.
+ */
+ // TODO: Support Injection of @EJB here when Arquillian for Embedded JBossAS will support it
+ private EntityManagerExposingLocalBusiness emHook;
+
+ //-------------------------------------------------------------------------------------||
+ // Lifecycle --------------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * Performs suite-wide initialization
+ */
+ @BeforeClass
+ public static void init() throws Exception
+ {
+ // After the server is up, we don't need to pass any explicit properties
+ jndiContext = new InitialContext();
+ }
+
+ /**
+ * Manually looks up EJBs in JNDI and assigns them
+ */
+ @Before
+ public void injectEjbsAndClearDB() throws Throwable
+ {
+ // Fake injection by doing manual lookups for the time being
+ //TODO Deprecated portion
+ txWrapper = (TxWrappingLocalBusiness) jndiContext.lookup(TxWrappingBean.class.getSimpleName() + "/local");
+ emHook = (EntityManagerExposingLocalBusiness) jndiContext.lookup(EntityManagerExposingBean.class.getSimpleName()
+ + "/local");
+
+ // Clear all employees before running, just in case
+ this.clearAllEmployees();
+ }
+
+ /**
+ * Issues a deletion to remove all employees from persistent storage
+ * @throws Throwable
+ */
+ @After
+ public void clearAllEmployees() throws Throwable
+ {
+ // Clear the DB of all Employees
+ try
+ {
+ txWrapper.wrapInTx(new Callable<Void>()
+ {
+
+ @Override
+ public Void call() throws Exception
+ {
+ // EJB QL String to remove all Employees
+ final EntityManager em = emHook.getEntityManager();
+ em.createQuery("DELETE FROM " + Employee.class.getSimpleName() + " o").executeUpdate();
+ return null;
+ }
+
+ });
+ }
+ catch (final TaskExecutionException tee)
+ {
+ // Unwrap
+ throw tee.getCause();
+ }
+ }
+
+ //-------------------------------------------------------------------------------------||
+ // Tests ------------------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * Tests that we can use the {@link EntityManager} to perform simple
+ * CRUD (Create, remove, update, delete) operations on an object view,
+ * and these changes will be persisted as expected.
+ */
+ @Test
+ public void persistAndModifyEmployees() throws Throwable
+ {
+
+ try
+ {
+
+ // Execute the addition of the employees, and conditional checks, in the context of a Transaction
+ txWrapper.wrapInTx(new Callable<Void>()
+ {
+
+ @Override
+ public Void call() throws Exception
+ {
+ // Create a few plain instances
+ final Employee josh = new Employee(ID_DAVE, NAME_DAVE);
+ final Employee dave = new Employee(ID_JOSH, NAME_JOSH);
+ final Employee rick = new Employee(ID_RICK, NAME_RICK);
+
+ // Get the EntityManager from our test hook
+ final EntityManager em = emHook.getEntityManager();
+
+ // Now first check if any employees are found in the underlying persistent
+ // storage (shouldn't be)
+ Assert
+ .assertNull("Employees should not have been added to the EM yet", em.find(Employee.class, ID_DAVE));
+
+ // Check if the object is managed (shouldn't be)
+ Assert.assertFalse("Employee should not be managed yet", em.contains(josh));
+
+ // Now persist the employees
+ em.persist(dave);
+ em.persist(josh);
+ em.persist(rick);
+ log.info("Added: " + rick + dave + josh);
+
+ // The employees should be managed now
+ Assert.assertTrue("Employee should be managed now, after call to persist", em.contains(josh));
+
+ // Return
+ return null;
+ }
+ });
+
+ // Now change Employee Dave's name in a Tx; we'll verify the changes were flushed to the DB later
+ txWrapper.wrapInTx(new Callable<Void>()
+ {
+
+ @Override
+ public Void call() throws Exception
+ {
+ // Get an EM
+ final EntityManager em = emHook.getEntityManager();
+
+ // Look up "Dave" by ID from the EM
+ final Employee dave = em.find(Employee.class, ID_DAVE);
+
+ // Change Dave's name
+ dave.setName(NAME_DAVE_NEW);
+ log.info("Changing Dave's name: " + dave);
+
+ // That's it - the new name should be flushed to the DB when the Tx completes
+ return null;
+ }
+ });
+
+ // Since we've changed Dave's name in the last transaction, ensure that we see the changes
+ // have been flushed and we can see them from a new Tx.
+ txWrapper.wrapInTx(new Callable<Void>()
+ {
+
+ @Override
+ public Void call() throws Exception
+ {
+ // Get an EM
+ final EntityManager em = emHook.getEntityManager();
+
+ // Look up "Dave" again
+ final Employee dave = em.find(Employee.class, ID_DAVE);
+
+ // Ensure we see the name change
+ Assert.assertEquals("Employee Dave's name should have been changed", NAME_DAVE_NEW, dave.getName());
+
+ // Now we'll detach Dave from the EM, this makes the object no longer managed
+ em.detach(dave);
+
+ // Change Dave's name again to some dummy value. Because the object is
+ // detached and no longer managed, we should not see this new value
+ // synchronized with the DB
+ dave.setName("A name we shouldn't see flushed to persistence");
+ log.info("Changing Dave's name after detached: " + dave);
+
+ // Return
+ return null;
+ }
+ });
+
+ // Another check. We changed Dave's name when the entity was no longer
+ // managed and attached to an EM, so ensure that any changes we made
+ // were not flushed out
+ txWrapper.wrapInTx(new Callable<Void>()
+ {
+
+ @Override
+ public Void call() throws Exception
+ {
+ // Get an EM
+ final EntityManager em = emHook.getEntityManager();
+
+ // Look up "Dave" again by ID from the EM
+ final Employee dave = em.find(Employee.class, ID_DAVE);
+ log.info("Lookup of Dave after we changed his name on a detached instance: " + dave);
+
+ // Ensure that the last name change we gave to Dave did not take affect
+ Assert
+ .assertEquals("Detached object values should not have been flushed", NAME_DAVE_NEW, dave.getName());
+
+ // Return
+ return null;
+
+ }
+ });
+
+ // Uh oh, Rick has decided to leave the company. Let's delete his record.
+ txWrapper.wrapInTx(new Callable<Void>()
+ {
+
+ @Override
+ public Void call() throws Exception
+ {
+ // Get an EM
+ final EntityManager em = emHook.getEntityManager();
+
+ // Look up Rick
+ final Employee rick = em.find(Employee.class, ID_RICK);
+
+ // Remove
+ em.remove(rick);
+ log.info("Deleted: " + rick);
+
+ // Return
+ return null;
+
+ }
+ });
+
+ // Ensure we can no longer find Rick in the DB
+ txWrapper.wrapInTx(new Callable<Void>()
+ {
+
+ @Override
+ public Void call() throws Exception
+ {
+ // Get an EM
+ final EntityManager em = emHook.getEntityManager();
+
+ // Look up Rick
+ final Employee rick = em.find(Employee.class, ID_RICK);
+
+ // Assert
+ Assert.assertNull("Rick should have been removed from the DB", rick);
+
+ // Return
+ return null;
+
+ }
+ });
+
+ }
+ catch (final TaskExecutionException tee)
+ {
+ // Unwrap
+ throw tee.getCause();
+ }
+
+ }
+}
Modified: projects/ejb-book/trunk/chxx-poker/pom.xml
===================================================================
--- projects/ejb-book/trunk/chxx-poker/pom.xml 2010-05-18 02:17:49 UTC (rev 104899)
+++ projects/ejb-book/trunk/chxx-poker/pom.xml 2010-05-18 03:52:58 UTC (rev 104900)
@@ -43,17 +43,17 @@
<scope>provided</scope>
</dependency>
-->
- <!--
-
- Test Dependencies
- -->
-
+
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>jboss-ejb3-examples-testsupport</artifactId>
<version>${project.version}</version>
- <scope>test</scope>
</dependency>
+
+ <!--
+
+ Test Dependencies
+ -->
<!-- Arquillian Test Harness -->
<dependency>
Modified: projects/ejb-book/trunk/chxx-poker/src/test/java/org/jboss/ejb3/examples/chxx/transactions/TransactionalPokerGameIntegrationTest.java
===================================================================
--- projects/ejb-book/trunk/chxx-poker/src/test/java/org/jboss/ejb3/examples/chxx/transactions/TransactionalPokerGameIntegrationTest.java 2010-05-18 02:17:49 UTC (rev 104899)
+++ projects/ejb-book/trunk/chxx-poker/src/test/java/org/jboss/ejb3/examples/chxx/transactions/TransactionalPokerGameIntegrationTest.java 2010-05-18 03:52:58 UTC (rev 104900)
@@ -44,11 +44,12 @@
import org.jboss.ejb3.examples.chxx.transactions.impl.BankBean;
import org.jboss.ejb3.examples.chxx.transactions.impl.PokerServiceConstants;
import org.jboss.ejb3.examples.testsupport.dbinit.DbInitializerLocalBusiness;
-import org.jboss.ejb3.examples.testsupport.dbquery.DbQueryBean;
-import org.jboss.ejb3.examples.testsupport.dbquery.DbQueryLocalBusiness;
+import org.jboss.ejb3.examples.testsupport.dbquery.EntityManagerExposingBean;
+import org.jboss.ejb3.examples.testsupport.dbquery.EntityManagerExposingLocalBusiness;
import org.jboss.ejb3.examples.testsupport.entity.IdentityBase;
import org.jboss.ejb3.examples.testsupport.txwrap.ForcedTestException;
import org.jboss.ejb3.examples.testsupport.txwrap.TaskExecutionException;
+import org.jboss.ejb3.examples.testsupport.txwrap.TxWrappingBean;
import org.jboss.ejb3.examples.testsupport.txwrap.TxWrappingLocalBusiness;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
@@ -97,7 +98,7 @@
BankLocalBusiness.class.getPackage(), User.class.getPackage()).addManifestResource("persistence.xml")
.addPackages(false, DbInitializerBean.class.getPackage(), TxWrappingLocalBusiness.class.getPackage(),
BankBean.class.getPackage(), DbInitializerLocalBusiness.class.getPackage(),
- DbQueryBean.class.getPackage(), IdentityBase.class.getPackage());
+ EntityManagerExposingBean.class.getPackage(), IdentityBase.class.getPackage());
log.info(archive.toString(true));
return archive;
}
@@ -123,7 +124,7 @@
* Must be called inside an existing Tx so that returned entities are not detached.
*/
// TODO: Support Injection of @EJB here when Arquillian for Embedded JBossAS will support it
- private DbQueryLocalBusiness db;
+ private EntityManagerExposingLocalBusiness emHook;
/**
* Bank EJB Proxy
@@ -160,9 +161,11 @@
public void injectEjbs() throws Exception
{
// Fake injection by doing manual lookups for the time being
- dbInitializer = (DbInitializerLocalBusiness) jndiContext.lookup("DbInitializerBean/local");
- txWrapper = (TxWrappingLocalBusiness) jndiContext.lookup("TxWrappingBean/local");
- db = (DbQueryLocalBusiness) jndiContext.lookup("DbQueryBean/local");
+ dbInitializer = (DbInitializerLocalBusiness) jndiContext.lookup(DbInitializerBean.class.getSimpleName()
+ + "/local");
+ txWrapper = (TxWrappingLocalBusiness) jndiContext.lookup(TxWrappingBean.class.getSimpleName() + "/local");
+ emHook = (EntityManagerExposingLocalBusiness) jndiContext.lookup(EntityManagerExposingBean.class.getSimpleName()
+ + "/local");
bank = (BankLocalBusiness) jndiContext.lookup(BankLocalBusiness.JNDI_NAME);
pokerGame = (PokerGameLocalBusiness) jndiContext.lookup(PokerGameLocalBusiness.JNDI_NAME);
}
@@ -403,7 +406,7 @@
@Override
public Void call() throws Exception
{
- final Account account = db.find(Account.class, accountId);
+ final Account account = emHook.getEntityManager().find(Account.class, accountId);
Assert.assertTrue("Balance was not as expected", expectedBalance.compareTo(account.getBalance()) == 0);
return null;
}
Modified: projects/ejb-book/trunk/pom.xml
===================================================================
--- projects/ejb-book/trunk/pom.xml 2010-05-18 02:17:49 UTC (rev 104899)
+++ projects/ejb-book/trunk/pom.xml 2010-05-18 03:52:58 UTC (rev 104900)
@@ -25,6 +25,7 @@
<module>chxx-interceptors</module>
<module>chxx-poker</module>
<module>chxx-timer</module>
+ <module>chxx-employeeregistry</module>
<module>testsupport</module>
</modules>
Deleted: projects/ejb-book/trunk/testsupport/src/main/java/org/jboss/ejb3/examples/testsupport/dbquery/DbQueryBean.java
===================================================================
--- projects/ejb-book/trunk/testsupport/src/main/java/org/jboss/ejb3/examples/testsupport/dbquery/DbQueryBean.java 2010-05-18 02:17:49 UTC (rev 104899)
+++ projects/ejb-book/trunk/testsupport/src/main/java/org/jboss/ejb3/examples/testsupport/dbquery/DbQueryBean.java 2010-05-18 03:52:58 UTC (rev 104900)
@@ -1,85 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source.
- * Copyright 2010, Red Hat Middleware LLC, and individual contributors
- * as indicated by the @author tags. See the copyright.txt file in the
- * distribution for a full listing of individual contributors.
- *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
- */
-package org.jboss.ejb3.examples.testsupport.dbquery;
-
-import javax.ejb.Local;
-import javax.ejb.Stateless;
-import javax.ejb.TransactionAttribute;
-import javax.ejb.TransactionAttributeType;
-import javax.persistence.EntityManager;
-import javax.persistence.PersistenceContext;
-
-/**
- * Implementation of a test EJB which exposes generic database
- * query operations directly via the {@link EntityManager}.
- * Used in validating pre- and postconditions during testing.
- * All methods will be executed in an existing Transaction, which
- * is {@link TransactionAttributeType#MANDATORY}.
- *
- * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
- * @version $Revision: $
- */
- at Stateless
- at Local(DbQueryLocalBusiness.class)
- at TransactionAttribute(TransactionAttributeType.MANDATORY)
-// We make a current Tx mandatory from the caller so that the
-// caller is sure to get back an entity instance which is still attached. This way we can run any tests/checks
-// on it without fear that we'll run into Exceptions. We can use a TxWrappingBean to submit the test logic and
-// execute it in the context of a new Transaction. David Blevins has a writeup of this technique:
-// http://openejb.apache.org/3.0/testing-transactions-example.html
-public class DbQueryBean implements DbQueryLocalBusiness
-{
-
- //-------------------------------------------------------------------------------------||
- // Instance Members -------------------------------------------------------------------||
- //-------------------------------------------------------------------------------------||
-
- /**
- * Underlying hook to persistence
- */
- @PersistenceContext
- private EntityManager em;
-
- //-------------------------------------------------------------------------------------||
- // Required Implementations -----------------------------------------------------------||
- //-------------------------------------------------------------------------------------||
-
- /**
- * @see org.jboss.ejb3.examples.testsupport.dbquery.DbQueryLocalBusiness#find(java.lang.Class, java.lang.Object)
- */
- @Override
- public <T> T find(final Class<T> type, final Object id) throws IllegalArgumentException
- {
- // Precondition checks
- if (type == null)
- {
- throw new IllegalArgumentException("type must be specified");
- }
- if (id == null)
- {
- throw new IllegalArgumentException("id must be specified");
- }
-
- // Find
- return em.find(type, id);
- }
-}
Deleted: projects/ejb-book/trunk/testsupport/src/main/java/org/jboss/ejb3/examples/testsupport/dbquery/DbQueryLocalBusiness.java
===================================================================
--- projects/ejb-book/trunk/testsupport/src/main/java/org/jboss/ejb3/examples/testsupport/dbquery/DbQueryLocalBusiness.java 2010-05-18 02:17:49 UTC (rev 104899)
+++ projects/ejb-book/trunk/testsupport/src/main/java/org/jboss/ejb3/examples/testsupport/dbquery/DbQueryLocalBusiness.java 2010-05-18 03:52:58 UTC (rev 104900)
@@ -1,51 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source.
- * Copyright 2010, Red Hat Middleware LLC, and individual contributors
- * as indicated by the @author tags. See the copyright.txt file in the
- * distribution for a full listing of individual contributors.
- *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
- */
-package org.jboss.ejb3.examples.testsupport.dbquery;
-
-import javax.ejb.TransactionAttributeType;
-import javax.persistence.EntityManager;
-
-/**
- * Contract of a test EJB which exposes generic database
- * query operations directly via the {@link EntityManager}.
- * Used in validating pre- and postconditions during testing.
- * All methods will be executed in an existing Transaction, which
- * is {@link TransactionAttributeType#MANDATORY}.
- *
- * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
- * @version $Revision: $
- */
-public interface DbQueryLocalBusiness
-{
-
- //-------------------------------------------------------------------------------------||
- // Contracts --------------------------------------------------------------------------||
- //-------------------------------------------------------------------------------------||
-
- /**
- * Delegates to {@link EntityManager#find(Class, Object)}
- *
- * @return
- * @throws IllegalArgumentException If either argument was not specified
- */
- <T> T find(Class<T> type, Object id) throws IllegalArgumentException;
-}
Copied: projects/ejb-book/trunk/testsupport/src/main/java/org/jboss/ejb3/examples/testsupport/dbquery/EntityManagerExposingBean.java (from rev 104894, projects/ejb-book/trunk/testsupport/src/main/java/org/jboss/ejb3/examples/testsupport/dbquery/DbQueryBean.java)
===================================================================
--- projects/ejb-book/trunk/testsupport/src/main/java/org/jboss/ejb3/examples/testsupport/dbquery/EntityManagerExposingBean.java (rev 0)
+++ projects/ejb-book/trunk/testsupport/src/main/java/org/jboss/ejb3/examples/testsupport/dbquery/EntityManagerExposingBean.java 2010-05-18 03:52:58 UTC (rev 104900)
@@ -0,0 +1,80 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2010, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.ejb3.examples.testsupport.dbquery;
+
+import javax.ejb.Local;
+import javax.ejb.Stateless;
+import javax.ejb.TransactionAttribute;
+import javax.ejb.TransactionAttributeType;
+import javax.persistence.EntityManager;
+import javax.persistence.PersistenceContext;
+
+/**
+ * Implementation of a test EJB which exposes generic database
+ * query operations directly via the {@link EntityManager}.
+ * Used in validating pre- and postconditions during testing.
+ * All methods will be executed in an existing Transaction, which
+ * is {@link TransactionAttributeType#MANDATORY}.
+ *
+ * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
+ * @version $Revision: $
+ */
+ at Stateless
+ at Local(EntityManagerExposingLocalBusiness.class)
+ at TransactionAttribute(TransactionAttributeType.MANDATORY)
+// We make a current Tx mandatory from the caller so that the
+// caller is sure to get back an entity instance which is still attached. This way we can run any tests/checks
+// on it without fear that we'll run into Exceptions. We can use a TxWrappingBean to submit the test logic and
+// execute it in the context of a new Transaction. David Blevins has a writeup of this technique:
+// http://openejb.apache.org/3.0/testing-transactions-example.html
+public class EntityManagerExposingBean implements EntityManagerExposingLocalBusiness
+{
+
+ //-------------------------------------------------------------------------------------||
+ // Instance Members -------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * Underlying hook to persistence
+ */
+ @PersistenceContext
+ private EntityManager em;
+
+ //-------------------------------------------------------------------------------------||
+ // Required Implementations -----------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * {@inheritDoc}
+ * @see org.jboss.ejb3.examples.testsupport.dbquery.EntityManagerExposingLocalBusiness#getEntityManager()
+ */
+ @Override
+ public EntityManager getEntityManager()
+ {
+ if (em == null)
+ {
+ throw new IllegalStateException(EntityManager.class.getSimpleName() + " was not injected.");
+ }
+ return em;
+ }
+
+}
Copied: projects/ejb-book/trunk/testsupport/src/main/java/org/jboss/ejb3/examples/testsupport/dbquery/EntityManagerExposingLocalBusiness.java (from rev 104889, projects/ejb-book/trunk/testsupport/src/main/java/org/jboss/ejb3/examples/testsupport/dbquery/DbQueryLocalBusiness.java)
===================================================================
--- projects/ejb-book/trunk/testsupport/src/main/java/org/jboss/ejb3/examples/testsupport/dbquery/EntityManagerExposingLocalBusiness.java (rev 0)
+++ projects/ejb-book/trunk/testsupport/src/main/java/org/jboss/ejb3/examples/testsupport/dbquery/EntityManagerExposingLocalBusiness.java 2010-05-18 03:52:58 UTC (rev 104900)
@@ -0,0 +1,51 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2010, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.ejb3.examples.testsupport.dbquery;
+
+import javax.ejb.TransactionAttributeType;
+import javax.persistence.EntityManager;
+
+/**
+ * Contract of a test EJB which exposes generic database
+ * operations directly via the {@link EntityManager}.
+ * Used in validating pre- and postconditions during testing.
+ * All methods will be executed in an existing Transaction, which
+ * is {@link TransactionAttributeType#MANDATORY}.
+ *
+ * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
+ * @version $Revision: $
+ */
+public interface EntityManagerExposingLocalBusiness
+{
+
+ //-------------------------------------------------------------------------------------||
+ // Contracts --------------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * Obtains a direct reference to the underlying {@link EntityManager},
+ * which may be used directly from tests (in the context of a running
+ * Transaction) to perform direct operations such as {@link EntityManager#persist(Object)},
+ * {@link EntityManager#find(Class, Object)}, and {@link EntityManager#remove(Object)}.
+ */
+ EntityManager getEntityManager();
+}
More information about the jboss-cvs-commits
mailing list