|
18 | 18 | */ |
19 | 19 | package org.apache.maven.plugins.dependency; |
20 | 20 |
|
21 | | -import java.io.File; |
| 21 | +import javax.inject.Inject; |
22 | 22 |
|
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; |
24 | 27 | import org.apache.maven.model.Plugin; |
25 | 28 | import org.apache.maven.plugin.Mojo; |
26 | 29 | import org.apache.maven.plugin.MojoExecution; |
27 | | -import org.apache.maven.plugin.descriptor.MojoDescriptor; |
28 | | -import org.apache.maven.plugin.descriptor.PluginDescriptor; |
29 | 30 | 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; |
36 | 53 | import static org.mockito.Mockito.verify; |
| 54 | +import static org.mockito.Mockito.when; |
37 | 55 |
|
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 { |
45 | 59 |
|
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; |
53 | 62 |
|
54 | | - public void testSkipAnalyzeDepMgt() throws Exception { |
55 | | - doTest("analyze-dep-mgt"); |
56 | | - } |
| 63 | + @Mock |
| 64 | + private Log log; |
57 | 65 |
|
58 | | - public void testSkipAnalyzeOnly() throws Exception { |
59 | | - doTest("analyze-only"); |
| 66 | + @Provides |
| 67 | + private Log logProvides() { |
| 68 | + return log; |
60 | 69 | } |
61 | 70 |
|
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); |
64 | 76 | } |
65 | 77 |
|
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); |
68 | 83 | } |
69 | 84 |
|
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); |
72 | 90 | } |
73 | 91 |
|
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"); |
77 | 101 |
|
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")); |
80 | 106 | } |
81 | 107 |
|
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); |
84 | 113 | } |
85 | 114 |
|
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); |
88 | 120 | } |
89 | 121 |
|
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); |
92 | 127 | } |
93 | 128 |
|
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); |
96 | 134 | } |
97 | 135 |
|
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); |
100 | 141 | } |
101 | 142 |
|
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); |
104 | 148 | } |
105 | 149 |
|
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); |
108 | 155 | } |
109 | 156 |
|
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); |
112 | 162 | } |
113 | 163 |
|
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); |
116 | 169 | } |
117 | 170 |
|
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); |
120 | 176 | } |
121 | 177 |
|
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); |
124 | 183 | } |
125 | 184 |
|
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); |
128 | 190 | } |
129 | 191 |
|
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); |
132 | 197 | } |
133 | 198 |
|
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); |
136 | 204 | } |
137 | 205 |
|
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); |
140 | 211 | } |
141 | 212 |
|
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 { |
152 | 214 | 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")); |
181 | 216 | } |
182 | 217 | } |
0 commit comments