1 /***
2 * Created by Nicolas FRANK
3 * @mail: nicolas.frank@laposte.net
4 * Date: Oct 27, 2002
5 * Time: 1:05:39 PM
6 */
7 package remoteTester.runner;
8
9 import remoteTester.framework.TestResultRemote;
10 import remoteTester.framework.TestResultSerializable;
11 import remoteTester.runner.server.interfaces.TestRunnerRemote;
12 import remoteTester.runner.server.interfaces.TestRunnerRemoteHome;
13
14 import javax.naming.InitialContext;
15 import javax.naming.NamingException;
16 import javax.rmi.PortableRemoteObject;
17
18 /***
19 * This testRunner allows to execute runner TestCase on server side, even if the
20 * TestCase class is only available on runner side.<BR>
21 * This is done by uploading the class codebyte on the server<br><br>
22 * To be able to locate the server a jndi.properties file
23 * must be available in the client classpath
24 */
25 public class TestRunner {
26
27 /***
28 * the JNDI name of the TestRunner SB Service
29 */
30 public static final String JNDI_TESTRUNNER = "ejb/remote/TestRunner";
31
32 /***
33 * upload the class from runner on server side and run a test method on it
34 *
35 * @param clazz the name of a class accessible on runner classloader
36 * @param helperClasses
37 * @param testMethodName the method to test
38 * @return a TestResultSerializable
39 * @throws java.lang.Exception can be thrown by the execution of the TestCase
40 */
41 public static TestResultSerializable uploadAndRunWithResult(Class clazz, Class[] helperClasses, String testMethodName) throws Exception {
42 byte[] classAsByte = BytableHelper.byteIt(Class.forName(clazz.getName()));
43 byte[][] helperClassesAsByte = null;
44 String[] helperClassesNames = null;
45
46 if (helperClasses != null) {
47 helperClassesAsByte = new byte[helperClasses.length][];
48 helperClassesNames = new String[helperClasses.length];
49 for (int i = 0; i < helperClasses.length; i++) {
50 helperClassesAsByte[i] = BytableHelper.byteIt(Class.forName(helperClasses[i].getName()));
51 helperClassesNames[i] = helperClasses[i].getName();
52 }
53 }
54 TestResultSerializable testResultRemote = getTestRunnerRemoteService().runWithResult(classAsByte, clazz.getName(), helperClassesAsByte, helperClassesNames, testMethodName);
55 return testResultRemote;
56 }
57
58
59 /***
60 * runTC the class on server side (class must be accessible to server classloader)
61 *
62 * @param className name of the class to test
63 * @throws java.lang.Exception can be thrown by the execution of the TestCase
64 */
65 public static boolean run(String className) throws Exception {
66
67 TestResultRemote testResultRemote = getTestRunnerRemoteService().run(className);
68 System.out.println(testResultRemote.getOutput());
69 return testResultRemote.wasSuccessful();
70 }
71
72 /***
73 * upload the class from runner on server side and run it
74 *
75 * @param className the name of a class accessible on runner classloader
76 * @throws java.lang.Exception can be thrown by the execution of the TestCase
77 */
78 public static boolean uploadAndRun(String className) throws Exception {
79 byte[] classAsByte = BytableHelper.byteIt(Class.forName(className));
80 TestResultRemote testResultRemote = getTestRunnerRemoteService().run(classAsByte, className);
81 System.out.println(testResultRemote.getOutput());
82 return testResultRemote.wasSuccessful();
83 }
84
85
86 /***
87 * upload the class from runner on server side and run it
88 *
89 * @param classToTest the class accessible on runner classloader
90 * @throws java.lang.Exception can be thrown by the execution of the TestCase
91 */
92 public static boolean uploadAndRun(Class classToTest) throws Exception {
93 return uploadAndRun(classToTest.getName());
94 }
95
96 /***
97 * upload the class from runner on server side and run it
98 *
99 * @param classToTest the class accessible on runner classloader
100 * @param helperClasses helper classes
101 * @throws java.lang.Exception can be thrown by the execution of the TestCase
102 */
103 public static boolean uploadAndRun(Class classToTest, Class[] helperClasses) throws Exception {
104 String helperClassNames [] = new String[helperClasses.length];
105 for (int i = 0; i < helperClasses.length; i++) {
106 helperClassNames[i] = helperClasses[i].getName();
107 }
108 return uploadAndRun(classToTest.getName(), helperClassNames);
109 }
110
111 /***
112 * upload the class and its helper classes from client to server side and run it
113 *
114 * @param className the name of a class accessible on runner classloader
115 * @param helperClassNames full qualified helper class name
116 * @throws java.lang.Exception
117 */
118 public static boolean uploadAndRun(String className, String[] helperClassNames) throws Exception {
119 byte[] classAsByte = BytableHelper.byteIt(Class.forName(className));
120 byte[][] helperClassesAsByte = new byte[helperClassNames.length][];
121 for (int i = 0; i < helperClassNames.length; i++) {
122 helperClassesAsByte[i] = BytableHelper.byteIt(Class.forName(helperClassNames[i]));
123 }
124
125 TestResultRemote testResultRemote = getTestRunnerRemoteService().run(classAsByte, className, helperClassesAsByte, helperClassNames);
126 System.out.println(testResultRemote.getOutput());
127 return testResultRemote.wasSuccessful();
128 }
129
130
131 /***
132 * retrieve the TestRunner Service on the server
133 *
134 * @return
135 * @throws javax.ejb.CreateException
136 * @throws java.rmi.RemoteException
137 */
138 protected static TestRunnerRemote getTestRunnerRemoteService() throws Exception {
139 // jndi properties needed to access to the server
140 // must be put into a jndi.properties files that can be located into the classpath
141 InitialContext ctx = new InitialContext();
142
143 TestRunnerRemoteHome home = null;
144 try {
145 home = (TestRunnerRemoteHome) PortableRemoteObject.narrow(ctx.lookup(JNDI_TESTRUNNER), TestRunnerRemoteHome.class);
146 } catch (NamingException e) {
147 System.out.println("Can't find TestRunner EJB. Please check that the EJB Server is running with the TestRunner ejb deployed and that you have a jndi.properties file in you classpath.");
148 throw e;
149 }
150 TestRunnerRemote testRunnerRemote = home.create();
151 return testRunnerRemote;
152 }
153
154 /***
155 * @param args indique le nom de la classe à tester.
156 */
157 public static void main(String[] args) {
158
159 String className = null;
160 if (args.length == 1) {
161 className = args[0];
162 } else {
163 System.out.println("TestRunner must have a parameter with the name of the class to test (ex: org.testrunner.MyTestCase).");
164 System.exit(1);
165 }
166
167 boolean resSucceded = false;
168 try {
169 resSucceded = TestRunner.uploadAndRun(className);
170 } catch (Exception e) {
171 System.out.println("Oupss... a problem occured when running with class " + className + " !");
172 e.printStackTrace();
173 resSucceded = false;
174 }
175
176 if (resSucceded) System.exit(0);
177 System.exit(1);
178 }
179
180 }
This page was automatically generated by Maven