Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion .github/workflows/java-agent.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ jobs:
verify:
name: JDK ${{ matrix.java-version }}
runs-on: ubuntu-latest
# A hung step would otherwise hold its runner for GitHub's default of
# 6 hours. The steps below take about 2 minutes on 4 CPUs with an empty
# Maven cache, and the smoke stops its own Tomcat case after 180 s, so a
# run that reaches 10 minutes is stuck, not slow.
timeout-minutes: 10
strategy:
fail-fast: false
matrix:
Expand All @@ -36,10 +41,34 @@ jobs:
cache: maven

- name: Build
run: mvn -B -DskipTests clean verify
# With the release profile, signing skipped (CI holds no release
# key), as release.yml builds the jars it deploys.
run: mvn -B -P release -Dgpg.skip -DskipTests clean verify

- name: Record keploy-sdk's jars
# The main jar and the sources jar. The javadoc jar is left out:
# every build makes it anew, with the time in its pages.
shell: bash
run: |
jars=$(ls keploy-sdk/target/keploy-sdk-*.jar | grep -v -e '-javadoc\.jar$')
sha256sum $jars | tee "$RUNNER_TEMP/keploy-sdk-jars.sha256"

- name: Unit tests
run: mvn -B -pl keploy-sdk -am test

- name: Smoke test Java agent
run: ./scripts/smoke-javaagent.sh

- name: A build without clean leaves keploy-sdk's jars as they were
# release.yml smoke-tests the jar of its clean build, then deploys to
# Maven Central from a second pass over the same target/, with the
# release profile and no clean. That pass must leave the bytes of the
# jar and of the sources jar as they were (shade writes the jar anew
# on every pass; only its bytes must stay), or Central gets jars
# other than the ones built and tested. The unit tests above stop at
# the test phase, but what they change in target/classes would reach
# the jar in this pass, so they are checked too.
shell: bash
run: |
mvn -B -P release -Dgpg.skip -DskipTests verify
sha256sum -c "$RUNNER_TEMP/keploy-sdk-jars.sha256"
59 changes: 48 additions & 11 deletions README.md

Large diffs are not rendered by default.

16 changes: 16 additions & 0 deletions keploy-junit5/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -181,5 +181,21 @@
</plugins>
</build>
</profile>
<!-- keploy-junit5 runs on Java 8. Built on JDK 9 or later (the release is
built on JDK 17), javac checks the code against Java 8's API too
(release 8), not only Java 8's class file version: with source and
target 1.8 alone it compiles a call to an API Java 8 lacks, which
fails only on Java 8, at run time (NoSuchMethodError,
NoClassDefFoundError). JDK 8's javac has no release option, and
needs none. The root pom has the same profile for its modules. -->
<profile>
<id>java8-api</id>
<activation>
<jdk>[9,)</jdk>
</activation>
<properties>
<maven.compiler.release>8</maven.compiler.release>
</properties>
</profile>
</profiles>
</project>
117 changes: 117 additions & 0 deletions keploy-junit5/src/test/java/io/keploy/junit5/Java8ApiTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package io.keploy.junit5;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPathFactory;

import org.junit.jupiter.api.Test;
import org.w3c.dom.Document;

