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