KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > xml > driver > RunTests


1 /*
2  * Enhydra Java Application Server Project
3  *
4  * The contents of this file are subject to the Enhydra Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License on
7  * the Enhydra web site ( http://www.enhydra.org/ ).
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11  * the License for the specific terms governing rights and limitations
12  * under the License.
13  *
14  * The Initial Developer of the Enhydra Application Server is Lutris
15  * Technologies, Inc. The Enhydra Application Server and portions created
16  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17  * All Rights Reserved.
18  *
19  * Contributor(s):
20  *
21  * $Id: RunTests.java,v 1.2 2005/01/26 08:29:24 jkjome Exp $
22  */

23
24 package org.enhydra.xml.driver;
25
26 import java.io.PrintWriter JavaDoc;
27 import java.lang.reflect.Method JavaDoc;
28 import java.util.Enumeration JavaDoc;
29 import java.util.HashSet JavaDoc;
30
31 import junit.framework.TestSuite;
32
33 /**
34  * Program for running tests without junit. This is a much easier environment
35  * for debugging.
36  * <PRE>
37  * run-tests [options] testcase1 ...
38  * </PRE>
39  * <UL>
40  * <LI> <CODE>-test-root <EM>dir</EM></CODE> - Specifyies test case root.
41  * <LI> <CODE>-parser <EM>name</EM></CODE> - Specify the parser to use.
42  * <LI> <CODE>-dom <EM>name</EM></CODE> - Specify the DOM to run the tests on.
43  * <LI> <CODE>-reloading</CODE> - Enable reloading testing.
44  * <LI> <CODE>-no-reloading</CODE> - Do normal loading testing.
45  * <LI> <CODE>-update</CODE> - Updated the expected test results.
46  * <LI> <CODE>-test <EM>method</EM></CODE> - Restrict test to listed methods.
47  * These are unqualified method names. This option maybe repeated.
48  * </UL>
49  */

50 public class RunTests implements TestCaseBase.TestSelector {
51     /** Compile-time debugging of tests */
52     private static boolean DEBUG = false;
53
54     /** Code for finding the static factory */
55     private Class JavaDoc[] FACTORY_SIG = {
56         Class JavaDoc.class, TestCaseBase.TestSelector.class
57     };
58     
59     /** Parameters */
60     private HashSet JavaDoc fStepRestrictions;
61
62     /** Output stream */
63     private PrintWriter JavaDoc fOut = new PrintWriter JavaDoc(System.err, true);
64
65     /**
66      * Determine if a test method should be executed
67      * @see TestCaseBase.TestSelector
68      */

69     public boolean select(Method JavaDoc method) {
70         if (fStepRestrictions == null) {
71             return true;
72         } else {
73             return fStepRestrictions.contains(method.getName());
74         }
75     }
76
77     /**
78      * Create a test suite.
79      */

80     private TestSuite createTestSuite(String JavaDoc className) {
81         // Create the suite though a reflective call to the static
82
// createSuite() method. This object servers as a selection
83
// filter to limit the tests actually choosen.
84
try {
85             Class JavaDoc testClass = getClass().getClassLoader().loadClass(className);
86             Method JavaDoc createSuite = testClass.getMethod("createSuite",
87                                                      FACTORY_SIG);
88             Object JavaDoc[] args = new Object JavaDoc[]{testClass, this};
89             return (TestSuite)createSuite.invoke(null, args);
90         } catch (Exception JavaDoc except) {
91             throw new TestException(except);
92         }
93     }
94
95     /** Run a test */
96     private void runTest(TestCaseBase test) {
97         fOut.println("RUN: " + test);
98         if (DEBUG) {
99             test.dumpInfo(fOut);
100         }
101         boolean succeed = false;
102         try {
103             test.runTest();
104             succeed = true;
105         } catch (Throwable JavaDoc except) {
106             fOut.println("FAILED: " + test + ": " + except);
107             if (!(except instanceof DiffException)) {
108                 except.printStackTrace();
109             }
110         }
111         if (succeed) {
112             fOut.println("SUCCEED: " + test);
113         }
114     }
115
116     /** All tests in a suite */
117     private void runTestSuite(TestSuite suite) {
118         Enumeration JavaDoc tests = suite.tests();
119         while (tests.hasMoreElements()) {
120             runTest((TestCaseBase)tests.nextElement());
121         }
122     }
123
124     /*
125      * Parse arguments and run
126      */

127     private void run(String JavaDoc[] args) {
128         int argi = 0;
129         while ((argi < args.length) && args[argi].startsWith("-")) {
130             if (args[argi].equals("-test-root")) {
131                 TestProperties.setTestRoot(args[++argi]);
132             } else if (args[argi].equals("-dom")) {
133                 TestProperties.setDom(args[++argi]);
134             } else if (args[argi].equals("-parser")) {
135                 TestProperties.setParser(args[++argi]);
136             } else if (args[argi].equals("-reloading")) {
137                 TestProperties.setReloading(true);
138             } else if (args[argi].equals("-no-reloading")) {
139                 TestProperties.setReloading(false);
140             } else if (args[argi].equals("-update")) {
141                 TestProperties.setUpdate(true);
142             } else if (args[argi].equals("-test")) {
143                 if (fStepRestrictions == null) {
144                     fStepRestrictions = new HashSet JavaDoc();
145                 }
146                 fStepRestrictions.add(args[++argi]);
147             } else {
148                 fOut.println("invalid opt: " + args[argi]);
149             }
150             argi++;
151         }
152         TestProperties.setVerbose(true);
153
154         for (; argi < args.length; argi++) {
155             runTestSuite(createTestSuite(args[argi]));
156         }
157     }
158
159     /** entry */
160     public static void main(String JavaDoc[] args) {
161         new RunTests().run(args);
162     }
163 }
164
165
Popular Tags