1   package org.apache.maven.plugin.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.Iterator;
27  import java.util.List;
28  
29  import org.apache.maven.artifact.Artifact;
30  import org.apache.maven.model.Dependency;
31  import org.apache.maven.plugin.MojoExecutionException;
32  import org.apache.maven.plugin.dependency.AbstractDependencyMojoTestCase;
33  import org.apache.maven.plugin.dependency.testUtils.DependencyArtifactStubFactory;
34  import org.apache.maven.plugin.dependency.testUtils.DependencyTestUtils;
35  import org.apache.maven.plugin.dependency.utils.markers.UnpackFileMarkerHandler;
36  import org.apache.maven.plugin.testing.stubs.StubArtifactCollector;
37  import org.apache.maven.plugin.testing.stubs.StubArtifactRepository;
38  import org.apache.maven.plugin.testing.stubs.StubArtifactResolver;
39  import org.apache.maven.project.MavenProject;
40  
41  public class TestUnpackMojo
42      extends AbstractDependencyMojoTestCase
43  {
44  
45      UnpackMojo mojo;
46  
47      public TestUnpackMojo()
48      {
49          super();
50      }
51  
52      protected void setUp()
53          throws Exception
54      {
55          super.setUp( "unpack", true );
56  
57          File testPom = new File( getBasedir(), "target/test-classes/unit/unpack-test/plugin-config.xml" );
58          mojo = (UnpackMojo) lookupMojo( "unpack", testPom );
59          mojo.setOutputDirectory( new File( this.testDir, "outputDirectory" ) );
60          mojo.setMarkersDirectory( new File( this.testDir, "markers" ) );
61          mojo.silent = true;
62  
63          assertNotNull( mojo );
64          assertNotNull( mojo.getProject() );
65          // MavenProject project = mojo.getProject();
66          // init classifier things
67          // it needs to get the archivermanager
68          stubFactory.setUnpackableFile( mojo.getArchiverManager() );
69          // i'm using one file repeatedly to archive so I can test the name
70          // programmatically.
71          stubFactory.setSrcFile( new File( getBasedir() + File.separatorChar
72              + "target/test-classes/unit/unpack-dependencies-test/test.txt" ) );
73  
74          mojo.setFactory( DependencyTestUtils.getArtifactFactory() );
75          mojo.setResolver( new StubArtifactResolver( stubFactory, false, false ) );
76          mojo.setLocal( new StubArtifactRepository( this.testDir.getAbsolutePath() ) );
77          mojo.setArtifactCollector( new StubArtifactCollector() );
78      }
79  
80      public ArtifactItem getSingleArtifactItem( boolean removeVersion )
81          throws MojoExecutionException
82      {
83          ArrayList list = mojo.getProcessedArtifactItems( removeVersion );
84          return (ArtifactItem) list.get( 0 );
85      }
86  
87      public void testGetArtifactItems()
88          throws MojoExecutionException
89      {
90  
91          ArtifactItem item = new ArtifactItem();
92  
93          item.setArtifactId( "artifact" );
94          item.setGroupId( "groupId" );
95          item.setVersion( "1.0" );
96  
97          ArrayList list = new ArrayList( 1 );
98          list.add( item );
99  
100         mojo.setArtifactItems( list );
101 
102         ArtifactItem result = getSingleArtifactItem( false );
103         assertEquals( mojo.getOutputDirectory(), result.getOutputDirectory() );
104 
105         File output = new File( mojo.getOutputDirectory(), "override" );
106         item.setOutputDirectory( output );
107         result = getSingleArtifactItem( false );
108         assertEquals( output, result.getOutputDirectory() );
109     }
110 
111     public void assertMarkerFiles( Collection items, boolean exist )
112     {
113         Iterator iter = items.iterator();
114         while ( iter.hasNext() )
115         {
116             assertMarkerFile( exist, (ArtifactItem) iter.next() );
117         }
118     }
119 
120     public void assertMarkerFile( boolean val, ArtifactItem item )
121     {
122         UnpackFileMarkerHandler handle = new UnpackFileMarkerHandler( item, mojo.getMarkersDirectory() );
123         try
124         {
125             assertEquals( val, handle.isMarkerSet() );
126         }
127         catch ( MojoExecutionException e )
128         {
129             fail( e.getLongMessage() );
130         }
131     }
132 
133     public void testUnpackFile()
134         throws IOException, MojoExecutionException
135     {
136         ArrayList list = stubFactory.getArtifactItems( stubFactory.getClassifiedArtifacts() );
137 
138         mojo.setArtifactItems( list );
139 
140         mojo.execute();
141 
142         assertMarkerFiles( list, true );
143     }
144 
145     public void testUnpackToLocation()
146         throws IOException, MojoExecutionException
147     {
148         ArrayList list = stubFactory.getArtifactItems( stubFactory.getClassifiedArtifacts() );
149         ArtifactItem item = (ArtifactItem) list.get( 0 );
150         item.setOutputDirectory( new File( mojo.getOutputDirectory(), "testOverride" ) );
151 
152         mojo.setArtifactItems( list );
153 
154         mojo.execute();
155 
156         assertMarkerFiles( list, true );
157     }
158 
159     public void testMissingVersionNotFound()
160         throws MojoExecutionException
161     {
162         ArtifactItem item = new ArtifactItem();
163 
164         item.setArtifactId( "artifactId" );
165         item.setClassifier( "" );
166         item.setGroupId( "groupId" );
167         item.setType( "type" );
168 
169         ArrayList list = new ArrayList();
170         list.add( item );
171         mojo.setArtifactItems( list );
172 
173         try
174         {
175             mojo.execute();
176             fail( "Expected Exception Here." );
177         }
178         catch ( MojoExecutionException e )
179         {
180             // caught the expected exception.
181         }
182     }
183 
184     public List getDependencyList( ArtifactItem item )
185     {
186         Dependency dep = new Dependency();
187         dep.setArtifactId( item.getArtifactId() );
188         dep.setClassifier( item.getClassifier() );
189         dep.setGroupId( item.getGroupId() );
190         dep.setType( item.getType() );
191         dep.setVersion( "2.0-SNAPSHOT" );
192 
193         Dependency dep2 = new Dependency();
194         dep2.setArtifactId( item.getArtifactId() );
195         dep2.setClassifier( "classifier" );
196         dep2.setGroupId( item.getGroupId() );
197         dep2.setType( item.getType() );
198         dep2.setVersion( "2.1" );
199 
200         List list = new ArrayList( 2 );
201         list.add( dep2 );
202         list.add( dep );
203 
204         return list;
205     }
206 
207     public void testMissingVersionFromDependencies()
208         throws MojoExecutionException
209     {
210         ArtifactItem item = new ArtifactItem();
211 
212         item.setArtifactId( "artifactId" );
213         item.setClassifier( "" );
214         item.setGroupId( "groupId" );
215         item.setType( "jar" );
216 
217         ArrayList list = new ArrayList();
218         list.add( item );
219         mojo.setArtifactItems( list );
220 
221         MavenProject project = mojo.getProject();
222         project.setDependencies( getDependencyList( item ) );
223 
224         mojo.execute();
225         assertMarkerFile( true, item );
226         assertEquals( "2.0-SNAPSHOT", item.getVersion() );
227     }
228 
229     public void testMissingVersionFromDependenciesWithClassifier()
230         throws MojoExecutionException
231     {
232         ArtifactItem item = new ArtifactItem();
233 
234         item.setArtifactId( "artifactId" );
235         item.setClassifier( "classifier" );
236         item.setGroupId( "groupId" );
237         item.setType( "war" );
238 
239         ArrayList list = new ArrayList();
240         list.add( item );
241         mojo.setArtifactItems( list );
242 
243         MavenProject project = mojo.getProject();
244         project.setDependencies( getDependencyList( item ) );
245 
246         mojo.execute();
247         assertMarkerFile( true, item );
248         assertEquals( "2.1", item.getVersion() );
249     }
250 
251     public List getDependencyMgtList( ArtifactItem item )
252     {
253         Dependency dep = new Dependency();
254         dep.setArtifactId( item.getArtifactId() );
255         dep.setClassifier( item.getClassifier() );
256         dep.setGroupId( item.getGroupId() );
257         dep.setType( item.getType() );
258         dep.setVersion( "3.0-SNAPSHOT" );
259 
260         Dependency dep2 = new Dependency();
261         dep2.setArtifactId( item.getArtifactId() );
262         dep2.setClassifier( "classifier" );
263         dep2.setGroupId( item.getGroupId() );
264         dep2.setType( item.getType() );
265         dep2.setVersion( "3.1" );
266 
267         List list = new ArrayList( 2 );
268         list.add( dep2 );
269         list.add( dep );
270 
271         return list;
272     }
273 
274     public void testMissingVersionFromDependencyMgt()
275         throws MojoExecutionException
276     {
277         ArtifactItem item = new ArtifactItem();
278 
279         item.setArtifactId( "artifactId" );
280         item.setClassifier( "" );
281         item.setGroupId( "groupId" );
282         item.setType( "jar" );
283 
284         MavenProject project = mojo.getProject();
285         project.setDependencies( getDependencyList( item ) );
286 
287         item = new ArtifactItem();
288 
289         item.setArtifactId( "artifactId-2" );
290         item.setClassifier( "" );
291         item.setGroupId( "groupId" );
292         item.setType( "jar" );
293 
294         ArrayList list = new ArrayList();
295         list.add( item );
296 
297         mojo.setArtifactItems( list );
298 
299         project.getDependencyManagement().setDependencies( getDependencyMgtList( item ) );
300 
301         mojo.execute();
302         assertMarkerFile( true, item );
303         assertEquals( "3.0-SNAPSHOT", item.getVersion() );
304     }
305 
306     public void testMissingVersionFromDependencyMgtWithClassifier()
307         throws MojoExecutionException
308     {
309         ArtifactItem item = new ArtifactItem();
310 
311         item.setArtifactId( "artifactId" );
312         item.setClassifier( "classifier" );
313         item.setGroupId( "groupId" );
314         item.setType( "jar" );
315 
316         MavenProject project = mojo.getProject();
317         project.setDependencies( getDependencyList( item ) );
318 
319         item = new ArtifactItem();
320 
321         item.setArtifactId( "artifactId-2" );
322         item.setClassifier( "classifier" );
323         item.setGroupId( "groupId" );
324         item.setType( "jar" );
325 
326         ArrayList list = new ArrayList();
327         list.add( item );
328 
329         mojo.setArtifactItems( list );
330 
331         project.getDependencyManagement().setDependencies( getDependencyMgtList( item ) );
332 
333         mojo.execute();
334 
335         assertMarkerFile( true, item );
336         assertEquals( "3.1", item.getVersion() );
337     }
338 
339     public void testArtifactNotFound()
340         throws Exception
341     {
342         dotestArtifactExceptions( false, true );
343     }
344 
345     public void testArtifactResolutionException()
346         throws Exception
347     {
348         dotestArtifactExceptions( true, false );
349     }
350 
351     public void dotestArtifactExceptions( boolean are, boolean anfe )
352         throws Exception
353     {
354         ArtifactItem item = new ArtifactItem();
355 
356         item.setArtifactId( "artifactId" );
357         item.setClassifier( "" );
358         item.setGroupId( "groupId" );
359         item.setType( "type" );
360         item.setVersion( "1.0" );
361 
362         ArrayList list = new ArrayList();
363         list.add( item );
364         mojo.setArtifactItems( list );
365 
366         // init classifier things
367         mojo.setFactory( DependencyTestUtils.getArtifactFactory() );
368         mojo.setResolver( new StubArtifactResolver( null, are, anfe ) );
369         mojo.setLocal( new StubArtifactRepository( this.testDir.getAbsolutePath() ) );
370 
371         try
372         {
373             mojo.execute();
374             fail( "ExpectedException" );
375         }
376         catch ( MojoExecutionException e )
377         {
378             if ( are )
379             {
380                 assertEquals( "Unable to resolve artifact.", e.getMessage() );
381             }
382             else
383             {
384                 assertEquals( "Unable to find artifact.", e.getMessage() );
385             }
386         }
387     }
388 
389     public void testNoArtifactItems()
390     {
391         try
392         {
393             mojo.getProcessedArtifactItems( false );
394             fail( "Expected Exception" );
395         }
396         catch ( MojoExecutionException e )
397         {
398             assertEquals( "There are no artifactItems configured.", e.getMessage() );
399         }
400 
401     }
402 
403     public void testUnpackDontOverWriteReleases()
404         throws IOException, MojoExecutionException, InterruptedException
405     {
406         stubFactory.setCreateFiles( true );
407         Artifact release = stubFactory.getReleaseArtifact();
408         release.getFile().setLastModified( System.currentTimeMillis() - 2000 );
409 
410         ArtifactItem item = new ArtifactItem( release );
411 
412         ArrayList list = new ArrayList( 1 );
413         list.add( item );
414         mojo.setArtifactItems( list );
415 
416         mojo.setOverWriteIfNewer( false );
417 
418         mojo.execute();
419 
420         assertUnpacked( item, false );
421     }
422 
423     public void testUnpackDontOverWriteSnapshots()
424         throws IOException, MojoExecutionException, InterruptedException
425     {
426         stubFactory.setCreateFiles( true );
427         Artifact artifact = stubFactory.getSnapshotArtifact();
428         artifact.getFile().setLastModified( System.currentTimeMillis() - 2000 );
429 
430         ArtifactItem item = new ArtifactItem( artifact );
431 
432         ArrayList list = new ArrayList( 1 );
433         list.add( item );
434         mojo.setArtifactItems( list );
435 
436         mojo.setOverWriteIfNewer( false );
437 
438         mojo.execute();
439 
440         assertUnpacked( item, false );
441     }
442 
443     public void testUnpackOverWriteReleases()
444         throws IOException, MojoExecutionException, InterruptedException
445     {
446         stubFactory.setCreateFiles( true );
447         Artifact release = stubFactory.getReleaseArtifact();
448         release.getFile().setLastModified( System.currentTimeMillis() - 2000 );
449 
450         ArtifactItem item = new ArtifactItem( release );
451 
452         ArrayList list = new ArrayList( 1 );
453         list.add( item );
454         mojo.setArtifactItems( list );
455 
456         mojo.setOverWriteIfNewer( false );
457         mojo.setOverWriteReleases( true );
458         mojo.execute();
459 
460         assertUnpacked( item, true );
461     }
462 
463     public void testUnpackOverWriteSnapshot()
464         throws IOException, MojoExecutionException, InterruptedException
465     {
466         stubFactory.setCreateFiles( true );
467         Artifact artifact = stubFactory.getSnapshotArtifact();
468         artifact.getFile().setLastModified( System.currentTimeMillis() - 2000 );
469 
470         ArtifactItem item = new ArtifactItem( artifact );
471 
472         ArrayList list = new ArrayList( 1 );
473         list.add( item );
474         mojo.setArtifactItems( list );
475 
476         mojo.setOverWriteIfNewer( false );
477         mojo.setOverWriteReleases( false );
478         mojo.setOverWriteSnapshots( true );
479         mojo.execute();
480 
481         assertUnpacked( item, true );
482     }
483 
484     public void testUnpackOverWriteIfNewer()
485         throws IOException, MojoExecutionException, InterruptedException
486     {
487         stubFactory.setCreateFiles( true );
488         Artifact artifact = stubFactory.getSnapshotArtifact();
489         artifact.getFile().setLastModified( System.currentTimeMillis() - 2000 );
490 
491         ArtifactItem item = new ArtifactItem( artifact );
492 
493         ArrayList list = new ArrayList( 1 );
494         list.add( item );
495         mojo.setArtifactItems( list );
496         mojo.setOverWriteIfNewer( true );
497         mojo.execute();
498         File unpackedFile = getUnpackedFile( item );
499 
500         // round down to the last second
501         long time = System.currentTimeMillis();
502         time = time - ( time % 1000 );
503         // go back 10 more seconds for linux
504         time -= 10000;
505         // set to known value
506         unpackedFile.setLastModified( time );
507         // set source to be newer
508         artifact.getFile().setLastModified( time + 4000 );
509 
510         // manually set markerfile (must match getMarkerFile in
511         // DefaultMarkerFileHandler)
512         File marker = new File( mojo.getMarkersDirectory(), artifact.getId().replace( ':', '-' ) + ".marker" );
513         marker.setLastModified( time );
514 
515         assertTrue( time == unpackedFile.lastModified() );
516         mojo.execute();
517         assertTrue( time != unpackedFile.lastModified() );
518     }
519 
520     public void assertUnpacked( ArtifactItem item, boolean overWrite )
521         throws InterruptedException, MojoExecutionException
522     {
523 
524         File unpackedFile = getUnpackedFile( item );
525 
526         Thread.sleep( 100 );
527         // round down to the last second
528         long time = System.currentTimeMillis();
529         time = time - ( time % 1000 );
530         unpackedFile.setLastModified( time );
531 
532         assertEquals( time, unpackedFile.lastModified() );
533         mojo.execute();
534 
535         if ( overWrite )
536         {
537             assertTrue( time != unpackedFile.lastModified() );
538         }
539         else
540         {
541             assertEquals( time, unpackedFile.lastModified() );
542         }
543     }
544 
545     public File getUnpackedFile( ArtifactItem item )
546     {
547         File unpackedFile = new File( item.getOutputDirectory(), DependencyArtifactStubFactory.getUnpackableFileName( item
548             .getArtifact() ) );
549 
550         assertTrue( unpackedFile.exists() );
551         return unpackedFile;
552 
553     }
554 }