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