Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for MavenInvoker #157

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
package io.jenkins.tools.pluginmodernizer.core.impl;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;

import io.jenkins.tools.pluginmodernizer.core.config.Config;
import io.jenkins.tools.pluginmodernizer.core.model.Plugin;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Comparator;
import java.util.List;
import org.apache.maven.artifact.versioning.ComparableVersion;
import org.apache.maven.shared.invoker.InvocationRequest;
import org.apache.maven.shared.invoker.InvocationResult;
import org.apache.maven.shared.invoker.Invoker;
import org.apache.maven.shared.invoker.MavenInvocationException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

class MavenInvokerTest {

private Config config;
private MavenInvoker mavenInvoker;
private Plugin plugin;

private static final Logger LOG = LoggerFactory.getLogger(MavenInvoker.class);

@BeforeEach
void setUp() {
config = mock(Config.class);
Path mavenHome = getMavenHome();
when(config.getMavenHome()).thenReturn(mavenHome);
when(config.getRecipes()).thenReturn(List.of("FetchMetadata"));

mavenInvoker = spy(new MavenInvoker(config));

plugin = mock(Plugin.class);
when(plugin.getName()).thenReturn("test-plugin");
when(plugin.getLocalRepository()).thenReturn(Path.of("src/test/resources/test-plugin"));
}

private Path getMavenHome() {
String mavenHomePath = System.getenv("MAVEN_HOME");
if (mavenHomePath == null) {
mavenHomePath = System.getenv("M2_HOME");
}
if (mavenHomePath == null) {
mavenHomePath = System.getProperty("maven.home");
}
if (mavenHomePath == null) {
throw new IllegalStateException(
"Maven home directory is not set. Please set the MAVEN_HOME environment variable or maven.home system property.");
}
return Path.of(mavenHomePath);
}

@Test
void testGetMavenVersion() {
Invoker invoker = mock(Invoker.class);
InvocationResult result = mock(InvocationResult.class);

try {
when(invoker.execute(any(InvocationRequest.class))).thenReturn(result);
when(result.getExitCode()).thenReturn(0);

ComparableVersion version = mavenInvoker.getMavenVersion();
assertNotNull(version, "Maven version should not be null");
} catch (MavenInvocationException e) {
fail("Exception should not be thrown");
}
}

@Test
void testInvokeGoal() throws Exception {
Path pluginDir = plugin.getLocalRepository();

Path targetDir = pluginDir.resolve("target");
Files.createDirectory(targetDir);

Path testFile = targetDir.resolve("test-file.txt");
Files.createFile(testFile);

try {
assertTrue(Files.exists(testFile), "Test file should exist before clean");

mavenInvoker.invokeGoal(plugin, "clean");

assertFalse(Files.exists(targetDir), "The target directory should be removed after clean");
} finally {
if (Files.exists(targetDir)) {
Files.walk(targetDir).sorted(Comparator.reverseOrder()).forEach(path -> {
try {
Files.delete(path);
} catch (Exception e) {
LOG.error("Error deleting file {}", path, e);
}
});
}
}
}

@Test
void testInvokeRewrite() throws Exception {
Path pluginDir = plugin.getLocalRepository();
Path targetDir = pluginDir.resolve("target");

try {
assertFalse(Files.exists(targetDir), "target should not exist before rewrite");
mavenInvoker.invokeRewrite(plugin);
assertTrue(Files.exists(targetDir), "target should exist after rewrite");
} finally {
if (Files.exists(targetDir)) {
Files.walk(targetDir).sorted(Comparator.reverseOrder()).forEach(path -> {
try {
Files.delete(path);
} catch (Exception e) {
LOG.error("Error deleting file {}", path, e);
}
});
}
}
}

@Test
void testValidateMavenHome() {
// Valid maven home
when(config.getMavenHome()).thenReturn(getMavenHome());
assertDoesNotThrow(() -> new MavenInvoker(config));

// Invalid maven home
when(config.getMavenHome()).thenReturn(Path.of("/invalid/path/to/maven"));
assertThrows(IllegalArgumentException.class, () -> new MavenInvoker(config));
}

@Test
void testValidateMavenVersion() {
// Valid maven version
when(config.getMavenHome()).thenReturn(getMavenHome());
assertDoesNotThrow(() -> new MavenInvoker(config));

// Invalid maven version
when(config.getMavenHome()).thenReturn(Path.of("/path/to/old/maven"));
assertThrows(IllegalArgumentException.class, () -> new MavenInvoker(config));
}

@Test
void testValidateSelectedRecipes() {
// Valid recipes
when(config.getRecipes()).thenReturn(List.of("FetchMetadata"));
assertDoesNotThrow(() -> new MavenInvoker(config));

// No valid recipes
when(config.getRecipes()).thenReturn(List.of());
assertThrows(IllegalArgumentException.class, () -> new MavenInvoker(config));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
See the documentation for more options:
https://github.com/jenkins-infra/pipeline-library/
*/
buildPlugin(
useContainerAgent: true, // Set to `false` if you need to use Docker for containerized tests
configurations: [
[platform: 'linux', jdk: 21],
[platform: 'windows', jdk: 17],
])c
49 changes: 49 additions & 0 deletions plugin-modernizer-core/src/test/resources/test-plugin/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>4.83</version>
<relativePath />
</parent>

<groupId>io.jenkins.plugins</groupId>
<artifactId>test-plugin</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>hpi</packaging>
<name>Test Plugin</name>

<properties>
<jenkins.version>2.440.3</jenkins.version>
<hpi.compatibleSinceVersion>78</hpi.compatibleSinceVersion>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.jenkins.tools.bom</groupId>
<artifactId>bom-2.440.x</artifactId>
<version>3105.v672692894683</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<repositories>
<repository>
<id>repo.jenkins-ci.org</id>
<url>https://repo.jenkins-ci.org/public/</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>repo.jenkins-ci.org</id>
<url>https://repo.jenkins-ci.org/public/</url>
</pluginRepository>
</pluginRepositories>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package io.jenkins.plugins.testplugin;

import hudson.Extension;

@Extension
public class Main {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?jelly escape-by-default='true'?>
<div>
This is a test plugin
</div>