Search in sources :

Example 91 with Test

use of junit.framework.Test in project intellij-community by JetBrains.

the class TestRunnerUtil method createMethodSuite.

private static Test createMethodSuite(JUnit3IdeaTestRunner runner, Class testClass, String methodName) {
    runner.clearStatus();
    try {
        Constructor constructor = testClass.getConstructor(new Class[] { String.class });
        return (Test) constructor.newInstance(new Object[] { methodName });
    } catch (NoSuchMethodException e) {
        try {
            Constructor constructor = testClass.getConstructor(new Class[0]);
            TestCase test = (TestCase) constructor.newInstance(new Object[0]);
            test.setName(methodName);
            return test;
        } catch (ClassCastException e1) {
            boolean methodExists;
            try {
                //noinspection SSBasedInspection
                testClass.getMethod(methodName, new Class[0]);
                methodExists = true;
            } catch (NoSuchMethodException e2) {
                methodExists = false;
            }
            if (!methodExists) {
                String error = MessageFormat.format(ourBundle.getString("junit.method.not.found"), new Object[] { methodName });
                String message = MessageFormat.format(ourBundle.getString("junit.cannot.instantiate.tests"), new Object[] { error });
                return new FailedTestCase(testClass, methodName, message, null);
            }
            runner.runFailed(MessageFormat.format(ourBundle.getString("junit.class.not.derived"), new Object[] { testClass.getName() }));
            return null;
        } catch (Exception e1) {
            String message = MessageFormat.format(ourBundle.getString("junit.cannot.instantiate.tests"), new Object[] { e1.toString() });
            return new FailedTestCase(testClass, methodName, message, e1);
        }
    } catch (Throwable e) {
        String message = MessageFormat.format(ourBundle.getString("junit.cannot.instantiate.tests"), new Object[] { e.toString() });
        return new FailedTestCase(testClass, methodName, message, e);
    }
}
Also used : Test(junit.framework.Test) TestCase(junit.framework.TestCase) Constructor(java.lang.reflect.Constructor) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 92 with Test

use of junit.framework.Test in project intellij-community by JetBrains.

the class TestRunnerUtil method getTestSuite.

public static Test getTestSuite(JUnit3IdeaTestRunner runner, String[] suiteClassNames) {
    if (suiteClassNames.length == 0) {
        return null;
    }
    Vector result = new Vector();
    for (int i = 0; i < suiteClassNames.length; i++) {
        String suiteClassName = suiteClassNames[i];
        Test test;
        if (suiteClassName.charAt(0) == '@') {
            // all tests in the package specified
            String[] classNames;
            String suiteName;
            try {
                BufferedReader reader = new BufferedReader(new FileReader(suiteClassName.substring(1)));
                Vector vector;
                try {
                    suiteName = reader.readLine();
                    //category
                    reader.readLine();
                    vector = new Vector();
                    String line;
                    while ((line = reader.readLine()) != null) {
                        vector.addElement(line);
                    }
                } finally {
                    reader.close();
                }
                // toArray cannot be used here because the class must be compilable with 1.1
                classNames = new String[vector.size()];
                for (int j = 0; j < classNames.length; j++) {
                    classNames[j] = (String) vector.elementAt(j);
                }
            } catch (Exception e) {
                runner.runFailed(MessageFormat.format(ourBundle.getString("junit.runner.error"), new Object[] { e.toString() }));
                return null;
            }
            test = new TestAllInPackage2(runner, suiteName, classNames);
        } else {
            test = createClassOrMethodSuite(runner, suiteClassName);
            if (test == null)
                return null;
        }
        result.addElement(test);
    }
    if (result.size() == 1) {
        return (Test) result.elementAt(0);
    } else {
        TestSuite suite = new TestSuite();
        for (int i = 0; i < result.size(); i++) {
            final Test test = (Test) result.elementAt(i);
            suite.addTest(test);
        }
        return suite;
    }
}
Also used : TestSuite(junit.framework.TestSuite) Test(junit.framework.Test) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) Vector(java.util.Vector) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 93 with Test

use of junit.framework.Test in project android_frameworks_base by AOSPA.

the class UiAutomatorInstrumentationTestRunner method getAndroidTestRunner.

@Override
protected AndroidTestRunner getAndroidTestRunner() {
    AndroidTestRunner testRunner = super.getAndroidTestRunner();
    testRunner.addTestListener(new TestListener() {

        @Override
        public void startTest(Test test) {
            if (test instanceof UiAutomatorTestCase) {
                ((UiAutomatorTestCase) test).initialize(getArguments());
            }
        }

        @Override
        public void endTest(Test test) {
        }

        @Override
        public void addFailure(Test test, AssertionFailedError e) {
        }

        @Override
        public void addError(Test test, Throwable t) {
        }
    });
    return testRunner;
}
Also used : Test(junit.framework.Test) TestListener(junit.framework.TestListener) AssertionFailedError(junit.framework.AssertionFailedError) AndroidTestRunner(android.test.AndroidTestRunner)

Example 94 with Test

use of junit.framework.Test in project voltdb by VoltDB.

the class JSR166TestCase method main.

