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