1 22 package org.jboss.ejb3.test.standalone.unit; 23 24 import java.io.IOException ; 25 import java.io.InputStream ; 26 import java.lang.reflect.Constructor ; 27 import java.lang.reflect.Method ; 28 import java.net.URL ; 29 30 import java.util.HashMap ; 31 import java.util.Hashtable ; 32 import java.util.Iterator ; 33 import java.util.LinkedList ; 34 import java.util.List ; 35 import java.util.Properties ; 36 import java.util.StringTokenizer ; 37 import javax.naming.InitialContext ; 38 import javax.naming.NamingEnumeration ; 39 40 41 import org.jboss.ejb3.embedded.EJB3StandaloneBootstrap; 42 import org.jboss.ejb3.embedded.EJB3StandaloneDeployer; 43 import junit.framework.TestCase; 44 import junit.framework.Test; 45 import junit.framework.TestSuite; 46 import junit.extensions.TestSetup; 47 48 import org.jboss.logging.Logger; 49 50 54 public class StandardTestCase extends TestCase 55 { 56 private static final Logger log = Logger 57 .getLogger(StandardTestCase.class); 58 59 private static boolean booted = false; 60 61 private static List tests = new LinkedList (); 62 63 static 64 { 65 tests.add(new StandardTestCaseTest("org.jboss.ejb3.test.jca.inflowmdb.unit.InflowUnitTestCase", "jmsinflowmdb.jar", "testJMS", "standard/jca-inflowmdb-beans.xml")); 66 tests.add(new StandardTestCaseTest("org.jboss.ejb3.test.mdb.unit.MDBUnitTestCase", "mdb-test.jar", null, "security-beans.xml,standard/testjms.xml")); 67 } 68 69 public StandardTestCase(String name) 70 { 71 super(name); 72 } 73 74 public static Test suite() throws Exception 75 { 76 TestSuite suite = new TestSuite(); 77 suite.addTestSuite(StandardTestCase.class); 78 79 return suite; 80 } 81 82 public static void startupEmbeddedJboss(StandardTestCaseTest test) 83 { 84 EJB3StandaloneBootstrap.boot(null); 85 EJB3StandaloneBootstrap.deployXmlResource("jboss-jms-beans.xml"); 86 87 if (test.xmlResources != null) 88 { 89 StringTokenizer tokenizer = new StringTokenizer (test.xmlResources, ","); 90 while (tokenizer.hasMoreTokens()) 91 { 92 String testXml = tokenizer.nextToken(); 93 EJB3StandaloneBootstrap.deployXmlResource(testXml); 94 } 95 } 96 } 97 98 public static void shutdownEmbeddedJboss() 99 { 100 EJB3StandaloneBootstrap.shutdown(); 101 } 102 103 104 protected InitialContext getInitialContext() throws Exception 105 { 106 return new InitialContext (getInitialContextProperties()); 107 } 108 109 protected Hashtable getInitialContextProperties() 110 { 111 return EJB3StandaloneBootstrap.getInitialContextProperties(); 112 } 113 114 private Properties getDefaultPersistenceProperties() 115 throws IOException 116 { 117 InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("default.persistence.properties"); 118 assertNotNull(is); 119 Properties defaults = new Properties (); 120 defaults.load(is); 121 return defaults; 122 } 123 124 public void testStandardTests() throws Throwable 125 { 126 Iterator standardTests = tests.iterator(); 127 while (standardTests.hasNext()) 128 { 129 StandardTestCaseTest test = (StandardTestCaseTest)standardTests.next(); 130 131 System.out.println("Testing standard test " + test.testClass); 132 133 startupEmbeddedJboss(test); 134 EJB3StandaloneDeployer deployer = new EJB3StandaloneDeployer(); 135 deployer.setKernel(EJB3StandaloneBootstrap.getKernel()); 136 deployer.setJndiProperties(getInitialContextProperties()); 137 138 if (test.deployments != null) 139 { 140 StringTokenizer tokenizer = new StringTokenizer (test.deployments, ","); 141 while (tokenizer.hasMoreTokens()) 142 { 143 String testJar = tokenizer.nextToken(); 144 URL archive = Thread.currentThread().getContextClassLoader().getResource(testJar); 145 deployer.getArchives().add(archive); 146 } 147 } 148 149 if (test.testMethods != null) 150 runTest(deployer, test.testClass, test.testMethods); 151 else 152 runTest(deployer, test.testClass); 153 154 shutdownEmbeddedJboss(); 155 } 156 } 157 158 private void runTest(EJB3StandaloneDeployer deployer, String testClassName, String methods) 159 throws Exception 160 { 161 startTest(deployer); 162 163 Class testClass = Thread.currentThread().getContextClassLoader().loadClass(testClassName); 164 String [] constructorParams = {testClass.getName()}; 165 Class [] constructorSignature = {String .class}; 166 Constructor constructor = testClass.getConstructor(constructorSignature); 167 Object testCase = constructor.newInstance(constructorParams); 168 169 Class [] signature = new Class [0]; 170 StringTokenizer methodTokenizer = new StringTokenizer (methods, ","); 171 Object [] params = new Object [0]; 172 while (methodTokenizer.hasMoreTokens()) 173 { 174 String methodName = methodTokenizer.nextToken(); 175 Method method = testClass.getMethod(methodName, signature); 176 System.out.println("-- executing method " + method.getName()); 177 method.invoke(testCase, params); 178 } 179 180 stopTest(deployer); 181 } 182 183 private void runTest(EJB3StandaloneDeployer deployer, String testClassName) 184 throws Exception 185 { 186 startTest(deployer); 187 188 Class testClass = Thread.currentThread().getContextClassLoader().loadClass(testClassName); 189 String [] constructorParams = {testClass.getName()}; 190 Class [] signature = {String .class}; 191 Constructor constructor = testClass.getConstructor(signature); 192 Object testCase = constructor.newInstance(constructorParams); 193 194 Object [] params = new Object [0]; 195 for (Method method: testClass.getMethods()) 196 { 197 if (method.getName().startsWith("test") && method.getParameterTypes().length == 0) 198 { 199 System.out.println("-- executing method " + method.getName()); 200 method.invoke(testCase, params); 201 } 202 } 203 204 stopTest(deployer); 205 } 206 207 private void startTest(EJB3StandaloneDeployer deployer) 208 throws Exception 209 { 210 deployer.create(); 211 deployer.start(); 212 } 213 214 private void stopTest(EJB3StandaloneDeployer deployer) 215 throws Exception 216 { 217 deployer.stop(); 218 deployer.destroy(); 219 } 220 221 private void lookup(String name) 222 { 223 System.out.println("lookup " + name); 224 try { 225 InitialContext jndiContext = new InitialContext (); 226 NamingEnumeration names = jndiContext.list(name); 227 if (names != null){ 228 while (names.hasMore()){ 229 System.out.println(" " + names.next()); 230 } 231 } 232 } catch (Exception e){ 233 } 234 } 235 236 static class StandardTestCaseTest 237 { 238 public String testClass; 239 public String deployments; 240 public String testMethods; 241 public String xmlResources; 242 243 public StandardTestCaseTest(String testClass, String deployments, String testMethods, String xmlResources) 244 { 245 this.testClass = testClass; 246 this.deployments = deployments; 247 this.testMethods = testMethods; 248 this.xmlResources = xmlResources; 249 } 250 } 251 } | Popular Tags |