1 package org.apache.maven.plugin.dependency.fromConfiguration;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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.List;
27
28 import org.apache.commons.lang.time.DateFormatUtils;
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
66
67
68 stubFactory.setUnpackableFile( mojo.getArchiverManager() );
69
70
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 List<ArtifactItem> list = mojo.getProcessedArtifactItems( removeVersion );
84 return 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<ArtifactItem> list = new ArrayList<ArtifactItem>( 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<ArtifactItem> items, boolean exist )
112 {
113 for ( ArtifactItem item : items )
114 {
115 assertMarkerFile( exist, item );
116 }
117 }
118
119 public void assertMarkerFile( boolean val, ArtifactItem item )
120 {
121 UnpackFileMarkerHandler handle = new UnpackFileMarkerHandler( item, mojo.getMarkersDirectory() );
122 try
123 {
124 assertEquals( val, handle.isMarkerSet() );
125 }
126 catch ( MojoExecutionException e )
127 {
128 fail( e.getLongMessage() );
129 }
130 }
131
132 public void testUnpackFile()
133 throws IOException, MojoExecutionException
134 {
135 List<ArtifactItem> list = stubFactory.getArtifactItems( stubFactory.getClassifiedArtifacts() );
136
137 mojo.setArtifactItems( list );
138
139 mojo.execute();
140
141 assertMarkerFiles( list, true );
142 }
143
144 public void testSkip()
145 throws IOException, MojoExecutionException
146 {
147 List<ArtifactItem> list = stubFactory.getArtifactItems( stubFactory.getClassifiedArtifacts() );
148
149 mojo.setSkip( true );
150 mojo.setArtifactItems( list );
151
152 mojo.execute();
153
154 assertMarkerFiles( list, false );
155 }
156
157 public void testUnpackToLocation()
158 throws IOException, MojoExecutionException
159 {
160 List<ArtifactItem> list = stubFactory.getArtifactItems( stubFactory.getClassifiedArtifacts() );
161 ArtifactItem item = (ArtifactItem) list.get( 0 );
162 item.setOutputDirectory( new File( mojo.getOutputDirectory(), "testOverride" ) );
163
164 mojo.setArtifactItems( list );
165
166 mojo.execute();
167
168 assertMarkerFiles( list, true );
169 }
170
171 public void testMissingVersionNotFound()
172 throws MojoExecutionException
173 {
174 ArtifactItem item = new ArtifactItem();
175
176 item.setArtifactId( "artifactId" );
177 item.setClassifier( "" );
178 item.setGroupId( "groupId" );
179 item.setType( "type" );
180
181 List<ArtifactItem> list = new ArrayList<ArtifactItem>();
182 list.add( item );
183 mojo.setArtifactItems( list );
184
185 try
186 {
187 mojo.execute();
188 fail( "Expected Exception Here." );
189 }
190 catch ( MojoExecutionException e )
191 {
192
193 }
194 }
195
196 public List<Dependency> getDependencyList( ArtifactItem item )
197 {
198 Dependency dep = new Dependency();
199 dep.setArtifactId( item.getArtifactId() );
200 dep.setClassifier( item.getClassifier() );
201 dep.setGroupId( item.getGroupId() );
202 dep.setType( item.getType() );
203 dep.setVersion( "2.0-SNAPSHOT" );
204
205 Dependency dep2 = new Dependency();
206 dep2.setArtifactId( item.getArtifactId() );
207 dep2.setClassifier( "classifier" );
208 dep2.setGroupId( item.getGroupId() );
209 dep2.setType( item.getType() );
210 dep2.setVersion( "2.1" );
211
212 List<Dependency> list = new ArrayList<Dependency>( 2 );
213 list.add( dep2 );
214 list.add( dep );
215
216 return list;
217 }
218
219 public void testMissingVersionFromDependencies()
220 throws MojoExecutionException
221 {
222 ArtifactItem item = new ArtifactItem();
223
224 item.setArtifactId( "artifactId" );
225 item.setClassifier( "" );
226 item.setGroupId( "groupId" );
227 item.setType( "jar" );
228
229 List<ArtifactItem> list = new ArrayList<ArtifactItem>();
230 list.add( item );
231 mojo.setArtifactItems( list );
232
233 MavenProject project = mojo.getProject();
234 project.setDependencies( getDependencyList( item ) );
235
236 mojo.execute();
237 assertMarkerFile( true, item );
238 assertEquals( "2.0-SNAPSHOT", item.getVersion() );
239 }
240
241 public void testMissingVersionFromDependenciesWithClassifier()
242 throws MojoExecutionException
243 {
244 ArtifactItem item = new ArtifactItem();
245
246 item.setArtifactId( "artifactId" );
247 item.setClassifier( "classifier" );
248 item.setGroupId( "groupId" );
249 item.setType( "war" );
250
251 List<ArtifactItem> list = new ArrayList<ArtifactItem>();
252 list.add( item );
253 mojo.setArtifactItems( list );
254
255 MavenProject project = mojo.getProject();
256 project.setDependencies( getDependencyList( item ) );
257
258 mojo.execute();
259 assertMarkerFile( true, item );
260 assertEquals( "2.1", item.getVersion() );
261 }
262
263 public List<Dependency> getDependencyMgtList( ArtifactItem item )
264 {
265 Dependency dep = new Dependency();
266 dep.setArtifactId( item.getArtifactId() );
267 dep.setClassifier( item.getClassifier() );
268 dep.setGroupId( item.getGroupId() );
269 dep.setType( item.getType() );
270 dep.setVersion( "3.0-SNAPSHOT" );
271
272 Dependency dep2 = new Dependency();
273 dep2.setArtifactId( item.getArtifactId() );
274 dep2.setClassifier( "classifier" );
275 dep2.setGroupId( item.getGroupId() );
276 dep2.setType( item.getType() );
277 dep2.setVersion( "3.1" );
278
279 List<Dependency> list = new ArrayList<Dependency>( 2 );
280 list.add( dep2 );
281 list.add( dep );
282
283 return list;
284 }
285
286 public void testMissingVersionFromDependencyMgt()
287 throws MojoExecutionException
288 {
289 ArtifactItem item = new ArtifactItem();
290
291 item.setArtifactId( "artifactId" );
292 item.setClassifier( "" );
293 item.setGroupId( "groupId" );
294 item.setType( "jar" );
295
296 MavenProject project = mojo.getProject();
297 project.setDependencies( getDependencyList( item ) );
298
299 item = new ArtifactItem();
300
301 item.setArtifactId( "artifactId-2" );
302 item.setClassifier( "" );
303 item.setGroupId( "groupId" );
304 item.setType( "jar" );
305
306 List<ArtifactItem> list = new ArrayList<ArtifactItem>();
307 list.add( item );
308
309 mojo.setArtifactItems( list );
310
311 project.getDependencyManagement().setDependencies( getDependencyMgtList( item ) );
312
313 mojo.execute();
314 assertMarkerFile( true, item );
315 assertEquals( "3.0-SNAPSHOT", item.getVersion() );
316 }
317
318 public void testMissingVersionFromDependencyMgtWithClassifier()
319 throws MojoExecutionException
320 {
321 ArtifactItem item = new ArtifactItem();
322
323 item.setArtifactId( "artifactId" );
324 item.setClassifier( "classifier" );
325 item.setGroupId( "groupId" );
326 item.setType( "jar" );
327
328 MavenProject project = mojo.getProject();
329 project.setDependencies( getDependencyList( item ) );
330
331 item = new ArtifactItem();
332
333 item.setArtifactId( "artifactId-2" );
334 item.setClassifier( "classifier" );
335 item.setGroupId( "groupId" );
336 item.setType( "jar" );
337
338 List<ArtifactItem> list = new ArrayList<ArtifactItem>();
339 list.add( item );
340
341 mojo.setArtifactItems( list );
342
343 project.getDependencyManagement().setDependencies( getDependencyMgtList( item ) );
344
345 mojo.execute();
346
347 assertMarkerFile( true, item );
348 assertEquals( "3.1", item.getVersion() );
349 }
350
351 public void testArtifactNotFound()
352 throws Exception
353 {
354 dotestArtifactExceptions( false, true );
355 }
356
357 public void testArtifactResolutionException()
358 throws Exception
359 {
360 dotestArtifactExceptions( true, false );
361 }
362
363 public void dotestArtifactExceptions( boolean are, boolean anfe )
364 throws Exception
365 {
366 ArtifactItem item = new ArtifactItem();
367
368 item.setArtifactId( "artifactId" );
369 item.setClassifier( "" );
370 item.setGroupId( "groupId" );
371 item.setType( "type" );
372 item.setVersion( "1.0" );
373
374 List<ArtifactItem> list = new ArrayList<ArtifactItem>();
375 list.add( item );
376 mojo.setArtifactItems( list );
377
378
379 mojo.setFactory( DependencyTestUtils.getArtifactFactory() );
380 mojo.setResolver( new StubArtifactResolver( null, are, anfe ) );
381 mojo.setLocal( new StubArtifactRepository( this.testDir.getAbsolutePath() ) );
382
383 try
384 {
385 mojo.execute();
386 fail( "ExpectedException" );
387 }
388 catch ( MojoExecutionException e )
389 {
390 if ( are )
391 {
392 assertEquals( "Unable to resolve artifact.", e.getMessage() );
393 }
394 else
395 {
396 assertEquals( "Unable to find artifact.", e.getMessage() );
397 }
398 }
399 }
400
401 public void testNoArtifactItems()
402 {
403 try
404 {
405 mojo.getProcessedArtifactItems( false );
406 fail( "Expected Exception" );
407 }
408 catch ( MojoExecutionException e )
409 {
410 assertEquals( "There are no artifactItems configured.", e.getMessage() );
411 }
412
413 }
414
415 public void testUnpackDontOverWriteReleases()
416 throws IOException, MojoExecutionException, InterruptedException
417 {
418 stubFactory.setCreateFiles( true );
419 Artifact release = stubFactory.getReleaseArtifact();
420 release.getFile().setLastModified( System.currentTimeMillis() - 2000 );
421
422 ArtifactItem item = new ArtifactItem( release );
423
424 List<ArtifactItem> list = new ArrayList<ArtifactItem>( 1 );
425 list.add( item );
426 mojo.setArtifactItems( list );
427
428 mojo.setOverWriteIfNewer( false );
429
430 mojo.execute();
431
432 assertUnpacked( item, false );
433 }
434
435 public void testUnpackDontOverWriteSnapshots()
436 throws IOException, MojoExecutionException, InterruptedException
437 {
438 stubFactory.setCreateFiles( true );
439 Artifact artifact = stubFactory.getSnapshotArtifact();
440 artifact.getFile().setLastModified( System.currentTimeMillis() - 2000 );
441
442 ArtifactItem item = new ArtifactItem( artifact );
443
444 List<ArtifactItem> list = new ArrayList<ArtifactItem>( 1 );
445 list.add( item );
446 mojo.setArtifactItems( list );
447
448 mojo.setOverWriteIfNewer( false );
449
450 mojo.execute();
451
452 assertUnpacked( item, false );
453 }
454
455 public void testUnpackOverWriteReleases()
456 throws IOException, MojoExecutionException, InterruptedException
457 {
458 stubFactory.setCreateFiles( true );
459 Artifact release = stubFactory.getReleaseArtifact();
460 release.getFile().setLastModified( System.currentTimeMillis() - 2000 );
461
462 ArtifactItem item = new ArtifactItem( release );
463
464 List<ArtifactItem> list = new ArrayList<ArtifactItem>( 1 );
465 list.add( item );
466 mojo.setArtifactItems( list );
467
468 mojo.setOverWriteIfNewer( false );
469 mojo.setOverWriteReleases( true );
470 mojo.execute();
471
472 assertUnpacked( item, true );
473 }
474
475 public void testUnpackOverWriteSnapshot()
476 throws IOException, MojoExecutionException, InterruptedException
477 {
478 stubFactory.setCreateFiles( true );
479 Artifact artifact = stubFactory.getSnapshotArtifact();
480 artifact.getFile().setLastModified( System.currentTimeMillis() - 2000 );
481
482 ArtifactItem item = new ArtifactItem( artifact );
483
484 List<ArtifactItem> list = new ArrayList<ArtifactItem>( 1 );
485 list.add( item );
486 mojo.setArtifactItems( list );
487
488 mojo.setOverWriteIfNewer( false );
489 mojo.setOverWriteReleases( false );
490 mojo.setOverWriteSnapshots( true );
491 mojo.execute();
492
493 assertUnpacked( item, true );
494 }
495
496 public void testUnpackOverWriteIfNewer()
497 throws IOException, MojoExecutionException, InterruptedException
498 {
499 mojo.silent = false;
500 stubFactory.setCreateFiles( true );
501 Artifact artifact = stubFactory.getSnapshotArtifact();
502 assertTrue( artifact.getFile().setLastModified( System.currentTimeMillis() - 2000 ) );
503
504 ArtifactItem item = new ArtifactItem( artifact );
505
506 List<ArtifactItem> list = new ArrayList<ArtifactItem>( 1 );
507 list.add( item );
508 mojo.setArtifactItems( list );
509 mojo.setOverWriteIfNewer( true );
510 mojo.execute();
511 File unpackedFile = getUnpackedFile( item );
512
513
514 long time = System.currentTimeMillis();
515 time = time - ( time % 1000 );
516
517 time -= 10000;
518
519 assertTrue( unpackedFile.setLastModified( time ) );
520
521 assertTrue( artifact.getFile().setLastModified( time + 5000 ) );
522
523
524 File marker = new File( mojo.getMarkersDirectory(), artifact.getId().replace( ':', '-' ) + ".marker" );
525 assertTrue( marker.setLastModified( time ) );
526
527 displayFile( "unpackedFile", unpackedFile );
528 displayFile( "artifact ", artifact.getFile() );
529 displayFile( "marker ", marker );
530 System.out.println( "mojo.execute()" );
531 mojo.execute();
532 displayFile( "unpackedFile", unpackedFile );
533 displayFile( "artifact ", artifact.getFile() );
534 displayFile( "marker ", marker );
535 System.out.println( "marker.lastModified() = " + time );
536 System.out.println( "unpackedFile.lastModified() = " + unpackedFile.lastModified() );
537 assertTrue( "unpackedFile '" + unpackedFile + "' lastModified() == " + time + ": should be different",
538 time != unpackedFile.lastModified() );
539 }
540
541 private void displayFile( String description, File file )
542 {
543 System.out.println( description + ' ' + DateFormatUtils.ISO_DATETIME_FORMAT.format( file.lastModified() ) + ' '
544 + file.getPath().substring( getBasedir().length() ) );
545 }
546
547 public void assertUnpacked( ArtifactItem item, boolean overWrite )
548 throws InterruptedException, MojoExecutionException
549 {
550
551 File unpackedFile = getUnpackedFile( item );
552
553 Thread.sleep( 100 );
554
555 long time = System.currentTimeMillis();
556 time = time - ( time % 1000 );
557 unpackedFile.setLastModified( time );
558
559 assertEquals( time, unpackedFile.lastModified() );
560 mojo.execute();
561
562 if ( overWrite )
563 {
564 assertTrue( time != unpackedFile.lastModified() );
565 }
566 else
567 {
568 assertEquals( time, unpackedFile.lastModified() );
569 }
570 }
571
572 public File getUnpackedFile( ArtifactItem item )
573 {
574 File unpackedFile =
575 new File( item.getOutputDirectory(),
576 DependencyArtifactStubFactory.getUnpackableFileName( item.getArtifact() ) );
577
578 assertTrue( unpackedFile.exists() );
579 return unpackedFile;
580
581 }
582 }