tomee-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Alexander Saint Croix" <saintx.opensou...@gmail.com>
Subject Re: Transactions, detached objects and fault loading in Unit Tests
Date Tue, 22 Jan 2008 00:00:07 GMT
In order to clean things up a bit and make them easier to show over e-mail,
I condensed the example test case so that it initializes its own entity
manager and does not involve itself with any session bean at all.  Here is
the code, marked where it fails.

Cheers,
--
Alex  (code below)

package org.eremite.corm.party;
>
> import junit.framework.Assert;
> import junit.framework.TestCase;
> import org.eremite.corm.party.address.Address;
> import org.eremite.corm.party.address.AssociatedAddress;
> import org.eremite.corm.party.relationship.Capability;
>
> import javax.naming.Context;
> import javax.naming.InitialContext;
> import javax.naming.NamingException;
> import javax.persistence.EntityManager;
> import javax.persistence.EntityManagerFactory;
> import javax.persistence.Persistence;
> import javax.transaction.Status;
> import javax.transaction.TransactionManager;
> import java.util.Properties;
> import java.util.Set;
>
> public class EfficientPartyCascadeTest extends TestCase {
>
>     private TransactionManager txMan;
>     private EntityManagerFactory emf;
>     private EntityManager em;
>     private Context context;
>
>     public void beginTransaction() throws Exception {
>         txMan.begin();
>     }
>
>     protected void completeTransaction() throws Exception {
>         int status = txMan.getStatus();
>         if (status == Status.STATUS_ACTIVE) {
>             txMan.commit();
>         } else if (status != Status.STATUS_NO_TRANSACTION) {
>             txMan.rollback();
>         }
>     }
>
>     public void setUp(){
>
>         Properties p = new Properties();
>         p.put(Context.INITIAL_CONTEXT_FACTORY, "
> org.apache.openejb.client.LocalInitialContextFactory");
>         p.put("partyDB", "new://Resource?type=DataSource");
>         p.put("partyDB.JdbcDriver", "org.hsqldb.jdbcDriver");
>         p.put("partyDB.JdbcUrl", "jdbc:hsqldb:mem:partydb");
>
>         p.put("partyDBUnmanaged", "new://Resource?type=DataSource");
>         p.put("partyDBUnmanaged.JdbcDriver", "org.hsqldb.jdbcDriver");
>         p.put("partyDBUnmanaged.JdbcUrl", "jdbc:hsqldb:mem:partydb");
>         p.put("partyDBUnmanaged.JtaManaged", "false");
>         try {
>             context = new InitialContext(p);
>         } catch (NamingException e) {
>             e.printStackTrace();
>         }
>         Assert.assertNotNull("context", context);
>
>         try {
>             txMan = (TransactionManager) context.lookup
> ("java:openejb/TransactionManager");
>         } catch (NamingException e) {
>             e.printStackTrace();
>         }
>         Assert.assertNotNull("transactionManager", txMan);
>
>         emf = Persistence.createEntityManagerFactory("party-test-unit");
>         Assert.assertNotNull("entity manager factory", emf);
>
>         em = emf.createEntityManager();
>         Assert.assertNotNull("entity manager", em);
>     }
>
>     public void testFaultLoading() throws Exception {
>         Party initial = new Party();
>         initial.addCapability(new Capability());
>         new AssociatedAddress(initial, new Address()); // wires itself
> into initial and address
>         long initialID = initial.getID();
>         initial.setName("Initial");
>         em.persist(initial);
>         Assert.assertTrue(initial.getID() != initialID);
>
>         beginTransaction();
>         try {
>             System.out.println("Getting party with ID: " + initial.getID
> ());
>             Party party = em.find(Party.class, initial.getID());
>             Assert.assertNotNull(party);
>             Set<Capability> caps = party.getCapabilities();
>             Assert.assertNotNull(caps); // <------------------------ Test
> fails here
>             Assert.assertTrue(caps.size() == 1);
>             Set<AssociatedAddress> addresses = party.getAddresses();
>             Assert.assertNotNull(addresses);
>             Assert.assertTrue(addresses.size() == 1);
>         } finally {
>             completeTransaction();
>         }
>     }
> }
>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message