KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > tutorial > junit > EmbeddedEjb3TestCase


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.tutorial.junit;
23
24 import java.util.Hashtable JavaDoc;
25 import javax.naming.InitialContext JavaDoc;
26 import javax.persistence.EntityManager;
27 import javax.transaction.TransactionManager JavaDoc;
28 import org.jboss.ejb3.embedded.EJB3StandaloneBootstrap;
29 import junit.framework.TestCase;
30 import junit.framework.Test;
31 import junit.framework.TestSuite;
32 import junit.extensions.TestSetup;
33
34 /**
35  * Comment
36  *
37  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
38  * @version $Revision: 42757 $
39  */

40 public class EmbeddedEjb3TestCase extends TestCase
41 {
42    public EmbeddedEjb3TestCase()
43    {
44       super("EmbeddedEjb3TestCase");
45    }
46
47
48    public static Test suite() throws Exception JavaDoc
49    {
50       TestSuite suite = new TestSuite();
51       suite.addTestSuite(EmbeddedEjb3TestCase.class);
52
53
54       // setup test so that embedded JBoss is started/stopped once for all tests here.
55
TestSetup wrapper = new TestSetup(suite)
56       {
57          protected void setUp()
58          {
59             startupEmbeddedJboss();
60          }
61
62          protected void tearDown()
63          {
64             shutdownEmbeddedJboss();
65          }
66       };
67
68       return wrapper;
69    }
70
71    public static void startupEmbeddedJboss()
72    {
73          EJB3StandaloneBootstrap.boot(null);
74          EJB3StandaloneBootstrap.scanClasspath("tutorial.jar");
75    }
76
77    public static void shutdownEmbeddedJboss()
78    {
79       EJB3StandaloneBootstrap.shutdown();
80    }
81
82    public void testEJBs() throws Exception JavaDoc
83    {
84
85       InitialContext JavaDoc ctx = getInitialContext();
86       CustomerDAOLocal local = (CustomerDAOLocal) ctx.lookup("CustomerDAOBean/local");
87       CustomerDAORemote remote = (CustomerDAORemote) ctx.lookup("CustomerDAOBean/remote");
88
89       System.out.println("----------------------------------------------------------");
90       System.out.println("This test scans the System Property java.class.path for all annotated EJB3 classes");
91       System.out.print(" ");
92
93       int id = local.createCustomer("Gavin");
94       Customer cust = local.findCustomer(id);
95       assertNotNull(cust);
96       System.out.println("Successfully created and found Gavin from @Local interface");
97
98       id = remote.createCustomer("Emmanuel");
99       cust = remote.findCustomer(id);
100       assertNotNull(cust);
101       System.out.println("Successfully created and found Emmanuel from @Remote interface");
102       System.out.println("----------------------------------------------------------");
103    }
104
105    public void testEntityManager() throws Exception JavaDoc
106    {
107       // This is a transactionally aware EntityManager and must be accessed within a JTA transaction
108
// Why aren't we using javax.persistence.Persistence? Well, our persistence.xml file uses
109
// jta-datasource which means that it is created by the EJB container/embedded JBoss.
110
// using javax.persistence.Persistence will just cause us an error
111
EntityManager em = (EntityManager) getInitialContext().lookup("java:/EntityManagers/custdb");
112
113       // Obtain JBoss transaction
114
TransactionManager JavaDoc tm = (TransactionManager JavaDoc) getInitialContext().lookup("java:/TransactionManager");
115
116       tm.begin();
117
118       Customer cust = new Customer();
119       cust.setName("Bill");
120       em.persist(cust);
121
122       assertTrue(cust.getId() > 0);
123
124       int id = cust.getId();
125
126       System.out.println("created bill in DB with id: " + id);
127
128       tm.commit();
129
130       tm.begin();
131       cust = em.find(Customer.class, id);
132       assertNotNull(cust);
133       tm.commit();
134    }
135
136    public static InitialContext JavaDoc getInitialContext() throws Exception JavaDoc
137    {
138       Hashtable JavaDoc props = getInitialContextProperties();
139       return new InitialContext JavaDoc(props);
140    }
141
142    private static Hashtable JavaDoc getInitialContextProperties()
143    {
144       Hashtable JavaDoc props = new Hashtable JavaDoc();
145       props.put("java.naming.factory.initial", "org.jnp.interfaces.LocalOnlyContextFactory");
146       props.put("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces");
147       return props;
148    }
149 }
150
Popular Tags