Skip to content

Commit

Permalink
refactor: bumping jacoc to 0.8.12 (#172)
Browse files Browse the repository at this point in the history
* refactor:path name

* refactor: remove debug logs

Signed-off-by: shivamsouravjha <[email protected]>

* refactor: bumping jacoc to 0.8.12

Signed-off-by: shivamsouravjha <[email protected]>

* refactor:  moving jacoco download to url

* refactor: add logs

* refactor:logs

* remove:logs

Signed-off-by: shivamsouravjha <[email protected]>

---------

Signed-off-by: shivamsouravjha <[email protected]>
  • Loading branch information
shivamsouravjha authored Apr 9, 2024
1 parent 4219528 commit bfa899b
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 17 deletions.
21 changes: 20 additions & 1 deletion v2/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,27 @@
<target>9</target>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.0.0</version> <!-- Use the latest version -->
<executions>
<execution>
<phase>process-classes</phase> <!-- Or another appropriate lifecycle phase -->
<goals>
<goal>java</goal>
</goals>
<configuration>
<mainClass>io.keploy.JaCoCoUtil</mainClass>
<arguments>
<argument>0.8.12</argument> <!-- JaCoCo version -->
<argument>${project.basedir}/src/main/resources</argument> <!-- Relative path -->
</arguments>
</configuration>
</execution>
</executions>
</plugin>
</plugins>

</build>

</project>
67 changes: 67 additions & 0 deletions v2/src/main/java/io/keploy/JaCoCoUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package io.keploy;

import java.io.InputStream;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class JaCoCoUtil {

public static void downloadAndExtractJaCoCoBinaries(String version, String resourceDir) throws Exception {
Path cliPath = Paths.get(resourceDir, "jacococli.jar");
Path agentPath = Paths.get(resourceDir, "jacocoagent.jar");
Files.createDirectories(cliPath.getParent());
Files.createDirectories(agentPath.getParent());

if (Files.exists(cliPath) && Files.exists(agentPath)) {
return;
}

String downloadUrl = "https://github.com/jacoco/jacoco/releases/download/v" + version + "/jacoco-" + version + ".zip";

try (InputStream inputStream = new URL(downloadUrl).openStream();
ZipInputStream zipInputStream = new ZipInputStream(inputStream)) {

ZipEntry entry;
while ((entry = zipInputStream.getNextEntry()) != null) {
if (entry.getName().endsWith("jacococli.jar")) {

Files.copy(zipInputStream, cliPath, StandardCopyOption.REPLACE_EXISTING);

} else if (entry.getName().endsWith("jacocoagent.jar")) {

Files.copy(zipInputStream, agentPath, StandardCopyOption.REPLACE_EXISTING);

}

if (Files.exists(cliPath) && Files.exists(agentPath)) {

break; // Both binaries extracted, no need to continue
}
}
}

if (!Files.exists(cliPath) || !Files.exists(agentPath)) {
throw new IllegalStateException("Failed to find JaCoCo binaries in the distribution.");
}

}

public static void main(String[] args) {
if (args.length != 2) {
throw new IllegalArgumentException("Expected two arguments: version and resourceDir");
}
String version = args[0];
String resourceDir = args[1];
try {
downloadAndExtractJaCoCoBinaries(version, resourceDir);
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
}
38 changes: 22 additions & 16 deletions v2/src/main/java/io/keploy/Keploy.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

//FOR CLI CODE COVERAGE REFERENCE: https://dzone.com/articles/code-coverage-report-generator-for-java-projects-a

// Jacococli & JacocoAgent version: 0.8.8
// Jacococli & JacocoAgent version: 0.8.12
public class Keploy {
public static class RunOptions {
private int delay;
Expand Down Expand Up @@ -134,7 +134,7 @@ public class Data {
String[] testSets;
Boolean stopTest;
TestSetStatus testSetStatus;
Boolean runTestSet;
Boolean runTestSet;
StartHooksData startHooks;
Boolean startApp;
Boolean stopHooks;
Expand Down Expand Up @@ -200,6 +200,7 @@ private static String attachJacocoAgent(String cmd) {
+ "=address=localhost,port=36320,destfile=coverage.exec,output=tcpserver";

jacocoAgentPath = tempFile.toAbsolutePath().toString();

return cmd.replaceFirst("java", "java " + agentString);
} catch (IOException e) {
e.printStackTrace();
Expand All @@ -208,12 +209,20 @@ private static String attachJacocoAgent(String cmd) {
}

public static void FindCoverage(String testSet) throws IOException, InterruptedException {
String dest = "target/" + testSet;
String runCmd = "java -jar " + getJacococliPath() + " dump --address localhost --port 36320 --destfile "
+ dest + ".exec";

// Split the runCmd string into command parts
String[] command = runCmd.split(" ");
String dest = "target/" + testSet + ".exec";
String jacocoCliPath = getJacococliPath();
List<String> command = Arrays.asList(
"java",
"-jar",
jacocoCliPath,
"dump",
"--address",
"localhost",
"--port",
"36320",
"--destfile",
dest
);

// Start the command using ProcessBuilder
ProcessBuilder processBuilder = new ProcessBuilder(command);
Expand Down Expand Up @@ -286,9 +295,6 @@ public static String StopUserApplication(String appId) {
return "Error stopping user application: " + e.getMessage();
}

// Additional logic to kill processes and delete JaCoCo files
deleteJacocoFiles();

return null;
}

Expand Down Expand Up @@ -481,12 +487,13 @@ public RunTestSetResult(boolean success, String error) {
}

public static void runTests(String jarPath, RunOptions runOptions) {
String runCmd = "java -jar "+ jarPath;

String runCmd = "java -jar " + jarPath;
if (runOptions.getPort() != 0) {
serverPort = runOptions.getPort();
}
runCmd = attachJacocoAgent(runCmd);
try {
runCmd = attachJacocoAgent(runCmd);
startKeploy(runCmd, runOptions.getDelay(), runOptions.isDebug(), serverPort);
Thread.sleep(5000);
String[] testSets = Keploy.FetchTestSets();
Expand Down Expand Up @@ -540,9 +547,8 @@ public static void runTests(String jarPath, RunOptions runOptions) {
}
// unload the ebpf hooks from the kernel
stopKeploy();

System.out.println("TestSets: " + Arrays.asList(testSets));

// delete jacoco files
deleteJacocoFiles();
} catch (Exception e) {
logger.error("Error occurred while fetching test sets: " + e.getMessage(), e);
}
Expand Down
Binary file removed v2/src/main/resources/jacocoagent.jar
Binary file not shown.
Binary file removed v2/src/main/resources/jacococli.jar
Binary file not shown.

0 comments on commit bfa899b

Please sign in to comment.