1 22 package org.jboss.ejb3.test.standalone.unit; 23 24 import java.io.IOException ; 25 import java.io.InputStream ; 26 import java.net.URL ; 27 import java.util.Hashtable ; 28 import java.util.Properties ; 29 import javax.ejb.NoSuchEJBException ; 30 import javax.naming.InitialContext ; 31 import javax.naming.NamingException ; 32 import org.jboss.ejb3.embedded.EJB3StandaloneBootstrap; 33 import org.jboss.ejb3.embedded.EJB3StandaloneDeployer; 34 import org.jboss.ejb3.embedded.EJB3StandaloneDeployment; 35 import org.jboss.ejb3.test.standalone.CalculatorRemote; 36 import org.jboss.ejb3.test.standalone.Customer; 37 import org.jboss.ejb3.test.standalone.CustomerDAO; 38 import org.jboss.ejb3.test.standalone.ShoppingCart; 39 import junit.framework.TestCase; 40 import junit.framework.Test; 41 import junit.framework.TestSuite; 42 import junit.extensions.TestSetup; 43 44 50 public class DeployerTestCase extends TestCase 51 { 52 private static boolean booted = false; 53 54 public DeployerTestCase(String name) 55 { 56 super(name); 57 } 58 59 public static Test suite() throws Exception 60 { 61 TestSuite suite = new TestSuite(); 62 suite.addTestSuite(DeployerTestCase.class); 63 64 65 TestSetup wrapper = new TestSetup(suite) 67 { 68 protected void setUp() 69 { 70 startupEmbeddedJboss(); 71 } 72 73 protected void tearDown() 74 { 75 shutdownEmbeddedJboss(); 76 } 77 }; 78 79 return wrapper; 80 } 81 82 public static void startupEmbeddedJboss() 83 { 84 EJB3StandaloneBootstrap.boot(null); 85 } 86 87 public static void shutdownEmbeddedJboss() 88 { 89 EJB3StandaloneBootstrap.shutdown(); 90 } 91 92 93 protected InitialContext getInitialContext() throws Exception 94 { 95 return new InitialContext (getInitialContextProperties()); 96 } 97 98 protected Hashtable getInitialContextProperties() 99 { 100 return EJB3StandaloneBootstrap.getInitialContextProperties(); 101 } 102 103 104 private Properties getDefaultPersistenceProperties() 105 throws IOException 106 { 107 InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("default.persistence.properties"); 108 assertNotNull(is); 109 Properties defaults = new Properties (); 110 defaults.load(is); 111 return defaults; 112 } 113 114 public void testArchives() throws Throwable 115 { 116 InitialContext ctx = getInitialContext(); 117 118 URL res = Thread.currentThread().getContextClassLoader().getResource("marker.txt"); 119 URL archive = EJB3StandaloneDeployer.getContainingUrlFromResource(res, "marker.txt"); 120 EJB3StandaloneDeployer deployer = new EJB3StandaloneDeployer(); 121 deployer.setKernel(EJB3StandaloneBootstrap.getKernel()); 122 deployer.setJndiProperties(getInitialContextProperties()); 123 deployer.getArchives().add(archive); 124 125 completeTest(deployer, ctx); 126 127 } 128 129 public void testDeploy() throws Throwable 130 { 131 InitialContext ctx = getInitialContext(); 132 133 URL res = Thread.currentThread().getContextClassLoader().getResource("marker.txt"); 134 URL deployDir = EJB3StandaloneDeployer.getDeployDirFromResource(res, "marker.txt"); 135 EJB3StandaloneDeployer deployer = new EJB3StandaloneDeployer(); 136 deployer.setKernel(EJB3StandaloneBootstrap.getKernel()); 137 deployer.setJndiProperties(getInitialContextProperties()); 138 deployer.setDefaultPersistenceProperties(getDefaultPersistenceProperties()); 139 System.out.println("deployDir = " + deployDir); 140 deployer.getDeployDirs().add(deployDir); 141 142 completeTest(deployer, ctx); 143 144 } 145 146 public void testArchivesByResource() throws Throwable 147 { 148 InitialContext ctx = getInitialContext(); 149 150 EJB3StandaloneDeployer deployer = new EJB3StandaloneDeployer(); 151 deployer.setKernel(EJB3StandaloneBootstrap.getKernel()); 152 deployer.setJndiProperties(getInitialContextProperties()); 153 deployer.setDefaultPersistenceProperties(getDefaultPersistenceProperties()); 154 deployer.getArchivesByResource().add("marker.txt"); 155 156 completeTest(deployer, ctx); 157 158 } 159 160 public void testDeployByResource() throws Throwable 161 { 162 InitialContext ctx = getInitialContext(); 163 164 EJB3StandaloneDeployer deployer = new EJB3StandaloneDeployer(); 165 deployer.setKernel(EJB3StandaloneBootstrap.getKernel()); 166 deployer.setJndiProperties(getInitialContextProperties()); 167 deployer.setDefaultPersistenceProperties(getDefaultPersistenceProperties()); 168 deployer.getDeployDirsByResource().add("marker.txt"); 169 170 completeTest(deployer, ctx); 171 172 } 173 174 public void testCleanup() throws Throwable 175 { 176 boolean exceptionThrown = false; 177 try 178 { 179 executeEJBs(getInitialContext()); 180 } 181 catch (Exception e) 182 { 183 exceptionThrown = true; 184 } 185 assertTrue(exceptionThrown); 186 187 } 188 189 private void completeTest(EJB3StandaloneDeployer deployer, InitialContext ctx) 190 throws Exception 191 { 192 deployer.create(); 193 deployer.start(); 194 195 executeEJBs(ctx); 196 197 deployer.stop(); 198 deployer.destroy(); 199 200 deployer.create(); 201 deployer.start(); 202 203 executeEJBs(ctx); 204 205 deployer.stop(); 206 deployer.destroy(); 207 } 208 209 private void executeEJBs(InitialContext ctx) 210 throws NamingException 211 { 212 CustomerDAO local = (CustomerDAO)ctx.lookup("CustomerDAOBean/local"); 213 long id = local.createCustomer(); 214 Customer cust = local.findCustomer(id); 215 assertEquals("Bill", cust.getName()); 216 217 ShoppingCart cart = (ShoppingCart)ctx.lookup("ShoppingCartBean/local"); 218 cart.getCart().add("beer"); 219 cart.getCart().add("wine"); 220 assertEquals(2, cart.getCart().size()); 221 222 cart.checkout(); 223 224 boolean exceptionThrown = false; 225 try 226 { 227 cart.getCart(); 228 } 229 catch (NoSuchEJBException e) 230 { 231 exceptionThrown = true; 232 } 233 234 assertTrue(exceptionThrown); 235 236 CalculatorRemote calc = (CalculatorRemote)ctx.lookup("CalculatorBean/remote"); 237 assertEquals(2, calc.add(1, 1)); 238 } 239 240 protected void configureLoggingAfterBootstrap() 241 { 242 } 244 } | Popular Tags |