1 package org.apache.maven.plugin.assembly.archive.task;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.maven.artifact.Artifact;
23 import org.apache.maven.model.Model;
24 import org.apache.maven.plugin.assembly.InvalidAssemblerConfigurationException;
25 import org.apache.maven.plugin.assembly.archive.ArchiveCreationException;
26 import org.apache.maven.plugin.assembly.archive.DefaultAssemblyArchiverTest;
27 import org.apache.maven.plugin.assembly.archive.task.testutils.ArtifactMock;
28 import org.apache.maven.plugin.assembly.archive.task.testutils.MockAndControlForAddDependencySetsTask;
29 import org.apache.maven.plugin.assembly.format.AssemblyFormattingException;
30 import org.apache.maven.plugin.assembly.model.DependencySet;
31 import org.apache.maven.project.MavenProject;
32 import org.apache.maven.project.ProjectBuildingException;
33 import org.codehaus.plexus.logging.Logger;
34 import org.codehaus.plexus.logging.console.ConsoleLogger;
35
36 import java.io.File;
37 import java.io.IOException;
38 import java.util.Collections;
39 import java.util.HashSet;
40 import java.util.Set;
41
42 import junit.framework.TestCase;
43 import org.easymock.classextension.EasyMockSupport;
44
45 @SuppressWarnings( "OctalInteger" )
46 public class AddDependencySetsTaskTest
47 extends TestCase
48 {
49
50 private final EasyMockSupport mockManager = new EasyMockSupport();
51
52 public void testAddDependencySet_ShouldInterpolateDefaultOutputFileNameMapping()
53 throws AssemblyFormattingException, ArchiveCreationException, InvalidAssemblerConfigurationException,
54 IOException
55 {
56 final String outDir = "tmp/";
57 final String mainAid = "main";
58 final String mainGid = "org.maingrp";
59 final String mainVer = "9";
60 final String depAid = "dep";
61 final String depGid = "org.depgrp";
62 final String depVer = "1";
63 final String depExt = "war";
64
65 final DependencySet ds = new DependencySet();
66 ds.setOutputDirectory( outDir );
67 ds.setDirectoryMode( Integer.toString( 10, 8 ) );
68 ds.setFileMode( Integer.toString( 10, 8 ) );
69
70 final Model mainModel = new Model();
71 mainModel.setArtifactId( mainAid );
72 mainModel.setGroupId( mainGid );
73 mainModel.setVersion( mainVer );
74
75 final MavenProject mainProject = new MavenProject( mainModel );
76
77 final ArtifactMock mainArtifactMock = new ArtifactMock( mockManager, mainGid, mainAid, mainVer, "jar", false );
78
79 mainProject.setArtifact( mainArtifactMock.getArtifact() );
80
81 final Model depModel = new Model();
82 depModel.setArtifactId( depAid );
83 depModel.setGroupId( depGid );
84 depModel.setVersion( depVer );
85 depModel.setPackaging( depExt );
86
87 final MavenProject depProject = new MavenProject( depModel );
88
89 final ArtifactMock depArtifactMock = new ArtifactMock( mockManager, depGid, depAid, depVer, depExt, false );
90
91 final File newFile = depArtifactMock.setNewFile();
92
93 depProject.setArtifact( depArtifactMock.getArtifact() );
94
95 final MockAndControlForAddDependencySetsTask macTask =
96 new MockAndControlForAddDependencySetsTask( mockManager, mainProject );
97
98 macTask.expectBuildFromRepository( depProject );
99 macTask.expectCSGetFinalName( mainAid + "-" + mainVer );
100
101 macTask.expectCSGetRepositories( null, null );
102
103 macTask.expectGetDestFile( new File( "junk" ) );
104 macTask.expectAddFile( newFile, outDir + depAid + "-" + depVer + "." + depExt, 10 );
105
106 macTask.expectGetSession( null );
107 macTask.expectGetMode( 0222, 0222 );
108
109 DefaultAssemblyArchiverTest.setupInterpolators( macTask.configSource );
110
111
112 mockManager.replayAll();
113
114 final Logger logger = new ConsoleLogger( Logger.LEVEL_DEBUG, "test" );
115
116 final AddDependencySetsTask task =
117 new AddDependencySetsTask( Collections.singletonList( ds ),
118 Collections.singleton( depArtifactMock.getArtifact() ), depProject,
119 macTask.projectBuilder, logger );
120
121 task.addDependencySet( ds, macTask.archiver, macTask.configSource );
122
123 mockManager.verifyAll();
124 }
125
126 public void testAddDependencySet_ShouldNotAddDependenciesWhenProjectHasNone()
127 throws AssemblyFormattingException, ArchiveCreationException, InvalidAssemblerConfigurationException
128 {
129 final MavenProject project = new MavenProject( new Model() );
130
131 final MockAndControlForAddDependencySetsTask macTask = new MockAndControlForAddDependencySetsTask( mockManager );
132
133 final DependencySet ds = new DependencySet();
134 ds.setOutputDirectory( "/out" );
135
136 mockManager.replayAll();
137
138 final Logger logger = new ConsoleLogger( Logger.LEVEL_DEBUG, "test" );
139
140 final AddDependencySetsTask task =
141 new AddDependencySetsTask( Collections.singletonList( ds ), null, project, macTask.projectBuilder, logger );
142
143 task.addDependencySet( ds, null, macTask.configSource );
144
145 mockManager.verifyAll();
146 }
147
148
149 public void testAddDependencySet_ShouldNotAddDependenciesWhenProjectIsStubbed()
150 throws AssemblyFormattingException, ArchiveCreationException, InvalidAssemblerConfigurationException,
151 IOException
152 {
153 final MavenProject project = new MavenProject( new Model() );
154
155 final ProjectBuildingException pbe = new ProjectBuildingException( "test", "Test error." );
156
157 final MockAndControlForAddDependencySetsTask macTask =
158 new MockAndControlForAddDependencySetsTask( mockManager, new MavenProject( new Model() ) );
159
160 final String gid = "org.test";
161 final String aid = "test-dep";
162 final String version = "2.0-SNAPSHOT";
163 final String type = "jar";
164
165 final File file = new File( "dep-artifact.jar" );
166
167 final ArtifactMock depMock = new ArtifactMock( mockManager, gid, aid, version, type, true );
168 depMock.setBaseVersion( version );
169 depMock.setFile( file );
170
171 final File destFile = new File( "assembly-dep-set.zip" );
172
173 macTask.expectGetDestFile( destFile );
174 macTask.expectBuildFromRepository( pbe );
175 macTask.expectCSGetRepositories( null, null );
176 macTask.expectCSGetFinalName( "final-name" );
177 macTask.expectAddFile( file, "out/" + aid + "-" + version + "." + type );
178
179 macTask.expectGetMode( 0222, 0222 );
180 macTask.expectGetSession( null );
181
182 final DependencySet ds = new DependencySet();
183 ds.setOutputDirectory( "/out" );
184 DefaultAssemblyArchiverTest.setupInterpolators( macTask.configSource );
185
186 mockManager.replayAll();
187
188 final Logger logger = new ConsoleLogger( Logger.LEVEL_DEBUG, "test" );
189
190 final AddDependencySetsTask task =
191 new AddDependencySetsTask( Collections.singletonList( ds ), Collections.singleton( depMock.getArtifact() ),
192 project, macTask.projectBuilder, logger );
193
194 task.addDependencySet( ds, macTask.archiver, macTask.configSource );
195
196 mockManager.verifyAll();
197 }
198
199 public void testAddDependencySet_ShouldAddOneDependencyFromProjectWithoutUnpacking()
200 throws AssemblyFormattingException, ArchiveCreationException, IOException,
201 InvalidAssemblerConfigurationException
202 {
203 verifyOneDependencyAdded( "out", false );
204 }
205
206 public void testAddDependencySet_ShouldAddOneDependencyFromProjectUnpacked()
207 throws AssemblyFormattingException, ArchiveCreationException, IOException,
208 InvalidAssemblerConfigurationException
209 {
210 verifyOneDependencyAdded( "out", true );
211 }
212
213 private void verifyOneDependencyAdded( final String outputLocation, final boolean unpack )
214 throws AssemblyFormattingException, ArchiveCreationException, IOException,
215 InvalidAssemblerConfigurationException
216 {
217 final MavenProject project = new MavenProject( new Model() );
218
219 final DependencySet ds = new DependencySet();
220 ds.setOutputDirectory( outputLocation );
221 ds.setOutputFileNameMapping( "artifact" );
222 ds.setUnpack( unpack );
223 ds.setScope( Artifact.SCOPE_COMPILE );
224
225 ds.setDirectoryMode( Integer.toString( 10, 8 ) );
226 ds.setFileMode( Integer.toString( 10, 8 ) );
227
228 final MockAndControlForAddDependencySetsTask macTask =
229 new MockAndControlForAddDependencySetsTask( mockManager, new MavenProject( new Model() ) );
230
231 final ArtifactMock artifactMock = new ArtifactMock( mockManager, "group", "artifact", "version", "jar", false );
232 final File artifactFile = artifactMock.setNewFile();
233
234 if ( unpack )
235 {
236 macTask.expectAddArchivedFileSet();
237
238 }
239 else
240 {
241 macTask.expectAddFile( artifactFile, outputLocation + "/artifact", 10 );
242 }
243
244 macTask.expectGetDestFile( new File( "junk" ) );
245 macTask.expectCSGetFinalName( "final-name" );
246 macTask.expectCSGetRepositories( null, null );
247
248 macTask.expectGetSession( null );
249
250 final MavenProject depProject = new MavenProject( new Model() );
251
252 macTask.expectBuildFromRepository( depProject );
253 macTask.expectGetMode( 0222, 0222 );
254
255 final Logger logger = new ConsoleLogger( Logger.LEVEL_DEBUG, "test" );
256
257 final AddDependencySetsTask task =
258 new AddDependencySetsTask( Collections.singletonList( ds ),
259 Collections.singleton( artifactMock.getArtifact() ), project,
260 macTask.projectBuilder, logger );
261 DefaultAssemblyArchiverTest.setupInterpolators( macTask.configSource );
262
263 mockManager.replayAll();
264
265 task.addDependencySet( ds, macTask.archiver, macTask.configSource );
266
267 mockManager.verifyAll();
268 }
269
270 public void testGetDependencyArtifacts_ShouldGetOneDependencyArtifact()
271 throws ArchiveCreationException, InvalidAssemblerConfigurationException
272 {
273 final MavenProject project = new MavenProject( new Model() );
274
275 final MockAndControlForAddDependencySetsTask macTask = new MockAndControlForAddDependencySetsTask( mockManager );
276
277 final ArtifactMock artifactMock = new ArtifactMock( mockManager, "group", "artifact", "version", "jar", false );
278
279 project.setArtifacts( Collections.singleton( artifactMock.getArtifact() ) );
280
281 final DependencySet dependencySet = new DependencySet();
282
283 final Logger logger = new ConsoleLogger( Logger.LEVEL_DEBUG, "test" );
284
285 mockManager.replayAll();
286
287 final AddDependencySetsTask task =
288 new AddDependencySetsTask( Collections.singletonList( dependencySet ),
289 Collections.singleton( artifactMock.getArtifact() ), project,
290 macTask.projectBuilder, logger );
291
292 final Set<Artifact> result = task.resolveDependencyArtifacts( dependencySet );
293
294 assertNotNull( result );
295 assertEquals( 1, result.size() );
296 assertSame( artifactMock.getArtifact(), result.iterator()
297 .next() );
298
299 mockManager.verifyAll();
300 }
301
302 public void testGetDependencyArtifacts_ShouldFilterOneDependencyArtifactViaInclude()
303 throws ArchiveCreationException, InvalidAssemblerConfigurationException
304 {
305 final MavenProject project = new MavenProject( new Model() );
306
307 final Set<Artifact> artifacts = new HashSet<Artifact>();
308
309 final ArtifactMock am = new ArtifactMock( mockManager, "group", "artifact", "1.0", "jar", false );
310 am.setDependencyTrail( Collections.singletonList( project.getId() ) );
311 artifacts.add( am.getArtifact() );
312
313 final ArtifactMock am2 = new ArtifactMock( mockManager, "group2", "artifact2", "1.0", "jar", false );
314 am2.setDependencyTrail( Collections.singletonList( project.getId() ) );
315 artifacts.add( am2.getArtifact() );
316
317 final DependencySet dependencySet = new DependencySet();
318
319 dependencySet.addInclude( "group:artifact" );
320 dependencySet.setUseTransitiveFiltering( true );
321
322 final Logger logger = new ConsoleLogger( Logger.LEVEL_DEBUG, "test" );
323
324 mockManager.replayAll();
325
326 final AddDependencySetsTask task =
327 new AddDependencySetsTask( Collections.singletonList( dependencySet ), artifacts, project, null, logger );
328
329 final Set<Artifact> result = task.resolveDependencyArtifacts( dependencySet );
330
331 assertNotNull( result );
332 assertEquals( 1, result.size() );
333 assertSame( am.getArtifact(), result.iterator()
334 .next() );
335
336 mockManager.verifyAll();
337 }
338
339 public void testGetDependencyArtifacts_ShouldIgnoreTransitivePathFilteringWhenIncludeNotTransitive()
340 throws ArchiveCreationException, InvalidAssemblerConfigurationException
341 {
342 final MavenProject project = new MavenProject( new Model() );
343
344 final Set<Artifact> artifacts = new HashSet<Artifact>();
345
346 final ArtifactMock am = new ArtifactMock( mockManager, "group", "artifact", "1.0", "jar", false );
347 artifacts.add( am.getArtifact() );
348
349 final ArtifactMock am2 = new ArtifactMock( mockManager, "group2", "artifact2", "1.0", "jar", false );
350 artifacts.add( am2.getArtifact() );
351
352 final DependencySet dependencySet = new DependencySet();
353
354 dependencySet.addInclude( "group:artifact" );
355 dependencySet.setUseTransitiveFiltering( false );
356
357 final Logger logger = new ConsoleLogger( Logger.LEVEL_DEBUG, "test" );
358
359 mockManager.replayAll();
360
361 final AddDependencySetsTask task =
362 new AddDependencySetsTask( Collections.singletonList( dependencySet ), artifacts, project, null, logger );
363
364 final Set<Artifact> result = task.resolveDependencyArtifacts( dependencySet );
365
366 assertNotNull( result );
367 assertEquals( 1, result.size() );
368 assertSame( am.getArtifact(), result.iterator()
369 .next() );
370
371 mockManager.verifyAll();
372 }
373
374 }