View Javadoc
1 package remoteTester.runner.server.ejbbean; 2 3 /*** 4 * Created by Nicolas FRANK 5 * @mail: nicolas.frank@laposte.net 6 * Date: 18 oct. 2002 7 * Time: 14:59:23 8 */ 9 10 import junit.framework.Test; 11 import junit.framework.TestResult; 12 import junit.framework.TestSuite; 13 import junit.framework.TestCase; 14 import junit.textui.ResultPrinter; 15 import junit.textui.TestRunner; 16 import remoteTester.framework.RemoteTestCase; 17 import remoteTester.framework.TestResultRemote; 18 import remoteTester.framework.TestResultSerializable; 19 import remoteTester.runner.server.ByteClassLoader; 20 21 import javax.ejb.EJBException; 22 import javax.ejb.SessionBean; 23 import javax.ejb.SessionContext; 24 import java.io.ByteArrayOutputStream; 25 import java.io.IOException; 26 import java.io.PrintStream; 27 import java.rmi.RemoteException; 28 import java.lang.reflect.Modifier; 29 import java.lang.reflect.Method; 30 import java.util.Vector; 31 32 /*** 33 * This Session Bean allows to execute a JUnit TestCase on server side 34 * WARNING : It must not be deployed in a production environnement as it allows access to Local Interfaces 35 */ 36 public class TestRunnerBean implements SessionBean { 37 38 /*** 39 * play a test method from a RemoteTestCase using a junit.textui.TestRunner with HelperClasses supports 40 * 41 * @param testClassAsBytes a JUnit TestCase class provided as a byte array 42 * @param testClassName the class name of a JUnit TestCase class 43 * @param helperClassesAsBytes 44 * @param helperClassNames 45 * @param testMethodName name of the test method to execute 46 * @return a TestResultSerializable for this test 47 * @throws ClassNotFoundException 48 */ 49 public TestResultSerializable runWithResult(byte[] testClassAsBytes, String testClassName, byte[][] helperClassesAsBytes, String[] helperClassNames, String testMethodName) throws ClassNotFoundException { 50 51 ByteArrayOutputStream out = null; 52 PrintStream printStream = null; 53 54 // create a classloader able to load a class from a byte array 55 ByteClassLoader classLoader = new ByteClassLoader(testClassAsBytes, testClassName, helperClassesAsBytes, helperClassNames); 56 57 // load the class className; 58 Class testCaseClass = classLoader.loadClass(); 59 RemoteTestCase.setIsRemote(true); 60 //if (testMethodName!=null) RemoteTestCase.setTestMethodName(testMethodName); 61 62 try { 63 out = new ByteArrayOutputStream(); 64 printStream = new PrintStream(out, true); 65 ResultPrinter resultPrinter = new ResultPrinter(printStream); 66 67 TestRunner testrunner = new TestRunner(resultPrinter) { 68 protected TestResult createTestResult() { 69 return new TestResultSerializable(); 70 } 71 }; 72 73 Test t = createTest(testCaseClass, testMethodName); 74 75 // here the test is really run 76 TestResultSerializable aTestResult = (TestResultSerializable) testrunner.doRun((t)); 77 78 aTestResult.setOutput(out.toString()); 79 80 return aTestResult; 81 } finally { 82 if (printStream != null) printStream.close(); 83 try { 84 if (out != null) out.close(); 85 } catch (IOException e) { 86 e.printStackTrace(); 87 } 88 } 89 } 90 91 /*** 92 * create a Test with only the testMethod to test 93 * 94 * @param testCaseClass 95 * @param testMethod the method to test 96 * @return 97 */ 98 private Test createTest(Class testCaseClass, String testMethod) { 99 TestSuite ts = new TestSuite(); 100 try { 101 TestSuite.getTestConstructor(testCaseClass); // Avoid generating multiple error messages 102 } catch (NoSuchMethodException e) { 103 ts.addTest(warning("Class " + testCaseClass.getName() + " has no public constructor TestCase(String name) or TestCase()")); 104 return ts; 105 } 106 107 if (!Modifier.isPublic(testCaseClass.getModifiers())) { 108 ts.addTest(warning("Class " + testCaseClass.getName() + " is not public")); 109 return ts; 110 } 111 112 Class superClass = testCaseClass; 113 Vector names = new Vector(); 114 while (Test.class.isAssignableFrom(superClass)) { 115 Method[] methods = superClass.getDeclaredMethods(); 116 for (int i = 0; i < methods.length; i++) { 117 if (testMethod == null || testMethod.equals(methods[i].getName())) { 118 final Test test = createTestMethod(methods[i], names, testCaseClass); 119 if (test != null) ts.addTest(test); 120 } 121 } 122 superClass = superClass.getSuperclass(); 123 } 124 if (ts.countTestCases() == 0) 125 ts.addTest(warning("No tests found in " + testCaseClass.getName())); 126 return ts; 127 } 128 129 /*** 130 * @param m 131 * @param names 132 * @param theClass 133 * @return 134 * @see TestSuite 135 */ 136 private Test createTestMethod(Method m, Vector names, Class theClass) { 137 String name = m.getName(); 138 if (names.contains(name)) 139 return null; 140 if (!isPublicTestMethod(m)) { 141 if (isTestMethod(m)) 142 return warning("Test method isn't public: " + m.getName()); 143 } 144 names.addElement(name); 145 return TestSuite.createTest(theClass, name); 146 } 147 148 /*** 149 * @param m 150 * @return 151 */ 152 private boolean isPublicTestMethod(Method m) { 153 return isTestMethod(m) && Modifier.isPublic(m.getModifiers()); 154 } 155 156 /*** 157 * @param m 158 * @return 159 */ 160 private boolean isTestMethod(Method m) { 161 String name = m.getName(); 162 Class[] parameters = m.getParameterTypes(); 163 Class returnType = m.getReturnType(); 164 return parameters.length == 0 && name.startsWith("test") && returnType.equals(Void.TYPE); 165 } 166 167 /*** 168 * Returns a test which will fail and log a warning message. 169 */ 170 private static Test warning(final String message) { 171 return new TestCase("warning") { 172 protected void runTest() { 173 fail(message); 174 } 175 }; 176 } 177 178 179 /*** 180 * play a TestCase using a junit.textui.TestRunner 181 * 182 * @param classAsByte a JUnit TestCase class provided as a byte array 183 * @param className the class name of a JUnit TestCase class 184 * @throws ClassNotFoundException 185 */ 186 public TestResultRemote run(byte[] classAsByte, String className) throws ClassNotFoundException { 187 188 // create a classloader able to load a class from a byte array 189 ByteClassLoader classLoader = new ByteClassLoader(classAsByte, className); 190 // load the class className; 191 Class testCaseClass = classLoader.loadClass(); 192 193 return run(testCaseClass); 194 195 } 196 197 /*** 198 * play a TestCase using a junit.textui.TestRunner with HelperClasses supports 199 * 200 * @param classAsBytes a JUnit TestCase class provided as a byte array 201 * @param className the class name of a JUnit TestCase class 202 * @throws ClassNotFoundException 203 */ 204 public TestResultRemote run(byte[] classAsBytes, String className, byte[][] helperClassesAsBytes, String[] helperClassNames) throws ClassNotFoundException { 205 206 // create a classloader able to load a class from a byte array 207 ByteClassLoader classLoader = new ByteClassLoader(classAsBytes, className, helperClassesAsBytes, helperClassNames); 208 // load the class className; 209 Class testCaseClass = classLoader.loadClass(); 210 211 return run(testCaseClass); 212 213 } 214 215 216 /*** 217 * play a TestCase using a junit.textui.TestRunner 218 * 219 * @param className the class name of a JUnit TestCase class.<BR> 220 * The test class must be available on serverside, so it can be located by server ClassLoader, 221 * otherwise you should use playTestCase(byte[] classAsByte, String className) 222 * @throws ClassNotFoundException 223 */ 224 public TestResultRemote run(String className) throws ClassNotFoundException { 225 226 Class testCaseClass = Class.forName(className); 227 return run(testCaseClass); 228 } 229 230 /*** 231 * play a TestCase using a junit.textui.TestRunner 232 * 233 * @param testCaseClass the TestCase Class to be runTC 234 */ 235 public TestResultRemote run(Class testCaseClass) { 236 237 ByteArrayOutputStream out = null; 238 PrintStream printStream = null; 239 240 try { 241 out = new ByteArrayOutputStream(); 242 printStream = new PrintStream(out, true); 243 244 ResultPrinter resultPrinter = new ResultPrinter(printStream); 245 TestRunner testrunner = new TestRunner(resultPrinter); 246 247 Test t = new TestSuite(testCaseClass); 248 249 // here the test is really run 250 TestResult testResult = null; 251 TestResultRemote testResultRemote = null; 252 try { 253 testResult = testrunner.doRun((t)); 254 // embbed the TestCase result so it can be returned to the client 255 testResultRemote = new TestResultRemote(out.toString()); 256 if (testResult.wasSuccessful()) testResultRemote.setWasSuccessful(); 257 258 } catch (Exception e) { 259 testResultRemote = new TestResultRemote(e.toString()); 260 } 261 262 return testResultRemote; 263 264 } finally { 265 if (printStream != null) printStream.close(); 266 try { 267 if (out != null) out.close(); 268 } catch (IOException e) { 269 e.printStackTrace(); 270 } 271 } 272 } 273 274 // Technical Contract 275 //---------------------------------------------------------------- 276 277 public void ejbCreate() { 278 } 279 280 public void setSessionContext(SessionContext sessionContext) throws EJBException, RemoteException { 281 } 282 283 public void ejbRemove() throws EJBException, RemoteException { 284 } 285 286 public void ejbActivate() throws EJBException, RemoteException { 287 } 288 289 public void ejbPassivate() throws EJBException, RemoteException { 290 } 291 }

This page was automatically generated by Maven