1 /**
2  * @copyright
3  * ====================================================================
4  *    Licensed to the Apache Software Foundation (ASF) under one
5  *    or more contributor license agreements.  See the NOTICE file
6  *    distributed with this work for additional information
7  *    regarding copyright ownership.  The ASF licenses this file
8  *    to you under the Apache License, Version 2.0 (the
9  *    "License"); you may not use this file except in compliance
10  *    with the License.  You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  *    Unless required by applicable law or agreed to in writing,
15  *    software distributed under the License is distributed on an
16  *    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17  *    KIND, either express or implied.  See the License for the
18  *    specific language governing permissions and limitations
19  *    under the License.
20  * ====================================================================
21  * @endcopyright
22  */
23 package org.apache.subversion.javahl;
24 
25 import java.lang.reflect.Constructor;
26 import java.util.StringTokenizer;
27 
28 import junit.framework.TestCase;
29 import junit.framework.TestResult;
30 import junit.framework.TestSuite;
31 import junit.textui.TestRunner;
32 
33 /**
34  * A test runner, and comprehensive test suite definition.
35  */
36 public class RunTests
37 {
38     /**
39      * The Subversion JavaHL test suite.
40      */
41     private static class SVNTestSuite extends TestSuite
42     {
43         /**
44          * Create a conglomerate test suite containing all our test
45          * suites, or the fully qualified method names specified by
46          * the <code>test.tests</code> system property.
47          *
48          * @return The complete test suite.
49          */
suite()50         public static TestSuite suite()
51         {
52             TestSuite suite = new SVNTestSuite();
53 
54             // Determine whether the caller requested that a specific
55             // set of test cases be run, and verify that they exist.
56             TestCase[] testCases = null;
57             String testNames = System.getProperty("test.tests");
58             if (testNames != null)
59             {
60                 StringTokenizer tok = new StringTokenizer(testNames, ", ");
61                 testCases = new TestCase[tok.countTokens()];
62                 int testCaseIndex = 0;
63                 while (tok.hasMoreTokens())
64                 {
65                     // ASSUMPTION: Class names are fully-qualified
66                     // (with package), and are included with method
67                     // names in test names.
68                     String methodName = tok.nextToken();
69                     int i = methodName.lastIndexOf('.');
70                     String className = methodName.substring(0, i);
71                     try
72                     {
73                         Class<?> clazz = Class.forName(className);
74                         final Class<?>[] argTypes = new Class<?>[] { String.class };
75                         Constructor<?> ctor =
76                             clazz.getDeclaredConstructor(argTypes);
77                         methodName = methodName.substring(i + 1);
78                         String[] args = { methodName };
79                         testCases[testCaseIndex++] =
80                             (TestCase) ctor.newInstance((Object[]) args);
81                     }
82                     catch (Exception e)
83                     {
84                         testCases = null;
85                         break;
86                     }
87                 }
88             }
89 
90             // Add the appropriate set of tests to our test suite.
91             if (testCases == null || testCases.length == 0)
92             {
93                 // Add default test suites.
94                 suite.addTestSuite(BasicTests.class);
95                 suite.addTestSuite(UtilTests.class);
96                 suite.addTestSuite(SVNRemoteTests.class);
97                 suite.addTestSuite(SVNReposTests.class);
98                 suite.addTestSuite(ExceptionTests.class);
99             }
100             else
101             {
102                 // Add specific test methods.
103                 for (int i = 0; i < testCases.length; i++)
104                 {
105                     suite.addTest(testCases[i]);
106                 }
107             }
108             return suite;
109         }
110     }
111 
112     /**
113      * Main method, will call all tests of all test classes
114      * @param args command line arguments
115      */
main(String[] args)116     public static void main(String[] args)
117     {
118         processArgs(args);
119         TestResult testResult = TestRunner.run(SVNTestSuite.suite());
120         if (testResult.errorCount() > 0 || testResult.failureCount() > 0)
121         {
122             System.exit(1);
123         }
124     }
125 
126     /**
127      * Retrieve the root directory and the root url from the
128      * command-line arguments, and set them on {@link
129      * org.apache.subversion.javahl.tests.SVNTests}.
130      *
131      * @param args The command line arguments to process.
132      */
processArgs(String[] args)133     private static void processArgs(String[] args)
134     {
135         if (args == null)
136             return;
137 
138         for (int i = 0; i < args.length; i++)
139         {
140             String arg = args[i];
141             if ("-d".equals(arg))
142             {
143                 if (i + 1 < args.length)
144                 {
145                     SVNTests.rootDirectoryName = args[++i];
146                 }
147             }
148             else if ("-u".equals(arg))
149             {
150                 if (i + 1 < args.length)
151                 {
152                     SVNTests.rootUrl = args[++i];
153                 }
154             }
155         }
156     }
157 }
158