/**
     * Runs all JSR166 unit tests using junit.textui.TestRunner
     */
public static void main(String[] args) {
    if (useSecurityManager) {
        System.err.println("Setting a permissive security manager");
        Policy.setPolicy(permissivePolicy());
        System.setSecurityManager(new SecurityManager());
    }
    int iters = (args.length == 0) ? 1 : Integer.parseInt(args[0]);
    Test s = suite();
    for (int i = 0; i < iters; ++i) {
        junit.textui.TestRunner.run(s);
        System.gc();
        System.runFinalization();
    }
    System.exit(0);
}
Also used : Test(junit.framework.Test)

Example 95 with Test

use of junit.framework.Test in project android_frameworks_base by ResurrectionRemix.

the class InstrumentationCoreTestRunner method getAndroidTestRunner.

@Override
protected AndroidTestRunner getAndroidTestRunner() {
    AndroidTestRunner runner = super.getAndroidTestRunner();
    runner.addTestListener(new TestListener() {

        /**
             * The last test class we executed code from.
             */
        private Class<?> lastClass;

        /**
             * The minimum time we expect a test to take.
             */
        private static final int MINIMUM_TIME = 100;

        /**
             * The start time of our current test in System.currentTimeMillis().
             */
        private long startTime;

        public void startTest(Test test) {
            if (test.getClass() != lastClass) {
                lastClass = test.getClass();
                printMemory(test.getClass());
            }
            Thread.currentThread().setContextClassLoader(test.getClass().getClassLoader());
            startTime = System.currentTimeMillis();
        }

        public void endTest(Test test) {
            if (test instanceof TestCase) {
                cleanup((TestCase) test);
                /*
                     * Make sure all tests take at least MINIMUM_TIME to
                     * complete. If they don't, we wait a bit. The Cupcake
                     * Binder can't handle too many operations in a very
                     * short time, which causes headache for the CTS.
                     */
                long timeTaken = System.currentTimeMillis() - startTime;
                if (timeTaken < MINIMUM_TIME) {
                    try {
                        Thread.sleep(MINIMUM_TIME - timeTaken);
                    } catch (InterruptedException ignored) {
                    // We don't care.
                    }
                }
            }
        }

        public void addError(Test test, Throwable t) {
        // This space intentionally left blank.
        }

        public void addFailure(Test test, AssertionFailedError t) {
        // This space intentionally left blank.
        }

        /**
             * Dumps some memory info.
             */
        private void printMemory(Class<? extends Test> testClass) {
            Runtime runtime = Runtime.getRuntime();
            long total = runtime.totalMemory();
            long free = runtime.freeMemory();
            long used = total - free;
            Log.d(TAG, "Total memory  : " + total);
            Log.d(TAG, "Used memory   : " + used);
            Log.d(TAG, "Free memory   : " + free);
            Log.d(TAG, "Now executing : " + testClass.getName());
        }

        /**
             * Nulls all non-static reference fields in the given test class.
             * This method helps us with those test classes that don't have an
             * explicit tearDown() method. Normally the garbage collector should
             * take care of everything, but since JUnit keeps references to all
             * test cases, a little help might be a good idea.
             */
        private void cleanup(TestCase test) {
            Class<?> clazz = test.getClass();
            while (clazz != TestCase.class) {
                Field[] fields = clazz.getDeclaredFields();
                for (int i = 0; i < fields.length; i++) {
                    Field f = fields[i];
                    if (!f.getType().isPrimitive() && !Modifier.isStatic(f.getModifiers())) {
                        try {
                            f.setAccessible(true);
                            f.set(test, null);
                        } catch (Exception ignored) {
                        // Nothing we can do about it.
                        }
                    }
                }
                clazz = clazz.getSuperclass();
            }
        }
    });
    return runner;
}
Also used : Field(java.lang.reflect.Field) Test(junit.framework.Test) TestCase(junit.framework.TestCase) TestListener(junit.framework.TestListener) AssertionFailedError(junit.framework.AssertionFailedError)

Aggregations

Test (junit.framework.Test)114 TestSuite (junit.framework.TestSuite)45 TestCase (junit.framework.TestCase)22 ArrayList (java.util.ArrayList)13 InvocationTargetException (java.lang.reflect.InvocationTargetException)12 Method (java.lang.reflect.Method)12 AssertionFailedError (junit.framework.AssertionFailedError)12 TestListener (junit.framework.TestListener)11 TestResult (junit.framework.TestResult)11 TestDescriptor (android.test.suitebuilder.ListTestCaseNames.TestDescriptor)10 SmallTest (android.test.suitebuilder.annotation.SmallTest)10 Enumeration (java.util.Enumeration)10 RepeatedTest (junit.extensions.RepeatedTest)7 AndroidTestRunner (android.test.AndroidTestRunner)5 Field (java.lang.reflect.Field)5 List (java.util.List)5 IOException (java.io.IOException)3 PrintWriter (java.io.PrintWriter)2 JUnit4TestAdapter (junit.framework.JUnit4TestAdapter)2 GridAbstractTest (org.apache.ignite.testframework.junits.GridAbstractTest)2