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