Skip to content

Commit 3d74441

Browse files
Migration to JUnit 5 - avoid using AbstractMojoTestCase in DeployFileMojoTest
- also use constructor inject instead od Component - remove unused deploy-file-pom-file directory
1 parent 7941163 commit 3d74441

File tree

13 files changed

+125
-148
lines changed

13 files changed

+125
-148
lines changed

pom.xml

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -162,13 +162,6 @@ under the License.
162162
<version>3.4.0</version>
163163
<scope>test</scope>
164164
</dependency>
165-
<dependency>
166-
<!-- used by maven-plugin-testing-harness, don't give it compile scope! -->
167-
<groupId>org.apache.maven</groupId>
168-
<artifactId>maven-compat</artifactId>
169-
<version>${mavenVersion}</version>
170-
<scope>test</scope>
171-
</dependency>
172165
<dependency>
173166
<groupId>org.apache.maven</groupId>
174167
<artifactId>maven-resolver-provider</artifactId>
@@ -205,6 +198,16 @@ under the License.
205198
<version>4.13.2</version>
206199
<scope>test</scope>
207200
</dependency>
201+
<dependency>
202+
<groupId>org.junit.jupiter</groupId>
203+
<artifactId>junit-jupiter-api</artifactId>
204+
<scope>test</scope>
205+
</dependency>
206+
<dependency>
207+
<groupId>org.junit.vintage</groupId>
208+
<artifactId>junit-vintage-engine</artifactId>
209+
<scope>test</scope>
210+
</dependency>
208211
<dependency>
209212
<groupId>org.slf4j</groupId>
210213
<artifactId>slf4j-nop</artifactId>

