View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugins.dependency.fromDependencies;
20  
21  import java.io.File;
22  import java.io.IOException;
23  import java.util.HashSet;
24  import java.util.Iterator;
25  import java.util.Set;
26  
27  import org.apache.maven.artifact.Artifact;
28  import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
29  import org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter;
30  import org.apache.maven.artifact.versioning.VersionRange;
31  import org.apache.maven.execution.MavenSession;
32  import org.apache.maven.plugin.LegacySupport;
33  import org.apache.maven.plugin.MojoExecutionException;
34  import org.apache.maven.plugin.MojoFailureException;
35  import org.apache.maven.plugins.dependency.AbstractDependencyMojoTestCase;
36  import org.apache.maven.plugins.dependency.testUtils.DependencyArtifactStubFactory;
37  import org.apache.maven.plugins.dependency.testUtils.stubs.DependencyProjectStub;
38  import org.apache.maven.plugins.dependency.utils.DependencyUtil;
39  import org.apache.maven.plugins.dependency.utils.ResolverUtil;
40  import org.apache.maven.plugins.dependency.utils.markers.DefaultFileMarkerHandler;
41  import org.apache.maven.project.MavenProject;
42  import org.codehaus.plexus.archiver.manager.ArchiverManager;
43  import org.eclipse.aether.RepositorySystem;
44  
45  public class TestUnpackDependenciesMojo extends AbstractDependencyMojoTestCase {
46  
47      private final String UNPACKABLE_FILE = "test.txt";
48  
49      private final String UNPACKABLE_FILE_PATH = "target/test-classes/unit/unpack-dependencies-test/" + UNPACKABLE_FILE;
50  
51      UnpackDependenciesMojo mojo;
52  
53      protected void setUp() throws Exception {
54          // required for mojo lookups to work
55          super.setUp("unpack-dependencies", true, false);
56  
57          MavenProject project = new DependencyProjectStub();
58          getContainer().addComponent(project, MavenProject.class.getName());
59  
60          MavenSession session = newMavenSession(project);
61          getContainer().addComponent(session, MavenSession.class.getName());
62  
63          RepositorySystem repositorySystem = lookup(RepositorySystem.class);
64          ResolverUtil resolverUtil = new ResolverUtil(repositorySystem, () -> session);
65          getContainer().addComponent(resolverUtil, ResolverUtil.class.getName());
66  
67          File testPom = new File(getBasedir(), "target/test-classes/unit/unpack-dependencies-test/plugin-config.xml");
68          mojo = (UnpackDependenciesMojo) lookupMojo("unpack-dependencies", testPom);
69          mojo.outputDirectory = new File(this.testDir, "outputDirectory");
70          // mojo.silent = true;
71  
72          // it needs to get the archivermanager
73          stubFactory.setUnpackableFile(lookup(ArchiverManager.class));
74          // i'm using one file repeatedly to archive so I can test the name
75          // programmatically.
76          stubFactory.setSrcFile(new File(getBasedir() + File.separatorChar + UNPACKABLE_FILE_PATH));
77  
78          assertNotNull(mojo);
79          assertNotNull(mojo.getProject());
80  
81          LegacySupport legacySupport = lookup(LegacySupport.class);
82          legacySupport.setSession(session);
83          installLocalRepository(legacySupport);
84  
85          Set<Artifact> artifacts = this.stubFactory.getScopedArtifacts();
86          Set<Artifact> directArtifacts = this.stubFactory.getReleaseAndSnapshotArtifacts();
87          artifacts.addAll(directArtifacts);
88  
89          project.setArtifacts(artifacts);
90          project.setDependencyArtifacts(directArtifacts);
91          mojo.markersDirectory = new File(this.testDir, "markers");
92  
93          ArtifactHandlerManager manager = lookup(ArtifactHandlerManager.class);
94          setVariableValueToObject(mojo, "artifactHandlerManager", manager);
95      }
96  
97      protected void tearDown() {
98          super.tearDown();
99  
100         mojo = null;
101         System.gc();
102     }
103 
104     public void assertUnpacked(Artifact artifact) {
105         assertUnpacked(true, artifact);
106     }
107 
108     public void assertUnpacked(boolean val, Artifact artifact) {
109         File folder = DependencyUtil.getFormattedOutputDirectory(
110                 mojo.useSubDirectoryPerScope,
111                 mojo.useSubDirectoryPerType,
112                 mojo.useSubDirectoryPerArtifact,
113                 mojo.useRepositoryLayout,
114                 mojo.stripVersion,
115                 mojo.stripType,
116                 mojo.outputDirectory,
117                 artifact);
118 
119         File destFile = new File(folder, DependencyArtifactStubFactory.getUnpackableFileName(artifact));
120 
121         assertEquals(val, destFile.exists());
122         assertMarkerFile(val, artifact);
123     }
124 
125     public void assertMarkerFile(boolean val, Artifact artifact) {
126         DefaultFileMarkerHandler handle = new DefaultFileMarkerHandler(artifact, mojo.markersDirectory);
127         try {
128             assertEquals(val, handle.isMarkerSet());
129         } catch (MojoExecutionException e) {
130             fail(e.getLongMessage());
131         }
132     }
133 
134     public void testMojo() throws Exception {
135         mojo.execute();
136         for (Artifact artifact : mojo.getProject().getArtifacts()) {
137             assertUnpacked(artifact);
138         }
139     }
140 
141     public void testNoTransitive() throws Exception {
142         mojo.excludeTransitive = true;
143         mojo.execute();
144         for (Artifact artifact : mojo.getProject().getDependencyArtifacts()) {
145             assertUnpacked(artifact);
146         }
147     }
148 
149     public void testExcludeType() throws Exception {
150         mojo.getProject().setArtifacts(stubFactory.getTypedArchiveArtifacts());
151         mojo.getProject().setDependencyArtifacts(new HashSet<>());
152         mojo.excludeTypes = "jar";
153         mojo.execute();
154 
155         for (Artifact artifact : mojo.getProject().getArtifacts()) {
156             assertUnpacked(!artifact.getType().equalsIgnoreCase("jar"), artifact);
157         }
158     }
159 
160     public void testExcludeProvidedScope() throws Exception {
161         mojo.getProject().setArtifacts(stubFactory.getScopedArtifacts());
162         mojo.getProject().setDependencyArtifacts(new HashSet<>());
163         mojo.excludeScope = "provided";
164         // mojo.silent = false;
165 
166         mojo.execute();
167 
168         for (Artifact artifact : mojo.getProject().getArtifacts()) {
169             assertUnpacked(!artifact.getScope().equals("provided"), artifact);
170         }
171     }
172 
173     public void testExcludeSystemScope() throws Exception {
174         mojo.getProject().setArtifacts(stubFactory.getScopedArtifacts());
175         mojo.getProject().setDependencyArtifacts(new HashSet<>());
176         mojo.excludeScope = "system";
177         // mojo.silent = false;
178 
179         mojo.execute();
180 
181         for (Artifact artifact : mojo.getProject().getArtifacts()) {
182             assertUnpacked(!artifact.getScope().equals("system"), artifact);
183         }
184     }
185 
186     public void testExcludeCompileScope() throws Exception {
187         mojo.getProject().setArtifacts(stubFactory.getScopedArtifacts());
188         mojo.getProject().setDependencyArtifacts(new HashSet<>());
189         mojo.excludeScope = "compile";
190         mojo.execute();
191         ScopeArtifactFilter saf = new ScopeArtifactFilter(mojo.excludeScope);
192 
193         for (Artifact artifact : mojo.getProject().getArtifacts()) {
194             assertUnpacked(!saf.include(artifact), artifact);
195         }
196     }
197 
198     public void testExcludeTestScope() throws IOException, MojoFailureException {
199         mojo.getProject().setArtifacts(stubFactory.getScopedArtifacts());
200         mojo.getProject().setDependencyArtifacts(new HashSet<>());
201         mojo.excludeScope = "test";
202 
203         try {
204             mojo.execute();
205             fail("expected an exception");
206         } catch (MojoExecutionException e) {
207 
208         }
209     }
210 
211     public void testExcludeRuntimeScope() throws Exception {
212         mojo.getProject().setArtifacts(stubFactory.getScopedArtifacts());
213         mojo.getProject().setDependencyArtifacts(new HashSet<>());
214         mojo.excludeScope = "runtime";
215         mojo.execute();
216         ScopeArtifactFilter saf = new ScopeArtifactFilter(mojo.excludeScope);
217 
218         for (Artifact artifact : mojo.getProject().getArtifacts()) {
219             assertUnpacked(!saf.include(artifact), artifact);
220         }
221     }
222 
223     public void testIncludeType() throws Exception {
224         mojo.getProject().setArtifacts(stubFactory.getTypedArchiveArtifacts());
225         mojo.getProject().setDependencyArtifacts(new HashSet<>());
226 
227         mojo.includeTypes = "jar";
228         mojo.excludeTypes = "jar";
229         // shouldn't get anything
230 
231         mojo.execute();
232 
233         Iterator<Artifact> iter = mojo.getProject().getArtifacts().iterator();
234         while (iter.hasNext()) {
235             Artifact artifact = iter.next();
236 
237             assertUnpacked(false, artifact);
238         }
239 
240         mojo.excludeTypes = "";
241         mojo.execute();
242 
243         iter = mojo.getProject().getArtifacts().iterator();
244         while (iter.hasNext()) {
245             Artifact artifact = iter.next();
246 
247             assertUnpacked(artifact.getType().equalsIgnoreCase("jar"), artifact);
248         }
249     }
250 
251     public void testSubPerType() throws Exception {
252         mojo.getProject().setArtifacts(stubFactory.getTypedArchiveArtifacts());
253         mojo.getProject().setDependencyArtifacts(new HashSet<>());
254         mojo.useSubDirectoryPerType = true;
255         mojo.execute();
256 
257         for (Artifact artifact : mojo.getProject().getArtifacts()) {
258             assertUnpacked(artifact);
259         }
260     }
261 
262     public void testSubPerArtifact() throws Exception {
263         mojo.useSubDirectoryPerArtifact = true;
264         mojo.execute();
265 
266         for (Artifact artifact : mojo.getProject().getArtifacts()) {
267             assertUnpacked(artifact);
268         }
269     }
270 
271     public void testSubPerArtifactAndType() throws Exception {
272         mojo.getProject().setArtifacts(stubFactory.getTypedArchiveArtifacts());
273         mojo.getProject().setDependencyArtifacts(new HashSet<>());
274         mojo.useSubDirectoryPerArtifact = true;
275         mojo.useSubDirectoryPerType = true;
276         mojo.execute();
277 
278         for (Artifact artifact : mojo.getProject().getArtifacts()) {
279             assertUnpacked(artifact);
280         }
281     }
282 
283     public void testSubPerArtifactRemoveVersion() throws Exception {
284         mojo.useSubDirectoryPerArtifact = true;
285         mojo.stripVersion = true;
286         mojo.execute();
287 
288         for (Artifact artifact : mojo.getProject().getArtifacts()) {
289             assertUnpacked(artifact);
290         }
291     }
292 
293     public void testSubPerArtifactAndTypeRemoveVersion() throws Exception {
294         mojo.getProject().setArtifacts(stubFactory.getTypedArchiveArtifacts());
295         mojo.getProject().setDependencyArtifacts(new HashSet<>());
296         mojo.useSubDirectoryPerArtifact = true;
297         mojo.useSubDirectoryPerType = true;
298         mojo.stripVersion = true;
299         mojo.execute();
300 
301         for (Artifact artifact : mojo.getProject().getArtifacts()) {
302             assertUnpacked(artifact);
303         }
304     }
305 
306     public void testIncludeCompileScope() throws Exception {
307         mojo.getProject().setArtifacts(stubFactory.getScopedArtifacts());
308         mojo.getProject().setDependencyArtifacts(new HashSet<>());
309         mojo.includeScope = "compile";
310         mojo.execute();
311         ScopeArtifactFilter saf = new ScopeArtifactFilter(mojo.includeScope);
312 
313         for (Artifact artifact : mojo.getProject().getArtifacts()) {
314             assertUnpacked(saf.include(artifact), artifact);
315         }
316     }
317 
318     public void testIncludeTestScope() throws Exception {
319         mojo.getProject().setArtifacts(stubFactory.getScopedArtifacts());
320         mojo.getProject().setDependencyArtifacts(new HashSet<>());
321         mojo.includeScope = "test";
322 
323         mojo.execute();
324         ScopeArtifactFilter saf = new ScopeArtifactFilter(mojo.includeScope);
325 
326         for (Artifact artifact : mojo.getProject().getArtifacts()) {
327             assertUnpacked(saf.include(artifact), artifact);
328         }
329     }
330 
331     public void testIncludeRuntimeScope() throws Exception {
332         mojo.getProject().setArtifacts(stubFactory.getScopedArtifacts());
333         mojo.getProject().setDependencyArtifacts(new HashSet<>());
334         mojo.includeScope = "runtime";
335         mojo.execute();
336         ScopeArtifactFilter saf = new ScopeArtifactFilter(mojo.includeScope);
337 
338         for (Artifact artifact : mojo.getProject().getArtifacts()) {
339             assertUnpacked(saf.include(artifact), artifact);
340         }
341     }
342 
343     public void testIncludeprovidedScope() throws Exception {
344         mojo.getProject().setArtifacts(stubFactory.getScopedArtifacts());
345         mojo.getProject().setDependencyArtifacts(new HashSet<>());
346         mojo.includeScope = "provided";
347 
348         mojo.execute();
349         for (Artifact artifact : mojo.getProject().getArtifacts()) {
350             assertUnpacked(Artifact.SCOPE_PROVIDED.equals(artifact.getScope()), artifact);
351         }
352     }
353 
354     public void testIncludesystemScope() throws Exception {
355         mojo.getProject().setArtifacts(stubFactory.getScopedArtifacts());
356         mojo.getProject().setDependencyArtifacts(new HashSet<>());
357         mojo.includeScope = "system";
358 
359         mojo.execute();
360 
361         for (Artifact artifact : mojo.getProject().getArtifacts()) {
362             assertUnpacked(Artifact.SCOPE_SYSTEM.equals(artifact.getScope()), artifact);
363         }
364     }
365 
366     public void testIncludeArtifactId() throws Exception {
367         mojo.getProject().setArtifacts(stubFactory.getArtifactArtifacts());
368         mojo.getProject().setDependencyArtifacts(new HashSet<>());
369 
370         mojo.includeArtifactIds = "one";
371         mojo.excludeArtifactIds = "one";
372         // shouldn't get anything
373         mojo.execute();
374 
375         Iterator<Artifact> iter = mojo.getProject().getArtifacts().iterator();
376         while (iter.hasNext()) {
377             Artifact artifact = iter.next();
378             assertUnpacked(false, artifact);
379         }
380         mojo.excludeArtifactIds = "";
381         mojo.execute();
382 
383         iter = mojo.getProject().getArtifacts().iterator();
384         while (iter.hasNext()) {
385             Artifact artifact = iter.next();
386             assertUnpacked(artifact.getArtifactId().equals("one"), artifact);
387         }
388     }
389 
390     public void testExcludeArtifactId() throws Exception {
391         mojo.getProject().setArtifacts(stubFactory.getArtifactArtifacts());
392         mojo.getProject().setDependencyArtifacts(new HashSet<>());
393         mojo.excludeArtifactIds = "one";
394         mojo.execute();
395 
396         // test - get all direct dependencies and verify that they exist if they
397         // do not have a classifier of "one"
398         // then delete the file and at the end, verify the folder is empty.
399         for (Artifact artifact : mojo.getProject().getArtifacts()) {
400             assertUnpacked(!artifact.getArtifactId().equals("one"), artifact);
401         }
402     }
403 
404     public void testExcludeGroupId() throws Exception {
405         mojo.getProject().setArtifacts(stubFactory.getGroupIdArtifacts());
406         mojo.getProject().setDependencyArtifacts(new HashSet<>());
407         mojo.excludeGroupIds = "one";
408         mojo.execute();
409 
410         for (Artifact artifact : mojo.getProject().getArtifacts()) {
411             assertUnpacked(!artifact.getGroupId().equals("one"), artifact);
412         }
413     }
414 
415     public void testIncludeGroupId() throws Exception {
416         mojo.getProject().setArtifacts(stubFactory.getGroupIdArtifacts());
417         mojo.getProject().setDependencyArtifacts(new HashSet<>());
418         mojo.includeGroupIds = "one";
419         mojo.excludeGroupIds = "one";
420         // shouldn't get anything
421 
422         mojo.execute();
423 
424         Iterator<Artifact> iter = mojo.getProject().getArtifacts().iterator();
425         while (iter.hasNext()) {
426             Artifact artifact = iter.next();
427             // Testing with artifact id because group id is not in filename
428             assertUnpacked(false, artifact);
429         }
430 
431         mojo.excludeGroupIds = "";
432         mojo.execute();
433 
434         iter = mojo.getProject().getArtifacts().iterator();
435         while (iter.hasNext()) {
436             Artifact artifact = iter.next();
437             // Testing with artifact id because group id is not in filename
438             assertUnpacked(artifact.getGroupId().equals("one"), artifact);
439         }
440     }
441 
442     public void testCDMClassifier() throws Exception {
443         dotestClassifierType("jdk14", null);
444     }
445 
446     public void testCDMType() throws Exception {
447         dotestClassifierType(null, "zip");
448     }
449 
450     public void testCDMClassifierType() throws Exception {
451         dotestClassifierType("jdk14", "war");
452     }
453 
454     public void dotestClassifierType(String testClassifier, String testType) throws Exception {
455         mojo.classifier = testClassifier;
456         mojo.type = testType;
457 
458         for (Artifact artifact : mojo.getProject().getArtifacts()) {
459             String type = testType != null ? testType : artifact.getType();
460             this.stubFactory.createArtifact(
461                     artifact.getGroupId(),
462                     artifact.getArtifactId(),
463                     VersionRange.createFromVersion(artifact.getBaseVersion()),
464                     artifact.getScope(),
465                     type,
466                     testClassifier,
467                     false);
468         }
469 
470         mojo.execute();
471 
472         for (Artifact artifact : mojo.getProject().getArtifacts()) {
473             String useClassifier = artifact.getClassifier();
474             String useType = artifact.getType();
475 
476             if (testClassifier != null && !testClassifier.isEmpty()) {
477                 useClassifier = testClassifier;
478                 // type is only used if classifier is used.
479                 if (testType != null && !testType.isEmpty()) {
480                     useType = testType;
481                 }
482             }
483             Artifact unpacked = stubFactory.createArtifact(
484                     artifact.getGroupId(),
485                     artifact.getArtifactId(),
486                     artifact.getVersion(),
487                     Artifact.SCOPE_COMPILE,
488                     useType,
489                     useClassifier);
490             assertUnpacked(unpacked);
491         }
492     }
493 
494     public void testArtifactResolutionException() throws MojoFailureException {
495         dotestArtifactExceptions();
496     }
497 
498     public void dotestArtifactExceptions() throws MojoFailureException {
499         mojo.classifier = "jdk";
500         mojo.failOnMissingClassifierArtifact = true;
501         mojo.type = "java-sources";
502 
503         try {
504             mojo.execute();
505             fail("ExpectedException");
506         } catch (MojoExecutionException e) {
507         }
508     }
509 
510     public File getUnpackedFile(Artifact artifact) {
511         File destDir = DependencyUtil.getFormattedOutputDirectory(
512                 mojo.isUseSubDirectoryPerScope(),
513                 mojo.isUseSubDirectoryPerType(),
514                 mojo.isUseSubDirectoryPerArtifact(),
515                 mojo.useRepositoryLayout,
516                 mojo.stripVersion,
517                 mojo.stripType,
518                 mojo.getOutputDirectory(),
519                 artifact);
520         File unpacked = new File(destDir, DependencyArtifactStubFactory.getUnpackableFileName(artifact));
521         assertTrue(unpacked.exists());
522         return unpacked;
523     }
524 
525     public DefaultFileMarkerHandler getUnpackedMarkerHandler(Artifact artifact) {
526         return new DefaultFileMarkerHandler(artifact, mojo.getMarkersDirectory());
527     }
528 
529     public void assertUnpacked(Artifact artifact, boolean overWrite)
530             throws InterruptedException, MojoExecutionException, MojoFailureException {
531         File unpackedFile = getUnpackedFile(artifact);
532 
533         Thread.sleep(100);
534         // round down to the last second
535         long time = System.currentTimeMillis();
536         time = time - (time % 1000);
537         assertTrue(unpackedFile.setLastModified(time));
538         // wait at least a second for filesystems that only record to the
539         // nearest second.
540         Thread.sleep(1000);
541 
542         assertEquals(time, unpackedFile.lastModified());
543         mojo.execute();
544 
545         if (overWrite) {
546             assertTrue(time != unpackedFile.lastModified());
547         } else {
548             assertEquals(time, unpackedFile.lastModified());
549         }
550     }
551 }