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.plugin.assembly.testutils.MockManager;
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  
44  public class AddDependencySetsTaskTest
45      extends TestCase
46  {
47  
48      private final MockManager mockManager = new MockManager();
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( artifactFile, outputLocation + "/",
230                                               AddArtifactTask.DEFAULT_INCLUDES_ARRAY, null );
231             macTask.expectModeChange( -1, -1, 10, 10, 2 );
232         }
233         else
234         {
235             macTask.expectAddFile( artifactFile, outputLocation + "/artifact", 10 );
236         }
237 
238         macTask.expectGetDestFile( new File( "junk" ) );
239         macTask.expectCSGetFinalName( "final-name" );
240         macTask.expectCSGetRepositories( null, null );
241 
242         macTask.expectGetSession( null );
243 
244         final MavenProject depProject = new MavenProject( new Model() );
245 
246         macTask.expectBuildFromRepository( depProject );
247 
248         final Logger logger = new ConsoleLogger( Logger.LEVEL_DEBUG, "test" );
249 
250         final AddDependencySetsTask task =
251             new AddDependencySetsTask( Collections.singletonList( ds ),
252                                        Collections.singleton( artifactMock.getArtifact() ), project,
253                                        macTask.projectBuilder, macTask.archiverManager, logger );
254 
255         mockManager.replayAll();
256 
257         task.addDependencySet( ds, macTask.archiver, macTask.configSource );
258 
259         mockManager.verifyAll();
260     }
261 
262     public void testGetDependencyArtifacts_ShouldGetOneDependencyArtifact()
263         throws ArchiveCreationException, InvalidAssemblerConfigurationException
264     {
265         final MavenProject project = new MavenProject( new Model() );
266 
267         final MockAndControlForAddDependencySetsTask macTask = new MockAndControlForAddDependencySetsTask( mockManager );
268 
269         final ArtifactMock artifactMock = new ArtifactMock( mockManager, "group", "artifact", "version", "jar", false );
270 
271         project.setArtifacts( Collections.singleton( artifactMock.getArtifact() ) );
272 
273         final DependencySet dependencySet = new DependencySet();
274 
275         final Logger logger = new ConsoleLogger( Logger.LEVEL_DEBUG, "test" );
276 
277         mockManager.replayAll();
278 
279         final AddDependencySetsTask task =
280             new AddDependencySetsTask( Collections.singletonList( dependencySet ),
281                                        Collections.singleton( artifactMock.getArtifact() ), project,
282                                        macTask.projectBuilder, macTask.archiverManager, logger );
283 
284         final Set<Artifact> result = task.resolveDependencyArtifacts( dependencySet );
285 
286         assertNotNull( result );
287         assertEquals( 1, result.size() );
288         assertSame( artifactMock.getArtifact(), result.iterator()
289                                                       .next() );
290 
291         mockManager.verifyAll();
292     }
293 
294     public void testGetDependencyArtifacts_ShouldFilterOneDependencyArtifactViaInclude()
295         throws ArchiveCreationException, InvalidAssemblerConfigurationException
296     {
297         final MavenProject project = new MavenProject( new Model() );
298 
299         final Set<Artifact> artifacts = new HashSet<Artifact>();
300 
301         final ArtifactMock am = new ArtifactMock( mockManager, "group", "artifact", "1.0", "jar", false );
302         am.setDependencyTrail( Collections.singletonList( project.getId() ) );
303         artifacts.add( am.getArtifact() );
304 
305         final ArtifactMock am2 = new ArtifactMock( mockManager, "group2", "artifact2", "1.0", "jar", false );
306         am2.setDependencyTrail( Collections.singletonList( project.getId() ) );
307         artifacts.add( am2.getArtifact() );
308 
309         final DependencySet dependencySet = new DependencySet();
310 
311         dependencySet.addInclude( "group:artifact" );
312         dependencySet.setUseTransitiveFiltering( true );
313 
314         final Logger logger = new ConsoleLogger( Logger.LEVEL_DEBUG, "test" );
315 
316         mockManager.replayAll();
317 
318         final AddDependencySetsTask task =
319             new AddDependencySetsTask( Collections.singletonList( dependencySet ), artifacts, project, null, null,
320                                        logger );
321 
322         final Set<Artifact> result = task.resolveDependencyArtifacts( dependencySet );
323 
324         assertNotNull( result );
325         assertEquals( 1, result.size() );
326         assertSame( am.getArtifact(), result.iterator()
327                                             .next() );
328 
329         mockManager.verifyAll();
330     }
331 
332     public void testGetDependencyArtifacts_ShouldIgnoreTransitivePathFilteringWhenIncludeNotTransitive()
333         throws ArchiveCreationException, InvalidAssemblerConfigurationException
334     {
335         final MavenProject project = new MavenProject( new Model() );
336 
337         final Set<Artifact> artifacts = new HashSet<Artifact>();
338 
339         final ArtifactMock am = new ArtifactMock( mockManager, "group", "artifact", "1.0", "jar", false );
340         artifacts.add( am.getArtifact() );
341 
342         final ArtifactMock am2 = new ArtifactMock( mockManager, "group2", "artifact2", "1.0", "jar", false );
343         artifacts.add( am2.getArtifact() );
344 
345         final DependencySet dependencySet = new DependencySet();
346 
347         dependencySet.addInclude( "group:artifact" );
348         dependencySet.setUseTransitiveFiltering( false );
349 
350         final Logger logger = new ConsoleLogger( Logger.LEVEL_DEBUG, "test" );
351 
352         mockManager.replayAll();
353 
354         final AddDependencySetsTask task =
355             new AddDependencySetsTask( Collections.singletonList( dependencySet ), artifacts, project, null, null,
356                                        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()
363                                             .next() );
364 
365         mockManager.verifyAll();
366     }
367 
368 }