Package

Source Code of TestAll

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;

import java.io.File;
import java.io.FileFilter;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.Iterator;

public class TestAll extends TestCase {

    private static final FileFilter directoryFilter = new FileFilter() {
        public boolean accept(File pathname) {
            return pathname.isDirectory() && pathname.getName().indexOf(".dependency-info") == -1;
        }
    };

    private static final FilenameFilter testFilter = new FilenameFilter() {
        public boolean accept(File dir, String name) {
            return isTestClass(name);
        }
    };

    public TestAll(String name) {
        super(name);
    }

    public static Test suite() {
        BasicConfigurator.configure();
        Logger.getRoot().setLevel(Level.FATAL);
        return makeTestSuite();
    }

    public static void printTestsInSuite() {
        TestSuite suite = makeTestSuite();
        java.util.Enumeration tests = suite.tests();
        while (tests.hasMoreElements()) {
            Test test = (Test)tests.nextElement();
            System.out.println("test = " + test.toString());
        }
    }

    private static TestSuite makeTestSuite() {
        TestSuite suite = new TestSuite("All Tests in classpath");
        File file = new File(getTestClassPath());
        ArrayList currentPath = new ArrayList();
        addTestsInHierarchyToSuite(suite, file, currentPath);
        return suite;
    }
    private static String getTestPath() {
       String relativeClassFilePath = TestAll.class.getName().replaceAll("\\.", "/")+".class";
       String absoluteClassFilePath = TestAll.class.getResource("/"+relativeClassFilePath).getFile();
       return absoluteClassFilePath.substring(0, absoluteClassFilePath.length() - relativeClassFilePath.length());
    }

    private static String getTestClassPath() {
        String classPath = System.getProperty("TestPath");
        if (classPath != null) return classPath;
        return getTestPath();
//        StringTokenizer tokenizer = new StringTokenizer(System.getProperty("java.class.path"), System.getProperty("path.separator"));
//        do {
//            classPath = tokenizer.nextToken();
//        } while (classPath.indexOf("junit.jar") != -1);
//        return classPath;
    }

    private static void addTestsInHierarchyToSuite(TestSuite suite, File classPathRoot, ArrayList currentPath) {
        addTestsInDirectoryToSuite(suite, classPathRoot, currentPath);
        File[] tests = classPathRoot.listFiles(directoryFilter);
        if (tests == null) {
            return;
        }
        for (int i = 0; i < tests.length; i++) {
            File directory = tests[i];
            currentPath.add(directory.getName());
            try {
                addTestsInHierarchyToSuite(suite, new File(classPathRoot.getCanonicalPath() + File.separator + directory.getName()), currentPath);
            } catch (Exception ex) {
                System.out.println("ex = " + ex);
            }
            currentPath.remove(currentPath.size() - 1);
        }
    }

    private static void addTestsInDirectoryToSuite(TestSuite suite, File classPathRoot, ArrayList currentPath) {
        File[] tests = classPathRoot.listFiles(testFilter);
        if (tests == null) {
            return;
        }
        for (int i = 0; i < tests.length; i++) {
            addTestToSuite(suite, currentPath, tests[i]);
        }
    }

    private static String makeFullyQualifiedClassName(ArrayList currentPath, File testcaseFile) {
        StringBuffer fullyQualified = new StringBuffer();
        for (Iterator iterator = currentPath.iterator(); iterator.hasNext();) {
            fullyQualified.append((String)iterator.next());
            fullyQualified.append(".");
        }
        fullyQualified.append(stripClassSuffix(testcaseFile.getName()));
        return fullyQualified.toString();
    }

    private static void addTestToSuite(TestSuite suite, ArrayList currentPath, File testcaseFile) {
        String fullClassName = makeFullyQualifiedClassName(currentPath, testcaseFile);
        try {
            suite.addTest(new TestSuite(Class.forName(fullClassName)));
        } catch (ClassNotFoundException ex) {
            throw new RuntimeException("TestAll: could not load class " + fullClassName);
        }
    }

    private static boolean isTestClass(String filename) {
        return filename.startsWith("Test") && filename.endsWith(".class") && !filename.startsWith("TestAll")
                && !(filename.indexOf("$") >= 0);
    }

    private static String stripClassSuffix(String filename) {
        int endingIndex = filename.indexOf(".class");
        return filename.substring(0, endingIndex);
    }
}
TOP

Related Classes of TestAll

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.