001: /*
002: * JBoss, the OpenSource J2EE webOS
003: *
004: * Distributable under LGPL license.
005: * See terms of license at gnu.org.
006: */
007: package org.jboss.ejb3.test.wls.embeddedwar.unit;
008:
009: import java.util.Hashtable;
010: import javax.naming.InitialContext;
011: import javax.persistence.EntityManager;
012: import javax.transaction.TransactionManager;
013:
014: import junit.framework.TestCase;
015:
016: import org.jboss.ejb3.test.wls.embeddedwar.*;
017:
018: /**
019: * Comment
020: *
021: * @author <a href="mailto:bill@jboss.org">Bill Burke</a>
022: * @version $Revision: 60233 $
023: */
024: public class EmbeddedEjb3TestCase extends TestCase {
025: public static void main(String[] args) throws Exception {
026: EmbeddedEjb3TestCase test = new EmbeddedEjb3TestCase();
027: test.testEJBs();
028: test.testEntityManager();
029: }
030:
031: public EmbeddedEjb3TestCase() {
032: }
033:
034: public void testEJBs() throws Exception {
035:
036: InitialContext ctx = getInitialContext();
037: CustomerDAOLocal local = (CustomerDAOLocal) ctx
038: .lookup("CustomerDAOBean/local");
039: CustomerDAORemote remote = (CustomerDAORemote) ctx
040: .lookup("CustomerDAOBean/remote");
041:
042: System.out
043: .println("----------------------------------------------------------");
044: System.out
045: .println("This test scans the System Property java.class.path for all annotated EJB3 classes");
046: System.out.print(" ");
047:
048: int id = local.createCustomer("Gavin");
049: Customer cust = local.findCustomer(id);
050: System.out
051: .println("Successfully created and found Gavin from @Local interface");
052:
053: id = remote.createCustomer("Emmanuel");
054: cust = remote.findCustomer(id);
055: System.out
056: .println("Successfully created and found Emmanuel from @Remote interface");
057: System.out
058: .println("----------------------------------------------------------");
059: }
060:
061: public void testEntityManager() throws Exception {
062: // This is a transactionally aware EntityManager and must be accessed within a JTA transaction
063: // Why aren't we using javax.persistence.Persistence? Well, our persistence.xml file uses
064: // jta-datasource which means that it is created by the EJB container/embedded JBoss.
065: // using javax.persistence.Persistence will just cause us an error
066: EntityManager em = (EntityManager) getInitialContext().lookup(
067: "java:/EntityManagers/custdb");
068:
069: // Obtain JBoss transaction
070: TransactionManager tm = (TransactionManager) getInitialContext()
071: .lookup("java:/TransactionManager");
072:
073: tm.begin();
074:
075: Customer cust = new Customer();
076: cust.setName("Bill");
077: em.persist(cust);
078:
079: int id = cust.getId();
080:
081: System.out.println("created bill in DB with id: " + id);
082:
083: tm.commit();
084:
085: tm.begin();
086: cust = em.find(Customer.class, id);
087:
088: tm.commit();
089: }
090:
091: public static InitialContext getInitialContext() throws Exception {
092: Hashtable props = getInitialContextProperties();
093: return new InitialContext(props);
094: }
095:
096: private static Hashtable getInitialContextProperties() {
097: Hashtable props = new Hashtable();
098: // props.put("java.naming.factory.initial", "org.jnp.interfaces.LocalOnlyContextFactory");
099: // props.put("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces");
100: props.put("java.naming.factory.initial",
101: "org.jnp.interfaces.NamingContextFactory");
102: props.put("java.naming.factory.url.pkgs",
103: "org.jboss.naming:org.jnp.interfaces");
104: props.put("java.naming.provider.url", "jnp://localhost:1099");
105: return props;
106: }
107: }
|