diff --git a/ballerina/build.gradle b/ballerina/build.gradle index ef2d2120a7..a3529a2137 100644 --- a/ballerina/build.gradle +++ b/ballerina/build.gradle @@ -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 = "" @@ -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}" @@ -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}" diff --git a/stdlib-integration-tests/index.json b/stdlib-integration-tests/index.json index 1086cca568..57081e7d37 100644 --- a/stdlib-integration-tests/index.json +++ b/stdlib-integration-tests/index.json @@ -63,5 +63,10 @@ "name": "WebSocket standard library", "path": "websocket", "enableTest": true + }, + { + "name": "Transaction standard library", + "path": "transaction", + "enableTest": true } ] diff --git a/stdlib-integration-tests/transaction/tests/xa_transactions_test.bal b/stdlib-integration-tests/transaction/tests/xa_transactions_test.bal index e9bba15c9d..f7b83f1b80 100644 --- a/stdlib-integration-tests/transaction/tests/xa_transactions_test.bal +++ b/stdlib-integration-tests/transaction/tests/xa_transactions_test.bal @@ -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 () { @@ -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); -}