Search in sources :

Example 26 with Test

use of junit.framework.Test in project guava by hceylan.

the class FeatureSpecificTestSuiteBuilder method filterSuite.

private TestSuite filterSuite(TestSuite suite) {
    TestSuite filtered = new TestSuite(suite.getName());
    final Enumeration<?> tests = suite.tests();
    while (tests.hasMoreElements()) {
        Test test = (Test) tests.nextElement();
        if (matches(test)) {
            filtered.addTest(test);
        }
    }
    return filtered;
}
Also used : TestSuite(junit.framework.TestSuite) Test(junit.framework.Test)

Example 27 with Test

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

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)

Example 28 with Test

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

the class TestCaseUtil method getTestAtIndex.

public static Test getTestAtIndex(TestSuite testSuite, int position) {
    int index = 0;
    Enumeration enumeration = testSuite.tests();
    while (enumeration.hasMoreElements()) {
        Test test = (Test) enumeration.nextElement();
        if (index == position) {
            return test;
        }
        index++;
    }
    return null;
}
Also used : Enumeration(java.util.Enumeration) Test(junit.framework.Test)

Example 29 with Test

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

the class TestCaseUtil method getTestCaseNames.

@SuppressWarnings("unchecked")
public static List<String> getTestCaseNames(Test test, boolean flatten) {
    List<Test> tests = (List<Test>) getTests(test, flatten);
    List<String> testCaseNames = Lists.newArrayList();
    for (Test aTest : tests) {
        testCaseNames.add(getTestName(aTest));
    }
    return testCaseNames;
}
Also used : Test(junit.framework.Test) List(java.util.List)

Example 30 with Test

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

the class TestRunner method run.

/*
    This class determines if more suites are added to this class then adds all individual
    test classes to a test suite for run
     */
public void run(String className) {
    try {
        mClassName = className;
        Class clazz = mContext.getClassLoader().loadClass(className);
        Method method = getChildrenMethod(clazz);
        if (method != null) {
            String[] children = getChildren(method);
            run(children);
        } else if (mRunnableClass.isAssignableFrom(clazz)) {
            Runnable test = (Runnable) clazz.newInstance();
            TestCase testcase = null;
            if (test instanceof TestCase) {
                testcase = (TestCase) test;
            }
            Throwable e = null;
            boolean didSetup = false;
            started(className);
            try {
                if (testcase != null) {
                    testcase.setUp(mContext);
                    didSetup = true;
                }
                if (mMode == PERFORMANCE) {
                    runInPerformanceMode(test, className, false, className);
                } else if (mMode == PROFILING) {
                    //Need a way to mark a test to be run in profiling mode or not.
                    startProfiling();
                    test.run();
                    finishProfiling();
                } else {
                    test.run();
                }
            } catch (Throwable ex) {
                e = ex;
            }
            if (testcase != null && didSetup) {
                try {
                    testcase.tearDown();
                } catch (Throwable ex) {
                    e = ex;
                }
            }
            finished(className);
            if (e == null) {
                passed(className);
            } else {
                failed(className, e);
            }
        } else if (mJUnitClass.isAssignableFrom(clazz)) {
            Throwable e = null;
            //Create a Junit Suite.
            JunitTestSuite suite = new JunitTestSuite();
            Method[] methods = getAllTestMethods(clazz);
            for (Method m : methods) {
                junit.framework.TestCase test = (junit.framework.TestCase) clazz.newInstance();
                test.setName(m.getName());
                if (test instanceof AndroidTestCase) {
                    AndroidTestCase testcase = (AndroidTestCase) test;
                    try {
                        testcase.setContext(mContext);
                        testcase.setTestContext(mContext);
                    } catch (Exception ex) {
                        Log.i("TestHarness", ex.toString());
                    }
                }
                suite.addTest(test);
            }
            if (mMode == PERFORMANCE) {
                final int testCount = suite.testCount();
                for (int j = 0; j < testCount; j++) {
                    Test test = suite.testAt(j);
                    started(test.toString());
                    try {
                        runInPerformanceMode(test, className, true, test.toString());
                    } catch (Throwable ex) {
                        e = ex;
                    }
                    finished(test.toString());
                    if (e == null) {
                        passed(test.toString());
                    } else {
                        failed(test.toString(), e);
                    }
                }
            } else if (mMode == PROFILING) {
                //Need a way to mark a test to be run in profiling mode or not.
                startProfiling();
                junit.textui.TestRunner.run(suite);
                finishProfiling();
            } else {
                junit.textui.TestRunner.run(suite);
            }
        } else {
            System.out.println("Test wasn't Runnable and didn't have a" + " children method: " + className);
        }
    } catch (ClassNotFoundException e) {
        Log.e("ClassNotFoundException for " + className, e.toString());
        if (isJunitTest(className)) {
            runSingleJunitTest(className);
        } else {
            missingTest(className, e);
        }
    } catch (InstantiationException e) {
        System.out.println("InstantiationException for " + className);
        missingTest(className, e);
    } catch (IllegalAccessException e) {
        System.out.println("IllegalAccessException for " + className);
        missingTest(className, e);
    }
}
Also used : Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) Test(junit.framework.Test)

Aggregations

Test (junit.framework.Test)112 TestSuite (junit.framework.TestSuite)44 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