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