FileDocCategorySizeDatePackage
EmbeddedEjb3TestCase.javaAPI DocJBoss 4.2.14966Fri Jul 13 20:55:28 BST 2007org.jboss.tutorial.junit

EmbeddedEjb3TestCase

public class EmbeddedEjb3TestCase extends TestCase
Comment
author
Bill Burke
version
$Revision: 60233 $

Fields Summary
Constructors Summary
public EmbeddedEjb3TestCase()

      super("EmbeddedEjb3TestCase");
   
Methods Summary
public static javax.naming.InitialContextgetInitialContext()

      Hashtable props = getInitialContextProperties();
      return new InitialContext(props);
   
private static java.util.HashtablegetInitialContextProperties()

      Hashtable props = new Hashtable();
      props.put("java.naming.factory.initial", "org.jnp.interfaces.LocalOnlyContextFactory");
      props.put("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces");
      return props;
   
public static voidshutdownEmbeddedJboss()

      EJB3StandaloneBootstrap.shutdown();
   
public static voidstartupEmbeddedJboss()

         EJB3StandaloneBootstrap.boot(null);
         EJB3StandaloneBootstrap.scanClasspath("tutorial.jar");
   
public static junit.framework.Testsuite()

      TestSuite suite = new TestSuite();
      suite.addTestSuite(EmbeddedEjb3TestCase.class);


      // setup test so that embedded JBoss is started/stopped once for all tests here.
      TestSetup wrapper = new TestSetup(suite)
      {
         protected void setUp()
         {
            startupEmbeddedJboss();
         }

         protected void tearDown()
         {
            shutdownEmbeddedJboss();
         }
      };

      return wrapper;
   
public voidtestEJBs()


      InitialContext ctx = getInitialContext();
      CustomerDAOLocal local = (CustomerDAOLocal) ctx.lookup("CustomerDAOBean/local");
      CustomerDAORemote remote = (CustomerDAORemote) ctx.lookup("CustomerDAOBean/remote");

      System.out.println("----------------------------------------------------------");
      System.out.println("This test scans the System Property java.class.path for all annotated EJB3 classes");
      System.out.print("    ");

      int id = local.createCustomer("Gavin");
      Customer cust = local.findCustomer(id);
      assertNotNull(cust);
      System.out.println("Successfully created and found Gavin from @Local interface");

      id = remote.createCustomer("Emmanuel");
      cust = remote.findCustomer(id);
      assertNotNull(cust);
      System.out.println("Successfully created and found Emmanuel from @Remote interface");
      System.out.println("----------------------------------------------------------");
   
public voidtestEntityManager()

      // This is a transactionally aware EntityManager and must be accessed within a JTA transaction
      // Why aren't we using javax.persistence.Persistence?  Well, our persistence.xml file uses
      // jta-datasource which means that it is created by the EJB container/embedded JBoss.
      // using javax.persistence.Persistence will just cause us an error
      EntityManager em = (EntityManager) getInitialContext().lookup("java:/EntityManagers/custdb");

      // Obtain JBoss transaction
      TransactionManager tm = (TransactionManager) getInitialContext().lookup("java:/TransactionManager");

      tm.begin();

      Customer cust = new Customer();
      cust.setName("Bill");
      em.persist(cust);

      assertTrue(cust.getId() > 0);

      int id = cust.getId();

      System.out.println("created bill in DB with id: " + id);

      tm.commit();

      tm.begin();
      cust = em.find(Customer.class, id);
      assertNotNull(cust);
      tm.commit();