View Javadoc

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