File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\maven-plugin\src\test\java\hudson\maven\MavenModuleTest.java.html

1 package hudson.maven;
2
3 import static org.mockito.Mockito.doCallRealMethod;
4 import static org.mockito.Mockito.mock;
5 import static org.mockito.Mockito.when;
6 import static org.powermock.api.support.membermodification.MemberMatcher.constructor;
7 import static org.powermock.api.support.membermodification.MemberModifier.suppress;
8 import hudson.maven.MavenModuleSet.DescriptorImpl;
9 import hudson.model.AbstractProject;
10 import hudson.model.DependencyGraph;
11 import hudson.model.MockHelper;
12
13 import java.util.Collection;
14 import java.util.Collections;
15 import java.util.List;
16
17 import junit.framework.Assert;
18
19 import org.apache.maven.model.Build;
20 import org.apache.maven.model.Dependency;
21 import org.apache.maven.model.Plugin;
22 import org.apache.maven.project.MavenProject;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.junit.runner.RunWith;
26 import org.jvnet.hudson.test.Bug;
27 import org.mockito.Matchers;
28 import org.powermock.core.classloader.annotations.PrepareForTest;
29 import org.powermock.modules.junit4.PowerMockRunner;
30
31 import com.google.common.collect.Lists;
32
33 @RunWith(PowerMockRunner.class)
34 @PrepareForTest( { MavenModuleSet.class, DescriptorImpl.class, AbstractProject.class})
35 public class MavenModuleTest {
36
37 private MavenModule module;
38
39 private MavenProject project;
40
41 @Before
42 public void before() {
43 suppress(constructor(AbstractProject.class));
44 suppress(constructor(DescriptorImpl.class));
45
46 this.module = mock(MavenModule.class);
47 basicMocking(this.module);
48
49 this.project = new MavenProject();
50 project.setGroupId("test");
51 project.setArtifactId("testmodule");
52 project.setVersion("2.0-SNAPSHOT");
53 project.setPackaging("jar");
54
55 this.module.reconfigure(new PomInfo(project, null, "relPath"));
56 this.module.doSetName("test$testmodule");
57 }
58
59 /**
60 * Tests that a {@link MavenModule} which builds a plugin is recognized as a snapshot
61 * dependency in another module using that plugin.
62 */
63 @Test
64 @Bug(10530)
65 public void testMavenModuleAsPluginDependency() {
66 MavenModule pluginModule = createPluginProject();
67
68 addModuleAsPluginDependency(this.module, pluginModule);
69
70 when(this.module.getAllMavenModules()).thenReturn(Lists.newArrayList(this.module, pluginModule));
71
72 DependencyGraph graph = MockHelper.mockDependencyGraph(
73 Lists.<AbstractProject<?,?>>newArrayList(this.module, pluginModule));
74 graph.build();
75
76 @SuppressWarnings("rawtypes")
77 List<AbstractProject> downstream = graph.getDownstream(pluginModule);
78 Assert.assertEquals(1, downstream.size());
79 Assert.assertSame(this.module, downstream.get(0));
80 }
81
82 private static void addModuleAsPluginDependency(MavenModule module, MavenModule pluginModule) {
83 Build build = new Build();
84 Plugin plugin = new Plugin();
85 plugin.setGroupId(pluginModule.getModuleName().groupId);
86 plugin.setArtifactId(pluginModule.getModuleName().artifactId);
87 plugin.setVersion(pluginModule.getVersion());
88 build.setPlugins(Collections.singletonList(plugin));
89
90 MavenProject project = new MavenProject();
91 project.setGroupId(module.getModuleName().groupId);
92 project.setArtifactId(module.getModuleName().artifactId);
93 project.setVersion(module.getVersion());
94 project.setPackaging("jar");
95 project.setBuild(build);
96
97 module.reconfigure(new PomInfo(project, null, "relPath"));
98 }
99
100 private static MavenModule createPluginProject() {
101 MavenModule pluginModule = mock(MavenModule.class);
102 basicMocking(pluginModule);
103
104 MavenProject proj = new MavenProject();
105 proj.setGroupId("test");
106 proj.setArtifactId("pluginmodule");
107 proj.setVersion("1.0-SNAPSHOT");
108 proj.setPackaging("maven-plugin");
109 PomInfo info = new PomInfo(proj, null, "relPath");
110 pluginModule.reconfigure(info);
111 pluginModule.doSetName("test$pluginmodule");
112
113 return pluginModule;
114 }
115
116 private static void basicMocking(MavenModule mock) {
117 when(mock.isBuildable()).thenReturn(Boolean.TRUE);
118 doCallRealMethod().when(mock).reconfigure(Matchers.any(PomInfo.class));
119 doCallRealMethod().when(mock).buildDependencyGraph(Matchers.any(DependencyGraph.class));
120 when(mock.asDependency()).thenCallRealMethod();
121 doCallRealMethod().when(mock).doSetName(Matchers.anyString());
122 when(mock.getModuleName()).thenCallRealMethod();
123 when(mock.getVersion()).thenCallRealMethod();
124
125 MavenModuleSet parent = mock(MavenModuleSet.class);
126 when(parent.isAggregatorStyleBuild()).thenReturn(Boolean.FALSE);
127 when(mock.getParent()).thenReturn(parent);
128
129 when(parent.getModules()).thenReturn(Collections.singleton(mock));
130 }
131 /**
132 * This test is a standard project that has a versioned dependency.
133 */
134 @Test
135 public void testSimpleVersion() {
136 TestComponents testComponents = createTestComponents("1.0.1-SNAPSHOT");
137
138 DependencyGraph graph = testComponents.graph;
139 MavenModule appMavenModule = testComponents.applicationMavenModule;
140 MavenModule libMavenModule = testComponents.libraryMavenModule;
141
142 graph.build();
143
144 List<AbstractProject> appDownstream = graph.getDownstream(appMavenModule);
145 List<AbstractProject> appUpstream = graph.getUpstream(appMavenModule);
146 List<AbstractProject> libDownstream = graph.getDownstream(libMavenModule);
147 List<AbstractProject> libUpstream = graph.getUpstream(libMavenModule);
148
149 Assert.assertEquals(0, appDownstream.size());
150 Assert.assertEquals(1, appUpstream.size());
151 Assert.assertEquals(1, libDownstream.size());
152 Assert.assertEquals(0, libUpstream.size());
153 }
154
155 /**
156 * This tests that a version range declaration in the dependency of a top level project
157 * resolves the up and downstream correctly.
158 */
159 @Test
160 public void testSimpleVersionRange() {
161 TestComponents testComponents = createTestComponents("[1.0.0, )");
162
163 DependencyGraph graph = testComponents.graph;
164 MavenModule appMavenModule = testComponents.applicationMavenModule;
165 MavenModule libMavenModule = testComponents.libraryMavenModule;
166
167 graph.build();
168
169 List<AbstractProject> appDownstream = graph.getDownstream(appMavenModule);
170 List<AbstractProject> appUpstream = graph.getUpstream(appMavenModule);
171 List<AbstractProject> libDownstream = graph.getDownstream(libMavenModule);
172 List<AbstractProject> libUpstream = graph.getUpstream(libMavenModule);
173
174 Assert.assertEquals(0, appDownstream.size());
175 Assert.assertEquals(1, appUpstream.size());
176 Assert.assertEquals(1, libDownstream.size());
177 Assert.assertEquals(0, libUpstream.size());
178 }
179
180 /**
181 * Test multiple projects with dependencies on differing library versions declared with
182 * multiple version definitions.
183 */
184 @Test
185 public void testMultipleDependencies() {
186
187 MavenProject projectA = createMavenProject("ProjectA", "test", "projectA", "1.0-SNAPSHOT", "jar");
188 Dependency dependencyA = createDependency("test", "library", "[1.0, 2.0)");
189 projectA.getDependencies().add(dependencyA);
190
191 MavenProject projectB = createMavenProject("ProjectB", "test", "projectB", "2.0-SNAPSHOT", "jar");
192 Dependency dependencyB = createDependency("test", "library", "[1.1, 2.1]");
193 projectB.getDependencies().add(dependencyB);
194
195 MavenProject dependX = createMavenProject("DependX-1.1", "test", "library", "1.1.3-SNAPSHOT", "jar");
196 MavenProject dependY = createMavenProject("DependX-1.2", "test", "library", "1.2.1-SNAPSHOT", "jar");
197 MavenProject dependZ = createMavenProject("DependX-2.0", "test", "library", "2.0.1-SNAPSHOT", "jar");
198
199 MavenModuleSet parent = mock(MavenModuleSet.class);
200 when(parent.isAggregatorStyleBuild()).thenReturn(Boolean.FALSE);
201
202 //Now create maven modules for all the projects
203 MavenModule mavenModuleA = mockMavenModule(projectA);
204 MavenModule mavenModuleB = mockMavenModule(projectB);
205 MavenModule mavenModuleX = mockMavenModule(dependX);
206 MavenModule mavenModuleY = mockMavenModule(dependY);
207 MavenModule mavenModuleZ = mockMavenModule(dependZ);
208
209 Collection<AbstractProject<?,?>> allModules = Lists.<AbstractProject<?,?>>newArrayList(mavenModuleA,
210 mavenModuleB, mavenModuleX, mavenModuleY, mavenModuleZ);
211
212 for (AbstractProject<?, ?> module : allModules) {
213 MavenModule mm = (MavenModule) module;
214 enhanceMavenModuleMock(mm, parent, allModules);
215 }
216
217 DependencyGraph graph = MockHelper.mockDependencyGraph(allModules);
218 doCallRealMethod().when(graph).getDownstream(Matchers.any(AbstractProject.class));
219 doCallRealMethod().when(graph).getUpstream(Matchers.any(AbstractProject.class));
220 doCallRealMethod().when(graph).compare(Matchers.<AbstractProject>any(), Matchers.<AbstractProject>any());
221 graph.build();
222
223 List<AbstractProject> downstreamA = graph.getDownstream(mavenModuleA);
224 List<AbstractProject> upstreamA = graph.getUpstream(mavenModuleA);
225
226 Assert.assertEquals(0, downstreamA.size());
227 Assert.assertEquals(1, upstreamA.size());
228 Assert.assertSame(dependY.getVersion(), ((MavenModule) upstreamA.get(0)).getVersion());
229
230 List<AbstractProject> downstreamB = graph.getDownstream(mavenModuleB);
231 List<AbstractProject> upstreamB = graph.getUpstream(mavenModuleB);
232
233 Assert.assertEquals(0, downstreamB.size());
234 Assert.assertEquals(1, upstreamA.size());
235 Assert.assertSame(dependZ.getVersion(), ((MavenModule) upstreamB.get(0)).getVersion());
236 }
237
238 /**
239 * This tests a project that has a dependency on a specific version of X.
240 * The project X has moved on and so should not have any dependencies on ProjectA.
241 */
242 @Test
243 public void testProjectWithSpecifiedVersionAndNoDependencies() {
244 MavenProject projectA = createMavenProject("ProjectA", "test", "projectA", "1.0-SNAPSHOT", "jar");
245 Dependency dependencyA = createDependency("test", "library", "1.0");
246 projectA.getDependencies().add(dependencyA);
247
248 MavenProject dependX = createMavenProject("DependX-1.1", "test", "library", "1.2-SNAPSHOT", "jar");
249
250 MavenModuleSet parent = mock(MavenModuleSet.class);
251 when(parent.isAggregatorStyleBuild()).thenReturn(Boolean.FALSE);
252
253 //Now create maven modules for all the projects
254 MavenModule mavenModuleA = mockMavenModule(projectA);
255 MavenModule mavenModuleX = mockMavenModule(dependX);
256
257 Collection<AbstractProject<?,?>> allModules = Lists.<AbstractProject<?,?>>newArrayList(mavenModuleA,
258 mavenModuleX);
259
260 for (AbstractProject<?, ?> module : allModules) {
261 MavenModule mm = (MavenModule) module;
262 enhanceMavenModuleMock(mm, parent, allModules);
263 }
264
265 DependencyGraph graph = MockHelper.mockDependencyGraph(allModules);
266 doCallRealMethod().when(graph).getDownstream(Matchers.any(AbstractProject.class));
267 doCallRealMethod().when(graph).getUpstream(Matchers.any(AbstractProject.class));
268 doCallRealMethod().when(graph).compare(Matchers.<AbstractProject>any(), Matchers.<AbstractProject>any());
269 graph.build();
270
271 List<AbstractProject> downstreamA = graph.getDownstream(mavenModuleA);
272 List<AbstractProject> upstreamA = graph.getUpstream(mavenModuleA);
273
274 Assert.assertEquals(0, downstreamA.size());
275 Assert.assertEquals(0, upstreamA.size());
276
277 List<AbstractProject> downstreamX = graph.getDownstream(mavenModuleX);
278 List<AbstractProject> upstreamX = graph.getUpstream(mavenModuleX);
279
280 Assert.assertEquals(0, downstreamX.size());
281 Assert.assertEquals(0, upstreamX.size());
282 }
283
284 private TestComponents createTestComponents(String libraryVersion) {
285 MavenProject appProject = createMavenProject("testapp", "test", "application", "1.0-SNAPSHOT", "jar");
286 Dependency dependency = createDependency("test", "library", libraryVersion);
287 appProject.getDependencies().add(dependency);
288
289 MavenModule appMavenModule = mockMavenModule(appProject);
290
291 MavenProject libProject = createLibrary();
292 MavenModule libMavenModule = mockMavenModule(libProject);
293
294 MavenModuleSet parent = mock(MavenModuleSet.class);
295 when(parent.isAggregatorStyleBuild()).thenReturn(Boolean.FALSE);
296 when(appMavenModule.getParent()).thenReturn(parent);
297 when(libMavenModule.getParent()).thenReturn(parent);
298
299 Collection<MavenModule> projects = Lists.newArrayList(appMavenModule, libMavenModule);
300 when(parent.getModules()).thenReturn(projects);
301 when(appMavenModule.getAllMavenModules()).thenReturn(projects);
302 when(libMavenModule.getAllMavenModules()).thenReturn(projects);
303
304 DependencyGraph graph = MockHelper.mockDependencyGraph(Lists.<AbstractProject<?,?>>newArrayList(appMavenModule, libMavenModule));
305 doCallRealMethod().when(graph).getDownstream(Matchers.any(AbstractProject.class));
306 doCallRealMethod().when(graph).getUpstream(Matchers.any(AbstractProject.class));
307
308 TestComponents testComponents = new TestComponents();
309 testComponents.graph = graph;
310 testComponents.applicationMavenModule = appMavenModule;
311 testComponents.libraryMavenModule = libMavenModule;
312
313 return testComponents;
314 }
315
316 private static void enhanceMavenModuleMock(MavenModule module,
317 MavenModuleSet parent,
318 Collection allProjects) {
319 when(module.getParent()).thenReturn(parent);
320 when(module.getAllMavenModules()).thenReturn(allProjects);
321 }
322
323 private static MavenModule mockMavenModule(MavenProject project) {
324 MavenModule mavenModule = mock(MavenModule.class);
325 when(mavenModule.getName()).thenReturn(project.getName());
326 basicMocking(mavenModule);
327 mavenModule.doSetName(project.getGroupId() + '$' + project.getArtifactId());
328
329 PomInfo pomInfo = new PomInfo(project, null, "relPath");
330 mavenModule.reconfigure(pomInfo);
331
332 return mavenModule;
333 }
334
335 private static MavenProject createMavenProject(String name,
336 String groupId,
337 String artifactId,
338 String version,
339 String packaging) {
340 MavenProject proj = new MavenProject();
341 proj.setName(name);
342 proj.setGroupId(groupId);
343 proj.setArtifactId(artifactId);
344 proj.setVersion(version);
345 proj.setPackaging(packaging);
346
347 return proj;
348 }
349
350 private static Dependency createDependency(String groupId, String artifactId, String version) {
351 Dependency dependency = new Dependency();
352 dependency.setGroupId(groupId);
353 dependency.setArtifactId(artifactId);
354 dependency.setVersion(version);
355 return dependency;
356 }
357
358 private static MavenProject createLibrary() {
359 MavenProject proj = createMavenProject("testlib", "test", "library", "1.0.1-SNAPSHOT", "jar");
360
361 Dependency dependency = new Dependency();
362 dependency.setArtifactId("log4j");
363 dependency.setGroupId("log4j");
364 dependency.setVersion("1.6.15");
365 proj.getDependencies().add(dependency);
366 return proj;
367 }
368
369 private static class TestComponents {
370 public DependencyGraph graph;
371 public MavenModule applicationMavenModule;
372 public MavenModule libraryMavenModule;
373 }
374 }
375