View Javadoc

1   package org.apache.maven.plugin.dependency.fromDependencies;
2   
3   /* 
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.    
20   */
21  
22  import org.apache.maven.plugin.MojoFailureException;
23  
24  import org.apache.maven.artifact.Artifact;
25  import org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter;
26  import org.apache.maven.plugin.MojoExecutionException;
27  import org.apache.maven.plugin.dependency.AbstractDependencyMojoTestCase;
28  import org.apache.maven.plugin.dependency.fromDependencies.UnpackDependenciesMojo;
29  import org.apache.maven.plugin.dependency.testUtils.DependencyArtifactStubFactory;
30  import org.apache.maven.plugin.dependency.testUtils.DependencyTestUtils;
31  import org.apache.maven.plugin.dependency.utils.DependencyUtil;
32  import org.apache.maven.plugin.dependency.utils.markers.DefaultFileMarkerHandler;
33  import org.apache.maven.plugin.testing.stubs.StubArtifactRepository;
34  import org.apache.maven.plugin.testing.stubs.StubArtifactResolver;
35  import org.apache.maven.project.MavenProject;
36  import org.codehaus.plexus.util.StringUtils;
37  
38  import java.io.File;
39  import java.io.IOException;
40  import java.util.HashSet;
41  import java.util.Iterator;
42  import java.util.Set;
43  
44  public class TestUnpackDependenciesMojo
45      extends AbstractDependencyMojoTestCase
46  {
47  
48      private final String UNPACKABLE_FILE = "test.txt";
49  
50      private final String UNPACKABLE_FILE_PATH = "target/test-classes/unit/unpack-dependencies-test/" + UNPACKABLE_FILE;
51  
52      UnpackDependenciesMojo mojo;
53  
54      protected void setUp()
55          throws Exception
56      {
57          // required for mojo lookups to work
58          super.setUp( "unpack-dependencies", true );
59  
60          File testPom = new File( getBasedir(), "target/test-classes/unit/unpack-dependencies-test/plugin-config.xml" );
61          mojo = (UnpackDependenciesMojo) lookupMojo( "unpack-dependencies", testPom );
62          mojo.outputDirectory = new File( this.testDir, "outputDirectory" );
63          mojo.setUseJvmChmod( true );
64          // mojo.silent = true;
65  
66          // it needs to get the archivermanager
67          stubFactory.setUnpackableFile( mojo.getArchiverManager() );
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          MavenProject project = mojo.getProject();
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      }
85  
86      protected void tearDown()
87      {
88          super.tearDown();
89  
90          mojo = null;
91          System.gc();
92      }
93  
94      public void assertUnpacked( Artifact artifact )
95      {
96          assertUnpacked( true, artifact );
97      }
98  
99      public void assertUnpacked( boolean val, Artifact artifact )
100     {
101         File folder =
102             DependencyUtil.getFormattedOutputDirectory( mojo.useSubDirectoryPerScope, mojo.useSubDirectoryPerType,
103                                                         mojo.useSubDirectoryPerArtifact, mojo.useRepositoryLayout,
104                                                         mojo.stripVersion, mojo.outputDirectory, artifact );
105 
106         File destFile = new File( folder, DependencyArtifactStubFactory.getUnpackableFileName( artifact ) );
107 
108         assertEquals( val, destFile.exists() );
109         assertMarkerFile( val, artifact );
110     }
111 
112     public void assertMarkerFile( boolean val, Artifact artifact )
113     {
114         DefaultFileMarkerHandler handle = new DefaultFileMarkerHandler( artifact, mojo.markersDirectory );
115         try
116         {
117             assertEquals( val, handle.isMarkerSet() );
118         }
119         catch ( MojoExecutionException e )
120         {
121             fail( e.getLongMessage() );
122         }
123     }
124 
125     public void testMojo()
126         throws Exception
127     {
128         mojo.execute();
129         Iterator<Artifact> iter = mojo.getProject().getArtifacts().iterator();
130         while ( iter.hasNext() )
131         {
132             Artifact artifact = iter.next();
133             assertUnpacked( artifact );
134         }
135     }
136 
137     public void testNoTransitive()
138         throws Exception
139     {
140         mojo.excludeTransitive = true;
141         mojo.execute();
142         Iterator<Artifact> iter = mojo.getProject().getDependencyArtifacts().iterator();
143         while ( iter.hasNext() )
144         {
145             Artifact artifact = iter.next();
146             assertUnpacked( artifact );
147         }
148     }
149 
150     public void testExcludeType()
151         throws Exception
152     {
153         mojo.getProject().setArtifacts( stubFactory.getTypedArchiveArtifacts() );
154         mojo.getProject().setDependencyArtifacts( new HashSet<Artifact>() );
155         mojo.excludeTypes = "jar";
156         mojo.execute();
157 
158         Iterator<Artifact> iter = mojo.getProject().getArtifacts().iterator();
159         while ( iter.hasNext() )
160         {
161             Artifact artifact = iter.next();
162 
163             assertUnpacked( !artifact.getType().equalsIgnoreCase( "jar" ), artifact );
164         }
165     }
166 
167     public void testExcludeProvidedScope()
168         throws Exception
169     {
170         mojo.getProject().setArtifacts( stubFactory.getScopedArtifacts() );
171         mojo.getProject().setDependencyArtifacts( new HashSet<Artifact>() );
172         mojo.excludeScope = "provided";
173         // mojo.silent = false;
174 
175         mojo.execute();
176 
177         Iterator<Artifact> iter = mojo.getProject().getArtifacts().iterator();
178         while ( iter.hasNext() )
179         {
180             Artifact artifact = iter.next();
181             assertUnpacked( !artifact.getScope().equals( "provided" ), artifact );
182         }
183 
184     }
185 
186     public void testExcludeSystemScope()
187         throws Exception
188     {
189         mojo.getProject().setArtifacts( stubFactory.getScopedArtifacts() );
190         mojo.getProject().setDependencyArtifacts( new HashSet<Artifact>() );
191         mojo.excludeScope = "system";
192         // mojo.silent = false;
193 
194         mojo.execute();
195 
196         Iterator<Artifact> iter = mojo.getProject().getArtifacts().iterator();
197         while ( iter.hasNext() )
198         {
199             Artifact artifact = iter.next();
200             assertUnpacked( !artifact.getScope().equals( "system" ), artifact );
201         }
202 
203     }
204 
205     public void testExcludeCompileScope()
206         throws Exception
207     {
208         mojo.getProject().setArtifacts( stubFactory.getScopedArtifacts() );
209         mojo.getProject().setDependencyArtifacts( new HashSet<Artifact>() );
210         mojo.excludeScope = "compile";
211         mojo.execute();
212         ScopeArtifactFilter saf = new ScopeArtifactFilter( mojo.excludeScope );
213 
214         Iterator<Artifact> iter = mojo.getProject().getArtifacts().iterator();
215         while ( iter.hasNext() )
216         {
217             Artifact artifact = iter.next();
218             assertUnpacked( !saf.include( artifact ), artifact );
219         }
220     }
221 
222     public void testExcludeTestScope()
223         throws IOException, MojoFailureException
224     {
225         mojo.getProject().setArtifacts( stubFactory.getScopedArtifacts() );
226         mojo.getProject().setDependencyArtifacts( new HashSet<Artifact>() );
227         mojo.excludeScope = "test";
228 
229         try
230         {
231             mojo.execute();
232             fail( "expected an exception" );
233         }
234         catch ( MojoExecutionException e )
235         {
236 
237         }
238 
239     }
240 
241     public void testExcludeRuntimeScope()
242         throws Exception
243     {
244         mojo.getProject().setArtifacts( stubFactory.getScopedArtifacts() );
245         mojo.getProject().setDependencyArtifacts( new HashSet<Artifact>() );
246         mojo.excludeScope = "runtime";
247         mojo.execute();
248         ScopeArtifactFilter saf = new ScopeArtifactFilter( mojo.excludeScope );
249 
250         Iterator<Artifact> iter = mojo.getProject().getArtifacts().iterator();
251         while ( iter.hasNext() )
252         {
253             Artifact artifact = iter.next();
254             assertUnpacked( !saf.include( artifact ), artifact );
255         }
256     }
257 
258     public void testIncludeType()
259         throws Exception
260     {
261         mojo.getProject().setArtifacts( stubFactory.getTypedArchiveArtifacts() );
262         mojo.getProject().setDependencyArtifacts( new HashSet<Artifact>() );
263 
264         mojo.includeTypes = "jar";
265         mojo.excludeTypes = "jar";
266         //shouldn't get anything
267 
268         mojo.execute();
269 
270         Iterator<Artifact> iter = mojo.getProject().getArtifacts().iterator();
271         while ( iter.hasNext() )
272         {
273             Artifact artifact = iter.next();
274 
275             assertUnpacked( false, artifact );
276         }
277 
278         mojo.excludeTypes = "";
279         mojo.execute();
280 
281         iter = mojo.getProject().getArtifacts().iterator();
282         while ( iter.hasNext() )
283         {
284             Artifact artifact = iter.next();
285 
286             assertUnpacked( artifact.getType().equalsIgnoreCase( "jar" ), artifact );
287         }
288     }
289 
290     public void testSubPerType()
291         throws Exception
292     {
293         mojo.getProject().setArtifacts( stubFactory.getTypedArchiveArtifacts() );
294         mojo.getProject().setDependencyArtifacts( new HashSet<Artifact>() );
295         mojo.useSubDirectoryPerType = true;
296         mojo.execute();
297 
298         Iterator<Artifact> iter = mojo.getProject().getArtifacts().iterator();
299         while ( iter.hasNext() )
300         {
301             Artifact artifact = iter.next();
302             assertUnpacked( artifact );
303         }
304     }
305 
306     public void testSubPerArtifact()
307         throws Exception
308     {
309         mojo.useSubDirectoryPerArtifact = true;
310         mojo.execute();
311 
312         Iterator<Artifact> iter = mojo.getProject().getArtifacts().iterator();
313         while ( iter.hasNext() )
314         {
315             Artifact artifact = iter.next();
316             assertUnpacked( artifact );
317         }
318     }
319 
320     public void testSubPerArtifactAndType()
321         throws Exception
322     {
323         mojo.getProject().setArtifacts( stubFactory.getTypedArchiveArtifacts() );
324         mojo.getProject().setDependencyArtifacts( new HashSet<Artifact>() );
325         mojo.useSubDirectoryPerArtifact = true;
326         mojo.useSubDirectoryPerType = true;
327         mojo.execute();
328 
329         Iterator<Artifact> iter = mojo.getProject().getArtifacts().iterator();
330         while ( iter.hasNext() )
331         {
332             Artifact artifact = iter.next();
333             assertUnpacked( artifact );
334         }
335     }
336 
337     public void testSubPerArtifactRemoveVersion()
338         throws Exception
339     {
340         mojo.useSubDirectoryPerArtifact = true;
341         mojo.stripVersion = true;
342         mojo.execute();
343 
344         Iterator<Artifact> iter = mojo.getProject().getArtifacts().iterator();
345         while ( iter.hasNext() )
346         {
347             Artifact artifact = iter.next();
348             assertUnpacked( artifact );
349         }
350     }
351 
352     public void testSubPerArtifactAndTypeRemoveVersion()
353         throws Exception
354     {
355         mojo.getProject().setArtifacts( stubFactory.getTypedArchiveArtifacts() );
356         mojo.getProject().setDependencyArtifacts( new HashSet<Artifact>() );
357         mojo.useSubDirectoryPerArtifact = true;
358         mojo.useSubDirectoryPerType = true;
359         mojo.stripVersion = true;
360         mojo.execute();
361 
362         Iterator<Artifact> iter = mojo.getProject().getArtifacts().iterator();
363         while ( iter.hasNext() )
364         {
365             Artifact artifact = iter.next();
366             assertUnpacked( artifact );
367         }
368     }
369 
370     public void testIncludeCompileScope()
371         throws Exception
372     {
373         mojo.getProject().setArtifacts( stubFactory.getScopedArtifacts() );
374         mojo.getProject().setDependencyArtifacts( new HashSet<Artifact>() );
375         mojo.includeScope = "compile";
376         mojo.execute();
377         ScopeArtifactFilter saf = new ScopeArtifactFilter( mojo.includeScope );
378 
379         Iterator<Artifact> iter = mojo.getProject().getArtifacts().iterator();
380         while ( iter.hasNext() )
381         {
382             Artifact artifact = iter.next();
383             assertUnpacked( saf.include( artifact ), artifact );
384         }
385     }
386 
387     public void testIncludeTestScope()
388         throws Exception
389     {
390         mojo.getProject().setArtifacts( stubFactory.getScopedArtifacts() );
391         mojo.getProject().setDependencyArtifacts( new HashSet<Artifact>() );
392         mojo.includeScope = "test";
393 
394         mojo.execute();
395         ScopeArtifactFilter saf = new ScopeArtifactFilter( mojo.includeScope );
396 
397         Iterator<Artifact> iter = mojo.getProject().getArtifacts().iterator();
398         while ( iter.hasNext() )
399         {
400             Artifact artifact = iter.next();
401             assertUnpacked( saf.include( artifact ), artifact );
402         }
403     }
404 
405     public void testIncludeRuntimeScope()
406         throws Exception
407     {
408         mojo.getProject().setArtifacts( stubFactory.getScopedArtifacts() );
409         mojo.getProject().setDependencyArtifacts( new HashSet<Artifact>() );
410         mojo.includeScope = "runtime";
411         mojo.execute();
412         ScopeArtifactFilter saf = new ScopeArtifactFilter( mojo.includeScope );
413 
414         Iterator<Artifact> iter = mojo.getProject().getArtifacts().iterator();
415         while ( iter.hasNext() )
416         {
417             Artifact artifact = iter.next();
418             assertUnpacked( saf.include( artifact ), artifact );
419         }
420     }
421 
422     public void testIncludeprovidedScope()
423         throws Exception
424     {
425         mojo.getProject().setArtifacts( stubFactory.getScopedArtifacts() );
426         mojo.getProject().setDependencyArtifacts( new HashSet<Artifact>() );
427         mojo.includeScope = "provided";
428 
429         mojo.execute();
430         Iterator<Artifact> iter = mojo.getProject().getArtifacts().iterator();
431         while ( iter.hasNext() )
432         {
433             Artifact artifact = iter.next();
434             assertUnpacked( Artifact.SCOPE_PROVIDED.equals( artifact.getScope() ), artifact );
435         }
436     }
437 
438     public void testIncludesystemScope()
439         throws Exception
440     {
441         mojo.getProject().setArtifacts( stubFactory.getScopedArtifacts() );
442         mojo.getProject().setDependencyArtifacts( new HashSet<Artifact>() );
443         mojo.includeScope = "system";
444 
445         mojo.execute();
446 
447         Iterator<Artifact> iter = mojo.getProject().getArtifacts().iterator();
448         while ( iter.hasNext() )
449         {
450             Artifact artifact = iter.next();
451             assertUnpacked( Artifact.SCOPE_SYSTEM.equals( artifact.getScope() ), artifact );
452         }
453     }
454 
455     public void testIncludeArtifactId()
456         throws Exception
457     {
458         mojo.getProject().setArtifacts( stubFactory.getArtifactArtifacts() );
459         mojo.getProject().setDependencyArtifacts( new HashSet<Artifact>() );
460 
461         mojo.includeArtifactIds = "one";
462         mojo.excludeArtifactIds = "one";
463         //shouldn't get anything
464         mojo.execute();
465 
466         Iterator<Artifact> iter = mojo.getProject().getArtifacts().iterator();
467         while ( iter.hasNext() )
468         {
469             Artifact artifact = iter.next();
470             assertUnpacked( false, artifact );
471         }
472         mojo.excludeArtifactIds = "";
473         mojo.execute();
474 
475         iter = mojo.getProject().getArtifacts().iterator();
476         while ( iter.hasNext() )
477         {
478             Artifact artifact = iter.next();
479             assertUnpacked( artifact.getArtifactId().equals( "one" ), artifact );
480         }
481 
482     }
483 
484     public void testExcludeArtifactId()
485         throws Exception
486     {
487         mojo.getProject().setArtifacts( stubFactory.getArtifactArtifacts() );
488         mojo.getProject().setDependencyArtifacts( new HashSet<Artifact>() );
489         mojo.excludeArtifactIds = "one";
490         mojo.execute();
491 
492         // test - get all direct dependencies and verify that they exist if they
493         // do not have a classifier of "one"
494         // then delete the file and at the end, verify the folder is empty.
495         Iterator<Artifact> iter = mojo.getProject().getArtifacts().iterator();
496         while ( iter.hasNext() )
497         {
498             Artifact artifact = iter.next();
499             assertUnpacked( !artifact.getArtifactId().equals( "one" ), artifact );
500         }
501     }
502 
503     public void testExcludeGroupId()
504         throws Exception
505     {
506         mojo.getProject().setArtifacts( stubFactory.getGroupIdArtifacts() );
507         mojo.getProject().setDependencyArtifacts( new HashSet<Artifact>() );
508         mojo.excludeGroupIds = "one";
509         mojo.execute();
510 
511         Iterator<Artifact> iter = mojo.getProject().getArtifacts().iterator();
512         while ( iter.hasNext() )
513         {
514             Artifact artifact = iter.next();
515             assertUnpacked( !artifact.getGroupId().equals( "one" ), artifact );
516         }
517     }
518 
519     public void testIncludeGroupId()
520         throws Exception
521     {
522         mojo.getProject().setArtifacts( stubFactory.getGroupIdArtifacts() );
523         mojo.getProject().setDependencyArtifacts( new HashSet<Artifact>() );
524         mojo.includeGroupIds = "one";
525         mojo.excludeGroupIds = "one";
526         //shouldn't get anything
527 
528         mojo.execute();
529 
530         Iterator<Artifact> iter = mojo.getProject().getArtifacts().iterator();
531         while ( iter.hasNext() )
532         {
533             Artifact artifact = iter.next();
534             // Testing with artifact id because group id is not in filename
535             assertUnpacked( false, artifact );
536         }
537 
538         mojo.excludeGroupIds = "";
539         mojo.execute();
540 
541         iter = mojo.getProject().getArtifacts().iterator();
542         while ( iter.hasNext() )
543         {
544             Artifact artifact = iter.next();
545             // Testing with artifact id because group id is not in filename
546             assertUnpacked( artifact.getGroupId().equals( "one" ), artifact );
547         }
548 
549     }
550 
551     public void testCDMClassifier()
552         throws Exception
553     {
554         dotestClassifierType( "jdk14", null );
555     }
556 
557     public void testCDMType()
558         throws Exception
559     {
560         dotestClassifierType( null, "zip" );
561     }
562 
563     public void testCDMClassifierType()
564         throws Exception
565     {
566         dotestClassifierType( "jdk14", "war" );
567     }
568 
569     public void dotestClassifierType( String testClassifier, String testType )
570         throws Exception
571     {
572         mojo.classifier = testClassifier;
573         mojo.type = testType;
574         mojo.setFactory( DependencyTestUtils.getArtifactFactory() );
575         mojo.setResolver( new StubArtifactResolver( stubFactory, false, false ) );
576         mojo.setLocal( new StubArtifactRepository( this.testDir.getAbsolutePath() ) );
577 
578         mojo.execute();
579 
580         Iterator<Artifact> iter = mojo.getProject().getArtifacts().iterator();
581         while ( iter.hasNext() )
582         {
583             Artifact artifact = iter.next();
584 
585             String useClassifier = artifact.getClassifier();
586             String useType = artifact.getType();
587 
588             if ( StringUtils.isNotEmpty( testClassifier ) )
589             {
590                 useClassifier = testClassifier;
591                 // type is only used if classifier is used.
592                 if ( StringUtils.isNotEmpty( testType ) )
593                 {
594                     useType = testType;
595                 }
596             }
597             Artifact unpacked =
598                 stubFactory.createArtifact( artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion(),
599                                             Artifact.SCOPE_COMPILE, useType, useClassifier );
600             assertUnpacked( unpacked );
601         }
602     }
603 
604     public void testArtifactNotFound()
605         throws Exception
606     {
607         dotestArtifactExceptions( false, true );
608     }
609 
610     public void testArtifactResolutionException()
611         throws Exception
612     {
613         dotestArtifactExceptions( true, false );
614     }
615 
616     public void dotestArtifactExceptions( boolean are, boolean anfe )
617         throws Exception
618     {
619         mojo.classifier = "jdk";
620         mojo.type = "java-sources";
621         // init classifier things
622         mojo.setFactory( DependencyTestUtils.getArtifactFactory() );
623         mojo.setResolver( new StubArtifactResolver( null, are, anfe ) );
624         mojo.setLocal( new StubArtifactRepository( this.testDir.getAbsolutePath() ) );
625 
626         try
627         {
628             mojo.execute();
629             fail( "ExpectedException" );
630         }
631         catch ( MojoExecutionException e )
632         {
633         }
634     }
635 
636     public File getUnpackedFile( Artifact artifact )
637     {
638         File destDir = DependencyUtil.getFormattedOutputDirectory( mojo.isUseSubDirectoryPerScope(),
639                                                                    mojo.isUseSubDirectoryPerType(),
640                                                                    mojo.isUseSubDirectoryPerArtifact(),
641                                                                    mojo.useRepositoryLayout, mojo.stripVersion,
642                                                                    mojo.getOutputDirectory(), artifact );
643         File unpacked = new File( destDir, DependencyArtifactStubFactory.getUnpackableFileName( artifact ) );
644         assertTrue( unpacked.exists() );
645         return unpacked;
646     }
647 
648     public DefaultFileMarkerHandler getUnpackedMarkerHandler( Artifact artifact )
649     {
650         return new DefaultFileMarkerHandler( artifact, mojo.getMarkersDirectory() );
651     }
652 
653 
654     public void assertUnpacked( Artifact artifact, boolean overWrite )
655         throws InterruptedException, MojoExecutionException, MojoFailureException
656     {
657         File unpackedFile = getUnpackedFile( artifact );
658 
659         Thread.sleep( 100 );
660         // round down to the last second
661         long time = System.currentTimeMillis();
662         time = time - ( time % 1000 );
663         unpackedFile.setLastModified( time );
664         // wait at least a second for filesystems that only record to the
665         // nearest second.
666         Thread.sleep( 1000 );
667 
668         assertEquals( time, unpackedFile.lastModified() );
669         mojo.execute();
670 
671         if ( overWrite )
672         {
673             assertTrue( time != unpackedFile.lastModified() );
674         }
675         else
676         {
677             assertEquals( time, unpackedFile.lastModified() );
678         }
679     }
680 }