View Javadoc
1   package org.apache.maven.plugins.dependency.fromConfiguration;
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.ArrayList;
25  import java.util.Collection;
26  import java.util.Collections;
27  import java.util.List;
28  
29  import org.apache.commons.lang.time.DateFormatUtils;
30  import org.apache.maven.artifact.Artifact;
31  import org.apache.maven.artifact.versioning.VersionRange;
32  import org.apache.maven.execution.MavenSession;
33  import org.apache.maven.model.Dependency;
34  import org.apache.maven.plugin.MojoExecutionException;
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.markers.UnpackFileMarkerHandler;
38  import org.apache.maven.project.MavenProject;
39  import org.sonatype.aether.impl.internal.SimpleLocalRepositoryManager;
40  import org.sonatype.aether.util.DefaultRepositorySystemSession;
41  
42  public class TestUnpackMojo
43      extends AbstractDependencyMojoTestCase
44  {
45  
46      UnpackMojo mojo;
47  
48      public TestUnpackMojo()
49      {
50          super();
51      }
52  
53      protected void setUp()
54          throws Exception
55      {
56          super.setUp( "unpack", true, false );
57  
58          File testPom = new File( getBasedir(), "target/test-classes/unit/unpack-test/plugin-config.xml" );
59          mojo = (UnpackMojo) lookupMojo( "unpack", testPom );
60          mojo.setOutputDirectory( new File( this.testDir, "outputDirectory" ) );
61          mojo.setMarkersDirectory( new File( this.testDir, "markers" ) );
62          mojo.setSilent( true );
63  
64          assertNotNull( mojo );
65          assertNotNull( mojo.getProject() );
66          // MavenProject project = mojo.getProject();
67          // init classifier things
68          // it needs to get the archivermanager
69          stubFactory.setUnpackableFile( mojo.getArchiverManager() );
70          // i'm using one file repeatedly to archive so I can test the name
71          // programmatically.
72          stubFactory.setSrcFile( new File( getBasedir() + File.separatorChar
73              + "target/test-classes/unit/unpack-dependencies-test/test.txt" ) );
74  
75          mojo.setUseJvmChmod( true );
76  
77          MavenSession session = newMavenSession( mojo.getProject() );
78          setVariableValueToObject( mojo, "session", session );
79  
80          DefaultRepositorySystemSession repoSession = (DefaultRepositorySystemSession) session.getRepositorySession();
81  
82          repoSession.setLocalRepositoryManager( new SimpleLocalRepositoryManager( stubFactory.getWorkingDir() ) );
83      }
84  
85      public ArtifactItem getSingleArtifactItem( boolean removeVersion )
86          throws MojoExecutionException
87      {
88          List<ArtifactItem> list = mojo.getProcessedArtifactItems( removeVersion );
89          return list.get( 0 );
90      }
91  
92      public void testGetArtifactItems()
93          throws Exception
94      {
95  
96          ArtifactItem item = new ArtifactItem();
97  
98          item.setArtifactId( "artifact" );
99          item.setGroupId( "groupId" );
100         item.setVersion( "1.0" );
101 
102         ArrayList<ArtifactItem> list = new ArrayList<ArtifactItem>( 1 );
103         list.add( createArtifact( item ) );
104 
105         mojo.setArtifactItems( list );
106 
107         ArtifactItem result = getSingleArtifactItem( false );
108         assertEquals( mojo.getOutputDirectory(), result.getOutputDirectory() );
109 
110         File output = new File( mojo.getOutputDirectory(), "override" );
111         item.setOutputDirectory( output );
112         result = getSingleArtifactItem( false );
113         assertEquals( output, result.getOutputDirectory() );
114     }
115 
116     public void assertMarkerFiles( Collection<ArtifactItem> items, boolean exist )
117     {
118         for ( ArtifactItem item : items )
119         {
120             assertMarkerFile( exist, item );
121         }
122     }
123 
124     public void assertMarkerFile( boolean val, ArtifactItem item )
125     {
126         UnpackFileMarkerHandler handle = new UnpackFileMarkerHandler( item, mojo.getMarkersDirectory() );
127         try
128         {
129             assertEquals( val, handle.isMarkerSet() );
130         }
131         catch ( MojoExecutionException e )
132         {
133             fail( e.getLongMessage() );
134         }
135     }
136 
137     public void testUnpackFile()
138         throws Exception
139     {
140         List<ArtifactItem> list = stubFactory.getArtifactItems( stubFactory.getClassifiedArtifacts() );
141 
142         mojo.setArtifactItems( list );
143 
144         mojo.execute();
145 
146         assertMarkerFiles( list, true );
147     }
148 
149     public void testSkip()
150         throws Exception
151     {
152         List<ArtifactItem> list = stubFactory.getArtifactItems( stubFactory.getClassifiedArtifacts() );
153 
154         mojo.setSkip( true );
155         mojo.setArtifactItems( list );
156 
157         mojo.execute();
158 
159         assertMarkerFiles( list, false );
160     }
161 
162     public void testUnpackToLocation()
163         throws Exception
164     {
165         List<ArtifactItem> list = stubFactory.getArtifactItems( stubFactory.getClassifiedArtifacts() );
166         ArtifactItem item = list.get( 0 );
167         item.setOutputDirectory( new File( mojo.getOutputDirectory(), "testOverride" ) );
168 
169         mojo.setArtifactItems( list );
170 
171         mojo.execute();
172 
173         assertMarkerFiles( list, true );
174     }
175 
176     public void testUnpackToLocationWhereLocationCannotBeCreatedThrowsException()
177         throws Exception
178     {
179         List<ArtifactItem> list = stubFactory.getArtifactItems( stubFactory.getClassifiedArtifacts() );
180         ArtifactItem item = list.get( 0 );
181         item.setOutputDirectory( new File( mojo.getOutputDirectory(), "testOverride" ) );
182 
183         mojo.setArtifactItems( list );
184         final File currentFile = mojo.getOutputDirectory();
185 
186         // pretend that the output directory cannot be found event after mkdirs has been called by the mojo
187         // ifor instance in the case when the outputDirectory cannot be created because of permissions on the
188         // parent of the output directory
189         mojo.setOutputDirectory( new File( currentFile.getAbsolutePath() )
190         {
191 
192             private static final long serialVersionUID = -8559876942040177020L;
193 
194             @Override
195             public boolean exists()
196             {
197                 // this file will always report that it does not exist
198                 return false;
199             }
200         } );
201         try
202         {
203             mojo.execute();
204             fail( "Expected Exception Here." );
205         }
206         catch ( MojoExecutionException e )
207         {
208             // caught the expected exception.
209         }
210     }
211 
212     public void testMissingVersionNotFound()
213         throws Exception
214     {
215         ArtifactItem item = new ArtifactItem();
216 
217         item.setArtifactId( "artifactId" );
218         item.setClassifier( "" );
219         item.setGroupId( "groupId" );
220         item.setType( "type" );
221 
222         List<ArtifactItem> list = new ArrayList<ArtifactItem>();
223         list.add( item );
224         mojo.setArtifactItems( list );
225 
226         try
227         {
228             mojo.execute();
229             fail( "Expected Exception Here." );
230         }
231         catch ( MojoExecutionException e )
232         {
233             // caught the expected exception.
234         }
235     }
236 
237     public List<Dependency> getDependencyList( ArtifactItem item )
238     {
239         Dependency dep = new Dependency();
240         dep.setArtifactId( item.getArtifactId() );
241         dep.setClassifier( item.getClassifier() );
242         dep.setGroupId( item.getGroupId() );
243         dep.setType( item.getType() );
244         dep.setVersion( "2.0-SNAPSHOT" );
245 
246         Dependency dep2 = new Dependency();
247         dep2.setArtifactId( item.getArtifactId() );
248         dep2.setClassifier( "classifier" );
249         dep2.setGroupId( item.getGroupId() );
250         dep2.setType( item.getType() );
251         dep2.setVersion( "2.1" );
252 
253         List<Dependency> list = new ArrayList<Dependency>( 2 );
254         list.add( dep2 );
255         list.add( dep );
256 
257         return list;
258     }
259 
260     public void testMissingVersionFromDependencies()
261         throws Exception
262     {
263         ArtifactItem item = new ArtifactItem();
264 
265         item.setArtifactId( "artifactId" );
266         item.setClassifier( "" );
267         item.setGroupId( "groupId" );
268         item.setType( "jar" );
269 
270         List<ArtifactItem> list = new ArrayList<ArtifactItem>();
271         list.add( item );
272         mojo.setArtifactItems( list );
273 
274         MavenProject project = mojo.getProject();
275         project.setDependencies( createArtifacts( getDependencyList( item ) ) );
276 
277         mojo.execute();
278         assertMarkerFile( true, item );
279         assertEquals( "2.0-SNAPSHOT", item.getVersion() );
280     }
281 
282     public void testMissingVersionFromDependenciesWithClassifier()
283         throws Exception
284     {
285         ArtifactItem item = new ArtifactItem();
286 
287         item.setArtifactId( "artifactId" );
288         item.setClassifier( "classifier" );
289         item.setGroupId( "groupId" );
290         item.setType( "war" );
291 
292         List<ArtifactItem> list = new ArrayList<ArtifactItem>();
293         list.add( item );
294         mojo.setArtifactItems( list );
295 
296         MavenProject project = mojo.getProject();
297         project.setDependencies( createArtifacts( getDependencyList( item ) ) );
298 
299         mojo.execute();
300         assertMarkerFile( true, item );
301         assertEquals( "2.1", item.getVersion() );
302     }
303 
304     public List<Dependency> getDependencyMgtList( ArtifactItem item )
305     {
306         Dependency dep = new Dependency();
307         dep.setArtifactId( item.getArtifactId() );
308         dep.setClassifier( item.getClassifier() );
309         dep.setGroupId( item.getGroupId() );
310         dep.setType( item.getType() );
311         dep.setVersion( "3.0-SNAPSHOT" );
312 
313         Dependency dep2 = new Dependency();
314         dep2.setArtifactId( item.getArtifactId() );
315         dep2.setClassifier( "classifier" );
316         dep2.setGroupId( item.getGroupId() );
317         dep2.setType( item.getType() );
318         dep2.setVersion( "3.1" );
319 
320         List<Dependency> list = new ArrayList<Dependency>( 2 );
321         list.add( dep2 );
322         list.add( dep );
323 
324         return list;
325     }
326 
327     public void testMissingVersionFromDependencyMgt()
328         throws Exception
329     {
330         ArtifactItem item = new ArtifactItem();
331 
332         item.setArtifactId( "artifactId" );
333         item.setClassifier( "" );
334         item.setGroupId( "groupId" );
335         item.setType( "jar" );
336 
337         MavenProject project = mojo.getProject();
338         project.setDependencies( createArtifacts( getDependencyList( item ) ) );
339 
340         item = new ArtifactItem();
341 
342         item.setArtifactId( "artifactId-2" );
343         item.setClassifier( "" );
344         item.setGroupId( "groupId" );
345         item.setType( "jar" );
346 
347         List<ArtifactItem> list = new ArrayList<ArtifactItem>();
348         list.add( item );
349 
350         mojo.setArtifactItems( list );
351 
352         project.getDependencyManagement().setDependencies( createArtifacts( getDependencyMgtList( item ) ) );
353 
354         mojo.execute();
355         assertMarkerFile( true, item );
356         assertEquals( "3.0-SNAPSHOT", item.getVersion() );
357     }
358 
359     public void testMissingVersionFromDependencyMgtWithClassifier()
360         throws Exception
361     {
362         ArtifactItem item = new ArtifactItem();
363 
364         item.setArtifactId( "artifactId" );
365         item.setClassifier( "classifier" );
366         item.setGroupId( "groupId" );
367         item.setType( "jar" );
368 
369         MavenProject project = mojo.getProject();
370         project.setDependencies( createArtifacts( getDependencyList( item ) ) );
371 
372         item = new ArtifactItem();
373 
374         item.setArtifactId( "artifactId-2" );
375         item.setClassifier( "classifier" );
376         item.setGroupId( "groupId" );
377         item.setType( "jar" );
378 
379         stubFactory.createArtifact( "groupId", "artifactId-2", VersionRange.createFromVersion( "3.0-SNAPSHOT" ), null,
380                                     "jar", "classifier", false );
381         stubFactory.createArtifact( "groupId", "artifactId-2", VersionRange.createFromVersion( "3.1" ), null, "jar",
382                                     "classifier", false );
383 
384         List<ArtifactItem> list = new ArrayList<ArtifactItem>();
385         list.add( item );
386 
387         mojo.setArtifactItems( list );
388 
389         project.getDependencyManagement().setDependencies( createArtifacts( getDependencyMgtList( item ) ) );
390 
391         mojo.execute();
392 
393         assertMarkerFile( true, item );
394         assertEquals( "3.1", item.getVersion() );
395     }
396 
397     public void testArtifactNotFound()
398         throws Exception
399     {
400         dotestArtifactExceptions( false, true );
401     }
402 
403     public void testArtifactResolutionException()
404         throws Exception
405     {
406         dotestArtifactExceptions( true, false );
407     }
408 
409     public void dotestArtifactExceptions( boolean are, boolean anfe )
410         throws Exception
411     {
412         ArtifactItem item = new ArtifactItem();
413 
414         item.setArtifactId( "artifactId" );
415         item.setClassifier( "" );
416         item.setGroupId( "groupId" );
417         item.setType( "type" );
418         item.setVersion( "1.0" );
419 
420         List<ArtifactItem> list = new ArrayList<ArtifactItem>();
421         list.add( item );
422         mojo.setArtifactItems( list );
423 
424         try
425         {
426             mojo.execute();
427             fail( "ExpectedException" );
428         }
429         catch ( MojoExecutionException e )
430         {
431             assertEquals( "Unable to find/resolve artifact.", e.getMessage() );
432         }
433     }
434 
435     public void testNoArtifactItems()
436     {
437         try
438         {
439             mojo.getProcessedArtifactItems( false );
440             fail( "Expected Exception" );
441         }
442         catch ( MojoExecutionException e )
443         {
444             assertEquals( "There are no artifactItems configured.", e.getMessage() );
445         }
446 
447     }
448 
449     public void testUnpackDontOverWriteReleases()
450         throws Exception
451     {
452         stubFactory.setCreateFiles( true );
453         Artifact release = stubFactory.getReleaseArtifact();
454         assertTrue( release.getFile().setLastModified( System.currentTimeMillis() - 2000 ) );
455 
456         ArtifactItem item = new ArtifactItem( createArtifact( release ) );
457 
458         List<ArtifactItem> list = new ArrayList<ArtifactItem>( 1 );
459         list.add( item );
460         mojo.setArtifactItems( list );
461 
462         mojo.setOverWriteIfNewer( false );
463 
464         mojo.execute();
465 
466         assertUnpacked( item, false );
467     }
468 
469     public void testUnpackDontOverWriteSnapshots()
470         throws Exception
471     {
472         stubFactory.setCreateFiles( true );
473         Artifact artifact = stubFactory.getSnapshotArtifact();
474         assertTrue( artifact.getFile().setLastModified( System.currentTimeMillis() - 2000 ) );
475 
476         ArtifactItem item = new ArtifactItem( createArtifact( artifact ) );
477 
478         List<ArtifactItem> list = new ArrayList<ArtifactItem>( 1 );
479         list.add( item );
480         mojo.setArtifactItems( list );
481 
482         mojo.setOverWriteIfNewer( false );
483 
484         mojo.execute();
485 
486         assertUnpacked( item, false );
487     }
488 
489     public void testUnpackOverWriteReleases()
490         throws Exception
491     {
492         stubFactory.setCreateFiles( true );
493         Artifact release = stubFactory.getReleaseArtifact();
494         assertTrue( release.getFile().setLastModified( System.currentTimeMillis() - 2000 ) );
495 
496         ArtifactItem item = new ArtifactItem( createArtifact( release ) );
497 
498         List<ArtifactItem> list = new ArrayList<ArtifactItem>( 1 );
499         list.add( item );
500         mojo.setArtifactItems( list );
501 
502         mojo.setOverWriteIfNewer( false );
503         mojo.setOverWriteReleases( true );
504         mojo.execute();
505 
506         assertUnpacked( item, true );
507     }
508 
509     public void testUnpackOverWriteSnapshot()
510         throws Exception
511     {
512         stubFactory.setCreateFiles( true );
513         Artifact artifact = stubFactory.getSnapshotArtifact();
514         assertTrue( artifact.getFile().setLastModified( System.currentTimeMillis() - 2000 ) );
515 
516         ArtifactItem item = new ArtifactItem( createArtifact( artifact ) );
517 
518         List<ArtifactItem> list = new ArrayList<ArtifactItem>( 1 );
519         list.add( item );
520         mojo.setArtifactItems( list );
521 
522         mojo.setOverWriteIfNewer( false );
523         mojo.setOverWriteReleases( false );
524         mojo.setOverWriteSnapshots( true );
525         mojo.execute();
526 
527         assertUnpacked( item, true );
528     }
529 
530     public void testUnpackOverWriteIfNewer()
531         throws Exception
532     {
533         final long now = System.currentTimeMillis();
534 
535         mojo.setSilent( false );
536         stubFactory.setCreateFiles( true );
537         Artifact artifact = stubFactory.getSnapshotArtifact();
538         assertTrue( artifact.getFile().setLastModified( now - 20000 ) );
539 
540         ArtifactItem item = new ArtifactItem( createArtifact( artifact ) );
541 
542         List<ArtifactItem> list = Collections.singletonList( item );
543         mojo.setArtifactItems( list );
544         mojo.setOverWriteIfNewer( true );
545         mojo.execute();
546         File unpackedFile = getUnpackedFile( item );
547 
548         // round down to the last second
549         long time = now;
550         time = time - ( time % 1000 );
551         // go back 10 more seconds for linux
552         time -= 10000;
553         // set to known value
554         assertTrue( unpackedFile.setLastModified( time ) );
555         // set source to be newer was 4s but test is brittle on MacOS if less than 5s
556         assertTrue( artifact.getFile().setLastModified( time + 5000 ) );
557 
558         // manually set markerfile (must match getMarkerFile in DefaultMarkerFileHandler)
559         File marker = new File( mojo.getMarkersDirectory(), artifact.getId().replace( ':', '-' ) + ".marker" );
560         assertTrue( marker.setLastModified( time ) );
561 
562         displayFile( "unpackedFile", unpackedFile );
563         displayFile( "artifact    ", artifact.getFile() );
564         displayFile( "marker      ", marker );
565         System.out.println( "mojo.execute()" );
566         mojo.execute();
567         displayFile( "unpackedFile", unpackedFile );
568         displayFile( "artifact    ", artifact.getFile() );
569         displayFile( "marker      ", marker );
570         System.out.println( "marker.lastModified() = " + marker.lastModified() );
571         System.out.println( "unpackedFile.lastModified() = " + unpackedFile.lastModified() );
572         assertTrue( "unpackedFile '" + unpackedFile + "' lastModified() == " + marker.lastModified()
573             + ": should be different", marker.lastModified() != unpackedFile.lastModified() );
574     }
575 
576     private void displayFile( String description, File file )
577     {
578         System.out.println( description + ' ' + DateFormatUtils.ISO_DATETIME_FORMAT.format( file.lastModified() ) + ' '
579             + file.getPath().substring( getBasedir().length() ) );
580     }
581 
582     public void assertUnpacked( ArtifactItem item, boolean overWrite )
583         throws Exception
584     {
585 
586         File unpackedFile = getUnpackedFile( item );
587 
588         Thread.sleep( 100 );
589         // round down to the last second
590         long time = System.currentTimeMillis();
591         time = time - ( time % 1000 );
592         assertTrue( unpackedFile.setLastModified( time ) );
593 
594         assertEquals( time, unpackedFile.lastModified() );
595         mojo.execute();
596 
597         if ( overWrite )
598         {
599             assertTrue( time != unpackedFile.lastModified() );
600         }
601         else
602         {
603             assertEquals( time, unpackedFile.lastModified() );
604         }
605     }
606 
607     public File getUnpackedFile( ArtifactItem item )
608     {
609         File unpackedFile = new File( item.getOutputDirectory(),
610                                       DependencyArtifactStubFactory.getUnpackableFileName( item.getArtifact() ) );
611 
612         assertTrue( unpackedFile.exists() );
613         return unpackedFile;
614 
615     }
616 
617     // respects the createUnpackableFile flag of the ArtifactStubFactory
618     private List<Dependency> createArtifacts( List<Dependency> items )
619         throws IOException
620     {
621         for ( Dependency item : items )
622         {
623             String classifier = "".equals( item.getClassifier() ) ? null : item.getClassifier();
624             stubFactory.createArtifact( item.getGroupId(), item.getArtifactId(),
625                                         VersionRange.createFromVersion( item.getVersion() ), null, item.getType(),
626                                         classifier, item.isOptional() );
627         }
628         return items;
629     }
630 
631     private Artifact createArtifact( Artifact art )
632         throws IOException
633     {
634         String classifier = "".equals( art.getClassifier() ) ? null : art.getClassifier();
635         stubFactory.createArtifact( art.getGroupId(), art.getArtifactId(),
636                                     VersionRange.createFromVersion( art.getVersion() ), null, art.getType(), classifier,
637                                     art.isOptional() );
638         return art;
639     }
640 
641     private ArtifactItem createArtifact( ArtifactItem item )
642         throws IOException
643     {
644         String classifier = "".equals( item.getClassifier() ) ? null : item.getClassifier();
645         stubFactory.createArtifact( item.getGroupId(), item.getArtifactId(), item.getVersion(), null, item.getType(),
646                                     classifier );
647         return item;
648     }
649 }