[jboss-cvs] JBossAS SVN: r104889 - in projects/ejb-book/trunk: build and 15 other directories.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Mon May 17 21:14:58 EDT 2010
Author: ALRubinger
Date: 2010-05-17 21:14:57 -0400 (Mon, 17 May 2010)
New Revision: 104889
Added:
projects/ejb-book/trunk/testsupport/
projects/ejb-book/trunk/testsupport/pom.xml
projects/ejb-book/trunk/testsupport/src/
projects/ejb-book/trunk/testsupport/src/main/
projects/ejb-book/trunk/testsupport/src/main/java/
projects/ejb-book/trunk/testsupport/src/main/java/org/
projects/ejb-book/trunk/testsupport/src/main/java/org/jboss/
projects/ejb-book/trunk/testsupport/src/main/java/org/jboss/ejb3/
projects/ejb-book/trunk/testsupport/src/main/java/org/jboss/ejb3/examples/
projects/ejb-book/trunk/testsupport/src/main/java/org/jboss/ejb3/examples/testsupport/
projects/ejb-book/trunk/testsupport/src/main/java/org/jboss/ejb3/examples/testsupport/dbinit/
projects/ejb-book/trunk/testsupport/src/main/java/org/jboss/ejb3/examples/testsupport/dbinit/DbInitializerBeanBase.java
projects/ejb-book/trunk/testsupport/src/main/java/org/jboss/ejb3/examples/testsupport/dbinit/DbInitializerLocalBusiness.java
projects/ejb-book/trunk/testsupport/src/main/java/org/jboss/ejb3/examples/testsupport/dbquery/
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
projects/ejb-book/trunk/testsupport/src/main/resources/
projects/ejb-book/trunk/testsupport/src/test/
projects/ejb-book/trunk/testsupport/src/test/java/
projects/ejb-book/trunk/testsupport/src/test/resources/
Removed:
projects/ejb-book/trunk/chxx-poker/src/test/java/org/jboss/ejb3/examples/chxx/transactions/ejb/DbInitializerBean.java
projects/ejb-book/trunk/chxx-poker/src/test/java/org/jboss/ejb3/examples/chxx/transactions/ejb/DbInitializerLocalBusiness.java
projects/ejb-book/trunk/chxx-poker/src/test/java/org/jboss/ejb3/examples/chxx/transactions/ejb/DbQueryBean.java
projects/ejb-book/trunk/chxx-poker/src/test/java/org/jboss/ejb3/examples/chxx/transactions/ejb/DbQueryLocalBusiness.java
Modified:
projects/ejb-book/trunk/build/pom.xml
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-25] Create a "testsupport" module to centralize common helper classes in testing
Modified: projects/ejb-book/trunk/build/pom.xml
===================================================================
--- projects/ejb-book/trunk/build/pom.xml 2010-05-18 00:25:06 UTC (rev 104888)
+++ projects/ejb-book/trunk/build/pom.xml 2010-05-18 01:14:57 UTC (rev 104889)
@@ -30,6 +30,7 @@
<version.org.glassfish>3.0.1-b18</version.org.glassfish>
<version.org.apache.openejb>3.1.2</version.org.apache.openejb>
<version.org.jboss.spec.javax.ejb>1.0.0.Beta2</version.org.jboss.spec.javax.ejb>
+ <version.javax_javaee.api>6.0</version.javax_javaee.api>
</properties>
@@ -219,6 +220,14 @@
<version>${version.org.apache.openejb}</version>
</dependency>
+ <dependency>
+ <groupId>javax</groupId>
+ <artifactId>javaee-api</artifactId>
+ <version>${version.javax_javaee.api}</version>
+ <scope>provided</scope>
+ <optional>true</optional>
+ </dependency>
+
</dependencies>
</dependencyManagement>
Modified: projects/ejb-book/trunk/chxx-poker/pom.xml
===================================================================
--- projects/ejb-book/trunk/chxx-poker/pom.xml 2010-05-18 00:25:06 UTC (rev 104888)
+++ projects/ejb-book/trunk/chxx-poker/pom.xml 2010-05-18 01:14:57 UTC (rev 104889)
@@ -13,8 +13,8 @@
<modelVersion>4.0.0</modelVersion>
<!-- Artifact Information -->
- <artifactId>jboss-ejb3-examples-chxx-transactions</artifactId>
- <name>JBoss EJB 3.x Examples - Chapter XX: Transactional EJBs</name>
+ <artifactId>jboss-ejb3-examples-chxx-pokergame</artifactId>
+ <name>JBoss EJB 3.x Examples - Chapter XX: Transactional Poker Game EJBs</name>
<description>Example to accompany O'Reilly "Enterprise Java Beans 6th Edition" Chapter XX</description>
<!-- Build -->
@@ -47,6 +47,13 @@
Test Dependencies
-->
+
+ <dependency>
+ <groupId>${project.groupId}</groupId>
+ <artifactId>jboss-ejb3-examples-testsupport</artifactId>
+ <version>${project.version}</version>
+ <scope>test</scope>
+ </dependency>
<!-- 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 00:25:06 UTC (rev 104888)
+++ projects/ejb-book/trunk/chxx-poker/src/test/java/org/jboss/ejb3/examples/chxx/transactions/TransactionalPokerGameIntegrationTest.java 2010-05-18 01:14:57 UTC (rev 104889)
@@ -38,8 +38,6 @@
import org.jboss.ejb3.examples.chxx.transactions.api.BankLocalBusiness;
import org.jboss.ejb3.examples.chxx.transactions.api.PokerGameLocalBusiness;
import org.jboss.ejb3.examples.chxx.transactions.ejb.DbInitializerBean;
-import org.jboss.ejb3.examples.chxx.transactions.ejb.DbInitializerLocalBusiness;
-import org.jboss.ejb3.examples.chxx.transactions.ejb.DbQueryLocalBusiness;
import org.jboss.ejb3.examples.chxx.transactions.ejb.ForcedTestException;
import org.jboss.ejb3.examples.chxx.transactions.ejb.TaskExecutionException;
import org.jboss.ejb3.examples.chxx.transactions.ejb.TxWrappingLocalBusiness;
@@ -47,6 +45,10 @@
import org.jboss.ejb3.examples.chxx.transactions.entity.User;
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.DbInitializerBeanBase;
+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.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.After;
@@ -92,7 +94,8 @@
{
final JavaArchive archive = ShrinkWrap.create("test.jar", JavaArchive.class).addPackages(true,
BankLocalBusiness.class.getPackage(), User.class.getPackage()).addManifestResource("persistence.xml")
- .addPackages(false, DbInitializerBean.class.getPackage(), BankBean.class.getPackage());
+ .addPackages(false, DbInitializerBean.class.getPackage(), BankBean.class.getPackage(),
+ DbInitializerLocalBusiness.class.getPackage(), DbQueryBean.class.getPackage());
log.info(archive.toString(true));
return archive;
}
@@ -155,9 +158,9 @@
public void injectEjbs() throws Exception
{
// Fake injection by doing manual lookups for the time being
- dbInitializer = (DbInitializerLocalBusiness) jndiContext.lookup(DbInitializerLocalBusiness.JNDI_NAME);
+ dbInitializer = (DbInitializerLocalBusiness) jndiContext.lookup("DbInitializerBean/local");
txWrapper = (TxWrappingLocalBusiness) jndiContext.lookup(TxWrappingLocalBusiness.JNDI_NAME);
- db = (DbQueryLocalBusiness) jndiContext.lookup(DbQueryLocalBusiness.JNDI_NAME);
+ db = (DbQueryLocalBusiness) jndiContext.lookup("DbQueryBean/local");
bank = (BankLocalBusiness) jndiContext.lookup(BankLocalBusiness.JNDI_NAME);
pokerGame = (PokerGameLocalBusiness) jndiContext.lookup(PokerGameLocalBusiness.JNDI_NAME);
}
@@ -185,7 +188,7 @@
{
// Init
- final long alrubingerAccountId = DbInitializerBean.ACCOUNT_ALRUBINGER_ID;
+ final long alrubingerAccountId = DbInitializerBeanBase.ACCOUNT_ALRUBINGER_ID;
final long pokerAccountId = PokerServiceConstants.ACCOUNT_POKERGAME_ID;
// Ensure there's the expected amounts in both the ALR and Poker accounts
Deleted: projects/ejb-book/trunk/chxx-poker/src/test/java/org/jboss/ejb3/examples/chxx/transactions/ejb/DbInitializerBean.java
===================================================================
--- projects/ejb-book/trunk/chxx-poker/src/test/java/org/jboss/ejb3/examples/chxx/transactions/ejb/DbInitializerBean.java 2010-05-18 00:25:06 UTC (rev 104888)
+++ projects/ejb-book/trunk/chxx-poker/src/test/java/org/jboss/ejb3/examples/chxx/transactions/ejb/DbInitializerBean.java 2010-05-18 01:14:57 UTC (rev 104889)
@@ -1,201 +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.chxx.transactions.ejb;
-
-import java.util.Collection;
-import java.util.logging.Logger;
-
-import javax.annotation.PostConstruct;
-import javax.annotation.Resource;
-import javax.ejb.Local;
-import javax.ejb.Singleton;
-import javax.ejb.Startup;
-import javax.ejb.TransactionManagement;
-import javax.ejb.TransactionManagementType;
-import javax.persistence.EntityManager;
-import javax.persistence.PersistenceContext;
-import javax.transaction.Transaction;
-import javax.transaction.TransactionManager;
-
-import org.jboss.ejb3.annotation.LocalBinding;
-import org.jboss.ejb3.examples.chxx.transactions.entity.Account;
-import org.jboss.ejb3.examples.chxx.transactions.entity.User;
-import org.jboss.ejb3.examples.chxx.transactions.impl.PokerServiceConstants;
-
-/**
- * Singleton EJB to initialize and prepropulate
- * the database state before running tests. Also permits
- * refreshing the DB with default state via
- * {@link DbInitializerLocalBusiness#refreshWithDefaultData()}.
- *
- * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
- * @version $Revision: $
- */
- at Singleton
- at Startup
- at Local(DbInitializerLocalBusiness.class)
- at LocalBinding(jndiBinding = DbInitializerLocalBusiness.JNDI_NAME)
-// JBoss-specific JNDI Binding annotation
- at TransactionManagement(TransactionManagementType.BEAN)
-// We'll use bean-managed Tx's here, because @PostConstruct is fired in a
-// non-transactional context anyway, and we want to have consistent
-// handling when we call via "refreshWithDefaultData".
-public class DbInitializerBean implements DbInitializerLocalBusiness
-{
-
- //-------------------------------------------------------------------------------------||
- // Class Members ----------------------------------------------------------------------||
- //-------------------------------------------------------------------------------------||
-
- /**
- * Logger
- */
- private static final Logger log = Logger.getLogger(DbInitializerBean.class.getName());
-
- //-------------------------------------------------------------------------------------||
- // Instance Members -------------------------------------------------------------------||
- //-------------------------------------------------------------------------------------||
-
- /**
- * Hook to interact w/ the database via JPA
- */
- @PersistenceContext
- private EntityManager em;
-
- /**
- * Because @PostConstruct runs in an unspecified
- * Tx context (as invoked by the container), we'll
- * make one via this manager. For EJBs that use
- * TransactionManagementType.BEAN, this is the hook
- * we use to programmatically demarcate transactional
- * boundaries.
- */
- @Resource(mappedName = "java:/TransactionManager")
- private TransactionManager txManager;
-
- //-------------------------------------------------------------------------------------||
- // Functional Methods -----------------------------------------------------------------||
- //-------------------------------------------------------------------------------------||
-
- /**
- * Called by the container on startup; populates the database with test data.
- * Because EJB lifecycle operations are invoked outside of a
- * transactional context, we manually demarcate the Tx boundaries
- * via the injected {@link TransactionManager}.
- */
- @PostConstruct
- public void populateDatabase() throws Exception
- {
-
- // Get the current Tx (if we have one, we may have been invoked via
- // "refreshWithDefaultData"
- final Transaction tx = txManager.getTransaction();
- final boolean startOurOwnTx = tx == null;
- // If we need to start our own Tx (ie. this was called by the container as @PostConstruct)
- if (startOurOwnTx)
- {
- // Start a Tx via the injected TransactionManager
- txManager.begin();
- }
-
- /*
- * Create some users
- */
-
- // ALR
- final User alrubinger = new User();
- alrubinger.setId(USER_ALRUBINGER_ID);
- alrubinger.setName(USER_ALRUBINGER_NAME);
- final Account alrubingerAccount = new Account();
- alrubingerAccount.deposit(INITIAL_ACCOUNT_BALANCE_ALR);
- alrubingerAccount.setOwner(alrubinger);
- alrubingerAccount.setId(ACCOUNT_ALRUBINGER_ID);
- alrubinger.setAccount(alrubingerAccount);
-
- // Poker Game Service
- final User pokerGameService = new User();
- pokerGameService.setId(PokerServiceConstants.USER_POKERGAME_ID);
- pokerGameService.setName(PokerServiceConstants.USER_POKERGAME_NAME);
- final Account pokerGameAccount = new Account();
- pokerGameAccount.deposit(PokerServiceConstants.INITIAL_ACCOUNT_BALANCE_POKERGAME);
- pokerGameAccount.setOwner(pokerGameService);
- pokerGameAccount.setId(PokerServiceConstants.ACCOUNT_POKERGAME_ID);
- pokerGameService.setAccount(pokerGameAccount);
-
- // Persist
- em.persist(alrubinger);
- log.info("Created: " + alrubinger);
- em.persist(pokerGameService);
- log.info("Created: " + pokerGameService);
-
- // Mark the end of the Tx if we started it; will trigger the EntityManager to flush
- // outgoing changes
- if (startOurOwnTx)
- {
- txManager.commit();
- }
-
- }
-
- /**
- * {@inheritDoc}
- * @see org.jboss.ejb3.examples.chxx.transactions.ejb.DbInitializerLocalBusiness#refreshWithDefaultData()
- */
- @Override
- public void refreshWithDefaultData() throws Exception
- {
- // Start a Tx
- txManager.begin();
- try
- {
-
- // Delete existing data
- final Collection<Account> accounts = em.createQuery("SELECT o FROM " + Account.class.getSimpleName() + " o",
- Account.class).getResultList();
- final Collection<User> users = em
- .createQuery("SELECT o FROM " + User.class.getSimpleName() + " o", User.class).getResultList();
- for (final Account account : accounts)
- {
- em.remove(account);
- }
- for (final User user : users)
- {
- em.remove(user);
- }
-
- // Repopulate
- try
- {
- this.populateDatabase();
- }
- catch (final Exception e)
- {
- throw new RuntimeException("Could not prepopulate DB, may be in inconsistent state", e);
- }
- }
- finally
- {
- txManager.commit();
- }
-
- }
-}
Deleted: projects/ejb-book/trunk/chxx-poker/src/test/java/org/jboss/ejb3/examples/chxx/transactions/ejb/DbInitializerLocalBusiness.java
===================================================================
--- projects/ejb-book/trunk/chxx-poker/src/test/java/org/jboss/ejb3/examples/chxx/transactions/ejb/DbInitializerLocalBusiness.java 2010-05-18 00:25:06 UTC (rev 104888)
+++ projects/ejb-book/trunk/chxx-poker/src/test/java/org/jboss/ejb3/examples/chxx/transactions/ejb/DbInitializerLocalBusiness.java 2010-05-18 01:14:57 UTC (rev 104889)
@@ -1,66 +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.chxx.transactions.ejb;
-
-import java.math.BigDecimal;
-
-/**
- * Contract of an EJB which can reset and populate a database with
- * known data for user tests
- *
- * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
- * @version $Revision: $
- */
-public interface DbInitializerLocalBusiness
-{
- //-------------------------------------------------------------------------------------||
- // Constants --------------------------------------------------------------------------||
- //-------------------------------------------------------------------------------------||
-
- /**
- * Name we'll ind to in JNDI
- */
- String JNDI_NAME = "DbInitializer";
-
- /*
- * Test Data
- */
-
- long USER_ALRUBINGER_ID = 2L;
-
- String USER_ALRUBINGER_NAME = "Andrew Lee Rubinger";
-
- long ACCOUNT_ALRUBINGER_ID = 2L;
-
- BigDecimal INITIAL_ACCOUNT_BALANCE_ALR = new BigDecimal(500);
-
- //-------------------------------------------------------------------------------------||
- // Contracts --------------------------------------------------------------------------||
- //-------------------------------------------------------------------------------------||
-
- /**
- * Clears and repopulates the database with default test data
- *
- * @throws Exception If an error occurred in refreshing with default data
- */
- void refreshWithDefaultData() throws Exception;
-}
Deleted: projects/ejb-book/trunk/chxx-poker/src/test/java/org/jboss/ejb3/examples/chxx/transactions/ejb/DbQueryBean.java
===================================================================
--- projects/ejb-book/trunk/chxx-poker/src/test/java/org/jboss/ejb3/examples/chxx/transactions/ejb/DbQueryBean.java 2010-05-18 00:25:06 UTC (rev 104888)
+++ projects/ejb-book/trunk/chxx-poker/src/test/java/org/jboss/ejb3/examples/chxx/transactions/ejb/DbQueryBean.java 2010-05-18 01:14:57 UTC (rev 104889)
@@ -1,101 +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.chxx.transactions.ejb;
-
-import javax.ejb.Local;
-import javax.ejb.Stateless;
-import javax.ejb.TransactionAttribute;
-import javax.ejb.TransactionAttributeType;
-import javax.persistence.EntityManager;
-import javax.persistence.PersistenceContext;
-
-import org.jboss.ejb3.annotation.LocalBinding;
-
-/**
- * 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
- at LocalBinding(jndiBinding = DbQueryLocalBusiness.JNDI_NAME)
-public class DbQueryBean implements DbQueryLocalBusiness
-{
-
- //-------------------------------------------------------------------------------------||
- // Class Members ----------------------------------------------------------------------||
- //-------------------------------------------------------------------------------------||
-
- //-------------------------------------------------------------------------------------||
- // Instance Members -------------------------------------------------------------------||
- //-------------------------------------------------------------------------------------||
-
- /**
- * Underlying hook to persistence
- */
- @PersistenceContext
- private EntityManager em;
-
- //-------------------------------------------------------------------------------------||
- // Required Implementations -----------------------------------------------------------||
- //-------------------------------------------------------------------------------------||
-
- /**
- * @see org.jboss.ejb3.examples.chxx.transactions.ejb.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);
- }
-
- //-------------------------------------------------------------------------------------||
- // Functional Methods -----------------------------------------------------------------||
- //-------------------------------------------------------------------------------------||
-
- //-------------------------------------------------------------------------------------||
- // Internal Helper Methods ------------------------------------------------------------||
- //-------------------------------------------------------------------------------------||
-}
Deleted: projects/ejb-book/trunk/chxx-poker/src/test/java/org/jboss/ejb3/examples/chxx/transactions/ejb/DbQueryLocalBusiness.java
===================================================================
--- projects/ejb-book/trunk/chxx-poker/src/test/java/org/jboss/ejb3/examples/chxx/transactions/ejb/DbQueryLocalBusiness.java 2010-05-18 00:25:06 UTC (rev 104888)
+++ projects/ejb-book/trunk/chxx-poker/src/test/java/org/jboss/ejb3/examples/chxx/transactions/ejb/DbQueryLocalBusiness.java 2010-05-18 01:14:57 UTC (rev 104889)
@@ -1,59 +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.chxx.transactions.ejb;
-
-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
-{
- //-------------------------------------------------------------------------------------||
- // Constants --------------------------------------------------------------------------||
- //-------------------------------------------------------------------------------------||
-
- /**
- * Name we'll ind to in JNDI
- */
- String JNDI_NAME = "DbQueryLocal";
-
- //-------------------------------------------------------------------------------------||
- // 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;
-}
Modified: projects/ejb-book/trunk/pom.xml
===================================================================
--- projects/ejb-book/trunk/pom.xml 2010-05-18 00:25:06 UTC (rev 104888)
+++ projects/ejb-book/trunk/pom.xml 2010-05-18 01:14:57 UTC (rev 104889)
@@ -25,6 +25,7 @@
<module>chxx-interceptors</module>
<module>chxx-poker</module>
<module>chxx-timer</module>
+ <module>testsupport</module>
</modules>
</project>
Property changes on: projects/ejb-book/trunk/testsupport
___________________________________________________________________
Name: svn:ignore
+ target
.settings
.classpath
.project
bin
Added: projects/ejb-book/trunk/testsupport/pom.xml
===================================================================
--- projects/ejb-book/trunk/testsupport/pom.xml (rev 0)
+++ projects/ejb-book/trunk/testsupport/pom.xml 2010-05-18 01:14:57 UTC (rev 104889)
@@ -0,0 +1,44 @@
+<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-testsupport</artifactId>
+ <name>JBoss EJB 3.x Examples - Test Support</name>
+ <description>Common Test Support for examples to accompany O'Reilly "Enterprise Java Beans 6th Edition"</description>
+
+ <!-- Build -->
+ <build>
+ </build>
+
+
+ <!-- Properties -->
+ <properties>
+
+ <!-- Versioning -->
+
+ </properties>
+
+ <!-- Dependencies -->
+ <dependencies>
+
+ <dependency>
+ <groupId>javax</groupId>
+ <artifactId>javaee-api</artifactId>
+ <scope>provided</scope>
+ <optional>true</optional> <!-- Don't export this, ever-->
+ </dependency>
+
+ </dependencies>
+
+</project>
Property changes on: projects/ejb-book/trunk/testsupport/pom.xml
___________________________________________________________________
Name: svn:executable
+ *
Copied: projects/ejb-book/trunk/testsupport/src/main/java/org/jboss/ejb3/examples/testsupport/dbinit/DbInitializerBeanBase.java (from rev 104828, projects/ejb-book/trunk/chxx-poker/src/test/java/org/jboss/ejb3/examples/chxx/transactions/ejb/DbInitializerBean.java)
===================================================================
--- projects/ejb-book/trunk/testsupport/src/main/java/org/jboss/ejb3/examples/testsupport/dbinit/DbInitializerBeanBase.java (rev 0)
+++ projects/ejb-book/trunk/testsupport/src/main/java/org/jboss/ejb3/examples/testsupport/dbinit/DbInitializerBeanBase.java 2010-05-18 01:14:57 UTC (rev 104889)
@@ -0,0 +1,143 @@
+/*
+ * 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.dbinit;
+
+import java.util.logging.Logger;
+
+import javax.annotation.PostConstruct;
+import javax.annotation.Resource;
+import javax.persistence.EntityManager;
+import javax.persistence.PersistenceContext;
+import javax.transaction.Transaction;
+import javax.transaction.TransactionManager;
+
+/**
+ * Base support for prepopulating the database with
+ * some default data. Also permits
+ * refreshing the DB with default state via
+ * {@link DbInitializerLocalBusiness#refreshWithDefaultData()}.
+ *
+ * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
+ * @version $Revision: $
+ */
+public abstract class DbInitializerBeanBase implements DbInitializerLocalBusiness
+{
+
+ //-------------------------------------------------------------------------------------||
+ // Class Members ----------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * Logger
+ */
+ protected static final Logger log = Logger.getLogger(DbInitializerBeanBase.class.getName());
+
+ //-------------------------------------------------------------------------------------||
+ // Instance Members -------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * Hook to interact w/ the database via JPA
+ */
+ @PersistenceContext
+ protected EntityManager em;
+
+ /**
+ * Because @PostConstruct runs in an unspecified
+ * Tx context (as invoked by the container), we'll
+ * make one via this manager. For EJBs that use
+ * TransactionManagementType.BEAN, this is the hook
+ * we use to programmatically demarcate transactional
+ * boundaries.
+ */
+ @Resource(mappedName = "java:/TransactionManager")
+ private TransactionManager txManager;
+
+ //-------------------------------------------------------------------------------------||
+ // Functional Methods -----------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ public abstract void populateDefaultData() throws Exception;
+
+ public abstract void cleanup() throws Exception;
+
+ /**
+ * Called by the container on startup; populates the database with test data.
+ * Because EJB lifecycle operations are invoked outside of a
+ * transactional context, we manually demarcate the Tx boundaries
+ * via the injected {@link TransactionManager}.
+ */
+ @PostConstruct
+ public void populateDatabase() throws Exception
+ {
+ // Get the current Tx (if we have one, we may have been invoked via
+ // "refreshWithDefaultData"
+ final Transaction tx = txManager.getTransaction();
+ final boolean startOurOwnTx = tx == null;
+ // If we need to start our own Tx (ie. this was called by the container as @PostConstruct)
+ if (startOurOwnTx)
+ {
+ // Start a Tx via the injected TransactionManager
+ txManager.begin();
+ }
+
+ // Populate with default data
+ try
+ {
+ this.populateDefaultData();
+ }
+ finally
+ {
+ // Mark the end of the Tx if we started it; will trigger the EntityManager to flush
+ // outgoing changes
+ if (startOurOwnTx)
+ {
+ txManager.commit();
+ }
+ }
+ }
+
+ /**
+ * {@inheritDoc}
+ * @see org.jboss.ejb3.examples.testsupport.dbinit.DbInitializerLocalBusiness#refreshWithDefaultData()
+ */
+ @Override
+ public void refreshWithDefaultData() throws Exception
+ {
+ // Start a Tx
+ txManager.begin();
+ try
+ {
+ // Cleanup
+ this.cleanup();
+
+ // Repopulate
+ this.populateDatabase();
+
+ }
+ finally
+ {
+ txManager.commit();
+ }
+
+ }
+}
Copied: projects/ejb-book/trunk/testsupport/src/main/java/org/jboss/ejb3/examples/testsupport/dbinit/DbInitializerLocalBusiness.java (from rev 104828, projects/ejb-book/trunk/chxx-poker/src/test/java/org/jboss/ejb3/examples/chxx/transactions/ejb/DbInitializerLocalBusiness.java)
===================================================================
--- projects/ejb-book/trunk/testsupport/src/main/java/org/jboss/ejb3/examples/testsupport/dbinit/DbInitializerLocalBusiness.java (rev 0)
+++ projects/ejb-book/trunk/testsupport/src/main/java/org/jboss/ejb3/examples/testsupport/dbinit/DbInitializerLocalBusiness.java 2010-05-18 01:14:57 UTC (rev 104889)
@@ -0,0 +1,61 @@
+/*
+ * 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.dbinit;
+
+import java.math.BigDecimal;
+
+/**
+ * Contract of an EJB which can reset and populate a database with
+ * known data for user tests
+ *
+ * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
+ * @version $Revision: $
+ */
+public interface DbInitializerLocalBusiness
+{
+ //-------------------------------------------------------------------------------------||
+ // Constants --------------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /*
+ * Test Data
+ */
+
+ long USER_ALRUBINGER_ID = 2L;
+
+ String USER_ALRUBINGER_NAME = "Andrew Lee Rubinger";
+
+ long ACCOUNT_ALRUBINGER_ID = 2L;
+
+ BigDecimal INITIAL_ACCOUNT_BALANCE_ALR = new BigDecimal(500);
+
+ //-------------------------------------------------------------------------------------||
+ // Contracts --------------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * Clears and repopulates the database with default test data
+ *
+ * @throws Exception If an error occurred in refreshing with default data
+ */
+ void refreshWithDefaultData() throws Exception;
+}
Copied: projects/ejb-book/trunk/testsupport/src/main/java/org/jboss/ejb3/examples/testsupport/dbquery/DbQueryBean.java (from rev 104828, projects/ejb-book/trunk/chxx-poker/src/test/java/org/jboss/ejb3/examples/chxx/transactions/ejb/DbQueryBean.java)
===================================================================
--- projects/ejb-book/trunk/testsupport/src/main/java/org/jboss/ejb3/examples/testsupport/dbquery/DbQueryBean.java (rev 0)
+++ projects/ejb-book/trunk/testsupport/src/main/java/org/jboss/ejb3/examples/testsupport/dbquery/DbQueryBean.java 2010-05-18 01:14:57 UTC (rev 104889)
@@ -0,0 +1,98 @@
+/*
+ * 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
+{
+
+ //-------------------------------------------------------------------------------------||
+ // Class Members ----------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ //-------------------------------------------------------------------------------------||
+ // 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);
+ }
+
+ //-------------------------------------------------------------------------------------||
+ // Functional Methods -----------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ //-------------------------------------------------------------------------------------||
+ // Internal Helper Methods ------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+}
Copied: projects/ejb-book/trunk/testsupport/src/main/java/org/jboss/ejb3/examples/testsupport/dbquery/DbQueryLocalBusiness.java (from rev 104788, projects/ejb-book/trunk/chxx-poker/src/test/java/org/jboss/ejb3/examples/chxx/transactions/ejb/DbQueryLocalBusiness.java)
===================================================================
--- projects/ejb-book/trunk/testsupport/src/main/java/org/jboss/ejb3/examples/testsupport/dbquery/DbQueryLocalBusiness.java (rev 0)
+++ projects/ejb-book/trunk/testsupport/src/main/java/org/jboss/ejb3/examples/testsupport/dbquery/DbQueryLocalBusiness.java 2010-05-18 01:14:57 UTC (rev 104889)
@@ -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
+ * 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;
+}
More information about the jboss-cvs-commits
mailing list