1 22 package org.jboss.tutorial.junit; 23 24 import java.util.Hashtable ; 25 import javax.naming.InitialContext ; 26 import javax.persistence.EntityManager; 27 import javax.transaction.TransactionManager ; 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 40 public class EmbeddedEjb3TestCase extends TestCase 41 { 42 public EmbeddedEjb3TestCase() 43 { 44 super("EmbeddedEjb3TestCase"); 45 } 46 47 48 public static Test suite() throws Exception 49 { 50 TestSuite suite = new TestSuite(); 51 suite.addTestSuite(EmbeddedEjb3TestCase.class); 52 53 54 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 83 { 84 85 InitialContext 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 106 { 107 EntityManager em = (EntityManager) getInitialContext().lookup("java:/EntityManagers/custdb"); 112 113 TransactionManager tm = (TransactionManager ) 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 getInitialContext() throws Exception 137 { 138 Hashtable props = getInitialContextProperties(); 139 return new InitialContext (props); 140 } 141 142 private static Hashtable getInitialContextProperties() 143 { 144 Hashtable props = new Hashtable (); 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 |