KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openejb > test > TestRunner


1 /**
2  * Redistribution and use of this software and associated documentation
3  * ("Software"), with or without modification, are permitted provided
4  * that the following conditions are met:
5  *
6  * 1. Redistributions of source code must retain copyright
7  * statements and notices. Redistributions must also contain a
8  * copy of this document.
9  *
10  * 2. Redistributions in binary form must reproduce the
11  * above copyright notice, this list of conditions and the
12  * following disclaimer in the documentation and/or other
13  * materials provided with the distribution.
14  *
15  * 3. The name "Exolab" must not be used to endorse or promote
16  * products derived from this Software without prior written
17  * permission of Exoffice Technologies. For written permission,
18  * please contact info@exolab.org.
19  *
20  * 4. Products derived from this Software may not be called "Exolab"
21  * nor may "Exolab" appear in their names without prior written
22  * permission of Exoffice Technologies. Exolab is a registered
23  * trademark of Exoffice Technologies.
24  *
25  * 5. Due credit should be given to the Exolab Project
26  * (http://www.exolab.org/).
27  *
28  * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
29  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32  * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39  * OF THE POSSIBILITY OF SUCH DAMAGE.
40  *
41  * Copyright 1999 (C) Exoffice Technologies Inc. All Rights Reserved.
42  *
43  * $Id: TestRunner.java 2513 2006-02-26 05:53:47Z dblevins $
44  */

45 package org.openejb.test;
46
47 import java.io.File JavaDoc;
48 import java.io.InputStream JavaDoc;
49 import java.io.OutputStream JavaDoc;
50 import java.io.PrintStream JavaDoc;
51 import java.net.URL JavaDoc;
52 import java.util.Properties JavaDoc;
53 import java.util.Iterator JavaDoc;
54 import java.util.Map JavaDoc;
55
56 import junit.framework.TestResult;
57
58 import org.openejb.util.JarUtils;
59
60 /**
61  *
62  * @author <a HREF="mailto:david.blevins@visi.com">David Blevins</a>
63  * @author <a HREF="mailto:Richard@Monson-Haefel.com">Richard Monson-Haefel</a>
64  */

