[jboss-cvs] JBossAS SVN: r104788 - in projects/ejb-book/trunk/chxx-poker/src: main/java/org/jboss/ejb3/examples/chxx/transactions/api and 5 other directories.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Thu May 13 23:05:33 EDT 2010
Author: ALRubinger
Date: 2010-05-13 23:05:32 -0400 (Thu, 13 May 2010)
New Revision: 104788
Added:
projects/ejb-book/trunk/chxx-poker/src/main/java/org/jboss/ejb3/examples/chxx/transactions/impl/
projects/ejb-book/trunk/chxx-poker/src/main/java/org/jboss/ejb3/examples/chxx/transactions/impl/BankBean.java
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/ejb/
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
projects/ejb-book/trunk/chxx-poker/src/test/java/org/jboss/ejb3/examples/chxx/transactions/ejb/ForcedTestException.java
projects/ejb-book/trunk/chxx-poker/src/test/java/org/jboss/ejb3/examples/chxx/transactions/ejb/TaskExecutionException.java
projects/ejb-book/trunk/chxx-poker/src/test/java/org/jboss/ejb3/examples/chxx/transactions/ejb/TxWrappingBean.java
projects/ejb-book/trunk/chxx-poker/src/test/java/org/jboss/ejb3/examples/chxx/transactions/ejb/TxWrappingLocalBusiness.java
Removed:
projects/ejb-book/trunk/chxx-poker/src/main/java/org/jboss/ejb3/examples/chxx/transactions/api/BankBean.java
projects/ejb-book/trunk/chxx-poker/src/test/java/org/jboss/ejb3/examples/chxx/transactions/DbInitializerBean.java
projects/ejb-book/trunk/chxx-poker/src/test/java/org/jboss/ejb3/examples/chxx/transactions/DbInitializerLocalBusiness.java
projects/ejb-book/trunk/chxx-poker/src/test/java/org/jboss/ejb3/examples/chxx/transactions/SimpleDeploymentIntegrationTest.java
Modified:
projects/ejb-book/trunk/chxx-poker/src/main/java/org/jboss/ejb3/examples/chxx/transactions/entity/User.java
projects/ejb-book/trunk/chxx-poker/src/main/resources/persistence.xml
Log:
[EJBBOOK-23] The beginnings of working Tx examples using EmbeddedAS via Arquillian
Deleted: projects/ejb-book/trunk/chxx-poker/src/main/java/org/jboss/ejb3/examples/chxx/transactions/api/BankBean.java
===================================================================
--- projects/ejb-book/trunk/chxx-poker/src/main/java/org/jboss/ejb3/examples/chxx/transactions/api/BankBean.java 2010-05-14 01:20:30 UTC (rev 104787)
+++ projects/ejb-book/trunk/chxx-poker/src/main/java/org/jboss/ejb3/examples/chxx/transactions/api/BankBean.java 2010-05-14 03:05:32 UTC (rev 104788)
@@ -1,177 +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.api;
-
-import java.math.BigDecimal;
-import java.util.logging.Logger;
-
-import javax.ejb.Local;
-import javax.ejb.Stateless;
-import javax.ejb.TransactionAttribute;
-import javax.ejb.TransactionAttributeType;
-import javax.persistence.EntityManager;
-import javax.persistence.EntityNotFoundException;
-import javax.persistence.PersistenceContext;
-
-import org.jboss.ejb3.annotation.LocalBinding;
-import org.jboss.ejb3.examples.chxx.transactions.entity.Account;
-
-/**
- * The bank with which users and the Poker provider
- * may interact with underlying accounts. For instance users
- * may wish to make cash deposits into their personal account,
- * or the Poker provider may transfer money from the user's account
- * to the poker account when the user places a bet.
- *
- * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
- * @version $Revision: $
- */
- at Stateless
- at Local(BankLocalBusiness.class)
- at LocalBinding(jndiBinding = BankLocalBusiness.JNDI_NAME)
-public class BankBean implements BankLocalBusiness
-{
-
- //-------------------------------------------------------------------------------------||
- // Class Members ----------------------------------------------------------------------||
- //-------------------------------------------------------------------------------------||
-
- /**
- * Logger
- */
- private static final Logger log = Logger.getLogger(BankBean.class.getName());
-
- //-------------------------------------------------------------------------------------||
- // Instance Members -------------------------------------------------------------------||
- //-------------------------------------------------------------------------------------||
-
- /**
- * JPA hook
- */
- @PersistenceContext
- private EntityManager em;
-
- //-------------------------------------------------------------------------------------||
- // Required Implementations -----------------------------------------------------------||
- //-------------------------------------------------------------------------------------||
-
- /**
- * {@inheritDoc}
- * @see org.jboss.ejb3.examples.chxx.transactions.api.BankLocalBusiness#deposit(long, java.math.BigDecimal)
- */
- @Override
- @TransactionAttribute(TransactionAttributeType.REQUIRED)
- // Default Tx Attribute; create a new Tx if not present, else use the existing
- public BigDecimal deposit(long accountId, final BigDecimal amount) throws IllegalArgumentException
- {
- // Get the account
- final Account account = this.getAccount(accountId);
-
- // Deposit
- return account.deposit(amount);
-
- }
-
- /**
- * {@inheritDoc}
- * @see org.jboss.ejb3.examples.chxx.transactions.api.BankLocalBusiness#getBalance(long)
- */
- @Override
- @TransactionAttribute(TransactionAttributeType.SUPPORTS)
- // Don't require a Tx is in play, but respect a currently-operating
- // one so we get the correct visibility from inside the Tx
- public BigDecimal getBalance(long accountId) throws IllegalArgumentException
- {
- // Get the account
- final Account account = this.getAccount(accountId);
-
- // Return the current balance
- return account.getBalance();
- }
-
- /**
- * {@inheritDoc}
- * @see org.jboss.ejb3.examples.chxx.transactions.api.BankLocalBusiness#transfer(long, long, java.math.BigDecimal)
- */
- @Override
- @TransactionAttribute(TransactionAttributeType.REQUIRED)
- // Default Tx Attribute; create a new Tx if not present, else use the existing
- public void transfer(long accountIdFrom, long accountIdTo, BigDecimal amount) throws IllegalArgumentException,
- InsufficientBalanceException
- {
- // Get the accounts in question
- final Account accountFrom = this.getAccount(accountIdFrom);
- final Account accountTo = this.getAccount(accountIdTo);
-
- // Withdraw (which will throw InsufficientBalance if that's the case)
- accountFrom.withdraw(amount);
-
- // And put the money into the new account
- accountTo.deposit(amount);
- log.info("Deposited " + amount + " to " + accountTo + " from " + accountFrom);
-
- }
-
- /**
- * {@inheritDoc}
- * @see org.jboss.ejb3.examples.chxx.transactions.api.BankLocalBusiness#withdraw(long, java.math.BigDecimal)
- */
- @Override
- @TransactionAttribute(TransactionAttributeType.REQUIRED)
- // Default Tx Attribute; create a new Tx if not present, else use the existing
- public BigDecimal withdraw(long accountId, BigDecimal amount) throws IllegalArgumentException,
- InsufficientBalanceException
- {
- // Get the account
- final Account account = this.getAccount(accountId);
-
- // Withdraw
- return account.withdraw(amount);
- }
-
- //-------------------------------------------------------------------------------------||
- // Internal Helper Methods ------------------------------------------------------------||
- //-------------------------------------------------------------------------------------||
-
- /**
- * Obtains the {@link Account} with the specified ID
- *
- * @throws IllegalArgumentException If the ID does not represent a valid Account
- */
- private Account getAccount(final long accountId) throws IllegalArgumentException
- {
- // Get the account
- final Account account;
- try
- {
- account = em.find(Account.class, new Long(accountId));
- }
- // Translate the exception; we were given a bad input
- catch (final EntityNotFoundException enfe)
- {
- throw new IllegalArgumentException("Could not find account with ID " + accountId);
- }
-
- // Return
- return account;
- }
-}
Modified: projects/ejb-book/trunk/chxx-poker/src/main/java/org/jboss/ejb3/examples/chxx/transactions/entity/User.java
===================================================================
--- projects/ejb-book/trunk/chxx-poker/src/main/java/org/jboss/ejb3/examples/chxx/transactions/entity/User.java 2010-05-14 01:20:30 UTC (rev 104787)
+++ projects/ejb-book/trunk/chxx-poker/src/main/java/org/jboss/ejb3/examples/chxx/transactions/entity/User.java 2010-05-14 03:05:32 UTC (rev 104788)
@@ -53,14 +53,8 @@
* The user's poker account
*/
@OneToOne(cascade = CascadeType.PERSIST)
- private Account pokerAccount;
+ private Account account;
- /**
- * The user's personal account
- */
- @OneToOne(cascade = CascadeType.PERSIST)
- private Account personalAccount;
-
//-------------------------------------------------------------------------------------||
// Accessors / Mutators ---------------------------------------------------------------||
//-------------------------------------------------------------------------------------||
@@ -98,37 +92,21 @@
}
/**
- * @return the pokerAccount
+ * @return the account
*/
- public Account getPokerAccount()
+ public Account getAccount()
{
- return pokerAccount;
+ return account;
}
/**
- * @param pokerAccount the pokerAccount to set
+ * @param account the account to set
*/
- public void setPokerAccount(Account pokerAccount)
+ public void setAccount(Account account)
{
- this.pokerAccount = pokerAccount;
+ this.account = account;
}
- /**
- * @return the personalAccount
- */
- public Account getPersonalAccount()
- {
- return personalAccount;
- }
-
- /**
- * @param personalAccount the personalAccount to set
- */
- public void setPersonalAccount(final Account personalAccount)
- {
- this.personalAccount = personalAccount;
- }
-
//-------------------------------------------------------------------------------------||
// Overridden Implementations ---------------------------------------------------------||
//-------------------------------------------------------------------------------------||
@@ -140,8 +118,7 @@
@Override
public String toString()
{
- return "User [id=" + this.getId() + ", email=" + email + ", name=" + name + ", personalAccount="
- + personalAccount + ", pokerAccount=" + pokerAccount + "]";
+ return "User [id=" + this.getId() + ", email=" + email + ", name=" + name + ", pokerAccount=" + account + "]";
}
}
Copied: projects/ejb-book/trunk/chxx-poker/src/main/java/org/jboss/ejb3/examples/chxx/transactions/impl/BankBean.java (from rev 104691, projects/ejb-book/trunk/chxx-poker/src/main/java/org/jboss/ejb3/examples/chxx/transactions/api/BankBean.java)
===================================================================
--- projects/ejb-book/trunk/chxx-poker/src/main/java/org/jboss/ejb3/examples/chxx/transactions/impl/BankBean.java (rev 0)
+++ projects/ejb-book/trunk/chxx-poker/src/main/java/org/jboss/ejb3/examples/chxx/transactions/impl/BankBean.java 2010-05-14 03:05:32 UTC (rev 104788)
@@ -0,0 +1,179 @@
+/*
+ * 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.impl;
+
+import java.math.BigDecimal;
+import java.util.logging.Logger;
+
+import javax.ejb.Local;
+import javax.ejb.Stateless;
+import javax.ejb.TransactionAttribute;
+import javax.ejb.TransactionAttributeType;
+import javax.persistence.EntityManager;
+import javax.persistence.EntityNotFoundException;
+import javax.persistence.PersistenceContext;
+
+import org.jboss.ejb3.annotation.LocalBinding;
+import org.jboss.ejb3.examples.chxx.transactions.api.BankLocalBusiness;
+import org.jboss.ejb3.examples.chxx.transactions.api.InsufficientBalanceException;
+import org.jboss.ejb3.examples.chxx.transactions.entity.Account;
+
+/**
+ * The bank with which users and the Poker provider
+ * may interact with underlying accounts. For instance users
+ * may wish to make cash deposits into their personal account,
+ * or the Poker provider may transfer money from the user's account
+ * to the poker account when the user places a bet.
+ *
+ * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
+ * @version $Revision: $
+ */
+ at Stateless
+ at Local(BankLocalBusiness.class)
+ at LocalBinding(jndiBinding = BankLocalBusiness.JNDI_NAME)
+public class BankBean implements BankLocalBusiness
+{
+
+ //-------------------------------------------------------------------------------------||
+ // Class Members ----------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * Logger
+ */
+ private static final Logger log = Logger.getLogger(BankBean.class.getName());
+
+ //-------------------------------------------------------------------------------------||
+ // Instance Members -------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * JPA hook
+ */
+ @PersistenceContext
+ private EntityManager em;
+
+ //-------------------------------------------------------------------------------------||
+ // Required Implementations -----------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * {@inheritDoc}
+ * @see org.jboss.ejb3.examples.chxx.transactions.api.BankLocalBusiness#deposit(long, java.math.BigDecimal)
+ */
+ @Override
+ @TransactionAttribute(TransactionAttributeType.REQUIRED)
+ // Default Tx Attribute; create a new Tx if not present, else use the existing
+ public BigDecimal deposit(long accountId, final BigDecimal amount) throws IllegalArgumentException
+ {
+ // Get the account
+ final Account account = this.getAccount(accountId);
+
+ // Deposit
+ return account.deposit(amount);
+
+ }
+
+ /**
+ * {@inheritDoc}
+ * @see org.jboss.ejb3.examples.chxx.transactions.api.BankLocalBusiness#getBalance(long)
+ */
+ @Override
+ @TransactionAttribute(TransactionAttributeType.SUPPORTS)
+ // Don't require a Tx is in play, but respect a currently-operating
+ // one so we get the correct visibility from inside the Tx
+ public BigDecimal getBalance(long accountId) throws IllegalArgumentException
+ {
+ // Get the account
+ final Account account = this.getAccount(accountId);
+
+ // Return the current balance
+ return account.getBalance();
+ }
+
+ /**
+ * {@inheritDoc}
+ * @see org.jboss.ejb3.examples.chxx.transactions.api.BankLocalBusiness#transfer(long, long, java.math.BigDecimal)
+ */
+ @Override
+ @TransactionAttribute(TransactionAttributeType.REQUIRED)
+ // Default Tx Attribute; create a new Tx if not present, else use the existing
+ public void transfer(long accountIdFrom, long accountIdTo, BigDecimal amount) throws IllegalArgumentException,
+ InsufficientBalanceException
+ {
+ // Get the accounts in question
+ final Account accountFrom = this.getAccount(accountIdFrom);
+ final Account accountTo = this.getAccount(accountIdTo);
+
+ // Withdraw (which will throw InsufficientBalance if that's the case)
+ accountFrom.withdraw(amount);
+
+ // And put the money into the new account
+ accountTo.deposit(amount);
+ log.info("Deposited " + amount + " to " + accountTo + " from " + accountFrom);
+
+ }
+
+ /**
+ * {@inheritDoc}
+ * @see org.jboss.ejb3.examples.chxx.transactions.api.BankLocalBusiness#withdraw(long, java.math.BigDecimal)
+ */
+ @Override
+ @TransactionAttribute(TransactionAttributeType.REQUIRED)
+ // Default Tx Attribute; create a new Tx if not present, else use the existing
+ public BigDecimal withdraw(long accountId, BigDecimal amount) throws IllegalArgumentException,
+ InsufficientBalanceException
+ {
+ // Get the account
+ final Account account = this.getAccount(accountId);
+
+ // Withdraw
+ return account.withdraw(amount);
+ }
+
+ //-------------------------------------------------------------------------------------||
+ // Internal Helper Methods ------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * Obtains the {@link Account} with the specified ID
+ *
+ * @throws IllegalArgumentException If the ID does not represent a valid Account
+ */
+ private Account getAccount(final long accountId) throws IllegalArgumentException
+ {
+ // Get the account
+ final Account account;
+ try
+ {
+ account = em.find(Account.class, new Long(accountId));
+ }
+ // Translate the exception; we were given a bad input
+ catch (final EntityNotFoundException enfe)
+ {
+ throw new IllegalArgumentException("Could not find account with ID " + accountId);
+ }
+
+ // Return
+ return account;
+ }
+}
Modified: projects/ejb-book/trunk/chxx-poker/src/main/resources/persistence.xml
===================================================================
--- projects/ejb-book/trunk/chxx-poker/src/main/resources/persistence.xml 2010-05-14 01:20:30 UTC (rev 104787)
+++ projects/ejb-book/trunk/chxx-poker/src/main/resources/persistence.xml 2010-05-14 03:05:32 UTC (rev 104788)
@@ -8,7 +8,12 @@
<jta-data-source>java:/DefaultDS</jta-data-source>
<properties>
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
- <property name="hibernate.show_sql" value="true"/>
+ <!--
+
+ You can enable this for Hibernate to dump SQL output to STDOUT
+ <property name="hibernate.show_sql" value="true"/>
+
+ -->
</properties>
</persistence-unit>
</persistence>
Deleted: projects/ejb-book/trunk/chxx-poker/src/test/java/org/jboss/ejb3/examples/chxx/transactions/DbInitializerBean.java
===================================================================
--- projects/ejb-book/trunk/chxx-poker/src/test/java/org/jboss/ejb3/examples/chxx/transactions/DbInitializerBean.java 2010-05-14 01:20:30 UTC (rev 104787)
+++ projects/ejb-book/trunk/chxx-poker/src/test/java/org/jboss/ejb3/examples/chxx/transactions/DbInitializerBean.java 2010-05-14 03:05:32 UTC (rev 104788)
@@ -1,186 +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;
-
-import java.util.Collection;
-import java.util.logging.Logger;
-
-import javax.annotation.PostConstruct;
-import javax.annotation.Resource;
-import javax.ejb.Singleton;
-import javax.ejb.Startup;
-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;
-
-/**
- * Singleton EJB to initialize and prepropulate
- * the database state before running tests.
- *
- * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
- * @version $Revision: $
- */
- at Singleton
- at Startup
- at LocalBinding(jndiBinding = DbInitializerLocalBusiness.JNDI_NAME)
-// JBoss-specific JNDI Binding annotation
-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.
- */
- @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
- {
-
- // See if we need to manage our own Tx (is this called by the container,
- // or from "refreshWithDefaultData"?)
- final Transaction currentTx = txManager.getTransaction();
- final boolean beanManaged = currentTx == null;
- // If there's no Tx currently in play, it's on us to manage things
- if (beanManaged)
- {
- // Start a Tx
- txManager.begin();
- }
-
- /*
- * Create some users
- */
-
- // ALR
- final User alrubinger = new User();
- alrubinger.setId(USER_ALRUBINGER_ID);
- alrubinger.setName(USER_ALRUBINGER_NAME);
- alrubinger.setEmail(USER_ALRUBINGER_EMAIL);
- final Account alrubingerPersonalAccount = new Account();
- alrubingerPersonalAccount.deposit(INITIAL_PERSONAL_ACCOUNT_BALANCE_ALR);
- alrubingerPersonalAccount.setOwner(alrubinger);
- alrubingerPersonalAccount.setId(ACCOUNT_ALRUBINGER_PERSONAL_ID);
- alrubinger.setPersonalAccount(alrubingerPersonalAccount);
- final Account alrubingerPokerAccount = new Account();
- alrubingerPokerAccount.setOwner(alrubinger);
- alrubingerPokerAccount.setId(ACCOUNT_ALRUBINGER_POKER_ID);
- alrubinger.setPokerAccount(alrubingerPokerAccount);
-
- // DER
- final User derudman = new User();
- derudman.setId(USER_DERUDMAN_ID);
- derudman.setName(USER_DERUDMAN_NAME);
- derudman.setEmail(USER_DERUDMAN_EMAIL);
- final Account derudmanPersonalAccount = new Account();
- derudmanPersonalAccount.deposit(INITIAL_PERSONAL_ACCOUNT_BALANCE_DER);
- derudmanPersonalAccount.setOwner(derudman);
- derudmanPersonalAccount.setId(ACCOUNT_DERUDMAN_PERSONAL_ID);
- derudman.setPersonalAccount(derudmanPersonalAccount);
- final Account derudmanPokerAccount = new Account();
- derudmanPokerAccount.setOwner(derudman);
- derudmanPokerAccount.setId(ACCOUNT_DERUDMAN_POKER_ID);
- derudman.setPokerAccount(derudmanPokerAccount);
-
- // Persist
- em.persist(alrubinger);
- log.info("Created: " + alrubinger);
- em.persist(derudman);
- log.info("Created: " + derudman);
-
- // We're done with the Tx; commit and have the EM flush everything out
- if (beanManaged)
- {
- txManager.commit();
- }
-
- }
-
- /**
- * {@inheritDoc}
- * @see org.jboss.ejb3.examples.chxx.transactions.DbInitializerLocalBusiness#refreshWithDefaultData()
- */
- @Override
- public void refreshWithDefaultData()
- {
- // 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);
- }
-
- }
-}
Deleted: projects/ejb-book/trunk/chxx-poker/src/test/java/org/jboss/ejb3/examples/chxx/transactions/DbInitializerLocalBusiness.java
===================================================================
--- projects/ejb-book/trunk/chxx-poker/src/test/java/org/jboss/ejb3/examples/chxx/transactions/DbInitializerLocalBusiness.java 2010-05-14 01:20:30 UTC (rev 104787)
+++ projects/ejb-book/trunk/chxx-poker/src/test/java/org/jboss/ejb3/examples/chxx/transactions/DbInitializerLocalBusiness.java 2010-05-14 03:05:32 UTC (rev 104788)
@@ -1,80 +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;
-
-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
- */
-
- public static final long USER_ALRUBINGER_ID = 1L;
-
- public static final String USER_ALRUBINGER_NAME = "Andrew Lee Rubinger";
-
- public static final String USER_ALRUBINGER_EMAIL = "alr at fake.com";
-
- public static final long ACCOUNT_ALRUBINGER_PERSONAL_ID = 1L;
-
- public static final long ACCOUNT_ALRUBINGER_POKER_ID = 2L;
-
- public static final long USER_DERUDMAN_ID = 2L;
-
- public static final String USER_DERUDMAN_NAME = "David Edward Rudman";
-
- public static final String USER_DERUDMAN_EMAIL = "der at fake.com";
-
- public static final long ACCOUNT_DERUDMAN_PERSONAL_ID = 3L;
-
- public static final long ACCOUNT_DERUDMAN_POKER_ID = 4L;
-
- public static final BigDecimal INITIAL_PERSONAL_ACCOUNT_BALANCE_ALR = new BigDecimal(500);
-
- public static final BigDecimal INITIAL_PERSONAL_ACCOUNT_BALANCE_DER = new BigDecimal(1000);
-
- //-------------------------------------------------------------------------------------||
- // Contracts --------------------------------------------------------------------------||
- //-------------------------------------------------------------------------------------||
-
- /**
- * Clears and repopulates the database with default test data
- */
- void refreshWithDefaultData();
-}
Deleted: projects/ejb-book/trunk/chxx-poker/src/test/java/org/jboss/ejb3/examples/chxx/transactions/SimpleDeploymentIntegrationTest.java
===================================================================
--- projects/ejb-book/trunk/chxx-poker/src/test/java/org/jboss/ejb3/examples/chxx/transactions/SimpleDeploymentIntegrationTest.java 2010-05-14 01:20:30 UTC (rev 104787)
+++ projects/ejb-book/trunk/chxx-poker/src/test/java/org/jboss/ejb3/examples/chxx/transactions/SimpleDeploymentIntegrationTest.java 2010-05-14 03:05:32 UTC (rev 104788)
@@ -1,145 +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;
-
-import java.math.BigDecimal;
-import java.util.logging.Logger;
-
-import javax.naming.Context;
-import javax.naming.InitialContext;
-
-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.chxx.transactions.api.BankLocalBusiness;
-import org.jboss.ejb3.examples.chxx.transactions.entity.User;
-import org.jboss.shrinkwrap.api.ShrinkWrap;
-import org.jboss.shrinkwrap.api.spec.JavaArchive;
-import org.junit.After;
-import org.junit.BeforeClass;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-
-/**
- * Testing dev only
- *
- * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
- * @version $Revision: $
- */
- at RunWith(Arquillian.class)
- at RunMode(RunModeType.LOCAL)
-public class SimpleDeploymentIntegrationTest
-{
-
- //-------------------------------------------------------------------------------------||
- // Class Members ----------------------------------------------------------------------||
- //-------------------------------------------------------------------------------------||
-
- /**
- * Logger
- */
- private static final Logger log = Logger.getLogger(SimpleDeploymentIntegrationTest.class.getName());
-
- /**
- * Naming Context
- */
- 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,
- BankLocalBusiness.class.getPackage(), User.class.getPackage()).addManifestResource("persistence.xml")
- .addClass(DbInitializerBean.class);
- log.info(archive.toString(true));
- return archive;
- }
-
- /**
- * Test-only DB initializer to sanitize and prepopulate the DB with each test run
- */
- // TODO: Support Injection of @EJB here when Arquillian for Embedded JBossAS will support it
- private static DbInitializerLocalBusiness dbInitializer;
-
- //-------------------------------------------------------------------------------------||
- // Instance Members -------------------------------------------------------------------||
- //-------------------------------------------------------------------------------------||
-
- /*
- * TODO: Support Injection of @EJB here when Arquillian for Embedded JBossAS will support it
- */
-
- //-------------------------------------------------------------------------------------||
- // 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();
-
- // Lookup the Singleton initializer and store a reference
- dbInitializer = (DbInitializerLocalBusiness) jndiContext.lookup(DbInitializerLocalBusiness.JNDI_NAME);
- }
-
- /**
- * Clears and repopulates the database with test data
- * after each run
- * @throws Exception
- */
- @After
- public void refreshWithDefaultData() throws Exception
- {
- dbInitializer.refreshWithDefaultData();
- }
-
- //-------------------------------------------------------------------------------------||
- // Required Implementations -----------------------------------------------------------||
- //-------------------------------------------------------------------------------------||
-
- //-------------------------------------------------------------------------------------||
- // Functional Methods -----------------------------------------------------------------||
- //-------------------------------------------------------------------------------------||
-
- @Test
- public void test() throws Exception
- {
-
- final BankLocalBusiness ejb = (BankLocalBusiness) jndiContext.lookup(BankLocalBusiness.JNDI_NAME);
-
- ejb.transfer(DbInitializerBean.ACCOUNT_ALRUBINGER_PERSONAL_ID, DbInitializerBean.ACCOUNT_ALRUBINGER_POKER_ID,
- new BigDecimal(100));
-
- }
-
- //-------------------------------------------------------------------------------------||
- // Internal Helper Methods ------------------------------------------------------------||
- //-------------------------------------------------------------------------------------||
-}
Copied: projects/ejb-book/trunk/chxx-poker/src/test/java/org/jboss/ejb3/examples/chxx/transactions/TransactionalPokerGameIntegrationTest.java (from rev 104691, projects/ejb-book/trunk/chxx-poker/src/test/java/org/jboss/ejb3/examples/chxx/transactions/SimpleDeploymentIntegrationTest.java)
===================================================================
--- projects/ejb-book/trunk/chxx-poker/src/test/java/org/jboss/ejb3/examples/chxx/transactions/TransactionalPokerGameIntegrationTest.java (rev 0)
+++ projects/ejb-book/trunk/chxx-poker/src/test/java/org/jboss/ejb3/examples/chxx/transactions/TransactionalPokerGameIntegrationTest.java 2010-05-14 03:05:32 UTC (rev 104788)
@@ -0,0 +1,308 @@
+/*
+ * 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;
+
+import java.math.BigDecimal;
+import java.util.concurrent.Callable;
+import java.util.logging.Logger;
+
+import javax.naming.Context;
+import javax.naming.InitialContext;
+import javax.persistence.EntityManager;
+
+import junit.framework.Assert;
+
+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.chxx.transactions.api.BankLocalBusiness;
+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;
+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.BankBean;
+import org.jboss.shrinkwrap.api.ShrinkWrap;
+import org.jboss.shrinkwrap.api.spec.JavaArchive;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+/**
+ * Testing dev only
+ *
+ * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
+ * @version $Revision: $
+ */
+ at RunWith(Arquillian.class)
+ at RunMode(RunModeType.LOCAL)
+public class TransactionalPokerGameIntegrationTest
+{
+
+ //-------------------------------------------------------------------------------------||
+ // Class Members ----------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * Logger
+ */
+ private static final Logger log = Logger.getLogger(TransactionalPokerGameIntegrationTest.class.getName());
+
+ /**
+ * Naming Context
+ */
+ 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,
+ BankLocalBusiness.class.getPackage(), User.class.getPackage()).addManifestResource("persistence.xml")
+ .addPackages(false, DbInitializerBean.class.getPackage(), BankBean.class.getPackage());
+ log.info(archive.toString(true));
+ return archive;
+ }
+
+ //-------------------------------------------------------------------------------------||
+ // Instance Members -------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * Test-only DB initializer to sanitize and prepopulate the DB with each test run
+ */
+ // TODO: Support Injection of @EJB here when Arquillian for Embedded JBossAS will support it
+ private DbInitializerLocalBusiness dbInitializer;
+
+ /**
+ * 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 DbQueryLocalBusiness db;
+
+ /**
+ * Bank EJB Proxy
+ */
+ // TODO: Support Injection of @EJB here when Arquillian for Embedded JBossAS will support it
+ private BankLocalBusiness bank;
+
+ //-------------------------------------------------------------------------------------||
+ // 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
+ * @deprecated Remove when Arquillian will handle the injection for us
+ */
+ @Deprecated
+ @Before
+ public void injectEjbs() throws Exception
+ {
+ // Fake injection by doing manual lookups for the time being
+ Object obj = jndiContext.lookup(DbInitializerLocalBusiness.JNDI_NAME);
+ log.info(obj.getClass().getClassLoader().toString());
+ log.info(DbInitializerLocalBusiness.class.getClassLoader().toString());
+ log.info((obj instanceof DbInitializerLocalBusiness) + "");
+
+ dbInitializer = (DbInitializerLocalBusiness) jndiContext.lookup(DbInitializerLocalBusiness.JNDI_NAME);
+ txWrapper = (TxWrappingLocalBusiness) jndiContext.lookup(TxWrappingLocalBusiness.JNDI_NAME);
+ db = (DbQueryLocalBusiness) jndiContext.lookup(DbQueryLocalBusiness.JNDI_NAME);
+ bank = (BankLocalBusiness) jndiContext.lookup(BankLocalBusiness.JNDI_NAME);
+ }
+
+ /**
+ * Clears and repopulates the database with test data
+ * after each run
+ * @throws Exception
+ */
+ @After
+ public void refreshWithDefaultData() throws Exception
+ {
+ dbInitializer.refreshWithDefaultData();
+ }
+
+ //-------------------------------------------------------------------------------------||
+ // Required Implementations -----------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ //-------------------------------------------------------------------------------------||
+ // Functional Methods -----------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * Ensures that Transfers between accounts obey the ACID properties of Transactions
+ */
+ @Test
+ public void transferRetainsIntegrity() throws Throwable
+ {
+
+ // Init
+ final long alrubingerAccountId = DbInitializerBean.ACCOUNT_ALRUBINGERL_ID;
+ final long pokerAccountId = DbInitializerBean.ACCOUNT_POKERGAME_ID;
+
+ // Ensure there's $500 in the ALR account, and $0 in the poker account
+ this.executeInTx(new CheckBalanceOfAccountTask(alrubingerAccountId, new BigDecimal(500)),
+ new CheckBalanceOfAccountTask(pokerAccountId, new BigDecimal(0)));
+
+ // Transfer $100
+ bank.transfer(alrubingerAccountId, pokerAccountId, new BigDecimal(100));
+
+ // Ensure there's $400 in the ALR account, and 100 in the poker account
+ this.executeInTx(new CheckBalanceOfAccountTask(alrubingerAccountId, new BigDecimal(400)),
+ new CheckBalanceOfAccountTask(pokerAccountId, new BigDecimal(100)));
+
+ // Now make a transfer, check it succeeded within the context of a Transaction, then
+ // intentionally throw an exception. The Tx should complete as rolled back,
+ // and the state should be consistent (as if the xfer request never took place).
+ boolean gotExpectedException = false;
+ final Callable<Void> transferTask = new Callable<Void>()
+ {
+ @Override
+ public Void call() throws Exception
+ {
+ bank.transfer(alrubingerAccountId, pokerAccountId, new BigDecimal(100));
+ return null;
+ }
+ };
+ try
+ {
+ this.executeInTx(transferTask,
+ new CheckBalanceOfAccountTask(alrubingerAccountId, new BigDecimal(300)),
+ new CheckBalanceOfAccountTask(pokerAccountId, new BigDecimal(200)),
+ ForcedTestExceptionTask.INSTANCE);
+ }
+ // Expected
+ catch (final ForcedTestException fte)
+ {
+ gotExpectedException = true;
+ }
+ Assert.assertTrue("Did not receive expected exception as signaled from the test; was not rolled back",
+ gotExpectedException);
+
+ // Now that we've checked the tranfer succeeded from within the Tx, then we threw an
+ // exception before committed, ensure the Tx rolled back and the transfer was reverted from the
+ // perspective of everyone outside the Tx.
+ this.executeInTx(new CheckBalanceOfAccountTask(alrubingerAccountId, new BigDecimal(400)),
+ new CheckBalanceOfAccountTask(pokerAccountId, new BigDecimal(100)));
+ }
+
+ //-------------------------------------------------------------------------------------||
+ // Internal Helpers -------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * A task that checks that the account balance of an {@link Account}
+ * with specified ID equals a specified expected value. Typically to be run
+ * inside of a Tx via {@link TransactionalPokerGameIntegrationTest#executeInTx(Callable...)}.
+ *
+ * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
+ * @version $Revision: $
+ */
+ private final class CheckBalanceOfAccountTask implements Callable<Void>
+ {
+
+ private long accountId;
+
+ private BigDecimal expectedBalance;
+
+ CheckBalanceOfAccountTask(final long accountId, final BigDecimal expectedBalance)
+ {
+ assert accountId > 0;
+ assert expectedBalance != null;
+ this.accountId = accountId;
+ this.expectedBalance = expectedBalance;
+ }
+
+ @Override
+ public Void call() throws Exception
+ {
+ final Account account = db.find(Account.class, accountId);
+ Assert.assertEquals("Balance was not as expected", expectedBalance, account.getBalance());
+ return null;
+ }
+
+ }
+
+ /**
+ * Task which throws a {@link TaskExecutionException} for use in testing
+ * for instance to force a Tx Rollback
+ *
+ *
+ * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
+ * @version $Revision: $
+ */
+ private enum ForcedTestExceptionTask implements Callable<Void> {
+ INSTANCE;
+
+ @Override
+ public Void call() throws Exception
+ {
+ throw new ForcedTestException();
+ }
+
+ }
+
+ /**
+ * Executes the specified tasks inside of a Tx, courtesy of the
+ * {@link TxWrappingLocalBusiness} view.
+ */
+ private void executeInTx(final Callable<?>... tasks) throws Throwable
+ {
+ // Precondition checks
+ assert tasks != null : "Tasks must be specified";
+
+ // Execute in a single new Tx, courtesy of the TxWrapping EJB
+ try
+ {
+ txWrapper.wrapInTx(tasks);
+ }
+ catch (final TaskExecutionException tee)
+ {
+ throw tee.getCause();
+ }
+ }
+}
Copied: projects/ejb-book/trunk/chxx-poker/src/test/java/org/jboss/ejb3/examples/chxx/transactions/ejb/DbInitializerBean.java (from rev 104691, projects/ejb-book/trunk/chxx-poker/src/test/java/org/jboss/ejb3/examples/chxx/transactions/DbInitializerBean.java)
===================================================================
--- projects/ejb-book/trunk/chxx-poker/src/test/java/org/jboss/ejb3/examples/chxx/transactions/ejb/DbInitializerBean.java (rev 0)
+++ projects/ejb-book/trunk/chxx-poker/src/test/java/org/jboss/ejb3/examples/chxx/transactions/ejb/DbInitializerBean.java 2010-05-14 03:05:32 UTC (rev 104788)
@@ -0,0 +1,200 @@
+/*
+ * 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;
+
+/**
+ * Singleton EJB to initialize and prepropulate
+ * the database state before running tests.
+ *
+ * @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);
+ alrubinger.setEmail(USER_ALRUBINGER_EMAIL);
+ final Account alrubingerAccount = new Account();
+ alrubingerAccount.deposit(INITIAL_ACCOUNT_BALANCE_ALR);
+ alrubingerAccount.setOwner(alrubinger);
+ alrubingerAccount.setId(ACCOUNT_ALRUBINGERL_ID);
+ alrubinger.setAccount(alrubingerAccount);
+
+ // Poker Game Service
+ final User pokerGameService = new User();
+ pokerGameService.setId(USER_POKERGAME_ID);
+ pokerGameService.setName(USER_POKERGAME_NAME);
+ pokerGameService.setEmail(USER_POKERGAME_EMAIL);
+ final Account pokerGameAccount = new Account();
+ pokerGameAccount.deposit(INITIAL_ACCOUNT_BALANCE_POKERGAME);
+ pokerGameAccount.setOwner(pokerGameService);
+ pokerGameAccount.setId(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();
+ }
+
+ }
+}
Copied: projects/ejb-book/trunk/chxx-poker/src/test/java/org/jboss/ejb3/examples/chxx/transactions/ejb/DbInitializerLocalBusiness.java (from rev 104691, projects/ejb-book/trunk/chxx-poker/src/test/java/org/jboss/ejb3/examples/chxx/transactions/DbInitializerLocalBusiness.java)
===================================================================
--- projects/ejb-book/trunk/chxx-poker/src/test/java/org/jboss/ejb3/examples/chxx/transactions/ejb/DbInitializerLocalBusiness.java (rev 0)
+++ projects/ejb-book/trunk/chxx-poker/src/test/java/org/jboss/ejb3/examples/chxx/transactions/ejb/DbInitializerLocalBusiness.java 2010-05-14 03:05:32 UTC (rev 104788)
@@ -0,0 +1,78 @@
+/*
+ * 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
+ */
+
+ public static final long USER_ALRUBINGER_ID = 1L;
+
+ public static final String USER_ALRUBINGER_NAME = "Andrew Lee Rubinger";
+
+ public static final String USER_ALRUBINGER_EMAIL = "alr at fake.com";
+
+ public static final long ACCOUNT_ALRUBINGERL_ID = 1L;
+
+ public static final long USER_POKERGAME_ID = 2L;
+
+ public static final String USER_POKERGAME_NAME = "The Poker Game System";
+
+ public static final String USER_POKERGAME_EMAIL = "pokergame at fake.com";
+
+ public static final long ACCOUNT_POKERGAME_ID = 2L;
+
+ public static final BigDecimal INITIAL_ACCOUNT_BALANCE_ALR = new BigDecimal(500);
+
+ public static final BigDecimal INITIAL_ACCOUNT_BALANCE_POKERGAME = new BigDecimal(0);
+
+ //-------------------------------------------------------------------------------------||
+ // 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;
+}
Added: 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 (rev 0)
+++ projects/ejb-book/trunk/chxx-poker/src/test/java/org/jboss/ejb3/examples/chxx/transactions/ejb/DbQueryBean.java 2010-05-14 03:05:32 UTC (rev 104788)
@@ -0,0 +1,97 @@
+/*
+ * 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;
+
+/**
+ *
+ *
+ *
+ * @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 ------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+}
Added: 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 (rev 0)
+++ projects/ejb-book/trunk/chxx-poker/src/test/java/org/jboss/ejb3/examples/chxx/transactions/ejb/DbQueryLocalBusiness.java 2010-05-14 03:05:32 UTC (rev 104788)
@@ -0,0 +1,59 @@
+/*
+ * 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;
+}
Added: projects/ejb-book/trunk/chxx-poker/src/test/java/org/jboss/ejb3/examples/chxx/transactions/ejb/ForcedTestException.java
===================================================================
--- projects/ejb-book/trunk/chxx-poker/src/test/java/org/jboss/ejb3/examples/chxx/transactions/ejb/ForcedTestException.java (rev 0)
+++ projects/ejb-book/trunk/chxx-poker/src/test/java/org/jboss/ejb3/examples/chxx/transactions/ejb/ForcedTestException.java 2010-05-14 03:05:32 UTC (rev 104788)
@@ -0,0 +1,62 @@
+/*
+ * 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.ApplicationException;
+
+/**
+ * May be thrown manually by the test to force a transactional
+ * rollback condition
+ *
+ * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
+ * @version $Revision: $
+ */
+ at ApplicationException(rollback = true)
+public class ForcedTestException extends Exception
+{
+ //-------------------------------------------------------------------------------------||
+ // Class Members ----------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * serialVersionUID
+ */
+ private static final long serialVersionUID = 1L;
+
+ /**
+ * Message to be thrown
+ */
+ private static final String MESSAGE = "Intentional Test Exception";
+
+ //-------------------------------------------------------------------------------------||
+ // Constructor ------------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * Creates a new instance
+ */
+ public ForcedTestException()
+ {
+ super(MESSAGE);
+ }
+
+}
Added: projects/ejb-book/trunk/chxx-poker/src/test/java/org/jboss/ejb3/examples/chxx/transactions/ejb/TaskExecutionException.java
===================================================================
--- projects/ejb-book/trunk/chxx-poker/src/test/java/org/jboss/ejb3/examples/chxx/transactions/ejb/TaskExecutionException.java (rev 0)
+++ projects/ejb-book/trunk/chxx-poker/src/test/java/org/jboss/ejb3/examples/chxx/transactions/ejb/TaskExecutionException.java 2010-05-14 03:05:32 UTC (rev 104788)
@@ -0,0 +1,68 @@
+/*
+ * 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.ApplicationException;
+import javax.ejb.EJBException;
+
+/**
+ * Indicates that an exception has occurred while submitting
+ * a task to {@link TxWrappingLocalBusiness#wrapInTx(java.util.concurrent.Callable...)}.
+ * Not wrapped in {@link EJBException} because this is an {@link ApplicationException}
+ *
+ * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
+ * @version $Revision: $
+ */
+ at ApplicationException(rollback = true)
+public class TaskExecutionException extends Exception
+{
+ //-------------------------------------------------------------------------------------||
+ // Class Members ----------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * serialVersionUID
+ */
+ private static final long serialVersionUID = 1L;
+
+ //-------------------------------------------------------------------------------------||
+ // Constructor ------------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * Creates a new instance with the specified root cause
+ */
+ public TaskExecutionException(final Throwable cause)
+ {
+ super(cause);
+ }
+
+ /**
+ * Creates a new instance with the specified message
+ * @param message
+ */
+ public TaskExecutionException(String message)
+ {
+ super(message);
+ }
+
+}
Added: projects/ejb-book/trunk/chxx-poker/src/test/java/org/jboss/ejb3/examples/chxx/transactions/ejb/TxWrappingBean.java
===================================================================
--- projects/ejb-book/trunk/chxx-poker/src/test/java/org/jboss/ejb3/examples/chxx/transactions/ejb/TxWrappingBean.java (rev 0)
+++ projects/ejb-book/trunk/chxx-poker/src/test/java/org/jboss/ejb3/examples/chxx/transactions/ejb/TxWrappingBean.java 2010-05-14 03:05:32 UTC (rev 104788)
@@ -0,0 +1,79 @@
+/*
+ * 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.concurrent.Callable;
+
+import javax.ejb.Local;
+import javax.ejb.Stateless;
+import javax.ejb.TransactionAttribute;
+import javax.ejb.TransactionAttributeType;
+
+import org.jboss.ejb3.annotation.LocalBinding;
+
+/**
+ * EJB which wraps
+ *
+ * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
+ * @version $Revision: $
+ */
+ at Stateless
+ at Local(TxWrappingLocalBusiness.class)
+ at TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
+// We always require a new Tx here, so we ensure to wrap
+ at LocalBinding(jndiBinding = TxWrappingLocalBusiness.JNDI_NAME)
+public class TxWrappingBean implements TxWrappingLocalBusiness
+{
+ //-------------------------------------------------------------------------------------||
+ // Required Implementations -----------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * {@inheritDoc}
+ * @see org.jboss.ejb3.examples.chxx.transactions.ejb.TxWrappingLocalBusiness#wrapInTx(java.util.concurrent.Callable<V>[])
+ */
+ @Override
+ public void wrapInTx(final Callable<?>... tasks) throws IllegalArgumentException, TaskExecutionException
+ {
+ // Precondition check
+ if (tasks == null)
+ {
+ throw new IllegalArgumentException("task must be specified");
+ }
+
+ // Just delegate along to the tasks in order; now it's executed inside of a Tx
+ for (final Callable<?> task : tasks)
+ {
+ try
+ {
+ task.call();
+ }
+ // Every problem we encounter here becomes an ApplicationException
+ // to be unwrapped later by the caller (so the container doesn't wrap
+ // in EJBException
+ catch (final Throwable e)
+ {
+ throw new TaskExecutionException(e);
+ }
+ }
+ }
+}
Added: projects/ejb-book/trunk/chxx-poker/src/test/java/org/jboss/ejb3/examples/chxx/transactions/ejb/TxWrappingLocalBusiness.java
===================================================================
--- projects/ejb-book/trunk/chxx-poker/src/test/java/org/jboss/ejb3/examples/chxx/transactions/ejb/TxWrappingLocalBusiness.java (rev 0)
+++ projects/ejb-book/trunk/chxx-poker/src/test/java/org/jboss/ejb3/examples/chxx/transactions/ejb/TxWrappingLocalBusiness.java 2010-05-14 03:05:32 UTC (rev 104788)
@@ -0,0 +1,58 @@
+/*
+ * 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.concurrent.Callable;
+
+/**
+ * Contract of an EJB which wraps arbitrary {@link Callable}
+ * tasks inside of a new Tx.
+ *
+ * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
+ * @version $Revision: $
+ */
+public interface TxWrappingLocalBusiness
+{
+
+ //-------------------------------------------------------------------------------------||
+ // Constants --------------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * Name we'll ind to in JNDI
+ */
+ String JNDI_NAME = "TxWrappingLocal";
+
+ //-------------------------------------------------------------------------------------||
+ // Contracts --------------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * Wraps the specified tasks in a new Transaction
+ *
+ * @param task
+ * @throws IllegalArgumentException If no tasks are specified
+ * @throws TaskExecutionException If an error occurred in invoking {@link Callable#call()}
+ */
+ void wrapInTx(Callable<?>... tasks) throws IllegalArgumentException, TaskExecutionException;
+
+}
More information about the jboss-cvs-commits
mailing list