KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > test > wls > embeddedwar > unit > EmbeddedEjb3TestCase


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.ejb3.test.wls.embeddedwar.unit;
8
9 import java.util.Hashtable JavaDoc;
10 import javax.naming.InitialContext JavaDoc;
11 import javax.persistence.EntityManager;
12 import javax.transaction.TransactionManager JavaDoc;
13
14 import junit.framework.TestCase;
15
16 import org.jboss.ejb3.test.wls.embeddedwar.*;
17
18 /**
19  * Comment
20  *
21  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
22  * @version $Revision: 39547 $
23  */

24 public class EmbeddedEjb3TestCase
25    extends TestCase
26 {
27    public static void main(String JavaDoc[] args) throws Exception JavaDoc
28    {
29       EmbeddedEjb3TestCase test = new EmbeddedEjb3TestCase();
30       test.testEJBs();
31       test.testEntityManager();
32    }
33    
34    public EmbeddedEjb3TestCase()
35    {
36    }
37
38    public void testEJBs() throws Exception JavaDoc
39    {
40
41       InitialContext JavaDoc ctx = getInitialContext();
42       CustomerDAOLocal local = (CustomerDAOLocal) ctx.lookup("CustomerDAOBean/local");
43       CustomerDAORemote remote = (CustomerDAORemote) ctx.lookup("CustomerDAOBean/remote");
44
45       System.out.println("----------------------------------------------------------");
46       System.out.println("This test scans the System Property java.class.path for all annotated EJB3 classes");
47       System.out.print(" ");
48
49       int id = local.createCustomer("Gavin");
50       Customer cust = local.findCustomer(id);
51       System.out.println("Successfully created and found Gavin from @Local interface");
52
53       id = remote.createCustomer("Emmanuel");
54       cust = remote.findCustomer(id);
55       System.out.println("Successfully created and found Emmanuel from @Remote interface");
56       System.out.println("----------------------------------------------------------");
57    }
58
59    public void testEntityManager() throws Exception JavaDoc
60    {
61       // This is a transactionally aware EntityManager and must be accessed within a JTA transaction
62
// Why aren't we using javax.persistence.Persistence? Well, our persistence.xml file uses
63
// jta-datasource which means that it is created by the EJB container/embedded JBoss.
64
// using javax.persistence.Persistence will just cause us an error
65
EntityManager em = (EntityManager) getInitialContext().lookup("java:/EntityManagers/custdb");
66
67       // Obtain JBoss transaction
68
TransactionManager JavaDoc tm = (TransactionManager JavaDoc) getInitialContext().lookup("java:/TransactionManager");
69
70       tm.begin();
71
72       Customer cust = new Customer();
73       cust.setName("Bill");
74       em.persist(cust);
75       
76       int id = cust.getId();
77
78       System.out.println("created bill in DB with id: " + id);
79
80       tm.commit();
81
82       tm.begin();
83       cust = em.find(Customer.class, id);
84  
85       tm.commit();
86    }
87
88    public static InitialContext JavaDoc getInitialContext() throws Exception JavaDoc
89    {
90       Hashtable JavaDoc props = getInitialContextProperties();
91       return new InitialContext JavaDoc(props);
92    }
93
94    private static Hashtable JavaDoc getInitialContextProperties()
95    {
96       Hashtable JavaDoc props = new Hashtable JavaDoc();
97  // props.put("java.naming.factory.initial", "org.jnp.interfaces.LocalOnlyContextFactory");
98
// props.put("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces");
99
props.put("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
100       props.put("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces");
101       props.put("java.naming.provider.url", "jnp://localhost:1099");
102       return props;
103    }
104 }
105
Popular Tags