Skip to content

Commit

Permalink
Miscellaneous code cleanup (#399)
Browse files Browse the repository at this point in the history
  • Loading branch information
basil committed Jan 18, 2023
1 parent 09bb46f commit 2e67c6f
Show file tree
Hide file tree
Showing 39 changed files with 248 additions and 249 deletions.
10 changes: 10 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,10 @@
<groupId>org.antlr</groupId>
<artifactId>antlr4-runtime</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.ant</groupId>
<artifactId>ant</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
Expand Down Expand Up @@ -327,6 +331,12 @@
<groupId>org.reflections</groupId>
<artifactId>reflections</artifactId>
<version>0.10.2</version>
<exclusions>
<exclusion>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>junit</groupId>
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/org/jenkins/tools/test/CliOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@
import java.util.ArrayList;
import java.util.List;

import javax.annotation.CheckForNull;

import org.jenkins.tools.test.model.PCTPlugin;
import org.jenkins.tools.test.model.TestStatus;

Expand All @@ -39,9 +37,11 @@
import com.beust.jcommander.Parameter;
import com.beust.jcommander.ParameterException;

import edu.umd.cs.findbugs.annotations.CheckForNull;
import edu.umd.cs.findbugs.annotations.NonNull;

import hudson.util.VersionNumber;
import java.util.Collections;
import javax.annotation.Nonnull;

/**
* POJO containing CLI arguments &amp; help.
Expand Down Expand Up @@ -232,7 +232,7 @@ public String getMavenPropertiesFile() {
return mavenPropertiesFile;
}

@Nonnull
@NonNull
public List<String> getMavenOptions() {
return mavenOptions != null ? Collections.unmodifiableList(mavenOptions) : Collections.emptyList();
}
Expand Down
55 changes: 28 additions & 27 deletions src/main/java/org/jenkins/tools/test/PluginCompatTester.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@
*/
package org.jenkins.tools.test;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.AbortException;
import hudson.Functions;
import hudson.model.UpdateSite;
Expand All @@ -39,7 +38,9 @@
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.lang.reflect.Constructor;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
Expand All @@ -65,7 +66,6 @@
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.annotation.Nonnull;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
Expand Down Expand Up @@ -189,7 +189,7 @@ public PluginCompatReport testPlugins()

// Scan bundled plugins
// If there is any bundled plugin, only these plugins will be taken under the consideration for the PCT run
UpdateSite.Data data = null;
UpdateSite.Data data;
if (config.getBom() != null) {
data = scanBom(pluginGroupIds, "([^/.]+)[.][hj]pi");
} else {
Expand Down Expand Up @@ -457,7 +457,7 @@ private TestExecutionResult testPluginAgainst(MavenCoordinates coreCoordinates,
System.out.println(String.format("%n%n%n%n%n"));

File pluginCheckoutDir = new File(config.workDirectory.getAbsolutePath() + File.separator + plugin.name + File.separator);
String parentFolder = StringUtils.EMPTY;
String parentFolder = "";

try {
// Run any precheckout hooks
Expand Down Expand Up @@ -534,7 +534,7 @@ private TestExecutionResult testPluginAgainst(MavenCoordinates coreCoordinates,
beforeCompile.put("pomData", pomData);
beforeCompile.put("config", config);
beforeCompile.put("core", coreCoordinates);
if (StringUtils.isNotEmpty(parentFolder)) {
if (parentFolder != null && !parentFolder.isEmpty()) {
beforeCompile.put("parentFolder", parentFolder);
}
Map<String, Object> hookInfo = pcth.runBeforeCompilation(beforeCompile);
Expand Down Expand Up @@ -573,7 +573,7 @@ private TestExecutionResult testPluginAgainst(MavenCoordinates coreCoordinates,
List<String> args = new ArrayList<>();

Map<String, String> userProperties = mconfig.userProperties;
args.add(String.format("--define=forkCount=%s",userProperties.containsKey("forkCount") ? userProperties.get("forkCount") : "1"));
args.add(String.format("--define=forkCount=%s", userProperties.getOrDefault("forkCount", "1")));
args.add("hpi:resolve-test-dependencies");
args.add("hpi:test-hpl");
args.add("surefire:test");
Expand All @@ -597,7 +597,7 @@ private TestExecutionResult testPluginAgainst(MavenCoordinates coreCoordinates,
userProperties.put("types", String.join(",", types));

// Execute with tests
runner.run(mconfig, pluginCheckoutDir, buildLogFile, args.toArray(new String[args.size()]));
runner.run(mconfig, pluginCheckoutDir, buildLogFile, args.toArray(new String[0]));
return new TestExecutionResult(((PomData)forExecutionHooks.get("pomData")).getWarningMessages(), new ExecutedTestNamesSolver().solve(types, runner.getExecutedTests(), pluginCheckoutDir));
} catch (ExecutedTestNamesSolverException e) {
throw new PomExecutionException(e);
Expand All @@ -614,13 +614,13 @@ private TestExecutionResult testPluginAgainst(MavenCoordinates coreCoordinates,
public void cloneFromSCM(PomData pomData, String name, String version, File checkoutDirectory, String tag) throws ComponentLookupException, ScmException, IOException {
String scmTag = !(tag.equals("")) ? tag : getScmTag(pomData, name, version);
String connectionURLPomData = pomData.getConnectionUrl();
List<String> connectionURLs = new ArrayList<String>();
List<String> connectionURLs = new ArrayList<>();
connectionURLs.add(connectionURLPomData);
if(config.getFallbackGitHubOrganization() != null){
connectionURLs = getFallbackConnectionURL(connectionURLs, connectionURLPomData, config.getFallbackGitHubOrganization());
}

Boolean repositoryCloned = false;
boolean repositoryCloned = false;
String errorMessage = "";
ScmRepository repository;
ScmManager scmManager = SCMManagerFactory.getInstance().createScmManager();
Expand Down Expand Up @@ -686,12 +686,17 @@ private boolean onlyOnePluginIncluded() {
*/
private UpdateSite.Data extractUpdateCenterData(Map<String, String> groupIDs){
URL url;
String jsonp;
try {
url = new URL(config.updateCenterUrl);
jsonp = IOUtils.toString(url.openStream());
} catch (MalformedURLException e) {
throw new UncheckedIOException(e);
}

String jsonp;
try (InputStream is = url.openStream()) {
jsonp = new String(is.readAllBytes(), Charset.defaultCharset());
}catch(IOException e){
throw new RuntimeException("Invalid update center url : "+config.updateCenterUrl, e);
throw new UncheckedIOException("Invalid Update Center URL: " + config.updateCenterUrl, e);
}

String json = jsonp.substring(jsonp.indexOf('(')+1,jsonp.lastIndexOf(')'));
Expand Down Expand Up @@ -775,7 +780,7 @@ private UpdateSite.Data scanBom(HashMap<String, String> pluginGroupIds, String p
if (!top.has("core")) {
// Not all boms have the jenkins core dependency explicit, so, assume the bom version matches the jenkins version
String core = solveCoreVersionFromBom();
if (StringUtils.isEmpty(core)) {
if (core == null || core.isEmpty()) {
throw new IllegalStateException("Unable to retrieve any version for the core");
}
top.put("core", new JSONObject().accumulate("name", "core").accumulate("version",core).accumulate("url", "https://foobar"));
Expand All @@ -800,8 +805,6 @@ private List<File> getBomEntries() throws IOException, XmlPullParserException, P

/**
* @return Provides the core version from the bomfile and in case it is not found, the bom version, if bom version does not exist, it provides from its parent
* @throws IOException
* @throws XmlPullParserException
*/
private String solveCoreVersionFromBom() throws IOException, XmlPullParserException {
Model model = new MavenBom(config.getBom()).getModel();
Expand All @@ -816,14 +819,12 @@ private String solveCoreVersionFromBom() throws IOException, XmlPullParserExcept
private String solveVersionFromModel(Model model) {
String version = model.getVersion();
Parent parent = model.getParent();
return version != null ? version : parent != null ? parent.getVersion() : StringUtils.EMPTY;
return version != null ? version : parent != null ? parent.getVersion() : "";
}

/**
* Given a value and a model, it checks if it is an interpolated value. In negative case it returns the same
* value. In affirmative case, it retrieves its value from the properties of the Maven model.
* @param model
* @param version
* @return the effective value of an specific value in a model
*/
private String getProperty(Model model, String value) {
Expand All @@ -846,7 +847,7 @@ private String getProperty(Model model, String value) {
* in the war file
* @return Update center data
*/
private UpdateSite.Data scanWAR(File war, @Nonnull Map<String, String> pluginGroupIds, String pluginRegExp) throws IOException {
private UpdateSite.Data scanWAR(File war, @NonNull Map<String, String> pluginGroupIds, String pluginRegExp) throws IOException {
JSONObject top = new JSONObject();
top.put("id", DEFAULT_SOURCE_ID);
JSONObject plugins = new JSONObject();
Expand Down Expand Up @@ -936,19 +937,19 @@ public static String getMavenModule(String plugin, File pluginPath, MavenRunner
if (absolutePath.endsWith(plugin)) {
return plugin;
}
String module = absolutePath.substring(absolutePath.lastIndexOf(File.separatorChar) + 1, absolutePath.length());
String module = absolutePath.substring(absolutePath.lastIndexOf(File.separatorChar) + 1);
File parentFile = pluginPath.getParentFile();
if (parentFile == null) {
return null;
}
File log = new File(parentFile.getAbsolutePath() + File.separatorChar + "modules.log");
runner.run(mavenConfig, parentFile, log, "-Dexpression=project.modules", "-q", "-DforceStdout", "help:evaluate");
for (String line : org.apache.commons.io.FileUtils.readLines(log)) {
for (String line : Files.readAllLines(log.toPath(), Charset.defaultCharset())) {
if (!StringUtils.startsWith(line.trim(), "<string>")) {
continue;
}
String mvnModule = line.replace("<string>", "").replace("</string>", "").trim();
if (StringUtils.contains(mvnModule, module)) {
if (mvnModule != null && mvnModule.contains(module)) {
return mvnModule;
}
}
Expand All @@ -961,11 +962,11 @@ private void addSplitPluginDependencies(String thisPlugin, MavenRunner.Config mc
Map<String,VersionNumber> pluginDeps = new HashMap<>();
Map<String,VersionNumber> pluginDepsTest = new HashMap<>();
try {
if (StringUtils.isBlank(parentFolder)) {
if (parentFolder == null || parentFolder.isBlank()) {
runner.run(mconfig, pluginCheckoutDir, tmp, "dependency:resolve");
} else {
String mavenModule = getMavenModule(thisPlugin, pluginCheckoutDir, runner, mconfig);
if (StringUtils.isBlank(mavenModule)) {
if (mavenModule == null || mavenModule.isBlank()) {
throw new IOException(String.format("Unable to retrieve the Maven module for plugin %s on %s", thisPlugin, pluginCheckoutDir));
}
runner.run(mconfig, pluginCheckoutDir.getParentFile(), tmp, "dependency:resolve", "-am", "-pl", mavenModule);
Expand Down Expand Up @@ -1227,7 +1228,7 @@ private List<String> removeSplitsBasedOnJDK(List<String> splits, JavaSpecificati
private static Stream<String> configLines(InputStream is) throws IOException {
return IOUtils.readLines(is, StandardCharsets.UTF_8).stream().filter(line -> !line.matches("#.*|\\s*"));
}
private static final ImmutableList<String> HISTORICAL_SPLITS = ImmutableList.of(
private static final List<String> HISTORICAL_SPLITS = List.of(
"maven-plugin 1.296 1.296",
"subversion 1.310 1.0",
"cvs 1.340 0.1",
Expand All @@ -1245,7 +1246,7 @@ private static Stream<String> configLines(InputStream is) throws IOException {
"bouncycastle-api 2.16 2.16.0",
"command-launcher 2.86 1.0"
);
private static final ImmutableSet<String> HISTORICAL_SPLIT_CYCLES = ImmutableSet.of(
private static final Set<String> HISTORICAL_SPLIT_CYCLES = Set.of(
"script-security matrix-auth",
"script-security windows-slaves",
"script-security antisamy-markup-formatter",
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/org/jenkins/tools/test/PluginCompatTesterCli.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.codehaus.plexus.PlexusContainerException;
import org.jenkins.tools.test.model.PluginCompatTesterConfig;
Expand Down Expand Up @@ -118,13 +118,13 @@ public static void main(String[] args) throws IOException, PlexusContainerExcept
config.setExternalMaven(options.getExternalMaven());

if(options.getIncludePlugins() != null && !options.getIncludePlugins().isEmpty()){
config.setIncludePlugins(Arrays.asList(options.getIncludePlugins().toLowerCase().split(",")));
config.setIncludePlugins(List.of(options.getIncludePlugins().toLowerCase().split(",")));
}
if(options.getExcludePlugins() != null && !options.getExcludePlugins().isEmpty()){
config.setExcludePlugins(Arrays.asList(options.getExcludePlugins().toLowerCase().split(",")));
config.setExcludePlugins(List.of(options.getExcludePlugins().toLowerCase().split(",")));
}
if(options.getExcludeHooks() != null && !options.getExcludeHooks().isEmpty()){
config.setExcludeHooks(Arrays.asList(options.getExcludeHooks().split(",")));
config.setExcludeHooks(List.of(options.getExcludeHooks().split(",")));
}
if(options.getSkipTestCache() != null){
config.setSkipTestCache(Boolean.parseBoolean(options.getSkipTestCache()));
Expand All @@ -136,7 +136,7 @@ public static void main(String[] args) throws IOException, PlexusContainerExcept
config.setCacheThresholdStatus(TestStatus.valueOf(options.getCacheThresholdStatus()));
}
if(options.getHookPrefixes() != null && !options.getHookPrefixes().isEmpty()){
config.setHookPrefixes(Arrays.asList(options.getHookPrefixes().split(",")));
config.setHookPrefixes(List.of(options.getHookPrefixes().split(",")));
}
if(options.getExternalHooksJars() != null && !options.getExternalHooksJars().isEmpty()){
config.setExternalHooksJars(options.getExternalHooksJars());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import org.jenkins.tools.test.util.ExecutedTestNamesDetails;

/**
Expand All @@ -49,7 +46,7 @@ public class PomExecutionException extends Exception {
private final ExecutedTestNamesDetails testDetails;

public PomExecutionException(Throwable cause) {
this(cause.toString(), Collections.emptyList(), Collections.singletonList(cause), Collections.emptyList(), new ExecutedTestNamesDetails());
this(cause.toString(), Collections.emptyList(), List.of(cause), Collections.emptyList(), new ExecutedTestNamesDetails());
}

public PomExecutionException(PomExecutionException exceptionToCopy){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,10 @@
import org.apache.maven.scm.ScmFileSet;
import org.apache.maven.scm.ScmTag;
import org.apache.maven.scm.command.checkout.CheckOutScmResult;
import org.apache.maven.scm.manager.NoSuchScmProviderException;
import org.apache.maven.scm.manager.ScmManager;
import org.apache.maven.scm.repository.ScmRepository;
import org.apache.maven.scm.repository.ScmRepositoryException;
import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
import org.codehaus.plexus.util.FileUtils;
import org.apache.commons.io.FileUtils;
import org.jenkins.tools.test.PluginCompatTester;
import org.jenkins.tools.test.SCMManagerFactory;
import org.jenkins.tools.test.model.PluginCompatTesterConfig;
Expand Down Expand Up @@ -82,15 +80,15 @@ public Map<String, Object> action(Map<String, Object> moreInfo) throws Exception
}

private void cloneFromSCM(UpdateSite.Plugin currentPlugin, File parentPath, String scmTag, String url, String fallbackGitHubOrganization)
throws ComponentLookupException, ScmRepositoryException, NoSuchScmProviderException, ScmException, IOException {
throws ComponentLookupException, ScmException, IOException {

List<String> connectionURLs = new ArrayList<String>();
List<String> connectionURLs = new ArrayList<>();
connectionURLs.add(url);
if(fallbackGitHubOrganization != null){
connectionURLs = PluginCompatTester.getFallbackConnectionURL(connectionURLs, url, fallbackGitHubOrganization);
}

Boolean repositoryCloned = false;
boolean repositoryCloned = false;
String errorMessage = "";
ScmRepository repository;
ScmManager scmManager = SCMManagerFactory.getInstance().createScmManager();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import hudson.model.UpdateSite.Plugin;
import java.io.File;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
Expand All @@ -24,7 +23,7 @@
public class ExampleMultiParent { //extends PluginCompatTesterHookBeforeCheckout {
private String parentUrl = "scm:git:git@github.com:jenkinsci/parent_repo.git";
private String parentName = "parent_repo";
private List<String> allBundlePlugins = Arrays.asList("possible", "plugins");
private List<String> allBundlePlugins = List.of("possible", "plugins");
boolean firstRun = true;

public ExampleMultiParent() {}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/jenkins/tools/test/hook/JacocoHook.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public boolean check(Map<String, Object> info) {
}

@Override
public Map<String, Object> action(Map<String, Object> info) throws Exception {
public Map<String, Object> action(Map<String, Object> info) {
List<String> args = (List<String>) info.get("args");

if (args != null) {
Expand Down

0 comments on commit 2e67c6f

Please sign in to comment.