View Javadoc
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 Intalio, Inc.  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 Intalio, Inc. Exolab is a registered
23   *    trademark of Intalio, Inc.
24   *
25   * 5. Due credit should be given to the Exolab Project
26   *    (http://www.exolab.org/).
27   *
28   * THIS SOFTWARE IS PROVIDED BY INTALIO, INC. 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   * INTALIO, INC. 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 2001-2003 (C) Intalio, Inc. All Rights Reserved.
42   *
43   * $Id:CastorTestSuiteRunner.java 6775 2007-01-28 20:04:11Z ekuns $
44   */
45  package org.castor.xmlctf;
46  
47  import java.io.File;
48  
49  
50  import junit.framework.Test;
51  import junit.framework.TestCase;
52  
53  /**
54   * Start tests for the Castor Testing Framework. This is the main class for the
55   * Castor Testing Framework. It is all driven from here.
56   *
57   * @author <a href="mailto:gignoux@kernelcenter.org">Sebastien Gignoux</a>
58   * @author <a href="mailto:blandin@intalio.com">Arnaud Blandin</a>
59   * @version $Revision:6775 $ $Date: 2005-12-31 08:08:19 -0700 (Sat, 31 Dec 2005) $
60   */
61  public class CastorTestSuiteRunner extends TestCase {
62  
63      /**
64       * The root directory of the place where all the files generated by the
65       * different tests have to be put.
66       */
67      public static final String TEST_OUTPUT_ROOT = "../xmlctf/build/tests/output/";
68  
69      /**
70       * Name of the system property storing the root directory of all test cases.
71       * @see #_testRoot
72       */
73      private static final String TEST_ROOT_PROPERTY = "org.exolab.castor.tests.TestRoot";
74  
75      /**
76       * Root directory of all test cases we will process. We look for test cases
77       * recursively starting in this directory.
78       */
79      private static String _testRoot;
80  
81      /**
82       * Command line argument that causes the help/options to be displayed.
83       */
84      private static final String HELP_ARG = "-help";
85  
86      /**
87       * Command line argument that sets verbose mode true.
88       */
89      private static final String VERBOSE_ARG = "-verbose";
90  
91      /**
92       * Command line argument that sets text mode true (no GUI).
93       */
94      private static final String TEXT_MODE_ARG = "-text";
95  
96      /**
97       * Command line argument to print or not the stack trace.
98       */
99      private static final String PRINT_STACK = "-printStack";
100 
101     /**
102      * Command line argument specifying the seed for the pseudo-random number
103      * generator.
104      */
105     private static final String SEED_ARG = "-seed";
106 
107     /**
108      * Default constructor that takes a name per test case.
109      * @param name test case name
110      */
111     public CastorTestSuiteRunner(final String name) {
112         super(name);
113     }
114 
115     /**
116      * Starts a TestCaseAggregator to collect all the tests in the test root
117      * directory and its subdirectories.
118      *
119      * @return A non-null test suite.
120      */
121     public static Test suite() {
122         _testRoot = System.getProperty(TEST_ROOT_PROPERTY);
123 
124         if (_testRoot.equals(".") || _testRoot.equals("..")) {
125             //-- convert relative directories "." and ".." to a Canonical path
126             File tmp = new File(_testRoot);
127             try {
128                 _testRoot = tmp.getCanonicalPath();
129             } catch (java.io.IOException iox) {
130                 iox.printStackTrace();
131                 System.exit(1);
132             }
133         } else if (_testRoot.startsWith("./") || _testRoot.startsWith(".\\")) {
134             //-- Remove leading ./ or .\ -- URLClassLoader can't handle such file URLs
135             _testRoot = _testRoot.substring(2);
136         }
137 
138         File testRoot = new File(_testRoot);
139 
140         if (!testRoot.exists()) {
141             System.out.println("\nUnable to locate the root directory for the test cases");
142             System.exit(1);
143         }
144         return new TestCaseAggregator(testRoot, TEST_OUTPUT_ROOT).suite();
145     }
146 
147     /**
148      * Runs the Castor Testing Framework.
149      * @param args the standard command-line arguments
150      */
151     public static void main(final String[] args) {
152         if (args.length == 0) {
153             error(); // Does not return
154         }
155 
156         boolean text = processArguments(args);
157 
158         if (System.getProperty(TEST_ROOT_PROPERTY) == null) {
159             error(); // Does not return
160         }
161 
162         System.out.println("Pseudo-random number generator seed:  " + RandomHelper.getSeed());
163 
164         if (text) {
165             String[] testCaseName = {CastorTestSuiteRunner.class.getName()};
166             junit.textui.TestRunner.main(testCaseName);
167         } else {
168             //TODO: re-enable if a Maven dependency exists
169 //            String[] testCaseName = {"-noloading", CastorTestSuiteRunner.class.getName()};
170 //            junit.swingui.TestRunner.main(testCaseName);
171         }
172     }
173 
174     /**
175      * Processes command-line arguments for main() and returns whether or not to
176      * use the text or GUI JUnit.
177      *
178      * @param args command-line arguments from main()
179      * @return true if JUnit should be run in text mode, not GUI
180      */
181     private static boolean processArguments(final String[] args) {
182         boolean textModeJUnit = true; // text by default
183 
184         for (int i = 0; i < args.length; ++i) {
185             String argument = args[i];
186             System.out.println("arg: '" + argument + "'");
187 
188             if (argument.equals(VERBOSE_ARG)) {
189                 System.out.println("Verbose on");
190                 System.setProperty(TestCaseAggregator.VERBOSE_PROPERTY, "true");
191             } else if (argument.equals(PRINT_STACK)) {
192                 System.out.println("Printing stack traces on error on.");
193                 System.setProperty(TestCaseAggregator.PRINT_STACK_TRACE, "true");
194             } else if (argument.equals(TEXT_MODE_ARG)) {
195                 System.out.println("Running in text mode.");
196                 textModeJUnit = true;
197             } else if (argument.equals(SEED_ARG)) {
198                 // The next argument should be a number...
199                 try {
200                     long seed = new Long(args[++i]).longValue();
201                     RandomHelper.setSeed(seed);
202                 } catch (NumberFormatException nfe) {
203                     System.out.println("Unable to parse the number for the seed");
204                     error(); // Does not return
205                 }
206             } else if (argument.equals(HELP_ARG)) {
207                 usage();
208                 System.exit(0);
209             } else if (System.getProperty(TEST_ROOT_PROPERTY) == null) {
210                 System.setProperty(TEST_ROOT_PROPERTY, argument);
211             } else {
212                 System.out.println("Unexpected command-line argument: '" + argument + "'");
213                 error(); // Does not return
214             }
215         }
216         return textModeJUnit;
217     }
218 
219     /**
220      * Print usage and exit with a non-zero return code.
221      */
222     private static void error() {
223         usage();
224         System.exit(1);
225     }
226 
227     /**
228      * Print usage.
229      */
230     private static void usage() {
231         System.out.println("Castor Testing Framework ");
232         System.out.println("------------------------ ");
233         System.out.println("argument: [" + VERBOSE_ARG + "] [" + TEXT_MODE_ARG + "] ["
234                            + PRINT_STACK + "] [" + SEED_ARG
235                            + " <seed value>] <root test directory or a castor jar test file>");
236         System.out.println("   " + HELP_ARG + " : displays this screen.");
237         System.out.println("   " + VERBOSE_ARG
238                            + " : give detailed execution information for the each test");
239         System.out.println("   " + TEXT_MODE_ARG
240                            + " : run the test without starting the swing gui");
241         System.out.println("   " + PRINT_STACK
242                            + " : Print the full stack trace if an exception is thrown");
243         System.out.println("   " + SEED_ARG + " <seed value>: "
244                            + "set a specific seed for the pseudo-random number generator");
245     }
246 
247 }