alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

// $Header: /home/cvs/jakarta-jmeter/src/jorphan/org/apache/jorphan/test/AllTests.java,v 1.27 2004/03/12 23:39:25 sebb Exp $
/*
 * Copyright 2001-2004 The Apache Software Foundation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
*/

package org.apache.jorphan.test;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.Iterator;
import java.util.Properties;

import junit.framework.TestCase;
import junit.framework.TestSuite;
import junit.textui.TestRunner;

import org.apache.jorphan.logging.LoggingManager;
import org.apache.jorphan.reflect.ClassFinder;
import org.apache.jorphan.util.JOrphanUtils;
import org.apache.log.Logger;

/**
 * Provides a quick and easy way to run all
 * junit unit tests in your java
 * project.  It will find all unit test classes and run all their test methods.
 * There is no need to configure it in any way to find these classes except to
 * give it a path to search.
 * 

* Here is an example Ant target (See Ant at * Apache) that runs all your unit * tests: *

 *      <target name="test" depends="compile">
 *          <java classname="org.apache.jorphan.test.AllTests" fork="yes">
 *              <classpath>
 *                  <path refid="YOUR_CLASSPATH"/>
 *                  <pathelement location="ROOT_DIR_OF_YOUR_COMPILED_CLASSES"/>
 *              </classpath>
 *              <arg value="SEARCH_PATH/"/>
 *              <arg value="PROPERTY_FILE"/>
 *              <arg value="NAME_OF_UNITTESTMANAGER_CLASS"/>
 *          </java>
 *      </target>
 * 
* *
*
YOUR_CLASSPATH
*
Refers to the classpath that includes all jars and libraries need to * run your unit tests
* *
ROOT_DIR_OF_YOUR_COMPILED_CLASSES
*
The classpath should include the directory where all your project's * classes are compiled to, if it doesn't already.
* *
SEARCH_PATH
*
The first argument tells AllTests where to look for unit test classes * to execute. In most cases, it is identical to * ROOT_DIR_OF_YOUR_COMPILED_CLASSES. You can specify multiple * directories or jars to search by providing a comma-delimited list.
* *
PROPERTY_FILE
*
A simple property file that sets logging parameters. It is optional * and is only relevant if you use the same logging packages that JOrphan * uses.
* *
NAME_OF_UNITTESTMANAGER_CLASS
*
If your system requires some configuration to run correctly, you can * implement the {@link UnitTestManager} interface and be given an * opportunity to initialize your system from a configuration file.
*
* * @see UnitTestManager * @author Michael Stover (mstover1 at apache.org) * @version $Revision: 1.27 $ */ public final class AllTests { transient private static Logger log = LoggingManager.getLoggerForClass(); /** * Private constructor to prevent instantiation. */ private AllTests() { } private static void logprop(String prop,boolean show) { String value = System.getProperty(prop); log.info(prop+"="+value); if (show) System.out.println(prop+"="+value); } private static void logprop(String prop) { logprop(prop,false); } /** * Starts a run through all unit tests found in the specified classpaths. * The first argument should be a list of paths to search. The second * argument is optional and specifies a properties file used to initialize * logging. The third argument is also optional, and specifies a class * that implements the UnitTestManager interface. This provides a means of * initializing your application with a configuration file prior to the * start of any unit tests. * * @param args the command line arguments */ public static void main(String[] args) { if (args.length < 1) { System.out.println( "You must specify a comma-delimited list of paths to search " + "for unit tests"); System.exit(0); } initializeLogging(args); initializeManager(args); // end : added - 11 July 2001 logprop("java.version",true); logprop("java.vendor"); logprop("java.home",true); logprop("user.home"); logprop("user.dir",true); logprop("os.name"); logprop("os.version"); logprop("os.arch"); logprop("java.class.version"); //logprop("java.class.path"); String cp = System.getProperty("java.class.path"); String cpe[]= JOrphanUtils.split(cp,java.io.File.pathSeparator); StringBuffer sb = new StringBuffer(3000); sb.append("java.class.path="); for (int i=0;i= 2) { Properties props = new Properties(); try { System.out.println( "Setting up logging props using file: " + args[1]); props.load(new FileInputStream(args[1])); LoggingManager.initializeLogging(props); } catch (FileNotFoundException e) { } catch (IOException e) { } } } /** * An overridable method that that instantiates a UnitTestManager (if one * was specified in the command-line arguments), and hands it the name of * the properties file to use to configure the system. * @param args */ protected static void initializeManager(String[] args) { if (args.length >= 3) { try { System.out.println( "Using initializeProperties() from " + args[2]); UnitTestManager um = (UnitTestManager) Class.forName(args[2]).newInstance(); System.out.println( "Setting up initial properties using: " + args[1]); um.initializeProperties(args[1]); } catch (Exception e) { System.out.println("Couldn't create: " + args[2]); e.printStackTrace(); } } } /* * Externally callable suite() method for use by JUnit * Allows tests to be run directly under JUnit, rather than using the * startup code in the rest of the module. No parameters can be passed in, * so it is less flexible. */ public static TestSuite suite() { String args[] = { "../lib/ext", "./jmetertest.properties", "org.apache.jmeter.util.JMeterUtils" }; initializeManager(args); return suite(args[0]); } /** * A unit test suite for JUnit. * * @return The test suite */ private static TestSuite suite(String searchPaths) { TestSuite suite = new TestSuite(); try { Iterator classes = ClassFinder .findClassesThatExtend( JOrphanUtils.split(searchPaths, ","), new Class[] { TestCase.class }, true) .iterator(); while (classes.hasNext()) { String name = (String) classes.next(); try { /* * TestSuite only finds testXXX() methods, and does not look for * suite() methods. * * To provide more compatibilty with stand-alone tests, where JUnit * does look for a suite() method, check for it first here. * */ Class clazz = Class.forName(name); TestSuite t= null; try { Method m = clazz.getMethod("suite", new Class[0]); t = (TestSuite) m.invoke(clazz,null); } catch (NoSuchMethodException e) {} // this is not an error, the others are //catch (SecurityException e) {} //catch (IllegalAccessException e) {} //catch (IllegalArgumentException e) {} //catch (InvocationTargetException e) {} if (t == null) { t= new TestSuite(clazz); } suite.addTest(t); } catch (Exception ex) { System.out.println("Error adding test for class "+name+" "+ex.toString()); log.error("error adding test :", ex); } } } catch (IOException e) { log.error("", e); } catch (ClassNotFoundException e) { log.error("", e); } return suite; } }
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.