Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit d5fd01c8 authored by Paul Duffin's avatar Paul Duffin Committed by android-build-merger
Browse files

Move JUnit classes from here into external/junit am: 0342ab5b am: b9e33cd5 am: f99687ee

am: e3b757a4

Change-Id: Ib11a330ca65e9c2d2904993cbd67e604b2b80d5d
parents 595e8339 e3b757a4
Loading
Loading
Loading
Loading
+0 −1
Original line number Diff line number Diff line
http://www.opensource.org/licenses/cpl1.0.php
+0 −81
Original line number Diff line number Diff line
package junit.runner;

import java.util.*;
import java.io.*;

/**
 * An implementation of a TestCollector that consults the
 * class path. It considers all classes on the class path
 * excluding classes in JARs. It leaves it up to subclasses
 * to decide whether a class is a runnable Test.
 *
 * @see TestCollector
 * {@hide} - Not needed for 1.0 SDK
 */
public abstract class ClassPathTestCollector implements TestCollector {

    static final int SUFFIX_LENGTH= ".class".length();

    public ClassPathTestCollector() {
    }

    public Enumeration collectTests() {
        String classPath= System.getProperty("java.class.path");
        Hashtable result = collectFilesInPath(classPath);
        return result.elements();
    }

    public Hashtable collectFilesInPath(String classPath) {
        Hashtable result= collectFilesInRoots(splitClassPath(classPath));
        return result;
    }

    Hashtable collectFilesInRoots(Vector roots) {
        Hashtable result= new Hashtable(100);
        Enumeration e= roots.elements();
        while (e.hasMoreElements())
            gatherFiles(new File((String)e.nextElement()), "", result);
        return result;
    }

    void gatherFiles(File classRoot, String classFileName, Hashtable result) {
        File thisRoot= new File(classRoot, classFileName);
        if (thisRoot.isFile()) {
            if (isTestClass(classFileName)) {
                String className= classNameFromFile(classFileName);
                result.put(className, className);
            }
            return;
        }
        String[] contents= thisRoot.list();
        if (contents != null) {
            for (int i= 0; i < contents.length; i++)
                gatherFiles(classRoot, classFileName+File.separatorChar+contents[i], result);
        }
    }

    Vector splitClassPath(String classPath) {
        Vector result= new Vector();
        String separator= System.getProperty("path.separator");
        StringTokenizer tokenizer= new StringTokenizer(classPath, separator);
        while (tokenizer.hasMoreTokens())
            result.addElement(tokenizer.nextToken());
        return result;
    }

    protected boolean isTestClass(String classFileName) {
        return
                classFileName.endsWith(".class") &&
                classFileName.indexOf('$') < 0 &&
                classFileName.indexOf("Test") > 0;
    }

    protected String classNameFromFile(String classFileName) {
        // convert /a/b.class to a.b
        String s= classFileName.substring(0, classFileName.length()-SUFFIX_LENGTH);
        String s2= s.replace(File.separatorChar, '.');
        if (s2.startsWith("."))
            return s2.substring(1);
        return s2;
    }
}
+0 −28
Original line number Diff line number Diff line
package junit.runner;

// The following line was removed for compatibility with Android libraries.
//import java.awt.Component;

import junit.framework.*;

/**
 * A view to show a details about a failure
 * {@hide} - Not needed for 1.0 SDK
 */
public interface FailureDetailView {
    // The following definition was removed for compatibility with Android
    // libraries.
    //  /**
    //   * Returns the component used to present the TraceView
    //   */
    //  public Component getComponent();

    /**
     * Shows details of a TestFailure
     */
    public void showFailure(TestFailure failure);
    /**
     * Clears the view
     */
    public void clear();
}
+0 −69
Original line number Diff line number Diff line
package junit.runner;

import java.lang.reflect.*;
import junit.framework.*;

/**
 * An implementation of a TestCollector that loads
 * all classes on the class path and tests whether
 * it is assignable from Test or provides a static suite method.
 * @see TestCollector
 * {@hide} - Not needed for 1.0 SDK
 */
public class LoadingTestCollector extends ClassPathTestCollector {

    TestCaseClassLoader fLoader;

    public LoadingTestCollector() {
        fLoader= new TestCaseClassLoader();
    }

    protected boolean isTestClass(String classFileName) {
        try {
            if (classFileName.endsWith(".class")) {
                Class testClass= classFromFile(classFileName);
                return (testClass != null) && isTestClass(testClass);
            }
        }
        catch (ClassNotFoundException expected) {
        }
        catch (NoClassDefFoundError notFatal) {
        }
        return false;
    }

    Class classFromFile(String classFileName) throws ClassNotFoundException {
        String className= classNameFromFile(classFileName);
        if (!fLoader.isExcluded(className))
            return fLoader.loadClass(className, false);
        return null;
    }

    boolean isTestClass(Class testClass) {
        if (hasSuiteMethod(testClass))
            return true;
        if (Test.class.isAssignableFrom(testClass) &&
                Modifier.isPublic(testClass.getModifiers()) &&
                hasPublicConstructor(testClass))
            return true;
        return false;
    }

    boolean hasSuiteMethod(Class testClass) {
        try {
            testClass.getMethod(BaseTestRunner.SUITE_METHODNAME, new Class[0]);
        } catch(Exception e) {
            return false;
        }
        return true;
    }

    boolean hasPublicConstructor(Class testClass) {
        try {
            TestSuite.getTestConstructor(testClass);
        } catch(NoSuchMethodException e) {
            return false;
        }
        return true;
    }
}
+0 −20
Original line number Diff line number Diff line
package junit.runner;

/**
 * A TestSuite loader that can reload classes.
 * {@hide} - Not needed for 1.0 SDK
 */
public class ReloadingTestSuiteLoader implements TestSuiteLoader {

    public Class load(String suiteClassName) throws ClassNotFoundException {
        return createLoader().loadClass(suiteClassName, true);
    }

    public Class reload(Class aClass) throws ClassNotFoundException {
        return createLoader().loadClass(aClass.getName(), true);
    }

    protected TestCaseClassLoader createLoader() {
        return new TestCaseClassLoader();
    }
}
Loading