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