From 095d861808988418776a423f22df4acd0083fa78 Mon Sep 17 00:00:00 2001 From: Basil Crow Date: Tue, 28 Feb 2023 12:40:26 -0800 Subject: [PATCH] Miscellaneous code cleanup (#476) --- .../tools/test/PluginCompatTester.java | 2 +- .../test/hook/MultiParentCompileHook.java | 9 ++++--- .../hook/PluginWithIntegrationTestsHook.java | 1 - .../jenkins/tools/test/model/MavenPom.java | 4 +-- .../model/hook/PluginCompatTesterHooks.java | 27 ++++++++++--------- .../tools/test/model/PluginRemotingTest.java | 3 +-- 6 files changed, 25 insertions(+), 21 deletions(-) diff --git a/src/main/java/org/jenkins/tools/test/PluginCompatTester.java b/src/main/java/org/jenkins/tools/test/PluginCompatTester.java index 3ed78a2bc..7abc972b6 100644 --- a/src/main/java/org/jenkins/tools/test/PluginCompatTester.java +++ b/src/main/java/org/jenkins/tools/test/PluginCompatTester.java @@ -81,7 +81,7 @@ public class PluginCompatTester { public static final String JENKINS_CORE_FILE_REGEX = "WEB-INF/lib/jenkins-core-([0-9.]+(?:-[0-9a-f.]+)*(?:-(?i)([a-z]+)(-)?([0-9a-f.]+)?)?(?:-(?i)([a-z]+)(-)?([0-9a-f_.]+)?)?(?:-SNAPSHOT)?)[.]jar"; - private PluginCompatTesterConfig config; + private final PluginCompatTesterConfig config; private final ExternalMavenRunner runner; public PluginCompatTester(PluginCompatTesterConfig config) { diff --git a/src/main/java/org/jenkins/tools/test/hook/MultiParentCompileHook.java b/src/main/java/org/jenkins/tools/test/hook/MultiParentCompileHook.java index a6eaf76fc..e3978db3b 100644 --- a/src/main/java/org/jenkins/tools/test/hook/MultiParentCompileHook.java +++ b/src/main/java/org/jenkins/tools/test/hook/MultiParentCompileHook.java @@ -27,6 +27,7 @@ import org.jenkins.tools.test.model.hook.PluginCompatTesterHookBeforeCompile; import org.jenkins.tools.test.model.hook.PluginCompatTesterHooks; import org.jenkins.tools.test.model.hook.Stage; +import org.jenkins.tools.test.model.hook.StageContext; @SuppressFBWarnings(value = "PATH_TRAVERSAL_IN", justification = "intended behavior") public class MultiParentCompileHook extends PluginCompatTesterHookBeforeCompile { @@ -89,10 +90,12 @@ public void action(@NonNull BeforeCompilationContext context) throws PomExecutio @Override public boolean check(@NonNull BeforeCompilationContext context) { - for (PluginCompatTesterHook hook : + for (PluginCompatTesterHook hook : PluginCompatTesterHooks.hooksByStage.get(Stage.CHECKOUT)) { - if (hook instanceof AbstractMultiParentHook - && hook.check( + PluginCompatTesterHook checkoutHook = + (PluginCompatTesterHook) hook; + if (checkoutHook instanceof AbstractMultiParentHook + && checkoutHook.check( new BeforeCheckoutContext( context.getPlugin(), context.getPomData(), diff --git a/src/main/java/org/jenkins/tools/test/hook/PluginWithIntegrationTestsHook.java b/src/main/java/org/jenkins/tools/test/hook/PluginWithIntegrationTestsHook.java index 4e22f18f8..783399472 100644 --- a/src/main/java/org/jenkins/tools/test/hook/PluginWithIntegrationTestsHook.java +++ b/src/main/java/org/jenkins/tools/test/hook/PluginWithIntegrationTestsHook.java @@ -12,7 +12,6 @@ public abstract class PluginWithIntegrationTestsHook extends PluginCompatTesterH @NonNull public abstract Collection getGoals(); - @SuppressWarnings("unchecked") @Override public void action(@NonNull BeforeExecutionContext context) { context.getArgs().addAll(getGoals()); diff --git a/src/main/java/org/jenkins/tools/test/model/MavenPom.java b/src/main/java/org/jenkins/tools/test/model/MavenPom.java index 893f8163d..5cedee30b 100644 --- a/src/main/java/org/jenkins/tools/test/model/MavenPom.java +++ b/src/main/java/org/jenkins/tools/test/model/MavenPom.java @@ -72,8 +72,8 @@ public class MavenPom { private static final String VERSION_ELEMENT = "version"; private static final String CLASSIFIER_ELEMENT = "classifier"; - private File rootDir; - private String pomFileName; + private final File rootDir; + private final String pomFileName; public MavenPom(File rootDir) { this(rootDir, "pom.xml"); diff --git a/src/main/java/org/jenkins/tools/test/model/hook/PluginCompatTesterHooks.java b/src/main/java/org/jenkins/tools/test/model/hook/PluginCompatTesterHooks.java index d5ef4f51f..eedd6a882 100644 --- a/src/main/java/org/jenkins/tools/test/model/hook/PluginCompatTesterHooks.java +++ b/src/main/java/org/jenkins/tools/test/model/hook/PluginCompatTesterHooks.java @@ -11,7 +11,6 @@ import java.util.ArrayList; import java.util.Comparator; import java.util.EnumMap; -import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.NavigableSet; @@ -40,7 +39,7 @@ public class PluginCompatTesterHooks { @NonNull private final List hookPrefixes; - public static final Map> hooksByStage = + public static final Map>> hooksByStage = new EnumMap<>(Stage.class); @NonNull private final List excludeHooks; @@ -98,7 +97,7 @@ public void runBeforeExecution(@NonNull BeforeExecutionContext context) * @param context relevant information to hooks at various stages. */ private void runHooks(@NonNull StageContext context) throws PluginCompatibilityTesterException { - for (PluginCompatTesterHook hook : hooksByStage.get(context.getStage())) { + for (PluginCompatTesterHook hook : hooksByStage.get(context.getStage())) { if (!excludeHooks.contains(hook.getClass().getName()) && hook.check(context)) { LOGGER.log(Level.INFO, "Running hook: {0}", hook.getClass().getName()); hook.action(context); @@ -108,8 +107,8 @@ private void runHooks(@NonNull StageContext context) throws PluginCompatibilityT } } - private List findHooks(Stage stage) { - List sortedHooks = new LinkedList<>(); + private List> findHooks(Stage stage) { + List> sortedHooks = new ArrayList<>(); // Search for all hooks defined within the given classpath prefix ConfigurationBuilder configurationBuilder = new ConfigurationBuilder(); @@ -118,7 +117,7 @@ private List findHooks(Stage stage) { configurationBuilder.forPackage(hookPrefix, classLoader); } Reflections reflections = new Reflections(configurationBuilder); - NavigableSet> subTypes; + NavigableSet>> subTypes; // Find all steps for a given stage. Long due to casting switch (stage) { @@ -153,11 +152,13 @@ private List findHooks(Stage stage) { throw new IllegalArgumentException("Invalid stage: " + stage); } - for (Class c : subTypes) { + for (Class> c : subTypes) { try { LOGGER.log(Level.FINE, "Loading hook: {0}", c.getName()); - Constructor constructor = c.getConstructor(); - PluginCompatTesterHook hook = (PluginCompatTesterHook) constructor.newInstance(); + Constructor> constructor = + c.getConstructor(); + PluginCompatTesterHook hook = + (PluginCompatTesterHook) constructor.newInstance(); sortedHooks.add(hook); } catch (ReflectiveOperationException e) { throw new RuntimeException("Error when loading " + c.getName(), e); @@ -167,7 +168,9 @@ private List findHooks(Stage stage) { return sortedHooks; } - private static Supplier>> navigableSet() { + private static Supplier< + NavigableSet>>> + navigableSet() { return () -> new TreeSet<>(Comparator.comparing(Class::getName)); } @@ -176,8 +179,8 @@ private static Supplier>> n * Set}s. Gets around a generics error: {@code incompatible types: inference variable T has * incompatible bounds}. */ - private Class casting( - Class c) { + private Class> casting( + Class> c) { return c; } } diff --git a/src/test/java/org/jenkins/tools/test/model/PluginRemotingTest.java b/src/test/java/org/jenkins/tools/test/model/PluginRemotingTest.java index 5914302f7..1034f622b 100644 --- a/src/test/java/org/jenkins/tools/test/model/PluginRemotingTest.java +++ b/src/test/java/org/jenkins/tools/test/model/PluginRemotingTest.java @@ -69,8 +69,7 @@ void negative() throws Exception { PluginRemoting pluginRemoting = new PluginRemoting(pomFile); PluginSourcesUnavailableException e = assertThrows( - PluginSourcesUnavailableException.class, - () -> pluginRemoting.retrievePomData()); + PluginSourcesUnavailableException.class, pluginRemoting::retrievePomData); assertThat(e.getMessage(), is("Failed to parse pom.xml")); } }