Skip to content

Commit 206c8cd

Browse files
Migration to JUnit 5
1 parent 59b868a commit 206c8cd

File tree

11 files changed

+197
-359
lines changed

11 files changed

+197
-359
lines changed

src/test/java/org/apache/maven/plugins/dependency/TestSkip.java

Lines changed: 143 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -18,165 +18,200 @@
1818
*/
1919
package org.apache.maven.plugins.dependency;
2020

21-
import java.io.File;
21+
import javax.inject.Inject;
2222

23-
import org.apache.maven.execution.MavenSession;
23+
import org.apache.maven.api.di.Provides;
24+
import org.apache.maven.api.plugin.testing.InjectMojo;
25+
import org.apache.maven.api.plugin.testing.MojoParameter;
26+
import org.apache.maven.api.plugin.testing.MojoTest;
2427
import org.apache.maven.model.Plugin;
2528
import org.apache.maven.plugin.Mojo;
2629
import org.apache.maven.plugin.MojoExecution;
27-
import org.apache.maven.plugin.descriptor.MojoDescriptor;
28-
import org.apache.maven.plugin.descriptor.PluginDescriptor;
2930
import org.apache.maven.plugin.logging.Log;
30-
import org.apache.maven.plugins.dependency.testUtils.stubs.DependencyProjectStub;
31-
import org.apache.maven.project.MavenProject;
32-
import org.mockito.ArgumentCaptor;
33-
34-
import static org.mockito.Mockito.atLeastOnce;
35-
import static org.mockito.Mockito.mock;
31+
import org.apache.maven.plugins.dependency.analyze.AnalyzeDepMgt;
32+
import org.apache.maven.plugins.dependency.analyze.AnalyzeDuplicateMojo;
33+
import org.apache.maven.plugins.dependency.analyze.AnalyzeMojo;
34+
import org.apache.maven.plugins.dependency.analyze.AnalyzeOnlyMojo;
35+
import org.apache.maven.plugins.dependency.analyze.AnalyzeReport;
36+
import org.apache.maven.plugins.dependency.fromConfiguration.CopyMojo;
37+
import org.apache.maven.plugins.dependency.fromConfiguration.UnpackMojo;
38+
import org.apache.maven.plugins.dependency.fromDependencies.BuildClasspathMojo;
39+
import org.apache.maven.plugins.dependency.fromDependencies.CopyDependenciesMojo;
40+
import org.apache.maven.plugins.dependency.fromDependencies.UnpackDependenciesMojo;
41+
import org.apache.maven.plugins.dependency.resolvers.GoOfflineMojo;
42+
import org.apache.maven.plugins.dependency.resolvers.ListMojo;
43+
import org.apache.maven.plugins.dependency.resolvers.OldResolveDependencySourcesMojo;
44+
import org.apache.maven.plugins.dependency.resolvers.ResolveDependenciesMojo;
45+
import org.apache.maven.plugins.dependency.resolvers.ResolvePluginsMojo;
46+
import org.apache.maven.plugins.dependency.tree.TreeMojo;
47+
import org.junit.jupiter.api.Test;
48+
import org.junit.jupiter.api.extension.ExtendWith;
49+
import org.mockito.Mock;
50+
import org.mockito.junit.jupiter.MockitoExtension;
51+
52+
import static org.mockito.ArgumentMatchers.contains;
3653
import static org.mockito.Mockito.verify;
54+
import static org.mockito.Mockito.when;
3755

