From 4b62d0675ea3a0917508d8b887bcbe5ecef28eaa Mon Sep 17 00:00:00 2001 From: dsplayerX Date: Fri, 22 Mar 2024 10:41:18 +0530 Subject: [PATCH 1/5] Enable xa transaction integration test --- stdlib-integration-tests/index.json | 5 +++++ 1 file changed, 5 insertions(+) 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 } ] From 787f09845bfce2559fd6b33c64c70a21102683c7 Mon Sep 17 00:00:00 2001 From: dsplayerX Date: Fri, 22 Mar 2024 14:43:10 +0530 Subject: [PATCH 2/5] Fix failing xa transaction test --- .../tests/xa_transactions_test.bal | 43 +++++++++++-------- 1 file changed, 24 insertions(+), 19 deletions(-) diff --git a/stdlib-integration-tests/transaction/tests/xa_transactions_test.bal b/stdlib-integration-tests/transaction/tests/xa_transactions_test.bal index e9bba15c9d..45ed053b81 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 () { @@ -53,11 +54,15 @@ function testXATransactions() returns error? { test:assertEquals("transaction started -> transaction committed -> transaction ended.", str); + // Verify that the data was inserted successfully on 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`); + + test:assertEquals(1, (check employeeResult.toJson().ID)); + test:assertEquals("John", (check employeeResult.toJson().NAME)); + test:assertEquals(1, (check salaryResult.toJson().ID)); + test:assertEquals(20000.0, (check salaryResult.toJson().VALUE)); + checkpanic dbClient1.close(); checkpanic dbClient2.close(); } - -function addConfigs() { - config:setConfig("b7a.transaction.log.base", "trxLogDir"); - config:setConfig("b7a.transaction.manager.enabled", true); -} From 49e303a8fd10a901398770bf86548f501ad5c13f Mon Sep 17 00:00:00 2001 From: dsplayerX Date: Fri, 22 Mar 2024 14:45:27 +0530 Subject: [PATCH 3/5] Add h2 dependency to build.gradle The transaction integration test requires the use of h2 databases and that dependency needs to be defined in the Ballerina.toml file. This was implemented by appending to the Ballerina.toml in the gradle task buildAndTestStand ardLibs. --- ballerina/build.gradle | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/ballerina/build.gradle b/ballerina/build.gradle index ef2d2120a7..5ae920b129 100644 --- a/ballerina/build.gradle +++ b/ballerina/build.gradle @@ -1101,6 +1101,13 @@ def buildAndTestStandardLibs = { distPath, stdlibTest, isStageTest, testMinorVer workingDir "${distPath}/${stdlibTest}" commandLine 'cmd', '/c', "${distPath}/bin/bal.bat init test" } + if (stdlibTest == 'transaction') { + def tomlFile = new File("${distPath}/${stdlibTest}/Ballerina.toml") + tomlFile.append("\n\n[[platform.java17.dependency]]") + tomlFile.append("\nartifactId = \"h2\"") + tomlFile.append("\nversion = \"2.2.224\"") + tomlFile.append("\ngroupId = \"com.h2database\"") + } } else { exec { workingDir "${distPath}/${stdlibTest}" @@ -1124,6 +1131,13 @@ def buildAndTestStandardLibs = { distPath, stdlibTest, isStageTest, testMinorVer workingDir "${distPath}/${stdlibTest}" commandLine 'sh', '-c', "${distPath}/bin/bal init test" } + if (stdlibTest == 'transaction') { + def tomlFile = new File("${distPath}/${stdlibTest}/Ballerina.toml") + tomlFile.append("\n\n[[platform.java17.dependency]]") + tomlFile.append("\nartifactId = \"h2\"") + tomlFile.append("\nversion = \"2.2.224\"") + tomlFile.append("\ngroupId = \"com.h2database\"") + } } else { exec { workingDir "${distPath}/${stdlibTest}" From 763a9a4969b4e499d90bf382bcc79b7fbd733b5c Mon Sep 17 00:00:00 2001 From: dsplayerX Date: Fri, 22 Mar 2024 15:17:07 +0530 Subject: [PATCH 4/5] Extract add h2 dependency logic Introduces a new function, `addH2Dependency`, to the build.gradle script. This function is responsible for appending the H2 database dependency details to the Ballerina TOML file. --- ballerina/build.gradle | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/ballerina/build.gradle b/ballerina/build.gradle index 5ae920b129..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 = "" @@ -1102,11 +1111,7 @@ def buildAndTestStandardLibs = { distPath, stdlibTest, isStageTest, testMinorVer commandLine 'cmd', '/c', "${distPath}/bin/bal.bat init test" } if (stdlibTest == 'transaction') { - def tomlFile = new File("${distPath}/${stdlibTest}/Ballerina.toml") - tomlFile.append("\n\n[[platform.java17.dependency]]") - tomlFile.append("\nartifactId = \"h2\"") - tomlFile.append("\nversion = \"2.2.224\"") - tomlFile.append("\ngroupId = \"com.h2database\"") + addH2Dependency("${distPath}/${stdlibTest}/Ballerina.toml") } } else { exec { @@ -1132,11 +1137,7 @@ def buildAndTestStandardLibs = { distPath, stdlibTest, isStageTest, testMinorVer commandLine 'sh', '-c', "${distPath}/bin/bal init test" } if (stdlibTest == 'transaction') { - def tomlFile = new File("${distPath}/${stdlibTest}/Ballerina.toml") - tomlFile.append("\n\n[[platform.java17.dependency]]") - tomlFile.append("\nartifactId = \"h2\"") - tomlFile.append("\nversion = \"2.2.224\"") - tomlFile.append("\ngroupId = \"com.h2database\"") + addH2Dependency("${distPath}/${stdlibTest}/Ballerina.toml") } } else { exec { From 136ca1b22dc93f69649b1b976894bef4ee6b7f81 Mon Sep 17 00:00:00 2001 From: dsplayerX Date: Fri, 22 Mar 2024 15:28:07 +0530 Subject: [PATCH 5/5] Address review suggestions --- .../transaction/tests/xa_transactions_test.bal | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/stdlib-integration-tests/transaction/tests/xa_transactions_test.bal b/stdlib-integration-tests/transaction/tests/xa_transactions_test.bal index 45ed053b81..f7b83f1b80 100644 --- a/stdlib-integration-tests/transaction/tests/xa_transactions_test.bal +++ b/stdlib-integration-tests/transaction/tests/xa_transactions_test.bal @@ -52,16 +52,18 @@ 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 on both databases + // 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(1, (check employeeResult.toJson().ID)); - test:assertEquals("John", (check employeeResult.toJson().NAME)); - test:assertEquals(1, (check salaryResult.toJson().ID)); - test:assertEquals(20000.0, (check salaryResult.toJson().VALUE)); + 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();