Search in sources :

Example 1 with TestSuite

use of org.ballerinalang.testerina.core.entity.TestSuite in project ballerina by ballerina-lang.

the class BTestRunner method execute.

/**
 * Run all tests.
 */
private void execute() {
    Map<String, TestSuite> testSuites = TesterinaRegistry.getInstance().getTestSuites();
    if (testSuites.isEmpty()) {
        throw new BallerinaException("No test functions found in the provided ballerina files.");
    }
    AtomicBoolean shouldSkip = new AtomicBoolean();
    testSuites.forEach((packageName, suite) -> {
        outStream.println("---------------------------------------------------------------------------");
        outStream.println("Running Tests of Package: " + packageName);
        outStream.println("---------------------------------------------------------------------------");
        TestAnnotationProcessor.injectMocks(suite);
        tReport.addPackageReport(packageName);
        if (suite.getInitFunction() != null) {
            suite.getInitFunction().invoke();
        }
        suite.getBeforeSuiteFunctions().forEach(test -> {
            String errorMsg;
            try {
                test.invoke();
            } catch (BallerinaException e) {
                shouldSkip.set(true);
                errorMsg = String.format("Failed to execute before test suite function [%s] of test suite " + "package [%s]. Cause: %s", test.getName(), packageName, e.getMessage());
                errStream.println(errorMsg);
            }
        });
        suite.getTests().forEach(test -> {
            if (!shouldSkip.get()) {
                // run the beforeEach tests
                suite.getBeforeEachFunctions().forEach(beforeEachTest -> {
                    String errorMsg;
                    try {
                        beforeEachTest.invoke();
                    } catch (BallerinaException e) {
                        shouldSkip.set(true);
                        errorMsg = String.format("Failed to execute before each test function [%s] for the " + "test [%s] of test suite package [%s]. Cause: %s", beforeEachTest.getName(), test.getTestFunction().getName(), packageName, e.getMessage());
                        errStream.println(errorMsg);
                    }
                });
            }
            if (!shouldSkip.get()) {
                // run before tests
                String errorMsg;
                try {
                    if (test.getBeforeTestFunctionObj() != null) {
                        test.getBeforeTestFunctionObj().invoke();
                    }
                } catch (BallerinaException e) {
                    shouldSkip.set(true);
                    errorMsg = String.format("Failed to execute before test function" + " [%s] for the test " + "[%s] of test suite package [%s]. Cause: %s", test.getBeforeTestFunctionObj().getName(), test.getTestFunction().getName(), packageName, e.getMessage());
                    errStream.println(errorMsg);
                }
            }
            // run the test
            TesterinaResult functionResult = null;
            try {
                if (!shouldSkip.get()) {
                    BValue[] valueSets = null;
                    if (test.getDataProviderFunction() != null) {
                        valueSets = test.getDataProviderFunction().invoke();
                    }
                    if (valueSets == null) {
                        test.getTestFunction().invoke();
                        // report the test result
                        functionResult = new TesterinaResult(test.getTestFunction().getName(), true, shouldSkip.get(), null);
                        tReport.addFunctionResult(packageName, functionResult);
                    } else {
                        List<BValue[]> argList = extractArguments(valueSets);
                        argList.forEach(arg -> {
                            test.getTestFunction().invoke(arg);
                            TesterinaResult result = new TesterinaResult(test.getTestFunction().getName(), true, shouldSkip.get(), null);
                            tReport.addFunctionResult(packageName, result);
                        });
                    }
                } else {
                    // report the test result
                    functionResult = new TesterinaResult(test.getTestFunction().getName(), false, shouldSkip.get(), null);
                    tReport.addFunctionResult(packageName, functionResult);
                }
            } catch (Exception e) {
                String errorMsg = String.format("Failed to execute the test function [%s] of test suite package " + "[%s]. Cause: %s", test.getTestFunction().getName(), packageName, e.getMessage());
                errStream.println(errorMsg);
                // report the test result
                functionResult = new TesterinaResult(test.getTestFunction().getName(), false, shouldSkip.get(), errorMsg);
                tReport.addFunctionResult(packageName, functionResult);
                return;
            }
            // run the after tests
            String error;
            try {
                if (test.getAfterTestFunctionObj() != null) {
                    test.getAfterTestFunctionObj().invoke();
                }
            } catch (BallerinaException e) {
                error = String.format("Failed to execute after test function" + " [%s] for the test [%s] of test " + "suite package [%s]. Cause: %s", test.getAfterTestFunctionObj().getName(), test.getTestFunction().getName(), packageName, e.getMessage());
                errStream.println(error);
            }
            // run the afterEach tests
            suite.getAfterEachFunctions().forEach(afterEachTest -> {
                String errorMsg2;
                try {
                    afterEachTest.invoke();
                } catch (BallerinaException e) {
                    errorMsg2 = String.format("Failed to execute after each test function" + " [%s] for the test " + "[%s] of test suite package [%s]. Cause: %s", afterEachTest.getName(), test.getTestFunction().getName(), packageName, e.getMessage());
                    errStream.println(errorMsg2);
                }
            });
        });
        TestAnnotationProcessor.resetMocks(suite);
        // Run After suite functions
        suite.getAfterSuiteFunctions().forEach(func -> {
            String errorMsg;
            try {
                func.invoke();
            } catch (BallerinaException e) {
                errorMsg = String.format("Failed to execute after test suite function [%s] of test suite " + "package [%s]. Cause: %s", func.getName(), packageName, e.getMessage());
                errStream.println(errorMsg);
            }
        });
        // print package test results
        tReport.printTestSuiteSummary(packageName);
    });
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TesterinaResult(org.ballerinalang.testerina.core.entity.TesterinaResult) TestSuite(org.ballerinalang.testerina.core.entity.TestSuite) BValue(org.ballerinalang.model.values.BValue) BallerinaException(org.ballerinalang.util.exceptions.BallerinaException) BallerinaException(org.ballerinalang.util.exceptions.BallerinaException)

Example 2 with TestSuite

use of org.ballerinalang.testerina.core.entity.TestSuite in project ballerina by ballerina-lang.

the class TestAnnotationProcessor method resolveFunctions.

/**
 * Resolve function names to {@link TesterinaFunction}s.
 *
 * @param suite {@link TestSuite} whose functions to be resolved.
 */
private static void resolveFunctions(TestSuite suite) {
    List<TesterinaFunction> functions = suite.getTestUtilityFunctions();
    List<String> functionNames = functions.stream().map(testerinaFunction -> testerinaFunction.getName()).collect(Collectors.toList());
    for (Test test : suite.getTests()) {
        if (test.getTestName() != null && functionNames.contains(test.getTestName())) {
            test.setTestFunction(functions.stream().filter(e -> e.getName().equals(test.getTestName())).findFirst().get());
        }
        if (test.getBeforeTestFunction() != null && functionNames.contains(test.getBeforeTestFunction())) {
            test.setBeforeTestFunctionObj(functions.stream().filter(e -> e.getName().equals(test.getBeforeTestFunction())).findFirst().get());
        }
        if (test.getAfterTestFunction() != null && functionNames.contains(test.getAfterTestFunction())) {
            test.setAfterTestFunctionObj(functions.stream().filter(e -> e.getName().equals(test.getAfterTestFunction())).findFirst().get());
        }
        if (test.getDataProvider() != null && functionNames.contains(test.getDataProvider())) {
            String dataProvider = test.getDataProvider();
            test.setDataProviderFunction(functions.stream().filter(e -> e.getName().equals(test.getDataProvider())).findFirst().map(func -> {
                // TODO these validations are not working properly with the latest refactoring
                if (func.getbFunction().getRetParamTypes().length == 1) {
                    BType bType = func.getbFunction().getRetParamTypes()[0];
                    if (bType.getTag() == TypeTags.ARRAY_TAG) {
                        BArrayType bArrayType = (BArrayType) bType;
                        if (bArrayType.getElementType().getTag() != TypeTags.ARRAY_TAG) {
                            String message = String.format("Data provider function [%s] should return an array of" + " arrays.", dataProvider);
                            throw new BallerinaException(message);
                        }
                    } else {
                        String message = String.format("Data provider function [%s] should return an array of " + "arrays.", dataProvider);
                        throw new BallerinaException(message);
                    }
                } else {
                    String message = String.format("Data provider function [%s] should have only one return type" + ".", dataProvider);
                    throw new BallerinaException(message);
                }
                return func;
            }).get());
            if (test.getDataProviderFunction() == null) {
                String message = String.format("Data provider function [%s] cannot be found.", dataProvider);
                throw new BallerinaException(message);
            }
        }
        for (String dependsOnFn : test.getDependsOnTestFunctions()) {
            // TODO handle missing func case
            test.addDependsOnTestFunction(functions.stream().filter(e -> e.getName().equals(dependsOnFn)).findFirst().get());
        }
    }
    // resolve mock functions
    suite.getMockFunctionNamesMap().forEach((id, functionName) -> {
        TesterinaFunction function = suite.getTestUtilityFunctions().stream().filter(e -> e.getName().equals(functionName)).findFirst().get();
        suite.addMockFunctionObj(id, function);
    });
    suite.getBeforeSuiteFunctionNames().forEach(functionName -> {
        TesterinaFunction function = suite.getTestUtilityFunctions().stream().filter(e -> e.getName().equals(functionName)).findFirst().get();
        suite.addBeforeSuiteFunctionObj(function);
    });
    suite.getAfterSuiteFunctionNames().forEach(functionName -> {
        TesterinaFunction function = suite.getTestUtilityFunctions().stream().filter(e -> e.getName().equals(functionName)).findFirst().get();
        suite.addAfterSuiteFunctionObj(function);
    });
    suite.getBeforeEachFunctionNames().forEach(functionName -> {
        TesterinaFunction function = suite.getTestUtilityFunctions().stream().filter(e -> e.getName().equals(functionName)).findFirst().get();
        suite.addBeforeEachFunctionObj(function);
    });
    suite.getAfterEachFunctionNames().forEach(functionName -> {
        TesterinaFunction function = suite.getTestUtilityFunctions().stream().filter(e -> e.getName().equals(functionName)).findFirst().get();
        suite.addAfterEachFunctionObj(function);
    });
}
Also used : Arrays(java.util.Arrays) PackageNode(org.ballerinalang.model.tree.PackageNode) BType(org.ballerinalang.model.types.BType) ProgramFile(org.ballerinalang.util.codegen.ProgramFile) TestSuite(org.ballerinalang.testerina.core.entity.TestSuite) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BallerinaException(org.ballerinalang.util.exceptions.BallerinaException) BLangRecordLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral) DiagnosticLog(org.ballerinalang.util.diagnostic.DiagnosticLog) PackageInfo(org.ballerinalang.util.codegen.PackageInfo) ArrayList(java.util.ArrayList) SupportedAnnotationPackages(org.ballerinalang.compiler.plugins.SupportedAnnotationPackages) AbstractCompilerPlugin(org.ballerinalang.compiler.plugins.AbstractCompilerPlugin) Vector(java.util.Vector) Map(java.util.Map) TesterinaFunction(org.ballerinalang.testerina.core.entity.TesterinaFunction) BLangArrayLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangArrayLiteral) LinkedList(java.util.LinkedList) BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) FunctionNode(org.ballerinalang.model.tree.FunctionNode) TypeTags(org.ballerinalang.model.types.TypeTags) Collectors(java.util.stream.Collectors) AnnotationAttachmentNode(org.ballerinalang.model.tree.AnnotationAttachmentNode) BArrayType(org.ballerinalang.model.types.BArrayType) List(java.util.List) Instruction(org.ballerinalang.util.codegen.Instruction) Test(org.ballerinalang.testerina.core.entity.Test) Queue(java.util.Queue) FunctionInfo(org.ballerinalang.util.codegen.FunctionInfo) BArrayType(org.ballerinalang.model.types.BArrayType) Test(org.ballerinalang.testerina.core.entity.Test) BType(org.ballerinalang.model.types.BType) BallerinaException(org.ballerinalang.util.exceptions.BallerinaException) TesterinaFunction(org.ballerinalang.testerina.core.entity.TesterinaFunction)

Aggregations

AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 TestSuite (org.ballerinalang.testerina.core.entity.TestSuite)2 BallerinaException (org.ballerinalang.util.exceptions.BallerinaException)2 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Map (java.util.Map)1 Queue (java.util.Queue)1 Vector (java.util.Vector)1 Collectors (java.util.stream.Collectors)1 AbstractCompilerPlugin (org.ballerinalang.compiler.plugins.AbstractCompilerPlugin)1 SupportedAnnotationPackages (org.ballerinalang.compiler.plugins.SupportedAnnotationPackages)1 AnnotationAttachmentNode (org.ballerinalang.model.tree.AnnotationAttachmentNode)1 FunctionNode (org.ballerinalang.model.tree.FunctionNode)1 PackageNode (org.ballerinalang.model.tree.PackageNode)1 BArrayType (org.ballerinalang.model.types.BArrayType)1 BType (org.ballerinalang.model.types.BType)1 TypeTags (org.ballerinalang.model.types.TypeTags)1 BValue (org.ballerinalang.model.values.BValue)1