Skip to content

Commit

Permalink
Merge pull request #5270 from dsplayerX/fix_xa_trans_test
Browse files Browse the repository at this point in the history
Fix xa transaction integration test
  • Loading branch information
KavinduZoysa committed Apr 10, 2024
2 parents 43a1d9e + 136ca1b commit bf82539
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 20 deletions.
15 changes: 15 additions & 0 deletions ballerina/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -1082,6 +1082,15 @@ task testExamples() {
}
}

def addH2Dependency = { filePath ->
// Add the H2 database dependency to Ballerina.toml file
def tomlFile = new File(filePath)
tomlFile.append("\n\n[[platform.java17.dependency]]")
tomlFile.append("\nartifactId = \"h2\"")
tomlFile.append("\nversion = \"2.2.224\"")
tomlFile.append("\ngroupId = \"com.h2database\"")
}

def buildAndTestStandardLibs = { distPath, stdlibTest, isStageTest, testMinorVersionDifference ->
def exitVal
def additionalBuildParams = ""
Expand All @@ -1101,6 +1110,9 @@ def buildAndTestStandardLibs = { distPath, stdlibTest, isStageTest, testMinorVer
workingDir "${distPath}/${stdlibTest}"
commandLine 'cmd', '/c', "${distPath}/bin/bal.bat init test"
}
if (stdlibTest == 'transaction') {
addH2Dependency("${distPath}/${stdlibTest}/Ballerina.toml")
}
} else {
exec {
workingDir "${distPath}/${stdlibTest}"
Expand All @@ -1124,6 +1136,9 @@ def buildAndTestStandardLibs = { distPath, stdlibTest, isStageTest, testMinorVer
workingDir "${distPath}/${stdlibTest}"
commandLine 'sh', '-c', "${distPath}/bin/bal init test"
}
if (stdlibTest == 'transaction') {
addH2Dependency("${distPath}/${stdlibTest}/Ballerina.toml")
}
} else {
exec {
workingDir "${distPath}/${stdlibTest}"
Expand Down
5 changes: 5 additions & 0 deletions stdlib-integration-tests/index.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,10 @@
"name": "WebSocket standard library",
"path": "websocket",
"enableTest": true
},
{
"name": "Transaction standard library",
"path": "transaction",
"enableTest": true
}
]
47 changes: 27 additions & 20 deletions stdlib-integration-tests/transaction/tests/xa_transactions_test.bal
Original file line number Diff line number Diff line change
@@ -1,46 +1,47 @@
// Copyright (c) 2020, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
// Copyright (c) 2020-2024, WSO2 LLC. (https://www.wso2.com).
//
// WSO2 Inc. licenses this file to you under the Apache License,
// WSO2 LLC. licenses this file to you under the Apache License,
// Version 2.0 (the "License"); you may not use this file except
// in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

import ballerina/config;
import ballerina/jdbc;
import ballerina/sql;
import ballerina/test;

import ballerinax/java.jdbc;

string xaDatasourceName = "org.h2.jdbcx.JdbcDataSource";

@test:Config {
before: addConfigs
}
function testXATransactions() returns error? {
string str = "";

jdbc:Client dbClient1 = check new (url = "jdbc:h2:file:./xa-transactions/testdb1",
user = "test", password = "test", options = {datasourceName: xaDatasourceName});
user = "test", password = "test", options = {datasourceName: xaDatasourceName}
);

jdbc:Client dbClient2 = check new (url = "jdbc:h2:file:./xa-transactions/testdb2",
user = "test", password = "test", options ={datasourceName: xaDatasourceName});
user = "test", password = "test", options = {datasourceName: xaDatasourceName}
);

_ = check dbClient1->execute("CREATE TABLE IF NOT EXISTS EMPLOYEE " +
"(ID INT, NAME VARCHAR(30))");
_ = check dbClient2->execute("CREATE TABLE IF NOT EXISTS SALARY " +
"(ID INT, VALUE FLOAT)");
_ = check dbClient1->execute(`CREATE TABLE IF NOT EXISTS EMPLOYEE (ID INT, NAME VARCHAR(30))`);
_ = check dbClient2->execute(`CREATE TABLE IF NOT EXISTS SALARY (ID INT, "VALUE" FLOAT)`);

transaction {
str += "transaction started";

var e1 = check dbClient1->execute("INSERT INTO EMPLOYEE(NAME) VALUES ('Anne')");
var e2 = check dbClient2->execute("INSERT INTO SALARY VALUES (1, 25000.00)");
_ = check dbClient1->execute(`INSERT INTO EMPLOYEE VALUES (1, 'John')`);
_ = check dbClient2->execute(`INSERT INTO SALARY VALUES (1, 20000.00)`);

var commitResult = commit;
if commitResult is () {
Expand All @@ -51,13 +52,19 @@ function testXATransactions() returns error? {
str += " -> transaction ended.";
}

test:assertEquals("transaction started -> transaction committed -> transaction ended.", str);
test:assertEquals(str, "transaction started -> transaction committed -> transaction ended.");

// Verify that the data was inserted successfully to both databases
sql:ExecutionResult employeeResult = check dbClient1->queryRow(`SELECT * FROM EMPLOYEE WHERE ID = 1`);
sql:ExecutionResult salaryResult = check dbClient2->queryRow(`SELECT * FROM SALARY WHERE ID = 1`);
json employeeResultJson = employeeResult.toJson();
json salaryResultJson = salaryResult.toJson();

test:assertEquals(employeeResultJson.ID, 1);
test:assertEquals(employeeResultJson.NAME, "John");
test:assertEquals(salaryResultJson.ID, 1);
test:assertEquals(salaryResultJson.VALUE, 20000.00);

checkpanic dbClient1.close();
checkpanic dbClient2.close();
}

function addConfigs() {
config:setConfig("b7a.transaction.log.base", "trxLogDir");
config:setConfig("b7a.transaction.manager.enabled", true);
}

0 comments on commit bf82539

Please sign in to comment.