/*
* This software and supporting documentation were developed by
*
* Siemens Corporate Technology
* Competence Center Knowledge Management and Business Transformation
* D-81730 Munich, Germany
*
* Authors (representing a really great team ;-) )
* Stefan B. Augustin, Thorbj�rn Hansen, Manfred Langen
*
* This software is Open Source under GNU General Public License (GPL).
* Read the text of this license in LICENSE.TXT
* or look at www.opensource.org/licenses/
*
* Once more we emphasize, that:
* THIS SOFTWARE IS MADE AVAILABLE, AS IS, WITHOUT ANY WARRANTY
* REGARDING THE SOFTWARE, ITS PERFORMANCE OR
* FITNESS FOR ANY PARTICULAR USE, FREEDOM FROM ANY COMPUTER DISEASES OR
* ITS CONFORMITY TO ANY SPECIFICATION. THE ENTIRE RISK AS TO QUALITY AND
* PERFORMANCE OF THE SOFTWARE IS WITH THE USER.
*
*/
// CompleteTest
// Package
package appl.Portal.Test;
// Imports
// This application/module packages
import appl.Portal.Test.*;
// Other application/module packages
import junit.framework.*;
// KFM packages
import KFM.DirNavigator.*;
// Library classes (JHDK, JSDK, RegExp, ...)
// Java packages
import java.io.*;
import java.lang.reflect.*;
import java.util.*;
/** One sentence about CompleteTest.
*
* <P>CompleteTest documentation.</P>
*
* <H2>Usage</H2>
*
* <P>Usage.</P>
*
* <H2>Related classes</H2>
*
* <P>Cut&paste-source was `P_Test�.</P>
*
* <PRE>
* - ServletTestCase -- Father class.
* </PRE>
*
* @version 0.1 (2001 04 25)
* @see ServletTestCase
*/
public class CompleteTest
extends ServletTestCase
{
static class NotFoundException extends Exception { }
/** CompleteTest
*
* Constructor for this class.
*/
public CompleteTest(String aName) {
super(aName);
}
public static Test suite()
{
final File tBasePath = new File("o:/KFM/java/lib");
// Create a new TestSuite with name of this class
// which contains all basic tests in this class (void test*())
final Vector tTestVector = new Vector(); // of Test
DirNavigator tNavigator = new DirNavigator(tBasePath, new FileWorker() {
public void workFile(File aFile) {
if(aFile.getName().endsWith(".class")) {
// ex: .KFM.GUI.MultiPart.class
String tPackage = KFM.Converter.replaceString(File.separator, ".", aFile.getAbsolutePath());
tPackage = tPackage.substring(tBasePath.toString().length() + 1, tPackage.length() - ".class".length());
try {
Class tClass = Class.forName(tPackage);
// only consider junit.framework.TestCase classes or subclasses
if(junit.framework.TestCase.class.isAssignableFrom(tClass)) {
if(CompleteTest.class.equals(tClass)) {
// do not consider Complete Test, otherwise endless recursion
return;
}
Test tTest = getTestSuite(tClass, tPackage);
if(tTest != null) {
tTestVector.add(tTest);
}
}
} catch(ClassNotFoundException e) {
// ignore classes which cannot be loaded, we can't test them either
}
}
}
});
tNavigator.traverse();
TestSuite tTestSuite = new TestSuite("CompleteTest");
for(Iterator tIterator = tTestVector.iterator(); tIterator.hasNext(); ) {
Test tTest = (Test) tIterator.next();
if(tTest instanceof TestSuite) {
tTestSuite.addTest(tTest);
}
}
lOuter:
for(Iterator tIterator = tTestVector.iterator(); tIterator.hasNext(); ) {
Test tTest = (Test) tIterator.next();
if(tTest instanceof TestCase) {
System.out.println(((TestCase) tTest).getName());
/*
try {
find(tTestSuite, (TestCase) tTest);
tTestSuite.addTest(tTest);
} catch(NotFoundException e) {
}
*/
}
}
return tTestSuite;
}
private static void find(TestSuite aSuite, TestCase aTestCase)
throws NotFoundException
{
String tName = aTestCase.getName();
System.out.println("checking: " + tName);
for(Enumeration tEnum = aSuite.tests(); tEnum.hasMoreElements(); ) {
Test tLoadedTest = (Test) tEnum.nextElement();
String tLoadedName = "";
if(tLoadedTest instanceof TestCase) {
tLoadedName = ((TestCase) tLoadedTest).getName();
System.out.println("compareto: " + tLoadedName);
if(tLoadedName.equals(tName)) throw new NotFoundException();
} else
if(tLoadedTest instanceof TestSuite) {
find((TestSuite) tLoadedTest, aTestCase);
}
}
}
/** Create a Test for the Test Class.
*
* @param aTestClass
* @param aDefaultName
*/
public static Test getTestSuite(Class aTestClass, String aDefaultName)
{
System.out.println(aDefaultName);
try {
try {
// if the class defines a suite method, create this suite
Method tSuiteMethod = aTestClass.getMethod("suite", new Class[0]);
if(tSuiteMethod != null) {
return (Test) tSuiteMethod.invoke(null, new Object[0]);
}
} catch(NoSuchMethodException e) {
// this can be ok, TestCase without a suite method is legitimate
}
// otherwise try to invoke the constructor passing the package/name of the class
Constructor tConstructor = aTestClass.getConstructor(new Class[] { String.class });
if(tConstructor != null) {
return (Test) tConstructor.newInstance(new Object[] { aDefaultName });
}
} catch (Exception e){
e.printStackTrace(System.err);
}
return null;
}
}