View Javadoc
1   package org.apache.maven.plugin.assembly.archive.task;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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  public class AddDependencySetsTaskTest
46      extends TestCase
47  {
48  
49      private final EasyMockSupport mockManager = new EasyMockSupport();
50  
51      public void testAddDependencySet_ShouldInterpolateDefaultOutputFileNameMapping()
52          throws AssemblyFormattingException, ArchiveCreationException, InvalidAssemblerConfigurationException,
53          IOException
54      {
55          final String outDir = "tmp/";
56          final String mainAid = "main";
57          final String mainGid = "org.maingrp";
58          final String mainVer = "9";
59          final String depAid = "dep";
60          final String depGid = "org.depgrp";
61          final String depVer = "1";
62          final String depExt = "war";
63  
64          final DependencySet ds = new DependencySet();
65          ds.setOutputDirectory( outDir );
66          ds.setDirectoryMode( Integer.toString( 10, 8 ) );
67          ds.setFileMode( Integer.toString( 10, 8 ) );
68  
69          final Model mainModel = new Model();
70          mainModel.setArtifactId( mainAid );
71          mainModel.setGroupId( mainGid );
72          mainModel.setVersion( mainVer );
73  
74          final MavenProject mainProject = new MavenProject( mainModel );
75  
76          final ArtifactMock mainArtifactMock = new ArtifactMock( mockManager, mainGid, mainAid, mainVer, "jar", false );
77  
78          mainProject.setArtifact( mainArtifactMock.getArtifact() );
79  
80          final Model depModel = new Model();
81          depModel.setArtifactId( depAid );
82          depModel.setGroupId( depGid );
83          depModel.setVersion( depVer );
84          depModel.setPackaging( depExt );
85  
86          final MavenProject depProject = new MavenProject( depModel );
87  
88          final ArtifactMock depArtifactMock = new ArtifactMock( mockManager, depGid, depAid, depVer, depExt, false );
89  
90          final File newFile = depArtifactMock.setNewFile();
91  
92          depProject.setArtifact( depArtifactMock.getArtifact() );
93  
94          final MockAndControlForAddDependencySetsTask macTask =
95              new MockAndControlForAddDependencySetsTask( mockManager, mainProject );
96  
97          macTask.expectBuildFromRepository( depProject );
98          macTask.expectCSGetFinalName( mainAid + "-" + mainVer );
99  
100         macTask.expectCSGetRepositories( null, null );
101 
102         macTask.expectGetDestFile( new File( "junk" ) );
103         macTask.expectAddFile( newFile, outDir + depAid + "-" + depVer + "." + depExt, 10 );
104 
105         macTask.expectGetSession( null );
106         macTask.expectGetMode( 0222, 0222 );
107 
108         DefaultAssemblyArchiverTest.setupInterpolators( macTask.configSource );
109 
110 
111         mockManager.replayAll();
112 
113         final Logger logger = new ConsoleLogger( Logger.LEVEL_DEBUG, "test" );
114 
115         final AddDependencySetsTask task =
116             new AddDependencySetsTask( Collections.singletonList( ds ),
117                                        Collections.singleton( depArtifactMock.getArtifact() ), depProject,
118                                        macTask.projectBuilder, logger );
119 
120         task.addDependencySet( ds, macTask.archiver, macTask.configSource );
121 
122         mockManager.verifyAll();
123     }
124 
125     public void testAddDependencySet_ShouldNotAddDependenciesWhenProjectHasNone()
126         throws AssemblyFormattingException, ArchiveCreationException, InvalidAssemblerConfigurationException
127     {
128         final MavenProject project = new MavenProject( new Model() );
129 
130         final MockAndControlForAddDependencySetsTask macTask = new MockAndControlForAddDependencySetsTask( mockManager );
131 
132         final DependencySet ds = new DependencySet();
133         ds.setOutputDirectory( "/out" );
134 
135         mockManager.replayAll();
136 
137         final Logger logger = new ConsoleLogger( Logger.LEVEL_DEBUG, "test" );
138 
139         final AddDependencySetsTask task =
140             new AddDependencySetsTask( Collections.singletonList( ds ), null, project, macTask.projectBuilder, logger );
141 
142         task.addDependencySet( ds, null, macTask.configSource );
143 
144         mockManager.verifyAll();
145     }
146 
147     // TODO: Find a better way of testing the project-stubbing behavior when a ProjectBuildingException takes place.
148     public void testAddDependencySet_ShouldNotAddDependenciesWhenProjectIsStubbed()
149         throws AssemblyFormattingException, ArchiveCreationException, InvalidAssemblerConfigurationException,
150         IOException
151     {
152         final MavenProject project = new MavenProject( new Model() );
153 
154         final ProjectBuildingException pbe = new ProjectBuildingException( "test", "Test error." );
155 
156         final MockAndControlForAddDependencySetsTask macTask =
157             new MockAndControlForAddDependencySetsTask( mockManager, new MavenProject( new Model() ) );
158 
159         final String gid = "org.test";
160         final String aid = "test-dep";
161         final String version = "2.0-SNAPSHOT";
162         final String type = "jar";
163 
164         final File file = new File( "dep-artifact.jar" );
165 
166         final ArtifactMock depMock = new ArtifactMock( mockManager, gid, aid, version, type, true );
167         depMock.setBaseVersion( version );
168         depMock.setFile( file );
169 
170         final File destFile = new File( "assembly-dep-set.zip" );
171 
172         macTask.expectGetDestFile( destFile );
173         macTask.expectBuildFromRepository( pbe );
174         macTask.expectCSGetRepositories( null, null );
175         macTask.expectCSGetFinalName( "final-name" );
176         macTask.expectAddFile( file, "out/" + aid + "-" + version + "." + type );
177 
178         macTask.expectGetMode( 0222, 0222 );
179         macTask.expectGetSession( null );
180 
181         final DependencySet ds = new DependencySet();
182         ds.setOutputDirectory( "/out" );
183         DefaultAssemblyArchiverTest.setupInterpolators( macTask.configSource );
184 
185         mockManager.replayAll();
186 
187         final Logger logger = new ConsoleLogger( Logger.LEVEL_DEBUG, "test" );
188 
189         final AddDependencySetsTask task =
190             new AddDependencySetsTask( Collections.singletonList( ds ), Collections.singleton( depMock.getArtifact() ),
191                                        project, macTask.projectBuilder, logger );
192 
193         task.addDependencySet( ds, macTask.archiver, macTask.configSource );
194 
195         mockManager.verifyAll();
196     }
197 
198     public void testAddDependencySet_ShouldAddOneDependencyFromProjectWithoutUnpacking()
199         throws AssemblyFormattingException, ArchiveCreationException, IOException,
200         InvalidAssemblerConfigurationException
201     {
202         verifyOneDependencyAdded( "out", false );
203     }
204 
205     public void testAddDependencySet_ShouldAddOneDependencyFromProjectUnpacked()
206         throws AssemblyFormattingException, ArchiveCreationException, IOException,
207         InvalidAssemblerConfigurationException
208     {
209         verifyOneDependencyAdded( "out", true );
210     }
211 
212     private void verifyOneDependencyAdded( final String outputLocation, final boolean unpack )
213         throws AssemblyFormattingException, ArchiveCreationException, IOException,
214         InvalidAssemblerConfigurationException
215     {
216         final MavenProject project = new MavenProject( new Model() );
217 
218         final DependencySet ds = new DependencySet();
219         ds.setOutputDirectory( outputLocation );
220         ds.setOutputFileNameMapping( "artifact" );
221         ds.setUnpack( unpack );
222         ds.setScope( Artifact.SCOPE_COMPILE );
223 
224         ds.setDirectoryMode( Integer.toString( 10, 8 ) );
225         ds.setFileMode( Integer.toString( 10, 8 ) );
226 
227         final MockAndControlForAddDependencySetsTask macTask =
228             new MockAndControlForAddDependencySetsTask( mockManager, new MavenProject( new Model() ) );
229 
230         final ArtifactMock artifactMock = new ArtifactMock( mockManager, "group", "artifact", "version", "jar", false );
231         final File artifactFile = artifactMock.setNewFile();
232 
233         if ( unpack )
234         {
235             macTask.expectAddArchivedFileSet();
236 //            macTask.expectModeChange( -1, -1, 10, 10, 2 );
237         }
238         else
239         {
240             macTask.expectAddFile( artifactFile, outputLocation + "/artifact", 10 );
241         }
242 
243         macTask.expectGetDestFile( new File( "junk" ) );
244         macTask.expectCSGetFinalName( "final-name" );
245         macTask.expectCSGetRepositories( null, null );
246 
247         macTask.expectGetSession( null );
248 
249         final MavenProject depProject = new MavenProject( new Model() );
250 
251         macTask.expectBuildFromRepository( depProject );
252         macTask.expectGetMode( 0222, 0222 );
253 
254         final Logger logger = new ConsoleLogger( Logger.LEVEL_DEBUG, "test" );
255 
256         final AddDependencySetsTask task =
257             new AddDependencySetsTask( Collections.singletonList( ds ),
258                                        Collections.singleton( artifactMock.getArtifact() ), project,
259                                        macTask.projectBuilder, logger );
260         DefaultAssemblyArchiverTest.setupInterpolators( macTask.configSource );
261 
262         mockManager.replayAll();
263 
264         task.addDependencySet( ds, macTask.archiver, macTask.configSource );
265 
266         mockManager.verifyAll();
267     }
268 
269     public void testGetDependencyArtifacts_ShouldGetOneDependencyArtifact()
270         throws ArchiveCreationException, InvalidAssemblerConfigurationException
271     {
272         final MavenProject project = new MavenProject( new Model() );
273 
274         final MockAndControlForAddDependencySetsTask macTask = new MockAndControlForAddDependencySetsTask( mockManager );
275 
276         final ArtifactMock artifactMock = new ArtifactMock( mockManager, "group", "artifact", "version", "jar", false );
277 
278         project.setArtifacts( Collections.singleton( artifactMock.getArtifact() ) );
279 
280         final DependencySet dependencySet = new DependencySet();
281 
282         final Logger logger = new ConsoleLogger( Logger.LEVEL_DEBUG, "test" );
283 
284         mockManager.replayAll();
285 
286         final AddDependencySetsTask task =
287             new AddDependencySetsTask( Collections.singletonList( dependencySet ),
288                                        Collections.singleton( artifactMock.getArtifact() ), project,
289                                        macTask.projectBuilder, logger );
290 
291         final Set<Artifact> result = task.resolveDependencyArtifacts( dependencySet );
292 
293         assertNotNull( result );
294         assertEquals( 1, result.size() );
295         assertSame( artifactMock.getArtifact(), result.iterator()
296                                                       .next() );
297 
298         mockManager.verifyAll();
299     }
300 
301     public void testGetDependencyArtifacts_ShouldFilterOneDependencyArtifactViaInclude()
302         throws ArchiveCreationException, InvalidAssemblerConfigurationException
303     {
304         final MavenProject project = new MavenProject( new Model() );
305 
306         final Set<Artifact> artifacts = new HashSet<Artifact>();
307 
308         final ArtifactMock am = new ArtifactMock( mockManager, "group", "artifact", "1.0", "jar", false );
309         am.setDependencyTrail( Collections.singletonList( project.getId() ) );
310         artifacts.add( am.getArtifact() );
311 
312         final ArtifactMock am2 = new ArtifactMock( mockManager, "group2", "artifact2", "1.0", "jar", false );
313         am2.setDependencyTrail( Collections.singletonList( project.getId() ) );
314         artifacts.add( am2.getArtifact() );
315 
316         final DependencySet dependencySet = new DependencySet();
317 
318         dependencySet.addInclude( "group:artifact" );
319         dependencySet.setUseTransitiveFiltering( true );
320 
321         final Logger logger = new ConsoleLogger( Logger.LEVEL_DEBUG, "test" );
322 
323         mockManager.replayAll();
324 
325         final AddDependencySetsTask task =
326             new AddDependencySetsTask( Collections.singletonList( dependencySet ), artifacts, project, null, logger );
327 
328         final Set<Artifact> result = task.resolveDependencyArtifacts( dependencySet );
329 
330         assertNotNull( result );
331         assertEquals( 1, result.size() );
332         assertSame( am.getArtifact(), result.iterator()
333                                             .next() );
334 
335         mockManager.verifyAll();
336     }
337 
338     public void testGetDependencyArtifacts_ShouldIgnoreTransitivePathFilteringWhenIncludeNotTransitive()
339         throws ArchiveCreationException, InvalidAssemblerConfigurationException
340     {
341         final MavenProject project = new MavenProject( new Model() );
342 
343         final Set<Artifact> artifacts = new HashSet<Artifact>();
344 
345         final ArtifactMock am = new ArtifactMock( mockManager, "group", "artifact", "1.0", "jar", false );
346         artifacts.add( am.getArtifact() );
347 
348         final ArtifactMock am2 = new ArtifactMock( mockManager, "group2", "artifact2", "1.0", "jar", false );
349         artifacts.add( am2.getArtifact() );
350 
351         final DependencySet dependencySet = new DependencySet();
352 
353         dependencySet.addInclude( "group:artifact" );
354         dependencySet.setUseTransitiveFiltering( false );
355 
356         final Logger logger = new ConsoleLogger( Logger.LEVEL_DEBUG, "test" );
357 
358         mockManager.replayAll();
359 
360         final AddDependencySetsTask task =
361             new AddDependencySetsTask( Collections.singletonList( dependencySet ), artifacts, project, null, logger );
362 
363         final Set<Artifact> result = task.resolveDependencyArtifacts( dependencySet );
364 
365         assertNotNull( result );
366         assertEquals( 1, result.size() );
367         assertSame( am.getArtifact(), result.iterator()
368                                             .next() );
369 
370         mockManager.verifyAll();
371     }
372 
373 }