Search in sources :

Example 46 with TestCase

use of junit.framework.TestCase in project kotlin by JetBrains.

the class CodegenTestsOnAndroidRunner method parseSingleReportInFolder.

private static List<TestCase> parseSingleReportInFolder(String reportFolder) throws IOException, SAXException, ParserConfigurationException {
    File folder = new File(reportFolder);
    File[] files = folder.listFiles();
    assert files != null;
    assert files.length == 1;
    File reportFile = files[0];
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    Document doc = dBuilder.parse(reportFile);
    Element root = doc.getDocumentElement();
    NodeList testCases = root.getElementsByTagName("testcase");
    List<TestCase> result = new ArrayList(testCases.getLength());
    for (int i = 0; i < testCases.getLength(); i++) {
        Element item = (Element) testCases.item(i);
        final NodeList failure = item.getElementsByTagName("failure");
        String name = item.getAttribute("name");
        String clazz = item.getAttribute("classname");
        if (failure.getLength() == 0) {
            result.add(new TestCase(name) {

                @Override
                protected void runTest() throws Throwable {
                }
            });
        } else {
            result.add(new TestCase(name) {

                @Override
                protected void runTest() throws Throwable {
                    Assert.fail(failure.item(0).getTextContent());
                }
            });
        }
    }
    return result;
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) ArrayList(java.util.ArrayList) Document(org.w3c.dom.Document) DocumentBuilder(javax.xml.parsers.DocumentBuilder) TestCase(junit.framework.TestCase) File(java.io.File)

Example 47 with TestCase

use of junit.framework.TestCase in project kotlin by JetBrains.

the class CodegenTestsOnAndroidRunner method generateTestSuite.

private TestSuite generateTestSuite() {
    TestSuite suite = new TestSuite("MySuite");
    String resultOutput = runTests();
    String reportFolder = pathManager.getTmpFolder() + "/build/outputs/androidTest-results/connected";
    try {
        List<TestCase> testCases = parseSingleReportInFolder(reportFolder);
        for (TestCase aCase : testCases) {
            suite.addTest(aCase);
        }
        Assert.assertNotEquals("There is no test results in report", 0, testCases.size());
    } catch (Exception e) {
        throw new RuntimeException("Can't parse test results in " + reportFolder + "\n" + resultOutput);
    }
    return suite;
}
Also used : TestSuite(junit.framework.TestSuite) TestCase(junit.framework.TestCase) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException)

Example 48 with TestCase

use of junit.framework.TestCase in project jena by apache.

the class TestFactoryTDB method makeTest.

@Override
protected Test makeTest(Resource manifest, Resource entry, String testName, Resource action, Resource result) {
    if (testRootName != null)
        testName = testRootName + testName;
    TestItem testItem = TestItem.create(entry, null);
    TestCase test = null;
    if (testItem.getTestType() != null) {
        if (testItem.getTestType().equals(TestManifestX.TestQuery))
            test = new QueryTestTDB(testName, report, testItem);
        if (testItem.getTestType().equals(TestManifestX.TestSurpressed))
            test = new SurpressedTest(testName, report, testItem);
        if (test == null)
            System.err.println("Unrecognized test type: " + testItem.getTestType());
    }
    // Default 
    if (test == null)
        test = new QueryTestTDB(testName, report, testItem);
    return test;
}
Also used : TestCase(junit.framework.TestCase) SurpressedTest(org.apache.jena.sparql.junit.SurpressedTest) TestItem(org.apache.jena.sparql.junit.TestItem)

Example 49 with TestCase

use of junit.framework.TestCase in project jmeter by apache.

the class JUnitSampler method sample.

