From 22a2a133f02cb1472157f2fd43ef5a4381846565 Mon Sep 17 00:00:00 2001 From: warunalakshitha Date: Tue, 26 Mar 2024 17:54:54 +0530 Subject: [PATCH] Fix runtime api test coverage issue --- .../test/runtime/api/RuntimeAPITest.java | 79 +++++++++++-------- 1 file changed, 46 insertions(+), 33 deletions(-) diff --git a/tests/jballerina-integration-test/src/test/java/org/ballerinalang/test/runtime/api/RuntimeAPITest.java b/tests/jballerina-integration-test/src/test/java/org/ballerinalang/test/runtime/api/RuntimeAPITest.java index ab3f12fe4c4c..2df4b4e17c25 100644 --- a/tests/jballerina-integration-test/src/test/java/org/ballerinalang/test/runtime/api/RuntimeAPITest.java +++ b/tests/jballerina-integration-test/src/test/java/org/ballerinalang/test/runtime/api/RuntimeAPITest.java @@ -16,6 +16,8 @@ package org.ballerinalang.test.runtime.api; +import io.ballerina.projects.JarLibrary; +import io.ballerina.projects.JarResolver; import org.ballerinalang.test.BaseTest; import org.ballerinalang.test.context.BMainInstance; import org.ballerinalang.test.context.BallerinaTestException; @@ -26,6 +28,7 @@ import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; +import java.io.File; import java.io.IOException; import java.nio.file.Path; import java.nio.file.Paths; @@ -33,6 +36,10 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.StringJoiner; + +import static io.ballerina.projects.util.ProjectConstants.USER_DIR; +import static io.ballerina.runtime.api.constants.RuntimeConstants.BALLERINA_HOME; /** * Test class to test the functionality of Ballerina runtime APIs for invoking functions. @@ -45,7 +52,7 @@ public class RuntimeAPITest extends BaseTest { .toAbsolutePath().toString(); private static final Path javaSrcLocation = Paths.get("src", "test", "java", "org", "ballerinalang", "test", "runtime", "api").toAbsolutePath(); - private static final String JAVA_OPTS = "JAVA_OPTS"; + private static final Path TARGET_OUTPUT_PATH = Paths.get(System.getProperty(USER_DIR)); private BMainInstance bMainInstance; @BeforeClass @@ -57,29 +64,26 @@ public void setup() throws BallerinaTestException { } @Test - public void testRuntimeAPIsForBalFunctionInvocation() throws BallerinaTestException { + public void testRuntimeAPIsForBalFunctionInvocation() throws BallerinaTestException, InterruptedException { Path jarPath = Paths.get(testFileLocation, "function_invocation", "target", "bin", "function_invocation.jar").toAbsolutePath(); compileJavaSource(jarPath, "RuntimeAPICall.java", "targetDir"); unzipJarFile(jarPath, "targetDir"); createExecutableJar("targetDir", "org.ballerinalang.test.runtime.api.RuntimeAPICall"); - Map envProperties = new HashMap<>(); - bMainInstance.addJavaAgents(envProperties); // Run the executable jar and assert the output Path execJarPath = Paths.get(javaSrcLocation.toString(), "targetDir", "test-exec.jar").toAbsolutePath(); List runCmdSet = new ArrayList<>(); runCmdSet.add("java"); - if (envProperties.containsKey(JAVA_OPTS)) { - runCmdSet.add(envProperties.get(JAVA_OPTS).trim()); - } - runCmdSet.add("-jar"); - runCmdSet.add(execJarPath.toString()); - ProcessBuilder runProcessBuilder = new ProcessBuilder(runCmdSet); - Map env = runProcessBuilder.environment(); - env.putAll(envProperties); + runCmdSet.add(getAgentArgs()); + runCmdSet.add("-cp"); + runCmdSet.add(getAllClassPaths(execJarPath)); + runCmdSet.add("org.ballerinalang.test.runtime.api.RuntimeAPICall"); + ProcessBuilder pb = new ProcessBuilder(runCmdSet); + bMainInstance.addJavaAgents(pb.environment()); + pb.redirectErrorStream(true); try { - Process runProcess = runProcessBuilder.start(); + Process runProcess = pb.start(); ServerLogReader serverInfoLogReader = new ServerLogReader("inputStream", runProcess.getInputStream()); List leechers = new ArrayList<>(); leechers.add(new LogLeecher("12")); @@ -103,24 +107,19 @@ public void testBalFunctionInvocationAPINegative() throws BallerinaTestException "function_invocation.jar").toAbsolutePath(); compileJavaSource(jarPath, "RuntimeAPICallNegative.java", "target-dir-negative"); unzipJarFile(jarPath, "target-dir-negative"); - createExecutableJar("target-dir-negative", - "org.ballerinalang.test.runtime.api.RuntimeAPICallNegative"); - Map envProperties = new HashMap<>(); - bMainInstance.addJavaAgents(envProperties); + createExecutableJar("target-dir-negative", "org.ballerinalang.test.runtime.api.RuntimeAPICallNegative"); // Run the executable jar and assert the output Path execJarPath = Paths.get(javaSrcLocation.toString(), "target-dir-negative", "test-exec.jar").toAbsolutePath(); List runCmdSet = new ArrayList<>(); runCmdSet.add("java"); - if (envProperties.containsKey(JAVA_OPTS)) { - runCmdSet.add(envProperties.get(JAVA_OPTS).trim()); - } - runCmdSet.add("-jar"); - runCmdSet.add(execJarPath.toString()); + runCmdSet.add(getAgentArgs()); + runCmdSet.add("-cp"); + runCmdSet.add(getAllClassPaths(execJarPath)); + runCmdSet.add("org.ballerinalang.test.runtime.api.RuntimeAPICallNegative"); ProcessBuilder runProcessBuilder = new ProcessBuilder(runCmdSet); - Map env = runProcessBuilder.environment(); - env.putAll(envProperties); + bMainInstance.addJavaAgents(runProcessBuilder.environment()); runProcessBuilder.redirectErrorStream(true); try { Process runProcess = runProcessBuilder.start(); @@ -147,22 +146,18 @@ public void testModuleStartCallNegative() throws BallerinaTestException { unzipJarFile(jarPath, "start-call-negative"); createExecutableJar("start-call-negative", "org.ballerinalang.test.runtime.api.ModuleStartCallNegative"); - Map envProperties = new HashMap<>(); - bMainInstance.addJavaAgents(envProperties); // Run the executable jar and assert the output Path execJarPath = Paths.get(javaSrcLocation.toString(), "start-call-negative", "test-exec.jar").toAbsolutePath(); List runCmdSet = new ArrayList<>(); runCmdSet.add("java"); - if (envProperties.containsKey(JAVA_OPTS)) { - runCmdSet.add(envProperties.get(JAVA_OPTS).trim()); - } - runCmdSet.add("-jar"); - runCmdSet.add(execJarPath.toString()); + runCmdSet.add(getAgentArgs()); + runCmdSet.add("-cp"); + runCmdSet.add(getAllClassPaths(execJarPath)); + runCmdSet.add("org.ballerinalang.test.runtime.api.ModuleStartCallNegative"); ProcessBuilder runProcessBuilder = new ProcessBuilder(runCmdSet); - Map env = runProcessBuilder.environment(); - env.putAll(envProperties); + bMainInstance.addJavaAgents(runProcessBuilder.environment()); runProcessBuilder.redirectErrorStream(true); try { Process runProcess = runProcessBuilder.start(); @@ -211,6 +206,7 @@ private static void unzipJarFile(Path jarPath, String targetDir) throws Ballerin List unzipProcessCmdSet = new ArrayList<>(); unzipProcessCmdSet.add("unzip"); unzipProcessCmdSet.add("-qq"); + unzipProcessCmdSet.add("-o"); unzipProcessCmdSet.add(jarPath.toString()); unzipProcessCmdSet.add("-d"); unzipProcessCmdSet.add(targetDir); @@ -246,4 +242,21 @@ private static void addToServerInfoLogReader(ServerLogReader serverInfoLogReader serverInfoLogReader.addLeecher(leecher); } } + + private String getAgentArgs() { + return "-javaagent:" + Paths.get(bMainInstance.getBalServerHome(), "bre", "lib", + "jacocoagent.jar") + "=destfile=" + TARGET_OUTPUT_PATH.resolve("build").resolve("jacoco") + .resolve("test.exec") + " "; + } + + private String getAgentJar() { + return Paths.get(bMainInstance.getBalServerHome(), "bre", "lib", "jacocoagent.jar").toString(); + } + + private String getAllClassPaths(Path targetJAr) { + StringJoiner cp = new StringJoiner(File.pathSeparator); + cp.add(targetJAr.toString()); + cp.add(getAgentJar()); + return cp.toString(); + } }