Search in sources :

Example 21 with CompileResult

use of org.ballerinalang.launcher.util.CompileResult in project ballerina by ballerina-lang.

the class SQLTest method testSelectWithUntaintedQueryProducingTaintedReturnNegative.

@Test
public void testSelectWithUntaintedQueryProducingTaintedReturnNegative() {
    CompileResult result = BCompileUtil.compile("test-src/taintchecking/connectors/sql-select-untainted-query-tainted-return-negative.bal");
    Assert.assertTrue(result.getDiagnostics().length == 1);
    BAssertUtil.validateError(result, 0, "tainted value passed to sensitive parameter 'anyValue'", 29, 50);
}
Also used : CompileResult(org.ballerinalang.launcher.util.CompileResult) Test(org.testng.annotations.Test)

Example 22 with CompileResult

use of org.ballerinalang.launcher.util.CompileResult in project ballerina by ballerina-lang.

the class TimerTest method testExecutionWithWorkersAndErrorFn.

@Test(description = "Tests running a timer started within workers  where the onTrigger function generates an error")
public void testExecutionWithWorkersAndErrorFn() {
    CompileResult timerCompileResult = BCompileUtil.compileAndSetup("test-src/task/timer-workers.bal");
    printDiagnostics(timerCompileResult);
    int w1InitialDelay = 500;
    int w1Interval = 1000;
    int w2InitialDelay = 800;
    int w2Interval = 1000;
    String w1ErrMsg = "w1: Timer error";
    String w2ErrMsg = "w2: Timer error";
    BValue[] returns = BRunUtil.invokeStateful(timerCompileResult, "scheduleTimer", new BValue[] { new BInteger(w1InitialDelay), new BInteger(w1Interval), new BInteger(w2InitialDelay), new BInteger(w2Interval), new BString(w1ErrMsg), new BString(w2ErrMsg) });
    String w1TaskId = returns[0].stringValue();
    String w2TaskId = returns[1].stringValue();
    // A non-null task ID should be returned
    assertNotEquals(w1TaskId, "", "Invalid task ID from worker w1");
    // A non-null task ID should be returned
    assertNotEquals(w2TaskId, "", "Invalid task ID from worker w2");
    await().atMost(10, SECONDS).until(() -> {
        BValue[] errors = BRunUtil.invokeStateful(timerCompileResult, "getErrors");
        return errors != null && errors[0] != null && !errors[0].stringValue().isEmpty() && errors[1] != null && !errors[1].stringValue().isEmpty();
    });
    // Now test whether the onError Ballerina function got called
    BValue[] error = BRunUtil.invokeStateful(timerCompileResult, "getErrors");
    assertNotNull(error[0], "Expected error not returned.");
    assertEquals(error[0].stringValue(), w1ErrMsg, "Expected error message not returned.");
    assertEquals(error[1].stringValue(), w2ErrMsg, "Expected error message not returned.");
    // Now let's try stopping the tasks
    BValue[] stopResult = BRunUtil.invokeStateful(timerCompileResult, "stopTasks", new BValue[] { new BString(w1TaskId), new BString(w2TaskId) });
    assertNull(stopResult[0], "Task stopping on worker w1 resulted in an error");
    assertNull(stopResult[1], "Task stopping on worker w2 resulted in an error");
    // One more check to see whether the task really stopped
    BValue[] counts = BRunUtil.invokeStateful(timerCompileResult, "getCounts");
    assertEquals(((BInteger) counts[0]).intValue(), -1, "Count hasn't been reset");
    assertEquals(((BInteger) counts[1]).intValue(), -1, "Count hasn't been reset");
}
Also used : BValue(org.ballerinalang.model.values.BValue) BString(org.ballerinalang.model.values.BString) BInteger(org.ballerinalang.model.values.BInteger) CompileResult(org.ballerinalang.launcher.util.CompileResult) BString(org.ballerinalang.model.values.BString) Test(org.testng.annotations.Test)

Example 23 with CompileResult

use of org.ballerinalang.launcher.util.CompileResult in project ballerina by ballerina-lang.

the class TimerTest method testSimpleExecutionWithWorkers.

