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