65 public class TestRunner extends junit.textui.TestRunner {
66     private static final String JavaDoc helpBase = "META-INF/org.openejb.cli/";
67
68     /**
69      * Constructs a TestRunner.
70      */

71     public TestRunner() {
72         this(System.out);
73     }
74
75     /**
76      * Constructs a TestRunner using the given stream for all the output
77      */

78     public TestRunner(PrintStream JavaDoc writer) {
79         this(new ResultPrinter(writer));
80     }
81
82     /**
83      * Constructs a TestRunner using the given ResultPrinter all the output
84      */

85     public TestRunner(ResultPrinter printer) {
86         super(printer);
87     }
88
89     /**
90      * main entry point.
91      */

92     public static void main(String JavaDoc args[]) {
93         if (args.length == 0) {
94             printHelp();
95         } else {
96             if (args[0].equals("--help")) {
97                 printHelp();
98
99                 return;
100             } else if (args[0].equals("local")) {
101                 runLocalTests();
102             } else if (args[0].equals("remote")) {
103                 runRemoteTests();
104             } else if (args[0].equals("http")) {
105                 runRemoteHttpTests();
106             } else if (args[0].equals("tomcat")) {
107                 runTomcatRemoteHttpTests();
108             } else {
109                 printHelp();
110
111                 return;
112             }
113
114             try {
115                 TestRunner aTestRunner = new TestRunner();
116                 TestResult r = aTestRunner
117                         .start(new String JavaDoc[] { "org.openejb.test.ClientTestSuite" });
118
119                 System.out.println("");
120                 System.out.println("_________________________________________________");
121                 System.out.println("CLIENT JNDI PROPERTIES");
122                 Properties JavaDoc env = TestManager.getServer().getContextEnvironment();
123                 for (Iterator JavaDoc iterator = env.entrySet().iterator(); iterator.hasNext();) {
124                     Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iterator.next();
125                     String JavaDoc key = (String JavaDoc) entry.getKey();
126                     Object JavaDoc value = entry.getValue();
127                     System.out.println(key+" = "+value);
128                 }
129                 System.out.println("_________________________________________________");
130
131                 if (!r.wasSuccessful())
132                     System.exit(FAILURE_EXIT);
133                 System.exit(SUCCESS_EXIT);
134             } catch (Exception JavaDoc e) {
135                 System.err.println(e.getMessage());
136                 System.exit(EXCEPTION_EXIT);
137             }
138
139
140         }
141     }
142
143     private static void runLocalTests() {
144         System.setProperty("openejb.test.server",
145                 "org.openejb.test.IvmTestServer");
146         System.setProperty("openejb.test.database",
147                 "org.openejb.test.InstantDbTestDatabase");
148
149         System.out.println("_________________________________________________");
150         System.out
151                 .println("|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|\n");
152         System.out.println("Running EJB compliance tests on IntraVM Server");
153         System.out.println("_________________________________________________");
154     }
155
156     private static void runRemoteTests() {
157         System.setProperty("openejb.test.server",
158                 "org.openejb.test.RemoteTestServer");
159         System.setProperty("openejb.test.database",
160                 "org.openejb.test.InstantDbTestDatabase");
161
162         System.out.println("_________________________________________________");
163         System.out
164                 .println("|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|\n");
165         System.out.println("Running EJB compliance tests on Remote Server");
166         System.out.println("_________________________________________________");
167     }
168
169     private static void runRemoteHttpTests() {
170         System.setProperty("openejb.test.server",
171                 "org.openejb.test.RemoteHttpTestServer");
172         System.setProperty("openejb.test.database",
173                 "org.openejb.test.InstantDbTestDatabase");
174
175         System.out.println("_________________________________________________");
176         System.out
177                 .println("|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|\n");
178         System.out.println("Running EJB compliance tests on HTTP/Remote Server");
179         System.out.println("_________________________________________________");
180     }
181
182     private static void runTomcatRemoteHttpTests() {
183         System.setProperty("openejb.test.server", TomcatRemoteTestServer.class.getName());
184         System.setProperty("openejb.test.database", "org.openejb.test.InstantDbTestDatabase");
185
186         System.out.println("_________________________________________________");
187         System.out
188                 .println("|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|\n");
189         System.out.println("Running EJB compliance tests on HTTP/Tomcat Server");
190         System.out.println("_________________________________________________");
191     }
192
193     private static void printHelp() {
194         String JavaDoc header = "OpenEJB Compliance Tests ";
195         try {
196             JarUtils.setHandlerSystemProperty();
197             Properties JavaDoc versionInfo = new Properties JavaDoc();
198             versionInfo.load(new URL JavaDoc("resource:/openejb-version.properties").openConnection().getInputStream());
199             header += versionInfo.get("version");
200         } catch (java.io.IOException JavaDoc e) {
201         }
202
203         System.out.println(header);
204
205         // Internationalize this
206
try {
207             InputStream JavaDoc in = Thread.currentThread().getContextClassLoader()
208                     .getResource(helpBase + "test.help").openConnection()
209                     .getInputStream();
210
211             int b = in.read();
212             while (b != -1) {
213                 System.out.write(b);
214                 b = in.read();
215             }
216         } catch (java.io.IOException JavaDoc e) {
217         }
218     }
219
220     public TestResult start(String JavaDoc args[]) throws Exception JavaDoc {
221         TestResult result = null;
222         try {
223
224             TestManager.init(null);
225             TestManager.start();
226         } catch (Exception JavaDoc e) {
227             System.out.println("Cannot initialize the test environment: "
228                     + e.getClass().getName() + " " + e.getMessage());
229             // e.printStackTrace();
230
// System.exit(-1);
231
throw e;
232         }
233
234         try {
235             result = super.start(args);
236         } catch (Exception JavaDoc ex) {
237         } finally {
238             try {
239                 TestManager.stop();
240             } catch (Exception JavaDoc e) {
241                 ; // ignore it
242
}
243         }
244         // System.exit(0);
245
return result;
246     }
247
248     private static final class Pipe implements Runnable JavaDoc {
249
250         private final InputStream JavaDoc is;
251
252         private final OutputStream JavaDoc out;
253
254         private Pipe(InputStream JavaDoc is, OutputStream JavaDoc out) {
255
256             super();
257
258             this.is = is;
259
260             this.out = out;
261
262         }
263
264         public void run() {
265
266             try {
267
268                 int i = is.read();
269
270                 out.write(i);
271
272                 while (i != -1) {
273
274                     i = is.read();
275
276                     out.write(i);
277
278                 }
279
280             } catch (Exception JavaDoc e) {
281
282                 e.printStackTrace();
283
284             }
285
286         }
287
288     }
289 }
290
Popular Tags