View Javadoc
1   /*
2    * Redistribution and use of this software and associated documentation ("Software"), with or
3    * without modification, are permitted provided that the following conditions are met:
4    *
5    * 1. Redistributions of source code must retain copyright statements and notices. Redistributions
6    * must also contain a copy of this document.
7    *
8    * 2. Redistributions in binary form must reproduce the above copyright notice, this list of
9    * conditions and the following disclaimer in the documentation and/or other materials provided with
10   * the distribution.
11   *
12   * 3. The name "Exolab" must not be used to endorse or promote products derived from this Software
13   * without prior written permission of Intalio, Inc. For written permission, please contact
14   * info@exolab.org.
15   *
16   * 4. Products derived from this Software may not be called "Exolab" nor may "Exolab" appear in
17   * their names without prior written permission of Intalio, Inc. Exolab is a registered trademark of
18   * Intalio, Inc.
19   *
20   * 5. Due credit should be given to the Exolab Project (http://www.exolab.org/).
21   *
22   * THIS SOFTWARE IS PROVIDED BY INTALIO, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESSED OR
23   * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
24   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTALIO, INC. OR ITS
25   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27   * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28   * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
29   * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30   *
31   * Copyright 2001-2003 (C) Intalio, Inc. All Rights Reserved.
32   *
33   * $Id:CastorTestSuiteRunner.java 6775 2007-01-28 20:04:11Z ekuns $
34   */
35  package org.castor.xmlctf;
36  
37  import java.io.File;
38  
39  import junit.framework.Test;
40  import junit.framework.TestCase;
41  
42  /**
43   * Start tests for the Castor Testing Framework. This is the main class for the Castor Testing
44   * Framework. It is all driven from here.
45   *
46   * @author <a href="mailto:gignoux@kernelcenter.org">Sebastien Gignoux</a>
47   * @author <a href="mailto:blandin@intalio.com">Arnaud Blandin</a>
48   * @version $Revision:6775 $ $Date: 2005-12-31 08:08:19 -0700 (Sat, 31 Dec 2005) $
49   */
50  public class CastorTestSuiteRunner extends TestCase {
51  
52    /**
53     * The root directory of the place where all the files generated by the different tests have to be
54     * put.
55     */
56    public static final String TEST_OUTPUT_ROOT = "../xmlctf/build/tests/output/";
57  
58    /**
59     * Name of the system property storing the root directory of all test cases.
60     * 
61     * @see #_testRoot
62     */
63    private static final String TEST_ROOT_PROPERTY = "org.exolab.castor.tests.TestRoot";
64  
65    /**
66     * Root directory of all test cases we will process. We look for test cases recursively starting
67     * in this directory.
68     */
69    private static String _testRoot;
70  
71    /**
72     * Command line argument that causes the help/options to be displayed.
73     */
74    private static final String HELP_ARG = "-help";
75  
76    /**
77     * Command line argument that sets verbose mode true.
78     */
79    private static final String VERBOSE_ARG = "-verbose";
80  
81    /**
82     * Command line argument that sets text mode true (no GUI).
83     */
84    private static final String TEXT_MODE_ARG = "-text";
85  
86    /**
87     * Command line argument to print or not the stack trace.
88     */
89    private static final String PRINT_STACK = "-printStack";
90  
91    /**
92     * Command line argument specifying the seed for the pseudo-random number generator.
93     */
94    private static final String SEED_ARG = "-seed";
95  
96    /**
97     * Default constructor that takes a name per test case.
98     * 
99     * @param name test case name
100    */
101   public CastorTestSuiteRunner(final String name) {
102     super(name);
103   }
104 
105   /**
106    * Starts a TestCaseAggregator to collect all the tests in the test root directory and its
107    * subdirectories.
108    *
109    * @return A non-null test suite.
110    */
111   public static Test suite() {
112     _testRoot = System.getProperty(TEST_ROOT_PROPERTY);
113 
114     if (_testRoot.equals(".") || _testRoot.equals("..")) {
115       // -- convert relative directories "." and ".." to a Canonical path
116       File tmp = new File(_testRoot);
117       try {
118         _testRoot = tmp.getCanonicalPath();
119       } catch (java.io.IOException iox) {
120         iox.printStackTrace();
121         System.exit(1);
122       }
123     } else if (_testRoot.startsWith("./") || _testRoot.startsWith(".\\")) {
124       // -- Remove leading ./ or .\ -- URLClassLoader can't handle such file URLs
125       _testRoot = _testRoot.substring(2);
126     }
127 
128     File testRoot = new File(_testRoot);
129 
130     if (!testRoot.exists()) {
131       System.out.println("\nUnable to locate the root directory for the test cases");
132       System.exit(1);
133     }
134     return new TestCaseAggregator(testRoot, TEST_OUTPUT_ROOT).suite();
135   }
136 
137   /**
138    * Runs the Castor Testing Framework.
139    * 
140    * @param args the standard command-line arguments
141    */
142   public static void main(final String[] args) {
143     if (args.length == 0) {
144       error(); // Does not return
145     }
146 
147     boolean text = processArguments(args);
148 
149     if (System.getProperty(TEST_ROOT_PROPERTY) == null) {
150       error(); // Does not return
151     }
152 
153     System.out.println("Pseudo-random number generator seed:  " + RandomHelper.getSeed());
154 
155     if (text) {
156       String[] testCaseName = {CastorTestSuiteRunner.class.getName()};
157       junit.textui.TestRunner.main(testCaseName);
158     } else {
159       // TODO: re-enable if a Maven dependency exists
160       // String[] testCaseName = {"-noloading", CastorTestSuiteRunner.class.getName()};
161       // junit.swingui.TestRunner.main(testCaseName);
162     }
163   }
164 
165   /**
166    * Processes command-line arguments for main() and returns whether or not to use the text or GUI
167    * JUnit.
168    *
169    * @param args command-line arguments from main()
170    * @return true if JUnit should be run in text mode, not GUI
171    */
172   private static boolean processArguments(final String[] args) {
173     boolean textModeJUnit = true; // text by default
174 
175     for (int i = 0; i < args.length; ++i) {
176       String argument = args[i];
177       System.out.println("arg: '" + argument + "'");
178 
179       if (argument.equals(VERBOSE_ARG)) {
180         System.out.println("Verbose on");
181         System.setProperty(TestCaseAggregator.VERBOSE_PROPERTY, "true");
182       } else if (argument.equals(PRINT_STACK)) {
183         System.out.println("Printing stack traces on error on.");
184         System.setProperty(TestCaseAggregator.PRINT_STACK_TRACE, "true");
185       } else if (argument.equals(TEXT_MODE_ARG)) {
186         System.out.println("Running in text mode.");
187         textModeJUnit = true;
188       } else if (argument.equals(SEED_ARG)) {
189         // The next argument should be a number...
190         try {
191           long seed = Long.parseLong(args[++i]);
192           RandomHelper.setSeed(seed);
193         } catch (NumberFormatException nfe) {
194           System.out.println("Unable to parse the number for the seed");
195           error(); // Does not return
196         }
197       } else if (argument.equals(HELP_ARG)) {
198         usage();
199         System.exit(0);
200       } else if (System.getProperty(TEST_ROOT_PROPERTY) == null) {
201         System.setProperty(TEST_ROOT_PROPERTY, argument);
202       } else {
203         System.out.println("Unexpected command-line argument: '" + argument + "'");
204         error(); // Does not return
205       }
206     }
207     return textModeJUnit;
208   }
209 
210   /**
211    * Print usage and exit with a non-zero return code.
212    */
213   private static void error() {
214     usage();
215     System.exit(1);
216   }
217 
218   /**
219    * Print usage.
220    */
221   private static void usage() {
222     System.out.println("Castor Testing Framework ");
223     System.out.println("------------------------ ");
224     System.out.println("argument: [" + VERBOSE_ARG + "] [" + TEXT_MODE_ARG + "] [" + PRINT_STACK
225         + "] [" + SEED_ARG + " <seed value>] <root test directory or a castor jar test file>");
226     System.out.println("   " + HELP_ARG + " : displays this screen.");
227     System.out
228         .println("   " + VERBOSE_ARG + " : give detailed execution information for the each test");
229     System.out.println("   " + TEXT_MODE_ARG + " : run the test without starting the swing gui");
230     System.out
231         .println("   " + PRINT_STACK + " : Print the full stack trace if an exception is thrown");
232     System.out.println("   " + SEED_ARG + " <seed value>: "
233         + "set a specific seed for the pseudo-random number generator");
234   }
235 
236 }