Skip to content

Commit

Permalink
Miscellaneous code cleanup (#476)
Browse files Browse the repository at this point in the history
  • Loading branch information
basil committed Feb 28, 2023
1 parent 66806f1 commit 095d861
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -89,10 +90,12 @@ public void action(@NonNull BeforeCompilationContext context) throws PomExecutio

@Override
public boolean check(@NonNull BeforeCompilationContext context) {
for (PluginCompatTesterHook hook :
for (PluginCompatTesterHook<? extends StageContext> hook :
PluginCompatTesterHooks.hooksByStage.get(Stage.CHECKOUT)) {
if (hook instanceof AbstractMultiParentHook
&& hook.check(
PluginCompatTesterHook<BeforeCheckoutContext> checkoutHook =
(PluginCompatTesterHook<BeforeCheckoutContext>) hook;
if (checkoutHook instanceof AbstractMultiParentHook
&& checkoutHook.check(
new BeforeCheckoutContext(
context.getPlugin(),
context.getPomData(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ public abstract class PluginWithIntegrationTestsHook extends PluginCompatTesterH
@NonNull
public abstract Collection<String> getGoals();

@SuppressWarnings("unchecked")
@Override
public void action(@NonNull BeforeExecutionContext context) {
context.getArgs().addAll(getGoals());
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/jenkins/tools/test/model/MavenPom.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -40,7 +39,7 @@ public class PluginCompatTesterHooks {

@NonNull private final List<String> hookPrefixes;

public static final Map<Stage, List<PluginCompatTesterHook>> hooksByStage =
public static final Map<Stage, List<PluginCompatTesterHook<StageContext>>> hooksByStage =
new EnumMap<>(Stage.class);

@NonNull private final List<String> excludeHooks;
Expand Down Expand Up @@ -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<StageContext> 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);
Expand All @@ -108,8 +107,8 @@ private void runHooks(@NonNull StageContext context) throws PluginCompatibilityT
}
}

private List<PluginCompatTesterHook> findHooks(Stage stage) {
List<PluginCompatTesterHook> sortedHooks = new LinkedList<>();
private List<PluginCompatTesterHook<StageContext>> findHooks(Stage stage) {
List<PluginCompatTesterHook<StageContext>> sortedHooks = new ArrayList<>();

// Search for all hooks defined within the given classpath prefix
ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();
Expand All @@ -118,7 +117,7 @@ private List<PluginCompatTesterHook> findHooks(Stage stage) {
configurationBuilder.forPackage(hookPrefix, classLoader);
}
Reflections reflections = new Reflections(configurationBuilder);
NavigableSet<Class<? extends PluginCompatTesterHook>> subTypes;
NavigableSet<Class<? extends PluginCompatTesterHook<? extends StageContext>>> subTypes;

// Find all steps for a given stage. Long due to casting
switch (stage) {
Expand Down Expand Up @@ -153,11 +152,13 @@ private List<PluginCompatTesterHook> findHooks(Stage stage) {
throw new IllegalArgumentException("Invalid stage: " + stage);
}

for (Class<?> c : subTypes) {
for (Class<? extends PluginCompatTesterHook<? extends StageContext>> c : subTypes) {
try {
LOGGER.log(Level.FINE, "Loading hook: {0}", c.getName());
Constructor<?> constructor = c.getConstructor();
PluginCompatTesterHook hook = (PluginCompatTesterHook) constructor.newInstance();
Constructor<? extends PluginCompatTesterHook<? extends StageContext>> constructor =
c.getConstructor();
PluginCompatTesterHook<StageContext> hook =
(PluginCompatTesterHook<StageContext>) constructor.newInstance();
sortedHooks.add(hook);
} catch (ReflectiveOperationException e) {
throw new RuntimeException("Error when loading " + c.getName(), e);
Expand All @@ -167,7 +168,9 @@ private List<PluginCompatTesterHook> findHooks(Stage stage) {
return sortedHooks;
}

private static Supplier<NavigableSet<Class<? extends PluginCompatTesterHook>>> navigableSet() {
private static Supplier<
NavigableSet<Class<? extends PluginCompatTesterHook<? extends StageContext>>>>
navigableSet() {
return () -> new TreeSet<>(Comparator.comparing(Class::getName));
}

Expand All @@ -176,8 +179,8 @@ private static Supplier<NavigableSet<Class<? extends PluginCompatTesterHook>>> n
* Set}s. Gets around a generics error: {@code incompatible types: inference variable T has
* incompatible bounds}.
*/
private Class<? extends PluginCompatTesterHook> casting(
Class<? extends PluginCompatTesterHook> c) {
private Class<? extends PluginCompatTesterHook<? extends StageContext>> casting(
Class<? extends PluginCompatTesterHook<? extends StageContext>> c) {
return c;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}
}

0 comments on commit 095d861

Please sign in to comment.