/** {@inheritDoc} */
@Override
public SampleResult sample(Entry entry) {
    if (getCreateOneInstancePerSample()) {
        initializeTestObject();
    }
    SampleResult sresult = new SampleResult();
    // Bug 41522 - don't use rlabel here
    sresult.setSampleLabel(getName());
    sresult.setSamplerData(className + "." + methodName);
    sresult.setDataType(SampleResult.TEXT);
    // Assume success
    sresult.setSuccessful(true);
    sresult.setResponseMessage(getSuccess());
    sresult.setResponseCode(getSuccessCode());
    if (this.testCase != null) {
        // create a new TestResult
        TestResult tr = new TestResult();
        final TestCase theClazz = this.testCase;
        try {
            if (setUpMethod != null) {
                setUpMethod.invoke(this.testObject, new Object[0]);
            }
            sresult.sampleStart();
            tr.startTest(this.testCase);
            // Do not use TestCase.run(TestResult) method, since it will
            // call setUp and tearDown. Doing that will result in calling
            // the setUp and tearDown method twice and the elapsed time
            // will include setup and teardown.
            tr.runProtected(theClazz, protectable);
            tr.endTest(this.testCase);
            sresult.sampleEnd();
            if (tearDownMethod != null) {
                tearDownMethod.invoke(testObject, new Object[0]);
            }
        } catch (InvocationTargetException e) {
            Throwable cause = e.getCause();
            if (cause instanceof AssertionFailedError) {
                tr.addFailure(theClazz, (AssertionFailedError) cause);
            } else if (cause instanceof AssertionError) {
                // Convert JUnit4 failure to Junit3 style
                AssertionFailedError afe = new AssertionFailedError(cause.toString());
                // copy the original stack trace
                afe.setStackTrace(cause.getStackTrace());
                tr.addFailure(theClazz, afe);
            } else if (cause != null) {
                tr.addError(theClazz, cause);
            } else {
                tr.addError(theClazz, e);
            }
        } catch (IllegalAccessException | IllegalArgumentException e) {
            tr.addError(theClazz, e);
        }
        if (!tr.wasSuccessful()) {
            sresult.setSuccessful(false);
            StringBuilder buf = new StringBuilder();
            StringBuilder buftrace = new StringBuilder();
            Enumeration<TestFailure> en;
            if (getAppendError()) {
                en = tr.failures();
                if (en.hasMoreElements()) {
                    sresult.setResponseCode(getFailureCode());
                    buf.append(getFailure());
                    buf.append("\n");
                }
                while (en.hasMoreElements()) {
                    TestFailure item = en.nextElement();
                    buf.append("Failure -- ");
                    buf.append(item.toString());
                    buf.append("\n");
                    buftrace.append("Failure -- ");
                    buftrace.append(item.toString());
                    buftrace.append("\n");
                    buftrace.append("Trace -- ");
                    buftrace.append(item.trace());
                }
                en = tr.errors();
                if (en.hasMoreElements()) {
                    sresult.setResponseCode(getErrorCode());
                    buf.append(getError());
                    buf.append("\n");
                }
                while (en.hasMoreElements()) {
                    TestFailure item = en.nextElement();
                    buf.append("Error -- ");
                    buf.append(item.toString());
                    buf.append("\n");
                    buftrace.append("Error -- ");
                    buftrace.append(item.toString());
                    buftrace.append("\n");
                    buftrace.append("Trace -- ");
                    buftrace.append(item.trace());
                }
            }
            sresult.setResponseMessage(buf.toString());
            sresult.setResponseData(buftrace.toString(), null);
        }
    } else {
        // we should log a warning, but allow the test to keep running
        sresult.setSuccessful(false);
        // this should be externalized to the properties
        sresult.setResponseMessage("Failed to create an instance of the class:" + getClassname() + ", reasons may be missing both empty constructor and one " + "String constructor or failure to instantiate constructor," + " check warning messages in jmeter log file");
        sresult.setResponseCode(getErrorCode());
    }
    return sresult;
}
Also used : TestFailure(junit.framework.TestFailure) TestResult(junit.framework.TestResult) InvocationTargetException(java.lang.reflect.InvocationTargetException) TestCase(junit.framework.TestCase) SampleResult(org.apache.jmeter.samplers.SampleResult) AssertionFailedError(junit.framework.AssertionFailedError)

Example 50 with TestCase

use of junit.framework.TestCase in project jena by apache.

the class QueryTestSDBFactory method makeTest.

@Override
public Test makeTest(Resource manifest, Resource entry, String testName, Resource action, Resource result) {
    // Defaults.
    Syntax querySyntax = getQuerySyntax(manifest);
    if (testRootName != null)
        testName = testRootName + testName;
    if (querySyntax != null) {
        if (!querySyntax.equals(Syntax.syntaxARQ) && !querySyntax.equals(Syntax.syntaxSPARQL))
            throw new QueryTestException("Unknown syntax: " + querySyntax);
    }
    TestItem testItem = TestItem.create(entry, TestManifestX.TestQuery);
    TestCase test = null;
    if (testItem.getTestType().equals(TestManifestX.TestQuery))
        test = new QueryTestSDB(storeDesc, testName, results, testItem);
    if (testItem.getTestType().equals(TestManifestX.TestSurpressed))
        test = new SurpressedTest(testName, results, testItem);
    if (test == null)
        System.err.println("Unrecognized test type: " + testItem.getTestType());
    Resource action2 = testItem.getAction();
    if (action2.hasProperty(TestManifestX.option))
        System.out.println("OPTION");
    return test;
}
Also used : TestCase(junit.framework.TestCase) Resource(org.apache.jena.rdf.model.Resource) SurpressedTest(org.apache.jena.sparql.junit.SurpressedTest) Syntax(org.apache.jena.query.Syntax) TestQueryUtils.getQuerySyntax(org.apache.jena.sparql.junit.TestQueryUtils.getQuerySyntax) QueryTestException(org.apache.jena.sparql.junit.QueryTestException) TestItem(org.apache.jena.sparql.junit.TestItem)

Aggregations

TestCase (junit.framework.TestCase)129 TestSuite (junit.framework.TestSuite)36 Test (junit.framework.Test)22 TestListener (junit.framework.TestListener)17 TestResult (junit.framework.TestResult)13 ArrayList (java.util.ArrayList)12 InvocationTargetException (java.lang.reflect.InvocationTargetException)10 AssertionFailedError (junit.framework.AssertionFailedError)10 Constructor (java.lang.reflect.Constructor)8 Enumeration (java.util.Enumeration)6 Context (android.content.Context)5 PackageManager (android.content.pm.PackageManager)5 ResolveInfo (android.content.pm.ResolveInfo)5 Bundle (android.os.Bundle)5 HandlerThread (android.os.HandlerThread)5 ShellUiAutomatorBridge (com.android.uiautomator.core.ShellUiAutomatorBridge)5 Tracer (com.android.uiautomator.core.Tracer)5 UiAutomationShellWrapper (com.android.uiautomator.core.UiAutomationShellWrapper)5 Field (java.lang.reflect.Field)5 HashSet (java.util.HashSet)5