/**
* keploy-junit5 runs on Java 8, and is released from JDK 17: its pom's
* java8-api profile makes javac on JDK 9 and later check the code against
* Java 8's API (release 8), as it does for this test class. A call to
* ByteBuffer.position(int) then refers to the method Java 8 has, Buffer's,
* as when JDK 8 compiles it. With source and target 1.8 alone, JDK 9 and
* later compile it to ByteBuffer's own position(int), which JDK 9 added and
* Java 8 lacks (NoSuchMethodError on Java 8). It reads this test class as a
* stand-in for the main classes, which take release 8 from the same
* property: if tests ever get their own maven.compiler.testRelease, point
* this check at a main class instead.
*/
class Java8ApiTest {
/** Holds only the call, so nothing else puts a position descriptor in its class file. */
static final class Call {
static void position(ByteBuffer buffer) {
buffer.position(0);
}
}

@Test
void aCallResolvesAgainstJava8sApi() throws IOException {
String classFile = new String(read(Call.class), StandardCharsets.ISO_8859_1);
assertTrue(classFile.contains("(I)Ljava/nio/Buffer;"),
"the call to ByteBuffer.position(int) does not refer to Buffer's: (I)Ljava/nio/Buffer; is not in its class file");
assertFalse(classFile.contains("(I)Ljava/nio/ByteBuffer;"),
"the call to ByteBuffer.position(int) refers to the one JDK 9 added, which Java 8 lacks: "
+ "javac did not compile against Java 8's API (keploy-junit5's java8-api profile)");
}

/**
* The java8-api profile works through maven-compiler-plugin's release
* parameter, which the plugin has since 3.6. A build that names no version
* gets the running Maven's default, 3.1 on Maven 3.8 and older, which
* ignores maven.compiler.release without a warning: a jar built there with
* -DskipTests, where the row above does not run, would carry calls Java 8
* lacks. keploy-junit5's pom names the version itself.
*/
@Test
void theCompilerPluginVersionHasRelease() throws Exception {
File module = new File(System.getProperty("basedir", System.getProperty("user.dir"))).getAbsoluteFile();
String version = compilerPluginVersion(pom(new File(module, "pom.xml"), "keploy-junit5"));
assertFalse(version.isEmpty(), "keploy-junit5's pom names no maven-compiler-plugin version: "
+ "Maven 3.8 and older then use 3.1, which ignores release 8");
assertTrue(hasRelease(version), "maven-compiler-plugin " + version + " has no release parameter "
+ "(3.6 and later have it): it ignores the java8-api profile's release 8");
}

/**
* The pom at file, which must be artifactId's: run from another directory
* without Maven's basedir (an IDE's working directory, say), the row would
* read another module's pom and could pass on its pin.
*/
static Document pom(File file, String artifactId) throws Exception {
String where = ": run this row from keploy-junit5's directory, or through Maven, which sets basedir to it";
assertTrue(file.isFile(), "no pom at " + file + where);
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
Document doc = factory.newDocumentBuilder().parse(file);
assertEquals(artifactId, XPathFactory.newInstance().newXPath().evaluate("/project/artifactId", doc).trim(),
file + " is not " + artifactId + "'s pom" + where);
return doc;
}

/** The version a pom gives maven-compiler-plugin in its build, or "" when it names none. */
static String compilerPluginVersion(Document doc) throws Exception {
String plugin = "plugins/plugin[artifactId='maven-compiler-plugin']/version";
String version = XPathFactory.newInstance().newXPath().evaluate("/project/build/" + plugin, doc).trim();
if (version.isEmpty()) {
version = XPathFactory.newInstance().newXPath().evaluate("/project/build/pluginManagement/" + plugin, doc).trim();
}
return version;
}

/** True for 3.6 and later: a plain version such as 3.13.0. Anything else, a property included, is false. */
static boolean hasRelease(String version) {
String[] parts = version.split("\\.");
if (parts.length < 2 || !parts[0].matches("[0-9]+") || !parts[1].matches("[0-9]+")) {
return false;
}
int major = Integer.parseInt(parts[0]);
return major > 3 || (major == 3 && Integer.parseInt(parts[1]) >= 6);
}

static byte[] read(Class<?> type) throws IOException {
String name = type.getName();
try (InputStream in = type.getResourceAsStream(name.substring(name.lastIndexOf('.') + 1) + ".class")) {
assertNotNull(in, "no class file for " + name);
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] chunk = new byte[4096];
for (int n; (n = in.read(chunk)) != -1; ) {
out.write(chunk, 0, n);
}
return out.toByteArray();
}
}
}
117 changes: 113 additions & 4 deletions keploy-sdk/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,13 @@
<version>${jacoco.version}</version>
</dependency>
<dependency>
<!-- jacocoagent.jar, carried unchanged, which the SDK starts with
includes= the app's classes when the command attaches no JaCoCo
agent of its own (EmbeddedJacoco). Same version as above. -->
<!-- The tests' copy of jacocoagent.jar (AgentJar). keploy-sdk.jar
carries only the jacocoagent.jar inside this artifact, unpacked
below as io/keploy/dedup/jacocoagent.jar, not its AgentJar class. -->
<groupId>org.jacoco</groupId>
<artifactId>org.jacoco.agent</artifactId>
<version>${jacoco.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.kohlschutter.junixsocket</groupId>
Expand All @@ -94,10 +95,118 @@
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<!-- The servers whose requests END waits for, run by ServerTailTest.
The last lines that run Java 8, as the SDK's tests do. -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-core</artifactId>
<version>9.0.98</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
<version>9.4.57.v20241219</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.undertow</groupId>
<artifactId>undertow-servlet</artifactId>
<version>2.2.37.Final</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.jboss.spec.javax.servlet</groupId>
<artifactId>jboss-servlet-api_4.0_spec</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>

<build>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
<!-- keploy-sdk's own license, Apache 2.0, which the Gson and junixsocket
the jar carries are under too: META-INF/LICENSE in the jar, next to
THIRD-PARTY-NOTICES.txt and JaCoCo's LICENSE-EPL-2.0.txt. -->
<resource>
<directory>${project.basedir}/..</directory>
<includes>
<include>LICENSE</include>
</includes>
<targetPath>META-INF</targetPath>
</resource>
</resources>
<plugins>
<plugin>
<!-- jacocoagent.jar, carried unchanged, which the SDK starts with
includes= the app's classes when the command attaches no JaCoCo
agent of its own (EmbeddedJacoco). It goes in under
io/keploy/dedup, not at the jar's root where org.jacoco.agent
keeps it, and without that artifact's AgentJar class:
keploy-sdk.jar is on the system class path, so either one would
be what an app's own org.jacoco.agent finds in a loader that
asks the system class loader first (Spring Boot's
BOOT-INF/lib), and an app's own on the class path ahead of
keploy-sdk.jar would be the agent the SDK starts. -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.6.1</version>
<executions>
<execution>
<!-- Unpacked once into target/jacocoagent, which also holds the
plugin's marker, so deleting that directory unpacks it again.
It is not unpacked into target/classes: replacing the file there
would touch its directory, and jar:jar would rebuild the jar on
release.yml's no-clean deploy pass. -->
<id>unpack-jacocoagent</id>
<phase>generate-resources</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<markersDirectory>${project.build.directory}/jacocoagent/markers</markersDirectory>
<artifactItems>
<artifactItem>
<groupId>org.jacoco</groupId>
<artifactId>org.jacoco.agent</artifactId>
<version>${jacoco.version}</version>
<type>jar</type>
<includes>jacocoagent.jar</includes>
<outputDirectory>${project.build.directory}/jacocoagent/classes/io/keploy/dedup</outputDirectory>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<!-- Copies the unpacked jacocoagent.jar into target/classes. It copies
only a missing or older file, so a cleared target/classes gets it
back and a build without clean leaves the jar as it was. -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.3.1</version>
<executions>
<execution>
<id>copy-jacocoagent</id>
<phase>process-resources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.outputDirectory}</outputDirectory>
<resources>
<resource>
<directory>${project.build.directory}/jacocoagent/classes</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
Expand All @@ -117,7 +226,7 @@
<Premain-Class>io.keploy.dedup.KeployDedupAgent</Premain-Class>
<Agent-Class>io.keploy.dedup.KeployDedupAgent</Agent-Class>
<Can-Redefine-Classes>false</Can-Redefine-Classes>
<Can-Retransform-Classes>false</Can-Retransform-Classes>
<Can-Retransform-Classes>true</Can-Retransform-Classes>
<Implementation-Title>${project.name}</Implementation-Title>
<Implementation-Version>${project.version}</Implementation-Version>
<Automatic-Module-Name>io.keploy.sdk</Automatic-Module-Name>
Expand Down
Loading
Loading