1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.plugin;
20
21 import javax.inject.Inject;
22
23 import java.util.List;
24
25 import org.apache.maven.AbstractCoreMavenComponentTestCase;
26 import org.apache.maven.artifact.Artifact;
27 import org.apache.maven.artifact.repository.DefaultRepositoryRequest;
28 import org.apache.maven.artifact.repository.RepositoryRequest;
29 import org.apache.maven.execution.MavenSession;
30 import org.apache.maven.model.Plugin;
31 import org.apache.maven.plugin.descriptor.MojoDescriptor;
32 import org.apache.maven.plugin.descriptor.PluginDescriptor;
33 import org.apache.maven.project.MavenProject;
34 import org.codehaus.plexus.classworlds.realm.ClassRealm;
35 import org.codehaus.plexus.component.repository.ComponentDescriptor;
36 import org.junit.jupiter.api.Test;
37
38 import static org.junit.jupiter.api.Assertions.assertEquals;
39 import static org.junit.jupiter.api.Assertions.assertNotNull;
40 import static org.junit.jupiter.api.Assertions.fail;
41
42 class PluginManagerTest extends AbstractCoreMavenComponentTestCase {
43 @Inject
44 private DefaultBuildPluginManager pluginManager;
45
46 protected String getProjectsDirectory() {
47 return "src/test/projects/plugin-manager";
48 }
49
50 @Test
51 void testPluginLoading() throws Exception {
52 MavenSession session = createMavenSession(null);
53 Plugin plugin = new Plugin();
54 plugin.setGroupId("org.apache.maven.its.plugins");
55 plugin.setArtifactId("maven-it-plugin");
56 plugin.setVersion("0.1");
57 PluginDescriptor pluginDescriptor = pluginManager.loadPlugin(
58 plugin, session.getCurrentProject().getRemotePluginRepositories(), session.getRepositorySession());
59 assertNotNull(pluginDescriptor);
60 }
61
62 @Test
63 void testMojoDescriptorRetrieval() throws Exception {
64 MavenSession session = createMavenSession(null);
65 String goal = "it";
66 Plugin plugin = new Plugin();
67 plugin.setGroupId("org.apache.maven.its.plugins");
68 plugin.setArtifactId("maven-it-plugin");
69 plugin.setVersion("0.1");
70
71 MojoDescriptor mojoDescriptor = pluginManager.getMojoDescriptor(
72 plugin,
73 goal,
74 session.getCurrentProject().getRemotePluginRepositories(),
75 session.getRepositorySession());
76 assertNotNull(mojoDescriptor);
77 assertEquals(goal, mojoDescriptor.getGoal());
78
79
80
81 PluginDescriptor pluginDescriptor = mojoDescriptor.getPluginDescriptor();
82 assertNotNull(pluginDescriptor);
83 assertEquals("org.apache.maven.its.plugins", pluginDescriptor.getGroupId());
84 assertEquals("maven-it-plugin", pluginDescriptor.getArtifactId());
85 assertEquals("0.1", pluginDescriptor.getVersion());
86 }
87
88
89
90
91
92
93
94
95
96
97
98
99 @Test
100 void testRemoteResourcesPlugin() throws Exception {
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124 }
125
126
127 public void testSurefirePlugin() throws Exception {
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147 }
148
149 @Test
150 void testMojoConfigurationIsMergedCorrectly() throws Exception {}
151
152
153
154
155
156
157 @Test
158 void testMojoWhereInternallyStatedDependencyIsOverriddenByProject() throws Exception {}
159
160
161
162
163
164 @Test
165 void testMojoThatIsPresentInTheCurrentBuild() throws Exception {}
166
167
168
169
170
171 @Test
172 void testAggregatorMojo() throws Exception {}
173
174
175
176
177
178 @Test
179 void testMojoThatRequiresExecutionToAGivenPhaseBeforeExecutingItself() throws Exception {}
180
181
182
183
184
185
186
187 @Test
188 void testThatPluginDependencyThatHasSystemScopeIsResolved() throws Exception {
189 MavenSession session = createMavenSession(getProject("project-contributing-system-scope-plugin-dep"));
190 MavenProject project = session.getCurrentProject();
191 Plugin plugin = project.getPlugin("org.apache.maven.its.plugins:maven-it-plugin");
192
193 RepositoryRequest repositoryRequest = new DefaultRepositoryRequest();
194 repositoryRequest.setLocalRepository(getLocalRepository());
195 repositoryRequest.setRemoteRepositories(getPluginArtifactRepositories());
196
197 PluginDescriptor pluginDescriptor = pluginManager.loadPlugin(
198 plugin, session.getCurrentProject().getRemotePluginRepositories(), session.getRepositorySession());
199 pluginManager.getPluginRealm(session, pluginDescriptor);
200 List<Artifact> artifacts = pluginDescriptor.getArtifacts();
201
202 for (Artifact a : artifacts) {
203 if (a.getGroupId().equals("org.apache.maven.its.mng3586")
204 && a.getArtifactId().equals("tools")) {
205
206 return;
207 }
208 }
209
210 fail("Can't find the system scoped dependency in the plugin artifacts.");
211 }
212
213
214
215
216
217 protected void assertPluginDescriptor(
218 MojoDescriptor mojoDescriptor, String groupId, String artifactId, String version) {
219 assertNotNull(mojoDescriptor);
220 PluginDescriptor pd = mojoDescriptor.getPluginDescriptor();
221 assertNotNull(pd);
222 assertEquals(groupId, pd.getGroupId());
223 assertEquals(artifactId, pd.getArtifactId());
224 assertEquals(version, pd.getVersion());
225 }
226
227 @Test
228 void testPluginRealmCache() throws Exception {
229 RepositoryRequest repositoryRequest = new DefaultRepositoryRequest();
230 repositoryRequest.setLocalRepository(getLocalRepository());
231 repositoryRequest.setRemoteRepositories(getPluginArtifactRepositories());
232
233
234 MavenSession session = createMavenSession(getProject("project-contributing-system-scope-plugin-dep"));
235 MavenProject project = session.getCurrentProject();
236 Plugin plugin = project.getPlugin("org.apache.maven.its.plugins:maven-it-plugin");
237
238 PluginDescriptor pluginDescriptor = pluginManager.loadPlugin(
239 plugin, session.getCurrentProject().getRemotePluginRepositories(), session.getRepositorySession());
240 pluginManager.getPluginRealm(session, pluginDescriptor);
241
242 assertEquals(1, pluginDescriptor.getDependencies().size());
243
244 for (ComponentDescriptor<?> descriptor : pluginDescriptor.getComponents()) {
245 assertNotNull(descriptor.getRealm());
246 assertNotNull(descriptor.getImplementationClass());
247 }
248
249
250 session = createMavenSession(getProject("project-contributing-system-scope-plugin-dep"));
251 project = session.getCurrentProject();
252 plugin = project.getPlugin("org.apache.maven.its.plugins:maven-it-plugin");
253
254 pluginDescriptor = pluginManager.loadPlugin(
255 plugin, session.getCurrentProject().getRemotePluginRepositories(), session.getRepositorySession());
256 pluginManager.getPluginRealm(session, pluginDescriptor);
257
258 assertEquals(1, pluginDescriptor.getDependencies().size());
259
260 for (ComponentDescriptor<?> descriptor : pluginDescriptor.getComponents()) {
261 assertNotNull(descriptor.getRealm());
262 assertNotNull(descriptor.getImplementationClass());
263 }
264 }
265
266 @Test
267 void testBuildExtensionsPluginLoading() throws Exception {
268 RepositoryRequest repositoryRequest = new DefaultRepositoryRequest();
269 repositoryRequest.setLocalRepository(getLocalRepository());
270 repositoryRequest.setRemoteRepositories(getPluginArtifactRepositories());
271
272
273 MavenSession session = createMavenSession(getProject("project-with-build-extensions-plugin"));
274 MavenProject project = session.getCurrentProject();
275 Plugin plugin = project.getPlugin("org.apache.maven.its.plugins:maven-it-plugin");
276
277 PluginDescriptor pluginDescriptor = pluginManager.loadPlugin(
278 plugin, session.getCurrentProject().getRemotePluginRepositories(), session.getRepositorySession());
279 ClassRealm pluginRealm = pluginManager.getPluginRealm(session, pluginDescriptor);
280
281 assertEquals(pluginRealm, pluginDescriptor.getComponents().get(0).getRealm());
282 }
283 }