Search in sources :

Example 81 with BString

use of org.ballerinalang.model.values.BString in project ballerina by ballerina-lang.

the class StringTest method testReplaceAll.

@Test
public void testReplaceAll() {
    BValue[] args = { new BString("abc is not abc as abc anymore"), new BString("abc"), new BString("xyz") };
    BValue[] returns = BRunUtil.invoke(result, "replaceAll", args);
    Assert.assertTrue(returns[0] instanceof BString);
    Assert.assertEquals(returns[0].stringValue(), "xyz is not xyz as xyz anymore");
}
Also used : BValue(org.ballerinalang.model.values.BValue) BString(org.ballerinalang.model.values.BString) Test(org.testng.annotations.Test)

Example 82 with BString

use of org.ballerinalang.model.values.BString in project ballerina by ballerina-lang.

the class TimeTest method testParseToRFC1123Time.

@Test(description = "Test parsing a given time string to RFC 1123 format")
public void testParseToRFC1123Time() {
    BValue[] args = { new BString("Wed, 28 Mar 2018 11:56:23 +0530") };
    BValue[] returns = BRunUtil.invoke(result, "testParseRFC1123Time", args);
    Assert.assertEquals(((BInteger) returns[0]).intValue(), 1522218383000L);
    Assert.assertEquals((returns[1]).stringValue(), "+05:30");
    Assert.assertEquals(((BInteger) returns[2]).intValue(), 19800);
    args = new BValue[] { new BString("Tue, 27 Mar 2018 10:00:24 GMT") };
    returns = BRunUtil.invoke(result, "testParseRFC1123Time", args);
    Assert.assertEquals(((BInteger) returns[0]).intValue(), 1522144824000L);
    Assert.assertEquals((returns[1]).stringValue(), "Z");
    Assert.assertEquals(((BInteger) returns[2]).intValue(), 0);
}
Also used : BValue(org.ballerinalang.model.values.BValue) BString(org.ballerinalang.model.values.BString) Test(org.testng.annotations.Test)

Example 83 with BString

use of org.ballerinalang.model.values.BString 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 84 with BString

use of org.ballerinalang.model.values.BString 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 85 with BString

use of org.ballerinalang.model.values.BString in project ballerina by ballerina-lang.

the class StructTest method testBasicStruct.

@Test(description = "Test Basic struct operations")
public void testBasicStruct() {
    BValue[] returns = BRunUtil.invoke(compileResult, "testCreateStruct");
    Assert.assertTrue(returns[0] instanceof BString);
    Assert.assertEquals(returns[0].stringValue(), "Jack");
    Assert.assertTrue(returns[1] instanceof BMap);
    BMap<String, ?> adrsMap = ((BMap) returns[1]);
    Assert.assertEquals(adrsMap.get("country"), new BString("USA"));
    Assert.assertEquals(adrsMap.get("state"), new BString("CA"));
    Assert.assertTrue(returns[2] instanceof BInteger);
    Assert.assertEquals(((BInteger) returns[2]).intValue(), 25);
}
Also used : BMap(org.ballerinalang.model.values.BMap) BValue(org.ballerinalang.model.values.BValue) BString(org.ballerinalang.model.values.BString) BInteger(org.ballerinalang.model.values.BInteger) BString(org.ballerinalang.model.values.BString) Test(org.testng.annotations.Test)

Aggregations

BString (org.ballerinalang.model.values.BString)418 BValue (org.ballerinalang.model.values.BValue)339 Test (org.testng.annotations.Test)321 BInteger (org.ballerinalang.model.values.BInteger)77 BStruct (org.ballerinalang.model.values.BStruct)76 BMap (org.ballerinalang.model.values.BMap)37 BBoolean (org.ballerinalang.model.values.BBoolean)36 BFloat (org.ballerinalang.model.values.BFloat)34 BJSON (org.ballerinalang.model.values.BJSON)28 BallerinaException (org.ballerinalang.util.exceptions.BallerinaException)21 BRefValueArray (org.ballerinalang.model.values.BRefValueArray)18 DefaultHttpHeaders (io.netty.handler.codec.http.DefaultHttpHeaders)11 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)11 CompileResult (org.ballerinalang.launcher.util.CompileResult)11 BBlob (org.ballerinalang.model.values.BBlob)11 BStringArray (org.ballerinalang.model.values.BStringArray)11 BType (org.ballerinalang.model.types.BType)10 BIntArray (org.ballerinalang.model.values.BIntArray)9 BRefType (org.ballerinalang.model.values.BRefType)9 UnsupportedEncodingException (java.io.UnsupportedEncodingException)7