1 package org.apache.maven.plugins.assembly.archive;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertFalse;
24 import static org.junit.Assert.assertNull;
25 import static org.junit.Assert.fail;
26 import static org.mockito.ArgumentMatchers.any;
27 import static org.mockito.ArgumentMatchers.eq;
28 import static org.mockito.Mockito.atLeastOnce;
29 import static org.mockito.Mockito.mock;
30 import static org.mockito.Mockito.times;
31 import static org.mockito.Mockito.verify;
32 import static org.mockito.Mockito.when;
33
34 import java.io.File;
35 import java.io.IOException;
36 import java.util.ArrayList;
37 import java.util.Collections;
38 import java.util.List;
39
40 import org.apache.maven.model.Model;
41 import org.apache.maven.plugins.assembly.AssemblerConfigurationSource;
42 import org.apache.maven.plugins.assembly.InvalidAssemblerConfigurationException;
43 import org.apache.maven.plugins.assembly.archive.phase.AssemblyArchiverPhase;
44 import org.apache.maven.plugins.assembly.model.Assembly;
45 import org.apache.maven.plugins.assembly.mojos.AbstractAssemblyMojo;
46 import org.apache.maven.project.MavenProject;
47 import org.codehaus.plexus.DefaultPlexusContainer;
48 import org.codehaus.plexus.PlexusContainer;
49 import org.codehaus.plexus.PlexusContainerException;
50 import org.codehaus.plexus.archiver.Archiver;
51 import org.codehaus.plexus.archiver.ArchiverException;
52 import org.codehaus.plexus.archiver.diags.NoOpArchiver;
53 import org.codehaus.plexus.archiver.manager.ArchiverManager;
54 import org.codehaus.plexus.archiver.tar.TarArchiver;
55 import org.codehaus.plexus.archiver.tar.TarLongFileMode;
56 import org.codehaus.plexus.archiver.war.WarArchiver;
57 import org.codehaus.plexus.archiver.zip.ZipArchiver;
58 import org.codehaus.plexus.interpolation.fixed.FixedStringSearchInterpolator;
59 import org.codehaus.plexus.logging.Logger;
60 import org.codehaus.plexus.logging.console.ConsoleLogger;
61 import org.junit.Before;
62 import org.junit.Rule;
63 import org.junit.Test;
64 import org.junit.rules.TemporaryFolder;
65 import org.junit.runner.RunWith;
66 import org.mockito.junit.MockitoJUnitRunner;
67
68 @RunWith( MockitoJUnitRunner.class )
69 public class DefaultAssemblyArchiverTest
70 {
71 @Rule
72 public TemporaryFolder temporaryFolder = new TemporaryFolder();
73
74 private PlexusContainer container;
75
76 public static void setupInterpolators( AssemblerConfigurationSource configSource )
77 {
78 when( configSource.getRepositoryInterpolator() ).thenReturn( FixedStringSearchInterpolator.create() );
79 when( configSource.getCommandLinePropsInterpolator() ).thenReturn( FixedStringSearchInterpolator.create() );
80 when( configSource.getEnvInterpolator() ).thenReturn( FixedStringSearchInterpolator.create() );
81 }
82
83 public static void setupInterpolators( AssemblerConfigurationSource configSource, MavenProject mavenProject )
84 {
85 when( configSource.getCommandLinePropsInterpolator() ).thenReturn( FixedStringSearchInterpolator.create() );
86 when( configSource.getEnvInterpolator() ).thenReturn( FixedStringSearchInterpolator.create() );
87 when( configSource.getMainProjectInterpolator() ).thenReturn( AbstractAssemblyMojo.mainProjectInterpolator( mavenProject ) );
88 }
89
90 @Before
91 public void setup()
92 throws PlexusContainerException
93 {
94 this.container = new DefaultPlexusContainer();
95 }
96
97 @Test( expected = InvalidAssemblerConfigurationException.class )
98 public void failWhenAssemblyIdIsNull()
99 throws Exception
100 {
101 final DefaultAssemblyArchiver archiver = createSubject( null, null, null );
102 archiver.createArchive( new Assembly(), "full-name", "zip", null, false, null, null );
103 }
104
105 @Test
106 public void testCreateArchive()
107 throws Exception
108 {
109 Archiver archiver = mock( Archiver.class );
110
111 final ArchiverManager archiverManager = mock( ArchiverManager.class );
112 when( archiverManager.getArchiver( "zip" ) ).thenReturn( archiver );
113
114 final AssemblyArchiverPhase phase = mock( AssemblyArchiverPhase.class );
115
116 final File outDir = temporaryFolder.newFolder( "out" );
117
118 final AssemblerConfigurationSource configSource = mock( AssemblerConfigurationSource.class );
119 when( configSource.getTemporaryRootDirectory() ).thenReturn( new File ( temporaryFolder.getRoot(), "temp" ) );
120 when( configSource.getOverrideUid() ).thenReturn( 0 );
121 when( configSource.getOverrideUserName() ).thenReturn( "root" );
122 when( configSource.getOverrideGid() ).thenReturn( 0 );
123 when( configSource.getOverrideGroupName() ).thenReturn( "root" );
124 when( configSource.getOutputDirectory() ).thenReturn( outDir );
125 when( configSource.getFinalName() ).thenReturn( "finalName" );
126 when( configSource.getWorkingDirectory() ).thenReturn( new File( "." ) );
127
128 final Assembly assembly = new Assembly();
129 assembly.setId( "id" );
130
131 final DefaultAssemblyArchiver subject = createSubject( archiverManager, Collections.singletonList( phase ), null );
132
133 subject.createArchive( assembly, "full-name", "zip", configSource, false, null, null );
134
135
136 verify( configSource ).getArchiverConfig();
137 verify( configSource ).getFinalName();
138 verify( configSource ).getOutputDirectory();
139 verify( configSource, atLeastOnce() ).getOverrideUid();
140 verify( configSource, atLeastOnce() ).getOverrideUserName();
141 verify( configSource, atLeastOnce() ).getOverrideGid();
142 verify( configSource, atLeastOnce() ).getOverrideGroupName();
143 verify( configSource ).getTemporaryRootDirectory();
144 verify( configSource ).getWorkingDirectory();
145 verify( configSource ).isDryRun();
146 verify( configSource ).isIgnoreDirFormatExtensions();
147 verify( configSource ).isIgnorePermissions();
148 verify( configSource, times( 2 ) ).isUpdateOnly();
149
150 verify( phase ).execute( eq( assembly ), any( Archiver.class ), eq( configSource ) );
151
152 verify( archiver ).createArchive();
153 verify( archiver ).setDestFile( new File( outDir, "full-name.zip" ) );
154 verify( archiver, times( 2 ) ).setForced( true );
155 verify( archiver ).setIgnorePermissions( false );
156 verify( archiver ).setOverrideUid( 0 );
157 verify( archiver ).setOverrideUserName( "root" );
158 verify( archiver ).setOverrideGid( 0 );
159 verify( archiver ).setOverrideGroupName( "root" );
160
161 verify( archiverManager ).getArchiver( "zip" );
162 }
163
164 @Test
165 public void testCreateArchiver_ShouldConfigureArchiver()
166 throws Exception
167 {
168 final TestArchiverWithConfig archiver = new TestArchiverWithConfig();
169
170 final ArchiverManager archiverManager = mock( ArchiverManager.class );
171 when( archiverManager.getArchiver( "dummy" ) ).thenReturn( archiver );
172
173 final AssemblerConfigurationSource configSource = mock( AssemblerConfigurationSource.class );
174
175 final String simpleConfig = "value";
176
177 when( configSource.getArchiverConfig() ).thenReturn(
178 "<configuration><simpleConfig>" + simpleConfig + "</simpleConfig></configuration>" );
179
180 final MavenProject project = new MavenProject( new Model() );
181
182 when( configSource.getProject() ).thenReturn( project );
183 when( configSource.getWorkingDirectory() ).thenReturn( new File( "." ) );
184
185 when( configSource.isIgnorePermissions() ).thenReturn( true );
186 setupInterpolators( configSource );
187
188 when( configSource.getOverrideUid() ).thenReturn( 0 );
189 when( configSource.getOverrideUserName() ).thenReturn( "root" );
190 when( configSource.getOverrideGid() ).thenReturn( 0 );
191 when( configSource.getOverrideGroupName() ).thenReturn( "root" );
192
193 final DefaultAssemblyArchiver subject =
194 createSubject( archiverManager, new ArrayList<AssemblyArchiverPhase>(), null );
195
196 subject.createArchiver( "dummy", false, "finalName", configSource, null, false, null, null );
197
198 assertEquals( simpleConfig, archiver.getSimpleConfig() );
199
200
201 verify( archiverManager ).getArchiver( "dummy" );
202 }
203
204 @Test
205 public void testCreateArchiver_ShouldCreateTarArchiverWithNoCompression()
206 throws Exception
207 {
208 final TestTarArchiver ttArchiver = new TestTarArchiver();
209
210 final ArchiverManager archiverManager = mock( ArchiverManager.class );
211 when( archiverManager.getArchiver( "tar" ) ).thenReturn( ttArchiver );
212
213 final AssemblerConfigurationSource configSource = mock( AssemblerConfigurationSource.class );
214 when( configSource.getTarLongFileMode() ).thenReturn( TarLongFileMode.fail.toString() );
215 when( configSource.getWorkingDirectory() ).thenReturn( new File( "." ) );
216 when( configSource.isIgnorePermissions() ).thenReturn( true );
217 when( configSource.getOverrideUid() ).thenReturn( 0 );
218 when( configSource.getOverrideUserName() ).thenReturn( "root" );
219 when( configSource.getOverrideGid() ).thenReturn( 0 );
220 when( configSource.getOverrideGroupName() ).thenReturn( "root" );
221
222 final DefaultAssemblyArchiver subject = createSubject( archiverManager, new ArrayList<AssemblyArchiverPhase>(), null );
223
224 subject.createArchiver( "tar", false, "finalName", configSource, null, false, null, null );
225
226 assertNull( ttArchiver.compressionMethod );
227 assertEquals( TarLongFileMode.fail, ttArchiver.longFileMode );
228
229
230 verify( configSource ).getArchiverConfig();
231 verify( configSource, times( 2 ) ).getOverrideGid();
232 verify( configSource, times( 2 ) ).getOverrideGroupName();
233 verify( configSource, times( 2 ) ).getOverrideUid();
234 verify( configSource, times( 2 ) ).getOverrideUserName();
235 verify( configSource ).getTarLongFileMode();
236 verify( configSource ).getWorkingDirectory();
237 verify( configSource ).isDryRun();
238 verify( configSource ).isIgnorePermissions();
239 verify( configSource, times( 2 ) ).isUpdateOnly();
240
241 verify( archiverManager ).getArchiver( "tar" );
242 }
243
244 @Test
245 public void testCreateArchiver_ShouldCreateWarArchiverWithIgnoreWebxmlSetToFalse()
246 throws Exception
247 {
248 final TestWarArchiver twArchiver = new TestWarArchiver();
249
250 final ArchiverManager archiverManager = mock( ArchiverManager.class );
251 when( archiverManager.getArchiver( "war" ) ).thenReturn( twArchiver );
252
253 final AssemblerConfigurationSource configSource = mock( AssemblerConfigurationSource.class );
254 when( configSource.getOverrideGid() ).thenReturn( 0 );
255 when( configSource.getOverrideGroupName() ).thenReturn( "root" );
256 when( configSource.getOverrideUid() ).thenReturn( 0 );
257 when( configSource.getOverrideUserName() ).thenReturn( "root" );
258 when( configSource.getProject() ).thenReturn( new MavenProject( new Model() ) );
259 when( configSource.getWorkingDirectory() ).thenReturn( new File( "." ) );
260 when( configSource.isIgnorePermissions() ).thenReturn( true );
261
262 final DefaultAssemblyArchiver subject = createSubject( archiverManager, new ArrayList<AssemblyArchiverPhase>(), null );
263
264 subject.createArchiver( "war", false, null, configSource, null, false, null, null );
265
266 assertFalse( twArchiver.ignoreWebxml );
267
268
269 verify( configSource ).getArchiverConfig();
270 verify( configSource ).getJarArchiveConfiguration();
271 verify( configSource ).getMavenSession();
272 verify( configSource, times( 2 ) ).getOverrideGid();
273 verify( configSource, times( 2 ) ).getOverrideGroupName();
274 verify( configSource, times( 2 ) ).getOverrideUid();
275 verify( configSource, times( 2 ) ).getOverrideUserName();
276 verify( configSource ).getProject();
277 verify( configSource ).getWorkingDirectory();
278 verify( configSource ).isDryRun();
279 verify( configSource ).isIgnorePermissions();
280 verify( configSource, times( 2 ) ).isUpdateOnly();
281
282 verify( archiverManager ).getArchiver( "war" );
283 }
284
285 @Test
286 public void testCreateArchiver_ShouldCreateZipArchiver()
287 throws Exception
288 {
289 final ZipArchiver archiver = new ZipArchiver();
290
291 final ArchiverManager archiverManager = mock( ArchiverManager.class );
292 when( archiverManager.getArchiver( "zip" ) ).thenReturn( archiver );
293
294 final AssemblerConfigurationSource configSource = mock( AssemblerConfigurationSource.class );
295 when( configSource.getOverrideGid() ).thenReturn( 0 );
296 when( configSource.getOverrideGroupName() ).thenReturn( "root" );
297 when( configSource.getOverrideUid() ).thenReturn( 0 );
298 when( configSource.getOverrideUserName() ).thenReturn( "root" );
299 when( configSource.getWorkingDirectory() ).thenReturn( new File( "." ) );
300 when( configSource.isIgnorePermissions() ).thenReturn( true );
301
302 final DefaultAssemblyArchiver subject =
303 createSubject( archiverManager, new ArrayList<AssemblyArchiverPhase>(), null );
304
305 subject.createArchiver( "zip", false, null, configSource, null, false, null, null );
306
307
308 verify( configSource ).getArchiverConfig();
309 verify( configSource, times( 2 ) ).getOverrideGid();
310 verify( configSource, times( 2 ) ).getOverrideGroupName();
311 verify( configSource, times( 2 ) ).getOverrideUid();
312 verify( configSource, times( 2 ) ).getOverrideUserName();
313 verify( configSource ).getWorkingDirectory();
314 verify( configSource ).isDryRun();
315 verify( configSource ).isIgnorePermissions();
316 verify( configSource, times( 2 ) ).isUpdateOnly();
317
318 verify( archiverManager ).getArchiver( "zip" );
319 }
320
321 @Test
322 public void testCreateWarArchiver_ShouldDisableIgnoreWebxmlOption()
323 throws Exception
324 {
325 final TestWarArchiver twArchiver = new TestWarArchiver();
326
327 final ArchiverManager archiverManager = mock( ArchiverManager.class );
328 when( archiverManager.getArchiver( "war" ) ).thenReturn( twArchiver );
329
330 final DefaultAssemblyArchiver subject =
331 createSubject( archiverManager, new ArrayList<AssemblyArchiverPhase>(), null );
332
333 subject.createWarArchiver();
334
335 assertFalse( twArchiver.ignoreWebxml );
336
337
338 verify( archiverManager ).getArchiver( "war" );
339 }
340
341 @Test
342 public void testCreateTarArchiver_ShouldNotInitializeCompression()
343 throws Exception
344 {
345 final TestTarArchiver archiver = new TestTarArchiver();
346
347 final ArchiverManager archiverManager = mock( ArchiverManager.class );
348 when( archiverManager.getArchiver( "tar" ) ).thenReturn( archiver );
349
350 final DefaultAssemblyArchiver subject = createSubject( archiverManager, new ArrayList<AssemblyArchiverPhase>(), null );
351
352 subject.createTarArchiver( "tar", TarLongFileMode.fail );
353
354 assertNull( new TestTarArchiver().compressionMethod );
355 assertEquals( TarLongFileMode.fail, archiver.longFileMode );
356
357
358 verify( archiverManager ).getArchiver( "tar" );
359 }
360
361 @Test
362 public void testCreateTarArchiver_TarGzFormat_ShouldInitializeGZipCompression()
363 throws Exception
364 {
365 final TestTarArchiver archiver = new TestTarArchiver();
366
367 final ArchiverManager archiverManager = mock( ArchiverManager.class );
368 when( archiverManager.getArchiver( "tar" ) ).thenReturn( archiver );
369
370 final DefaultAssemblyArchiver subject = createSubject( archiverManager, new ArrayList<AssemblyArchiverPhase>(), null );
371
372 subject.createTarArchiver( "tar.gz", TarLongFileMode.fail );
373
374 assertEquals( TarArchiver.TarCompressionMethod.gzip, archiver.compressionMethod );
375 assertEquals( TarLongFileMode.fail, archiver.longFileMode );
376
377
378 verify( archiverManager ).getArchiver( "tar" );
379 }
380
381 @Test
382 public void testCreateTarArchiver_TgzFormat_ShouldInitializeGZipCompression()
383 throws Exception
384 {
385 final TestTarArchiver archiver = new TestTarArchiver();
386
387 final ArchiverManager archiverManager = mock( ArchiverManager.class );
388 when( archiverManager.getArchiver( "tar" ) ).thenReturn( archiver );
389
390 final DefaultAssemblyArchiver subject = createSubject( archiverManager, new ArrayList<AssemblyArchiverPhase>(), null );
391
392 subject.createTarArchiver( "tgz", TarLongFileMode.fail );
393
394 assertEquals( TarArchiver.TarCompressionMethod.gzip, archiver.compressionMethod );
395 assertEquals( TarLongFileMode.fail, archiver.longFileMode );
396
397
398 verify( archiverManager ).getArchiver( "tar" );
399 }
400
401 @Test
402 public void testCreateTarArchiver_TarBz2Format_ShouldInitializeBZipCompression()
403 throws Exception
404 {
405 final TestTarArchiver archiver = new TestTarArchiver();
406
407 final ArchiverManager archiverManager = mock( ArchiverManager.class );
408 when( archiverManager.getArchiver( "tar" ) ).thenReturn( archiver );
409
410 final DefaultAssemblyArchiver subject = createSubject( archiverManager, new ArrayList<AssemblyArchiverPhase>(), null );
411
412 subject.createTarArchiver( "tar.bz2", TarLongFileMode.fail );
413
414 assertEquals( TarArchiver.TarCompressionMethod.bzip2, archiver.compressionMethod );
415 assertEquals( TarLongFileMode.fail, archiver.longFileMode );
416
417
418 verify( archiverManager ).getArchiver( "tar" );
419 }
420
421 @Test
422 public void testCreateTarArchiver_Tbz2Format_ShouldInitializeBZipCompression()
423 throws Exception
424 {
425 final TestTarArchiver archiver = new TestTarArchiver();
426
427 final ArchiverManager archiverManager = mock( ArchiverManager.class );
428 when( archiverManager.getArchiver( "tar" ) ).thenReturn( archiver );
429
430 final DefaultAssemblyArchiver subject = createSubject( archiverManager, new ArrayList<AssemblyArchiverPhase>(), null );
431
432 subject.createTarArchiver( "tbz2", TarLongFileMode.fail );
433
434 assertEquals( TarArchiver.TarCompressionMethod.bzip2, archiver.compressionMethod );
435 assertEquals( TarLongFileMode.fail, archiver.longFileMode );
436
437
438 verify( archiverManager ).getArchiver( "tar" );
439 }
440
441 @Test
442 public void testCreateTarArchiver_TarXzFormat_ShouldInitializeXzCompression()
443 throws Exception
444 {
445 final TestTarArchiver archiver = new TestTarArchiver();
446
447 final ArchiverManager archiverManager = mock( ArchiverManager.class );
448 when( archiverManager.getArchiver( "tar" ) ).thenReturn( archiver );
449
450 final DefaultAssemblyArchiver subject = createSubject( archiverManager, new ArrayList<AssemblyArchiverPhase>(), null );
451
452 subject.createTarArchiver( "tar.xz", TarLongFileMode.fail );
453
454 assertEquals( TarArchiver.TarCompressionMethod.xz, archiver.compressionMethod );
455 assertEquals( TarLongFileMode.fail, archiver.longFileMode );
456
457
458 verify( archiverManager ).getArchiver( "tar" );
459 }
460
461 @Test
462 public void testCreateTarArchiver_TXzFormat_ShouldInitializeXzCompression()
463 throws Exception
464 {
465 final TestTarArchiver archiver = new TestTarArchiver();
466
467 final ArchiverManager archiverManager = mock( ArchiverManager.class );
468 when( archiverManager.getArchiver( "tar" ) ).thenReturn( archiver );
469
470 final DefaultAssemblyArchiver subject = createSubject( archiverManager, new ArrayList<AssemblyArchiverPhase>(), null );
471
472 subject.createTarArchiver( "txz", TarLongFileMode.fail );
473
474 assertEquals( TarArchiver.TarCompressionMethod.xz, archiver.compressionMethod );
475 assertEquals( TarLongFileMode.fail, archiver.longFileMode );
476
477
478 verify( archiverManager ).getArchiver( "tar" );
479 }
480
481 @Test
482 public void testCreateTarArchiver_InvalidFormat_ShouldFailWithInvalidCompression()
483 throws Exception
484 {
485 final TestTarArchiver ttArchiver = new TestTarArchiver();
486
487 final ArchiverManager archiverManager = mock( ArchiverManager.class );
488 when( archiverManager.getArchiver( "tar" ) ).thenReturn( ttArchiver );
489
490 final DefaultAssemblyArchiver subject = createSubject( archiverManager, new ArrayList<AssemblyArchiverPhase>(), null );
491
492 try
493 {
494 subject.createTarArchiver( "tar.Z", null );
495
496 fail( "Invalid compression formats should throw an error." );
497 }
498 catch ( final IllegalArgumentException e )
499 {
500
501 }
502
503
504 verify( archiverManager ).getArchiver( "tar" );
505 }
506
507 private DefaultAssemblyArchiver createSubject( final ArchiverManager archiverManager,
508 final List<AssemblyArchiverPhase> phases, Logger logger )
509 {
510 final DefaultAssemblyArchiver subject = new DefaultAssemblyArchiver( archiverManager, phases );
511
512 subject.setContainer( container );
513
514 if ( logger == null )
515 {
516 logger = new ConsoleLogger( Logger.LEVEL_DEBUG, "test" );
517 }
518
519 subject.enableLogging( logger );
520
521 return subject;
522 }
523
524 private static final class TestTarArchiver
525 extends TarArchiver
526 {
527
528 TarCompressionMethod compressionMethod;
529
530 TarLongFileMode longFileMode;
531
532 @Override
533 protected void execute()
534 throws ArchiverException, IOException
535 {
536 super.createArchive();
537 }
538
539 @Override
540 public void setCompression( final TarCompressionMethod mode )
541 {
542 compressionMethod = mode;
543 super.setCompression( mode );
544 }
545
546 @Override
547 public void setLongfile( final TarLongFileMode mode )
548 {
549 longFileMode = mode;
550 super.setLongfile( mode );
551 }
552
553 }
554
555 private static final class TestWarArchiver
556 extends WarArchiver
557 {
558
559 boolean ignoreWebxml;
560
561 @Override
562 public void setIgnoreWebxml( final boolean ignore )
563 {
564 ignoreWebxml = ignore;
565 super.setIgnoreWebxml( ignore );
566 }
567
568 }
569
570 public static final class TestArchiverWithConfig
571 extends NoOpArchiver
572 {
573
574 private String simpleConfig;
575
576 public String getSimpleConfig()
577 {
578 return simpleConfig;
579 }
580
581
582 public String getDuplicateBehavior()
583 {
584 return Archiver.DUPLICATES_ADD;
585 }
586 }
587
588 }