38-
public class TestSkip extends AbstractDependencyMojoTestCase {
39-
40-
@Override
41-
protected void setUp() throws Exception {
42-
super.setUp();
43-
MavenProject project = new DependencyProjectStub();
44-
getContainer().addComponent(project, MavenProject.class.getName());
56+
@ExtendWith(MockitoExtension.class)
57+
@MojoTest
58+
class TestSkip {
4559

46-
MavenSession session = newMavenSession(project);
47-
getContainer().addComponent(session, MavenSession.class.getName());
48-
}
49-
50-
public void testSkipAnalyze() throws Exception {
51-
doTest("analyze");
52-
}
60+
@Inject
61+
private MojoExecution mojoExecution;
5362

54-
public void testSkipAnalyzeDepMgt() throws Exception {
55-
doTest("analyze-dep-mgt");
56-
}
63+
@Mock
64+
private Log log;
5765

58-
public void testSkipAnalyzeOnly() throws Exception {
59-
doTest("analyze-only");
66+
@Provides
67+
private Log logProvides() {
68+
return log;
6069
}
6170

62-
public void testSkipAnalyzeReport() throws Exception {
63-
doSpecialTest("analyze-report", true);
71+
@Test
72+
@InjectMojo(goal = "analyze")
73+
@MojoParameter(name = "skip", value = "true")
74+
void testSkipAnalyze(AnalyzeMojo mojo) throws Exception {
75+
doTest(mojo);
6476
}
6577

66-
public void testSkipAnalyzeDuplicate() throws Exception {
67-
doTest("analyze-duplicate");
78+
@Test
79+
@InjectMojo(goal = "analyze-dep-mgt")
80+
@MojoParameter(name = "skip", value = "true")
81+
void testSkipAnalyzeDepMgt(AnalyzeDepMgt mojo) throws Exception {
82+
doTest(mojo);
6883
}
6984

70-
public void testSkipBuildClasspath() throws Exception {
71-
doTest("build-classpath");
85+
@Test
86+
@InjectMojo(goal = "analyze-only")
87+
@MojoParameter(name = "skip", value = "true")
88+
void testSkipAnalyzeOnly(AnalyzeOnlyMojo mojo) throws Exception {
89+
doTest(mojo);
7290
}
7391

74-
public void testSkipCopy() throws Exception {
75-
doTest("copy");
76-
}
92+
@Test
93+
@InjectMojo(goal = "analyze-report")
94+
@MojoParameter(name = "skip", value = "true")
95+
void testSkipAnalyzeReport(AnalyzeReport mojo) throws Exception {
96+
Plugin plugin = new Plugin();
97+
plugin.setArtifactId("maven-dependency-plugin");
98+
plugin.setVersion("1.0.0");
99+
when(mojoExecution.getPlugin()).thenReturn(plugin);
100+
when(mojoExecution.getGoal()).thenReturn("analyze-report");
77101

78-
public void testSkipCopyDependencies() throws Exception {
79-
doTest("copy-dependencies");
102+
mojo.execute();
103+
verify(log)
104+
.info(contains(
105+
"Skipping org.apache.maven.plugins:maven-dependency-plugin:1.0.0:analyze-report report goal"));
80106
}
81107

82-
public void testSkipGet() throws Exception {
83-
doSpecialTest("get");
108+
@Test
109+
@InjectMojo(goal = "analyze-duplicate")
110+
@MojoParameter(name = "skip", value = "true")
111+
void testSkipAnalyzeDuplicate(AnalyzeDuplicateMojo mojo) throws Exception {
112+
doTest(mojo);
84113
}
85114

86-
public void testSkipGoOffline() throws Exception {
87-
doTest("go-offline");
115+
@Test
116+
@InjectMojo(goal = "build-classpath")
117+
@MojoParameter(name = "skip", value = "true")
118+
void testSkipBuildClasspath(BuildClasspathMojo mojo) throws Exception {
119+
doTest(mojo);
88120
}
89121

90-
public void testSkipList() throws Exception {
91-
doTest("list");
122+
@Test
123+
@InjectMojo(goal = "copy")
124+
@MojoParameter(name = "skip", value = "true")
125+
void testSkipCopy(CopyMojo mojo) throws Exception {
126+
doTest(mojo);
92127
}
93128

94-
public void testSkipProperties() throws Exception {
95-
doTest("properties");
129+
@Test
130+
@InjectMojo(goal = "copy-dependencies")
131+
@MojoParameter(name = "skip", value = "true")
132+
void testSkipCopyDependencies(CopyDependenciesMojo mojo) throws Exception {
133+
doTest(mojo);
96134
}
97135

98-
public void testSkipPurgeLocalRepository() throws Exception {
99-
doSpecialTest("purge-local-repository");
136+
@Test
137+
@InjectMojo(goal = "get")
138+
@MojoParameter(name = "skip", value = "true")
139+
void testSkipGet(GetMojo mojo) throws Exception {
140+
doTest(mojo);
100141
}
101142

102-
public void testSkipResolve() throws Exception {
103-
doTest("resolve");
143+
@Test
144+
@InjectMojo(goal = "go-offline")
145+
@MojoParameter(name = "skip", value = "true")
146+
void testSkipGoOffline(GoOfflineMojo mojo) throws Exception {
147+
doTest(mojo);
104148
}
105149

106-
public void testSkipResolvePlugins() throws Exception {
107-
doTest("resolve-plugins");
150+
@Test
151+
@InjectMojo(goal = "list")
152+
@MojoParameter(name = "skip", value = "true")
153+
void testSkipList(ListMojo mojo) throws Exception {
154+
doTest(mojo);
108155
}
109156

110-
public void testSkipSources() throws Exception {
111-
doTest("sources");
157+
@Test
158+
@InjectMojo(goal = "properties")
159+
@MojoParameter(name = "skip", value = "true")
160+
void testSkipProperties(PropertiesMojo mojo) throws Exception {
161+
doTest(mojo);
112162
}
113163

114-
public void testSkipTree() throws Exception {
115-
doTest("tree");
164+
@Test
165+
@InjectMojo(goal = "purge-local-repository")
166+
@MojoParameter(name = "skip", value = "true")
167+
void testSkipPurgeLocalRepository(PurgeLocalRepositoryMojo mojo) throws Exception {
168+
doTest(mojo);
116169
}
117170

118-
public void testSkipUnpack() throws Exception {
119-
doTest("unpack");
171+
@Test
172+
@InjectMojo(goal = "resolve")
173+
@MojoParameter(name = "skip", value = "true")
174+
void testSkipResolve(ResolveDependenciesMojo mojo) throws Exception {
175+
doTest(mojo);
120176
}
121177

122-
public void testSkipUnpackDependencies() throws Exception {
123-
doTest("unpack-dependencies");
178+
@Test
179+
@InjectMojo(goal = "resolve-plugins")
180+
@MojoParameter(name = "skip", value = "true")
181+
void testSkipResolvePlugins(ResolvePluginsMojo mojo) throws Exception {
182+
doTest(mojo);
124183
}
125184

126-
protected void doTest(String mojoName) throws Exception {
127-
doConfigTest(mojoName, "plugin-config.xml");
185+
@Test
186+
@InjectMojo(goal = "sources")
187+
@MojoParameter(name = "skip", value = "true")
188+
void testSkipSources(OldResolveDependencySourcesMojo mojo) throws Exception {
189+
doTest(mojo);
128190
}
129191

130-
protected void doSpecialTest(String mojoName) throws Exception {
131-
doConfigTest(mojoName, "plugin-" + mojoName + "-config.xml", false);
192+
@Test
193+
@InjectMojo(goal = "tree")
194+
@MojoParameter(name = "skip", value = "true")
195+
void testSkipTree(TreeMojo mojo) throws Exception {
196+
doTest(mojo);
132197
}
133198

134-
protected void doSpecialTest(String mojoName, boolean addMojoExecution) throws Exception {
135-
doConfigTest(mojoName, "plugin-" + mojoName + "-config.xml", addMojoExecution);
199+
@Test
200+
@InjectMojo(goal = "unpack")
201+
@MojoParameter(name = "skip", value = "true")
202+
void testSkipUnpack(UnpackMojo mojo) throws Exception {
203+
doTest(mojo);
136204
}
137205

138-
private void doConfigTest(String mojoName, String configFile) throws Exception {
139-
doConfigTest(mojoName, configFile, false);
206+
@Test
207+
@InjectMojo(goal = "unpack-dependencies")
208+
@MojoParameter(name = "skip", value = "true")
209+
void testSkipUnpackDependencies(UnpackDependenciesMojo mojo) throws Exception {
210+
doTest(mojo);
140211
}
141212

142-
private void doConfigTest(String mojoName, String configFile, boolean addMojoExecution) throws Exception {
143-
File testPom = new File(getBasedir(), "target/test-classes/unit/skip-test/" + configFile);
144-
Mojo mojo = lookupMojo(mojoName, testPom);
145-
assertNotNull("Mojo not found.", mojo);
146-
147-
if (addMojoExecution) {
148-
setVariableValueToObject(mojo, "mojoExecution", getMockMojoExecution(mojoName));
149-
}
150-
Log log = mock(Log.class);
151-
mojo.setLog(log);
213+
private void doTest(Mojo mojo) throws Exception {
152214
mojo.execute();
153-
154-
ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
155-
verify(log, atLeastOnce()).info(captor.capture());
156-
String skipMessage;
157-
if (addMojoExecution) {
158-
MojoExecution me = getMockMojoExecution(mojoName);
159-
String reportMojoInfo = me.getPlugin().getId() + ":" + me.getGoal();
160-
skipMessage = "Skipping " + reportMojoInfo + " report goal";
161-
} else {
162-
skipMessage = "Skipping plugin execution";
163-
}
164-
assertTrue(captor.getValue().contains(skipMessage));
165-
}
166-
167-
private MojoExecution getMockMojoExecution(String goal) {
168-
MojoDescriptor md = new MojoDescriptor();
169-
md.setGoal(goal);
170-
171-
MojoExecution me = new MojoExecution(md);
172-
173-
PluginDescriptor pd = new PluginDescriptor();
174-
Plugin p = new Plugin();
175-
p.setGroupId("org.apache.maven.plugins");
176-
p.setArtifactId("maven-dependency-plugin");
177-
pd.setPlugin(p);
178-
md.setPluginDescriptor(pd);
179-
180-
return me;
215+
verify(log).info(contains("Skipping plugin"));
181216
}
182217
}

src/test/java/org/apache/maven/plugins/dependency/exclusion/ExclusionCheckerTest.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,23 @@
2323
import java.util.HashSet;
2424
import java.util.Set;
2525

26-
import org.junit.Before;
27-
import org.junit.Test;
26+
import org.junit.jupiter.api.BeforeEach;
27+
import org.junit.jupiter.api.Test;
2828

2929
import static org.apache.maven.plugins.dependency.exclusion.Coordinates.coordinates;
3030
import static org.assertj.core.api.Assertions.assertThat;
3131

32-
public class ExclusionCheckerTest {
32+
class ExclusionCheckerTest {
3333

3434
private ExclusionChecker checker;
3535

36-
@Before
37-
public void setUp() throws Exception {
36+
@BeforeEach
37+
void setUp() throws Exception {
3838
checker = new ExclusionChecker();
3939
}
4040

4141
@Test
42-
public void shallReportInvalidExclusions() {
42+
void shallReportInvalidExclusions() {
4343
Coordinates artifact = coordinates("com.current", "artifact");
4444
Set<Coordinates> excludes = new HashSet<>(Arrays.asList(
4545
coordinates("com.example", "one"),
@@ -59,21 +59,21 @@ public void shallReportInvalidExclusions() {
5959
}
6060

6161
@Test
62-
public void noViolationsWhenEmptyExclusions() {
62+
void noViolationsWhenEmptyExclusions() {
6363
checker.check(coordinates("a", "b"), new HashSet<>(), new HashSet<>());
6464
assertThat(checker.getViolations()).isEmpty();
6565
}
6666

6767
@Test
68-
public void shallReportInvalidExclusionsWhenNoDependencies() {
68+
void shallReportInvalidExclusionsWhenNoDependencies() {
6969
Coordinates artifact = coordinates("a", "b");
7070
HashSet<Coordinates> actualDependencies = new HashSet<>();
7171
checker.check(artifact, new HashSet<>(Collections.singletonList(coordinates("p", "m"))), actualDependencies);
7272
assertThat(checker.getViolations()).containsEntry(artifact, Collections.singletonList(coordinates("p", "m")));
7373
}
7474

7575
@Test
76-
public void shallHandleWildcardExclusions() {
76+
void shallHandleWildcardExclusions() {
7777
Coordinates artifact = coordinates("com.current", "artifact");
7878
Set<Coordinates> excludes = new HashSet<>(Collections.singletonList(coordinates("*", "*")));
7979

@@ -86,7 +86,7 @@ public void shallHandleWildcardExclusions() {
8686
}
8787

8888
@Test
89-
public void shallHandleWildcardGroupIdExclusion() {
89+
void shallHandleWildcardGroupIdExclusion() {
9090
Coordinates artifact = coordinates("com.current", "artifact");
9191
Set<Coordinates> excludes = new HashSet<>(Collections.singletonList(coordinates("javax", "*")));
9292

0 commit comments

Comments
 (0)