src/main/java/org/apache/maven/plugins/deploy/AbstractDeployMojo.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import org.apache.maven.plugin.AbstractMojo;
2323
import org.apache.maven.plugin.MojoExecutionException;
2424
import org.apache.maven.plugin.MojoFailureException;
25-
import org.apache.maven.plugins.annotations.Component;
2625
import org.apache.maven.plugins.annotations.Parameter;
2726
import org.apache.maven.rtinfo.RuntimeInformation;
2827
import org.eclipse.aether.RepositorySystem;
@@ -52,19 +51,22 @@ public abstract class AbstractDeployMojo extends AbstractMojo {
5251
@Parameter(property = "retryFailedDeploymentCount", defaultValue = "1")
5352
private int retryFailedDeploymentCount;
5453

55-
@Component
56-
private RuntimeInformation runtimeInformation;
54+
private final RuntimeInformation runtimeInformation;
5755

5856
@Parameter(defaultValue = "${session}", readonly = true, required = true)
5957
protected MavenSession session;
6058

61-
@Component
62-
protected RepositorySystem repositorySystem;
59+
protected final RepositorySystem repositorySystem;
6360

6461
private static final String AFFECTED_MAVEN_PACKAGING = "maven-plugin";
6562

6663
private static final String FIXED_MAVEN_VERSION = "3.9.0";
6764

65+
protected AbstractDeployMojo(RuntimeInformation runtimeInformation, RepositorySystem repositorySystem) {
66+
this.runtimeInformation = runtimeInformation;
67+
this.repositorySystem = repositorySystem;
68+
}
69+
6870
/* Setters and Getters */
6971

7072
void failIfOffline() throws MojoFailureException {

src/main/java/org/apache/maven/plugins/deploy/DeployFileMojo.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
*/
1919
package org.apache.maven.plugins.deploy;
2020

21+
import javax.inject.Inject;
22+
2123
import java.io.File;
2224
import java.io.FileNotFoundException;
2325
import java.io.IOException;
@@ -41,12 +43,14 @@
4143
import org.apache.maven.plugin.MojoFailureException;
4244
import org.apache.maven.plugins.annotations.Mojo;
4345
import org.apache.maven.plugins.annotations.Parameter;
46+
import org.apache.maven.rtinfo.RuntimeInformation;
4447
import org.codehaus.plexus.util.FileUtils;
4548
import org.codehaus.plexus.util.IOUtil;
4649
import org.codehaus.plexus.util.StringUtils;
4750
import org.codehaus.plexus.util.xml.ReaderFactory;
4851
import org.codehaus.plexus.util.xml.WriterFactory;
4952
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
53+
import org.eclipse.aether.RepositorySystem;
5054
import org.eclipse.aether.RepositorySystemSession;
5155
import org.eclipse.aether.artifact.Artifact;
5256
import org.eclipse.aether.artifact.ArtifactType;
@@ -197,6 +201,11 @@ public class DeployFileMojo extends AbstractDeployMojo {
197201
@Parameter(property = "maven.deploy.file.skip", defaultValue = "false")
198202
private String skip = Boolean.FALSE.toString();
199203

204+
@Inject
205+
protected DeployFileMojo(RuntimeInformation runtimeInformation, RepositorySystem repositorySystem) {
206+
super(runtimeInformation, repositorySystem);
207+
}
208+
200209
void initProperties() throws MojoExecutionException {
201210
if (pomFile == null) {
202211
boolean foundPom = false;

src/main/java/org/apache/maven/plugins/deploy/DeployMojo.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
*/
1919
package org.apache.maven.plugins.deploy;
2020

21+
import javax.inject.Inject;
22+
2123
import java.io.File;
2224
import java.util.ArrayList;
2325
import java.util.LinkedHashMap;
@@ -38,6 +40,8 @@
3840
import org.apache.maven.plugins.annotations.Parameter;
3941
import org.apache.maven.project.MavenProject;
4042
import org.apache.maven.project.artifact.ProjectArtifact;
43+
import org.apache.maven.rtinfo.RuntimeInformation;
44+
import org.eclipse.aether.RepositorySystem;
4145
import org.eclipse.aether.artifact.Artifact;
4246
import org.eclipse.aether.deployment.DeployRequest;
4347
import org.eclipse.aether.repository.RemoteRepository;
@@ -140,6 +144,11 @@ public class DeployMojo extends AbstractDeployMojo {
140144
@Parameter(defaultValue = "false", property = "allowIncompleteProjects")
141145
private boolean allowIncompleteProjects;
142146

147+
@Inject
148+
protected DeployMojo(RuntimeInformation runtimeInformation, RepositorySystem repositorySystem) {
149+
super(runtimeInformation, repositorySystem);
150+
}
151+
143152
private enum State {
144153
SKIPPED,
145154
DEPLOYED,

src/test/java/org/apache/maven/plugins/deploy/DeployFileMojoTest.java

Lines changed: 45 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -18,46 +18,51 @@
1818
*/
1919
package org.apache.maven.plugins.deploy;
2020

21+
import javax.inject.Inject;
22+
2123
import java.io.File;
2224
import java.util.ArrayList;
2325
import java.util.List;
2426
import java.util.Objects;
2527

28+
import org.apache.maven.api.plugin.testing.Basedir;
29+
import org.apache.maven.api.plugin.testing.InjectMojo;
30+
import org.apache.maven.api.plugin.testing.MojoTest;
2631
import org.apache.maven.execution.MavenSession;
2732
import org.apache.maven.model.Model;
28-
import org.apache.maven.plugin.testing.AbstractMojoTestCase;
2933
import org.eclipse.aether.DefaultRepositorySystemSession;
3034
import org.eclipse.aether.internal.impl.DefaultLocalPathComposer;
3135
import org.eclipse.aether.internal.impl.SimpleLocalRepositoryManagerFactory;
3236
import org.eclipse.aether.repository.LocalRepository;
33-
import org.mockito.InjectMocks;
34-
import org.mockito.Mock;
35-
import org.mockito.MockitoAnnotations;
36-
37+
import org.junit.jupiter.api.BeforeEach;
38+
import org.junit.jupiter.api.Test;
39+
40+
import static org.apache.maven.api.plugin.testing.MojoExtension.getBasedir;
41+
import static org.apache.maven.api.plugin.testing.MojoExtension.getVariableValueFromObject;
42+
import static org.junit.jupiter.api.Assertions.assertEquals;
43+
import static org.junit.jupiter.api.Assertions.assertNotNull;
44+
import static org.junit.jupiter.api.Assertions.assertTrue;
45+
import static org.junit.jupiter.api.Assertions.fail;
3746
import static org.mockito.Mockito.when;
3847

3948
/**
4049
* @author <a href="mailto:[email protected]">Allan Ramirez</a>
4150
*/
42-
public class DeployFileMojoTest extends AbstractMojoTestCase {
43-
private static final String LOCAL_REPO = getBasedir() + "/target/local-repo";
44-
45-
private List<String> expectedFiles;
46-
47-
private List<String> fileList;
51+
@MojoTest
52+
class DeployFileMojoTest {
4853

4954
private File remoteRepo;
5055

51-
private AutoCloseable openMocks;
52-
53-
@Mock
56+
@Inject
5457
private MavenSession session;
5558

56-
@InjectMocks
57-
private DeployFileMojo mojo;
58-
59-
public void setUp() throws Exception {
60-
super.setUp();
59+
@BeforeEach
60+
void setUp() throws Exception {
61+
DefaultRepositorySystemSession repositorySession = new DefaultRepositorySystemSession();
62+
repositorySession.setLocalRepositoryManager(
63+
new SimpleLocalRepositoryManagerFactory(new DefaultLocalPathComposer())
64+
.newInstance(repositorySession, new LocalRepository(getBasedir() + "/target/local-repo")));
65+
when(session.getRepositorySession()).thenReturn(repositorySession);
6166

6267
remoteRepo = new File(getBasedir(), "target/remote-repo");
6368

@@ -66,37 +71,19 @@ public void setUp() throws Exception {
6671
}
6772
}
6873

69-
@Override
70-
public void tearDown() throws Exception {
71-
super.tearDown();
72-
if (openMocks != null) {
73-
openMocks.close();
74-
}
75-
}
76-
77-
public void testDeployTestEnvironment() throws Exception {
78-
File testPom = new File(getBasedir(), "target/test-classes/unit/deploy-file-test/plugin-config.xml");
79-
80-
AbstractDeployMojo mojo = (AbstractDeployMojo) lookupMojo("deploy-file", testPom);
81-
74+
@Test
75+
@InjectMojo(goal = "deploy-file")
76+
void testDeployTestEnvironment(DeployFileMojo mojo) throws Exception {
8277
assertNotNull(mojo);
8378
}
8479

85-
public void testBasicDeployFile() throws Exception {
86-
File testPom = new File(getBasedir(), "target/test-classes/unit/deploy-file-test/plugin-config.xml");
87-
88-
mojo = (DeployFileMojo) lookupMojo("deploy-file", testPom);
89-
90-
openMocks = MockitoAnnotations.openMocks(this);
80+
@Test
81+
@InjectMojo(goal = "deploy-file", pom = "plugin-config.xml")
82+
@Basedir("/unit/deploy-file-test")
83+
void testBasicDeployFile(DeployFileMojo mojo) throws Exception {
9184

9285
assertNotNull(mojo);
9386

94-
DefaultRepositorySystemSession repositorySession = new DefaultRepositorySystemSession();
95-
repositorySession.setLocalRepositoryManager(
96-
new SimpleLocalRepositoryManagerFactory(new DefaultLocalPathComposer())
97-
.newInstance(repositorySession, new LocalRepository(LOCAL_REPO)));
98-
when(session.getRepositorySession()).thenReturn(repositorySession);
99-
10087
String groupId = (String) getVariableValueFromObject(mojo, "groupId");
10188

10289
String artifactId = (String) getVariableValueFromObject(mojo, "artifactId");
@@ -155,8 +142,8 @@ public void testBasicDeployFile() throws Exception {
155142
assertEquals("POM was created from deploy:deploy-file", model.getDescription());
156143

157144
// check the remote-repo
158-
expectedFiles = new ArrayList<>();
159-
fileList = new ArrayList<>();
145+
ArrayList<String> expectedFiles = new ArrayList<>();
146+
List<String> fileList = new ArrayList<>();
160147

161148
File repo = new File(remoteRepo, "deploy-file-test");
162149

@@ -187,21 +174,13 @@ public void testBasicDeployFile() throws Exception {
187174
assertEquals(0, getSizeOfExpectedFiles(fileList, expectedFiles));
188175
}
189176

190-
public void testDeployIfClassifierIsSet() throws Exception {
191-
File testPom = new File(getBasedir(), "target/test-classes/unit/deploy-file-classifier/plugin-config.xml");
192-
193-
mojo = (DeployFileMojo) lookupMojo("deploy-file", testPom);
194-
195-
openMocks = MockitoAnnotations.openMocks(this);
177+
@Test
178+
@InjectMojo(goal = "deploy-file", pom = "plugin-config.xml")
179+
@Basedir("/unit/deploy-file-classifier")
180+
void testDeployIfClassifierIsSet(DeployFileMojo mojo) throws Exception {
196181

197182
assertNotNull(mojo);
198183

199-
DefaultRepositorySystemSession repositorySession = new DefaultRepositorySystemSession();
200-
repositorySession.setLocalRepositoryManager(
201-
new SimpleLocalRepositoryManagerFactory(new DefaultLocalPathComposer())
202-
.newInstance(repositorySession, new LocalRepository(LOCAL_REPO)));
203-
when(session.getRepositorySession()).thenReturn(repositorySession);
204-
205184
String classifier = (String) getVariableValueFromObject(mojo, "classifier");
206185

207186
String groupId = (String) getVariableValueFromObject(mojo, "groupId");
@@ -237,22 +216,13 @@ public void testDeployIfClassifierIsSet() throws Exception {
237216
assertTrue(prodDeployedArtifact.exists());
238217
}
239218

240-
public void testDeployIfArtifactIsNotJar() throws Exception {
241-
File testPom =
242-
new File(getBasedir(), "target/test-classes/unit/deploy-file-artifact-not-jar/plugin-config.xml");
243-
244-
mojo = (DeployFileMojo) lookupMojo("deploy-file", testPom);
245-
246-
openMocks = MockitoAnnotations.openMocks(this);
219+
@Test
220+
@InjectMojo(goal = "deploy-file", pom = "plugin-config.xml")
221+
@Basedir("/unit/deploy-file-artifact-not-jar")
222+
void testDeployIfArtifactIsNotJar(DeployFileMojo mojo) throws Exception {
247223

248224
assertNotNull(mojo);
249225

250-
DefaultRepositorySystemSession repositorySession = new DefaultRepositorySystemSession();
251-
repositorySession.setLocalRepositoryManager(
252-
new SimpleLocalRepositoryManagerFactory(new DefaultLocalPathComposer())
253-
.newInstance(repositorySession, new LocalRepository(LOCAL_REPO)));
254-
when(session.getRepositorySession()).thenReturn(repositorySession);
255-
256226
String groupId = (String) getVariableValueFromObject(mojo, "groupId");
257227

258228
String artifactId = (String) getVariableValueFromObject(mojo, "artifactId");
@@ -276,20 +246,13 @@ public void testDeployIfArtifactIsNotJar() throws Exception {
276246
assertTrue(file.exists());
277247
}
278248

279-
public void testDeployFileIfPackagingIsSet() throws Exception {
280-
File testPom = new File(getBasedir(), "target/test-classes/unit/deploy-file-packaging/plugin-config.xml");
281-
mojo = (DeployFileMojo) lookupMojo("deploy-file", testPom);
282-
283-
openMocks = MockitoAnnotations.openMocks(this);
249+
@Test
250+
@InjectMojo(goal = "deploy-file", pom = "plugin-config.xml")
251+
@Basedir("/unit/deploy-file-packaging")
252+
void testDeployFileIfPackagingIsSet(DeployFileMojo mojo) throws Exception {
284253

285254
assertNotNull(mojo);
286255

287-
DefaultRepositorySystemSession repositorySession = new DefaultRepositorySystemSession();
288-
repositorySession.setLocalRepositoryManager(
289-
new SimpleLocalRepositoryManagerFactory(new DefaultLocalPathComposer())
290-
.newInstance(repositorySession, new LocalRepository(LOCAL_REPO)));
291-
when(session.getRepositorySession()).thenReturn(repositorySession);
292-
293256
String packaging = (String) getVariableValueFromObject(mojo, "packaging");
294257

295258
String groupId = (String) getVariableValueFromObject(mojo, "groupId");

0 commit comments

Comments
 (0)