@Test(description = "Tests running a timer started within workers")
public void testSimpleExecutionWithWorkers() {
    CompileResult timerCompileResult = BCompileUtil.compileAndSetup("test-src/task/timer-workers.bal");
    printDiagnostics(timerCompileResult);
    int w1InitialDelay = 500;
    int w1Interval = 1000;
    int w2InitialDelay = 800;
    int w2Interval = 2000;
    BValue[] returns = BRunUtil.invokeStateful(timerCompileResult, "scheduleTimer", new BValue[] { new BInteger(w1InitialDelay), new BInteger(w1Interval), new BInteger(w2InitialDelay), new BInteger(w2Interval), new BString(""), new BString("") });
    String w1TaskId = returns[0].stringValue();
    String w2TaskId = returns[1].stringValue();
    // A non-null task ID should be returned
    assertNotEquals(w1TaskId, "", "Invalid task ID from worker w1");
    // A non-null task ID should be returned
    assertNotEquals(w2TaskId, "", "Invalid task ID from worker w2");
    await().atMost(30, SECONDS).until(() -> {
        BValue[] counts = BRunUtil.invokeStateful(timerCompileResult, "getCounts");
        return counts != null && counts[0] != null && counts[1] != null && ((BInteger) counts[0]).intValue() >= 5 && ((BInteger) counts[1]).intValue() >= 5;
    });
    // Now let's try stopping the tasks
    BValue[] stopResult = BRunUtil.invokeStateful(timerCompileResult, "stopTasks", new BValue[] { new BString(w1TaskId), new BString(w2TaskId) });
    assertNull(stopResult[0], "Task stopping on worker w1 resulted in an error");
    assertNull(stopResult[1], "Task stopping on worker w2 resulted in an error");
    // One more check to see whether the task really stopped
    BValue[] counts = BRunUtil.invokeStateful(timerCompileResult, "getCounts");
    assertEquals(((BInteger) counts[0]).intValue(), -1, "Count hasn't been reset");
    assertEquals(((BInteger) counts[1]).intValue(), -1, "Count hasn't been reset");
}
Also used : BValue(org.ballerinalang.model.values.BValue) BString(org.ballerinalang.model.values.BString) BInteger(org.ballerinalang.model.values.BInteger) CompileResult(org.ballerinalang.launcher.util.CompileResult) BString(org.ballerinalang.model.values.BString) Test(org.testng.annotations.Test)

Example 24 with CompileResult

use of org.ballerinalang.launcher.util.CompileResult in project ballerina by ballerina-lang.

the class BAnyTypeInvalidCastError method testInvalidAnyCasting.

@Test
public void testInvalidAnyCasting() {
    CompileResult resultNegative = BCompileUtil.compile("test-src/types/any/any-type-cast-negative.bal");
    Assert.assertEquals(resultNegative.getErrorCount(), 2);
    BAssertUtil.validateError(resultNegative, 0, 3, 15);
    BAssertUtil.validateErrorMessageOnly(resultNegative, 0, "incompatible types: expected 'float', found ");
    BAssertUtil.validateErrorMessageOnly(resultNegative, 0, new String[] { "float|error", "error|float" });
    BAssertUtil.validateError(resultNegative, 1, 14, 11);
    BAssertUtil.validateErrorMessageOnly(resultNegative, 1, "incompatible types: expected 'int', found ");
    BAssertUtil.validateErrorMessageOnly(resultNegative, 1, new String[] { "int|error", "error|int" });
// TODO: This needs to have another error, for casting a null value. Add that check when it's fixed.
}
Also used : CompileResult(org.ballerinalang.launcher.util.CompileResult) Test(org.testng.annotations.Test)

Example 25 with CompileResult

use of org.ballerinalang.launcher.util.CompileResult in project ballerina by ballerina-lang.

the class StructTest method testStructLiteralInitFunc.

@Test
public void testStructLiteralInitFunc() {
    CompileResult result = BCompileUtil.compile("test-src/structs/nested-struct-inline-init.bal");
    BValue[] returns = BRunUtil.invoke(result, "testCreateStruct");
    Assert.assertEquals(returns[0].stringValue(), "{name:\"default first name\", fname:\"\", lname:\"Doe\", adrs:{}, age:999, " + "family:{spouse:\"Jane\", noOfChildren:0, children:[\"Alex\", \"Bob\"]}}");
}
Also used : BValue(org.ballerinalang.model.values.BValue) CompileResult(org.ballerinalang.launcher.util.CompileResult) Test(org.testng.annotations.Test)

Aggregations

CompileResult (org.ballerinalang.launcher.util.CompileResult)291 Test (org.testng.annotations.Test)287 BValue (org.ballerinalang.model.values.BValue)60 PackageNode (org.ballerinalang.model.tree.PackageNode)16 BLangDocumentation (org.wso2.ballerinalang.compiler.tree.BLangDocumentation)14 BString (org.ballerinalang.model.values.BString)13 BInteger (org.ballerinalang.model.values.BInteger)12 Path (java.nio.file.Path)5 BLangVariable (org.wso2.ballerinalang.compiler.tree.BLangVariable)5 File (java.io.File)4 GrpcCmd (org.ballerinalang.protobuf.cmd.GrpcCmd)4 BStringArray (org.ballerinalang.model.values.BStringArray)3 BLangDeprecatedNode (org.wso2.ballerinalang.compiler.tree.BLangDeprecatedNode)3 BLangFunction (org.wso2.ballerinalang.compiler.tree.BLangFunction)3 BLangStruct (org.wso2.ballerinalang.compiler.tree.BLangStruct)3 BFloat (org.ballerinalang.model.values.BFloat)2 BIntArray (org.ballerinalang.model.values.BIntArray)2 BMap (org.ballerinalang.model.values.BMap)2 BeforeTest (org.testng.annotations.BeforeTest)2 BLangService (org.wso2.ballerinalang.compiler.tree.BLangService)2