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-2002 (C) Intalio, Inc. All Rights Reserved. 32 * 33 * $Id: TestCaseAggregator.java 6787 2007-01-29 06:00:49Z ekuns $ 34 */ 35 package org.castor.xmlctf; 36 37 import java.io.File; 38 39 import org.castor.xmlctf.util.FileServices; 40 41 import junit.framework.Test; 42 import junit.framework.TestCase; 43 import junit.framework.TestSuite; 44 45 /** 46 * This class is used to inspect recursively a hierarchy of directories that contain CTF tests (jars 47 * or directories). A JUnit TestSuite is created for each directory. 48 * 49 * @author <a href="mailto:gignoux@kernelcenter.org">Sebastien Gignoux</a> 50 * @author <a href="mailto:blandin@intalio.com">Arnaud Blandin</a> 51 * @version $Revision: 6787 $ $Date: 2006-04-26 15:14:53 -0600 (Wed, 26 Apr 2006) $ 52 */ 53 public class TestCaseAggregator extends TestCase { 54 55 /** File separator for this system. */ 56 private static final String FILE_SEPARATOR = System.getProperty("file.separator"); 57 /** Name of the system property to set up the verbose mode. */ 58 public static final String VERBOSE_PROPERTY = "org.exolab.castor.tests.Verbose"; 59 /** Name of the system property to set up the printStackTrace mode. */ 60 public static final String PRINT_STACK_TRACE = "org.exolab.castor.tests.printStack"; 61 /** True if we desire a lot of info on what happen. */ 62 private static boolean _verbose; 63 64 static { 65 String v = System.getProperty(VERBOSE_PROPERTY); 66 _verbose = (v != null && v.equals("true")); 67 } 68 69 /** The directory that contains CTF test cases. */ 70 private final File _directory; 71 /** Location of the temporary files when the tests are run. */ 72 private final String _testOutputRoot; 73 /** String containing the directory path from the test root to here. */ 74 private final String _directoryToHere; 75 76 /** 77 * Creates a new TestCaseAggregator with the given name. 78 * 79 * @param name the name of this TestCaseAggregator 80 */ 81 public TestCaseAggregator(final String name) { 82 super(name); 83 _directory = null; 84 _directoryToHere = null; 85 _testOutputRoot = null; 86 } 87 88 /** 89 * Create a new TestCaseAggregator which will inspect the directory given in parameter. 90 * 91 * @param directory the directory to inspect for test case and subdirectory 92 * @param testOutputRoot the path to the directory where the test in this directory can put there 93 * temporary files. 94 */ 95 public TestCaseAggregator(final File directory, final String testOutputRoot) { 96 super(directory.getName()); 97 _directory = directory; 98 _directoryToHere = ""; 99 _testOutputRoot = testOutputRoot; 100 } 101 102 /** 103 * Create a new TestCaseAggregator which will inspect the directory given in parameter. 104 * 105 * @param directory the directory to inspect for test case and subdirectory 106 * @param directoryToHere the directory path above "directory" 107 * @param testOutputRoot the path to the directory where the test in this directory can put there 108 * temporary files. 109 */ 110 protected TestCaseAggregator(final File directory, final String directoryToHere, 111 final String testOutputRoot) { 112 super(directory.getName()); 113 _directory = directory; 114 _directoryToHere = directoryToHere + "/"; 115 _testOutputRoot = testOutputRoot; 116 } 117 118 /** 119 * Assembles and returns a test suite containing all known tests. 120 * 121 * @return A non-null test suite. 122 */ 123 public Test suite() { 124 final TestSuite suite = new TestSuite(); 125 final String suiteName = 126 (_directoryToHere.length() == 0) ? _directory.getName() : _directoryToHere; 127 suite.setName(suiteName); 128 129 if (!_directory.isDirectory()) { 130 // Maybe it is a jar file, it happens if we run the 131 // CastorTestSuiteRunner with just one jar in param 132 if (_directory.getName().endsWith(".jar")) { 133 CastorTestCase tc = new CastorTestCase(_directory, _directoryToHere, _testOutputRoot); 134 return tc.suite(); 135 } 136 // If not a jar file, just return the empty TestSuite 137 return suite; 138 } 139 140 String outputRoot = _testOutputRoot; 141 if (outputRoot.endsWith("/") || outputRoot.endsWith(FILE_SEPARATOR)) { 142 outputRoot = outputRoot + _directory.getName(); 143 } else { 144 outputRoot = outputRoot + FILE_SEPARATOR + _directory.getName(); 145 } 146 147 verbose("\n=================================================================="); 148 verbose("Processing directory:\n" + _directory.getAbsolutePath()); 149 verbose("==================================================================\n"); 150 151 final File[] list = _directory.listFiles(); 152 for (int i = 0; i < list.length; ++i) { 153 processOneFileOrDirectory(suite, outputRoot, list[i]); 154 } 155 156 return suite; 157 } 158 159 /** 160 * Processes a single file or directory, recurses into directories and for files, if a JAR file or 161 * a test descriptor XML file, create a new test case and add it to our test suite. 162 * 163 * @param suite the test suite to add new tests to 164 * @param outputRoot output directory for temporary files in our tests 165 * @param file the file or directory to process 166 */ 167 private void processOneFileOrDirectory(final TestSuite suite, final String outputRoot, 168 final File file) { 169 String name = file.getName(); 170 171 // If a directory (and not a source control directory), recurse 172 if (file.isDirectory()) { 173 // Directories that contain TestDescriptor.xml do not get recursed any deeper 174 if (!FileServices.isScmDirectory(name) 175 && !((new File(file.getParentFile(), "TestDescriptor.xml")).exists())) { 176 // look for jars or testDescriptor files inside the directory 177 Test test = 178 new TestCaseAggregator(file, _directoryToHere + file.getName(), outputRoot).suite(); 179 if (test != null) { 180 suite.addTest(test); 181 } 182 } 183 return; 184 } 185 186 // Otherwise make a test case if a JAR file or test descriptor 187 188 Test test = null; 189 if (name.endsWith(".jar")) { 190 test = (new CastorTestCase(file, _directoryToHere, outputRoot)).suite(); 191 } else if (name.endsWith(CastorTestCase.TEST_DESCRIPTOR)) { 192 test = (new CastorTestCase(_directory, _directoryToHere, outputRoot)).suite(); 193 } 194 195 if (test != null) { 196 suite.addTest(test); 197 } 198 } 199 200 /** 201 * Prints the provided message if verbose is true. 202 * 203 * @param message the message to print 204 */ 205 private void verbose(final String message) { 206 if (_verbose) { 207 System.out.println(message); 208 } 209 } 210 211 }