Skip to content

Commit

Permalink
[JENKINS-73283] Run PCT with a Jetty 12 EE 9 test harness when core i…
Browse files Browse the repository at this point in the history
…s Jetty 12 EE 9 (#676)
  • Loading branch information
basil committed Jun 21, 2024
1 parent 58221e0 commit 3027df3
Showing 1 changed file with 131 additions and 0 deletions.
131 changes: 131 additions & 0 deletions src/main/java/org/jenkins/tools/test/hook/ServletApiWorkaround.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package org.jenkins.tools.test.hook;

import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.util.VersionNumber;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.jar.JarInputStream;
import java.util.zip.ZipEntry;
import org.jenkins.tools.test.exception.PluginCompatibilityTesterException;
import org.jenkins.tools.test.exception.PomExecutionException;
import org.jenkins.tools.test.maven.ExternalMavenRunner;
import org.jenkins.tools.test.maven.MavenRunner;
import org.jenkins.tools.test.model.PluginCompatTesterConfig;
import org.jenkins.tools.test.model.hook.BeforeExecutionContext;
import org.jenkins.tools.test.model.hook.PluginCompatTesterHookBeforeExecution;
import org.kohsuke.MetaInfServices;

/**
* Old versions of the plugin parent POM hard-code EE 8. If such a version is in use but the core
* uses EE 9 or later, work around the issue by overriding the version.
*
* @see <a href="https://github.com/jenkinsci/plugin-pom/issues/936">jenkinsci/plugin-pom#936</a>
*/
@MetaInfServices(PluginCompatTesterHookBeforeExecution.class)
public class ServletApiWorkaround extends PluginCompatTesterHookBeforeExecution {

@Override
public boolean check(@NonNull BeforeExecutionContext context) {
PluginCompatTesterConfig config = context.getConfig();
MavenRunner runner =
new ExternalMavenRunner(config.getExternalMaven(), config.getMavenSettings(), config.getMavenArgs());
VersionNumber jakartaServletApiVersion = getJakartaServletApiVersion(
context.getCloneDirectory(), context.getPlugin().getModule(), runner);
if (jakartaServletApiVersion.isOlderThan(new VersionNumber("5"))) {

Check warning on line 44 in src/main/java/org/jenkins/tools/test/hook/ServletApiWorkaround.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 44 is only partially covered, one branch is missing
VersionNumber enterpriseEditionVersion = getEnterpriseEditionVersion(config.getWar());
if (enterpriseEditionVersion != null && enterpriseEditionVersion.isNewerThan(new VersionNumber("8"))) {

Check warning on line 46 in src/main/java/org/jenkins/tools/test/hook/ServletApiWorkaround.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 46 is only partially covered, 2 branches are missing
return true;

Check warning on line 47 in src/main/java/org/jenkins/tools/test/hook/ServletApiWorkaround.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 47 is not covered by tests
}
}
return false;
}

private VersionNumber getJakartaServletApiVersion(File pluginPath, String module, MavenRunner runner) {
Path log = pluginPath.toPath().resolve("jakarta.servlet-api.log");
try {
runner.run(
Map.of(
"includeGroupIds",
"jakarta.servlet",
"includeArtifactIds",
"jakarta.servlet-api",
"includeScope",
"provided",
"outputFile",
log.toAbsolutePath().toString(),
"set.changelist",
"true",
"ignore.dirt",
"true"),
pluginPath,
module,
null,
"-q",
"dependency:collect");
} catch (PomExecutionException e) {
throw new RuntimeException(e);

Check warning on line 76 in src/main/java/org/jenkins/tools/test/hook/ServletApiWorkaround.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 75-76 are not covered by tests
}
List<String> output;
try {
output = Files.readAllLines(log, Charset.defaultCharset());
Files.deleteIfExists(log);
} catch (IOException e) {
throw new UncheckedIOException(e);

Check warning on line 83 in src/main/java/org/jenkins/tools/test/hook/ServletApiWorkaround.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 82-83 are not covered by tests
}
for (String line : output) {

Check warning on line 85 in src/main/java/org/jenkins/tools/test/hook/ServletApiWorkaround.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 85 is only partially covered, one branch is missing
if (!line.trim().startsWith("jakarta.servlet:jakarta.servlet-api:")) {
continue;
}
return new VersionNumber(line.trim().split(":")[3]);
}
throw new RuntimeException("Failed to determine jakarta.servlet-api version");

Check warning on line 91 in src/main/java/org/jenkins/tools/test/hook/ServletApiWorkaround.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 91 is not covered by tests
}

private VersionNumber getEnterpriseEditionVersion(File war) {
try (JarFile jarFile = new JarFile(war)) {
ZipEntry zipEntry = jarFile.getEntry("executable/winstone.jar");
if (zipEntry == null) {

Check warning on line 97 in src/main/java/org/jenkins/tools/test/hook/ServletApiWorkaround.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 97 is only partially covered, one branch is missing
throw new IllegalArgumentException("Failed to find winstone.jar in " + war);

Check warning on line 98 in src/main/java/org/jenkins/tools/test/hook/ServletApiWorkaround.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 98 is not covered by tests
}
try (InputStream is = jarFile.getInputStream(zipEntry);
BufferedInputStream bis = new BufferedInputStream(is);
JarInputStream jis = new JarInputStream(bis)) {
JarEntry jarEntry;
while ((jarEntry = jis.getNextJarEntry()) != null) {

Check warning on line 104 in src/main/java/org/jenkins/tools/test/hook/ServletApiWorkaround.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 104 is only partially covered, one branch is missing
if (!jarEntry.isDirectory()) {
continue;
}
if ("META-INF/maven/org.eclipse.jetty.ee11/jetty-ee11-servlet/".equals(jarEntry.getName())) {

Check warning on line 108 in src/main/java/org/jenkins/tools/test/hook/ServletApiWorkaround.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 108 is only partially covered, one branch is missing
return new VersionNumber("11");

Check warning on line 109 in src/main/java/org/jenkins/tools/test/hook/ServletApiWorkaround.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 109 is not covered by tests
} else if ("META-INF/maven/org.eclipse.jetty.ee10/jetty-ee10-servlet/".equals(jarEntry.getName())) {

Check warning on line 110 in src/main/java/org/jenkins/tools/test/hook/ServletApiWorkaround.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 110 is only partially covered, one branch is missing
return new VersionNumber("10");

Check warning on line 111 in src/main/java/org/jenkins/tools/test/hook/ServletApiWorkaround.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 111 is not covered by tests
} else if ("META-INF/maven/org.eclipse.jetty.ee9/jetty-ee9-servlet/".equals(jarEntry.getName())) {

Check warning on line 112 in src/main/java/org/jenkins/tools/test/hook/ServletApiWorkaround.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 112 is only partially covered, one branch is missing
return new VersionNumber("9");

Check warning on line 113 in src/main/java/org/jenkins/tools/test/hook/ServletApiWorkaround.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 113 is not covered by tests
} else if ("META-INF/maven/org.eclipse.jetty.ee8/jetty-ee8-servlet/".equals(jarEntry.getName())) {

Check warning on line 114 in src/main/java/org/jenkins/tools/test/hook/ServletApiWorkaround.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 114 is only partially covered, one branch is missing
return new VersionNumber("8");

Check warning on line 115 in src/main/java/org/jenkins/tools/test/hook/ServletApiWorkaround.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 115 is not covered by tests
} else if ("META-INF/maven/org.eclipse.jetty/jetty-servlet/".equals(jarEntry.getName())) {
return new VersionNumber("8");
}
}
return null; // unknown

Check warning on line 120 in src/main/java/org/jenkins/tools/test/hook/ServletApiWorkaround.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 120 is not covered by tests
}

Check warning on line 121 in src/main/java/org/jenkins/tools/test/hook/ServletApiWorkaround.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 121 is only partially covered, 9 branches are missing
} catch (IOException e) {
throw new UncheckedIOException("Failed to read Java EE version in " + war, e);
}
}

@Override
public void action(@NonNull BeforeExecutionContext context) throws PluginCompatibilityTesterException {
context.getArgs().add("-DoverrideVersions=jakarta.servlet:jakarta.servlet-api:5.0.0");
}

Check warning on line 130 in src/main/java/org/jenkins/tools/test/hook/ServletApiWorkaround.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 123-130 are not covered by tests
}

0 comments on commit 3027df3

Please sign in to comment.