1 7 package org.jboss.ejb3.test.wls.embeddedwar.unit; 8 9 import java.util.Hashtable ; 10 import javax.naming.InitialContext ; 11 import javax.persistence.EntityManager; 12 import javax.transaction.TransactionManager ; 13 14 import junit.framework.TestCase; 15 16 import org.jboss.ejb3.test.wls.embeddedwar.*; 17 18 24 public class EmbeddedEjb3TestCase 25 extends TestCase 26 { 27 public static void main(String [] args) throws Exception 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 39 { 40 41 InitialContext 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 60 { 61 EntityManager em = (EntityManager) getInitialContext().lookup("java:/EntityManagers/custdb"); 66 67 TransactionManager tm = (TransactionManager ) 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 getInitialContext() throws Exception 89 { 90 Hashtable props = getInitialContextProperties(); 91 return new InitialContext (props); 92 } 93 94 private static Hashtable getInitialContextProperties() 95 { 96 Hashtable props = new Hashtable (); 97 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 |