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