Search in sources :

Example 11 with TestFailureException

use of org.apache.openejb.test.TestFailureException in project tomee by apache.

the class PersistenceContextStatefulBean method testPropagatedPersistenceContext.

public void testPropagatedPersistenceContext() throws TestFailureException {
    try {
        try {
            final InitialContext ctx = new InitialContext();
            Assert.assertNotNull("The InitialContext is null", ctx);
            final EntityManager em = (EntityManager) ctx.lookup("java:comp/env/persistence/ExtendedTestContext");
            Assert.assertNotNull("The EntityManager is null", em);
            // call a do nothing method to assure entity manager actually exists
            em.getFlushMode();
            // get the raw entity manager so we can test it below
            inheritedDelegate = (EntityManager) em.getDelegate();
            // The extended entity manager is not propigated to a non-extended entity manager unless there is a transaction
            final EntityManager nonExtendedEm = (EntityManager) ctx.lookup("java:comp/env/persistence/TestContext");
            nonExtendedEm.getFlushMode();
            final EntityManager nonExtendedDelegate = ((EntityManager) nonExtendedEm.getDelegate());
            Assert.assertTrue("non-extended entity manager should be open", nonExtendedDelegate.isOpen());
            Assert.assertNotSame("Extended non-extended entity manager shound not be the same instance as extendend entity manager when accessed out side of a transactions", inheritedDelegate, nonExtendedDelegate);
            // When the non-extended entity manager is accessed within a transaction is should see the stateful extended context.
            //
            // Note: this code also tests EBJ 3.0 Persistence spec 5.9.1 "UserTransaction is begun within the method, the
            // container associates the persistence context with the JTA transaction and calls EntityManager.joinTransaction."
            // If our the extended entity manager were not associted with the transaction, the non-extended entity manager would
            // not see it.
            final UserTransaction userTransaction = ejbContext.getUserTransaction();
            userTransaction.begin();
            try {
                Assert.assertSame("Extended non-extended entity manager to be same instance as extendend entity manager", inheritedDelegate, nonExtendedEm.getDelegate());
            } finally {
                userTransaction.commit();
            }
            // When a stateful bean with an extended entity manager creates another stateful bean, the new bean will
            // inherit the extended entity manager (assuming it contains an extended entity manager for the same persistence
            // unit).
            final PersistenceContextStatefulHome home = (PersistenceContextStatefulHome) ejbContext.getEJBHome();
            final PersistenceContextStatefulObject object = home.create();
            // test the new stateful bean recieved the context
            object.testPropgation();
            // remove the bean
            object.remove();
        } catch (final Exception e) {
            Assert.fail("Received Exception " + e.getClass() + " : " + e.getMessage());
        }
    } catch (final AssertionFailedError afe) {
        throw new TestFailureException(afe);
    }
}
Also used : UserTransaction(javax.transaction.UserTransaction) EntityManager(javax.persistence.EntityManager) TestFailureException(org.apache.openejb.test.TestFailureException) AssertionFailedError(junit.framework.AssertionFailedError) InitialContext(javax.naming.InitialContext) TestFailureException(org.apache.openejb.test.TestFailureException)

Example 12 with TestFailureException

use of org.apache.openejb.test.TestFailureException in project tomee by apache.

the class SetterInjectionStatefulBean method lookupFloatEntry.

public void lookupFloatEntry() throws TestFailureException {
    try {
        final Float expected = new Float(1.0F);
        Assert.assertNotNull("The Float looked up is null", flooatField);
        Assert.assertEquals(expected, flooatField);
    } catch (final AssertionFailedError afe) {
        throw new TestFailureException(afe);
    }
}
Also used : TestFailureException(org.apache.openejb.test.TestFailureException) AssertionFailedError(junit.framework.AssertionFailedError)

Example 13 with TestFailureException

use of org.apache.openejb.test.TestFailureException in project tomee by apache.

the class SetterInjectionStatefulBean method lookupLongEntry.

public void lookupLongEntry() throws TestFailureException {
    try {
        final Long expected = new Long(1L);
        Assert.assertNotNull("The Long looked up is null", loongField);
        Assert.assertEquals(expected, loongField);
    } catch (final AssertionFailedError afe) {
        throw new TestFailureException(afe);
    }
}
Also used : TestFailureException(org.apache.openejb.test.TestFailureException) AssertionFailedError(junit.framework.AssertionFailedError)

Example 14 with TestFailureException

use of org.apache.openejb.test.TestFailureException in project tomee by apache.

the class SetterInjectionStatefulBean method lookupCharacterEntry.

public void lookupCharacterEntry() throws TestFailureException {
    try {
        final Character expected = new Character('D');
        Assert.assertNotNull("The Character looked up is null", chaaracterField);
        Assert.assertEquals(expected, chaaracterField);
    } catch (final AssertionFailedError afe) {
        throw new TestFailureException(afe);
    }
}
Also used : TestFailureException(org.apache.openejb.test.TestFailureException) AssertionFailedError(junit.framework.AssertionFailedError)

Example 15 with TestFailureException

use of org.apache.openejb.test.TestFailureException in project tomee by apache.

the class AnnotatedFieldInjectionStatelessBean method lookupJMSConnectionFactory.

public void lookupJMSConnectionFactory() throws TestFailureException {
    try {
        try {
            testJmsConnection(coonnectionFactory.createConnection());
            testJmsConnection(queueCoonnectionFactory.createConnection());
            testJmsConnection(topicCoonnectionFactory.createConnection());
        } catch (final Exception e) {
            e.printStackTrace();
            Assert.fail("Received Exception " + e.getClass() + " : " + e.getMessage());
        }
    } catch (final AssertionFailedError afe) {
        throw new TestFailureException(afe);
    }
}
Also used : TestFailureException(org.apache.openejb.test.TestFailureException) AssertionFailedError(junit.framework.AssertionFailedError) JMSException(javax.jms.JMSException) TestFailureException(org.apache.openejb.test.TestFailureException) CreateException(javax.ejb.CreateException)

Aggregations

AssertionFailedError (junit.framework.AssertionFailedError)503 TestFailureException (org.apache.openejb.test.TestFailureException)503 JMSException (javax.jms.JMSException)336 EJBException (javax.ejb.EJBException)334 RemoteException (java.rmi.RemoteException)268 InitialContext (javax.naming.InitialContext)168 RemoveException (javax.ejb.RemoveException)80 CreateException (javax.ejb.CreateException)77 NamingException (javax.naming.NamingException)65 BasicStatefulObject (org.apache.openejb.test.stateful.BasicStatefulObject)42 BasicBmpObject (org.apache.openejb.test.entity.bmp.BasicBmpObject)41 BasicStatelessObject (org.apache.openejb.test.stateless.BasicStatelessObject)38 EntityManager (javax.persistence.EntityManager)21 EntityManagerFactory (javax.persistence.EntityManagerFactory)17 DataSource (javax.sql.DataSource)16 ConnectionFactory (javax.jms.ConnectionFactory)15 QueueConnectionFactory (javax.jms.QueueConnectionFactory)15 TopicConnectionFactory (javax.jms.TopicConnectionFactory)15 EJBContext (javax.ejb.EJBContext)12 BasicBmpHome (org.apache.openejb.test.entity.bmp.BasicBmpHome)12