View Javadoc
1   package org.apache.maven.plugins.assembly.archive.phase;
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 static org.mockito.ArgumentMatchers.anyListOf;
23  import static org.mockito.ArgumentMatchers.eq;
24  import static org.mockito.ArgumentMatchers.isNull;
25  import static org.mockito.Mockito.mock;
26  import static org.mockito.Mockito.verify;
27  import static org.mockito.Mockito.when;
28  
29  import java.io.IOException;
30  import java.util.Collections;
31  import java.util.LinkedHashMap;
32  import java.util.Set;
33  
34  import org.apache.maven.artifact.Artifact;
35  import org.apache.maven.model.Model;
36  import org.apache.maven.plugins.assembly.AssemblerConfigurationSource;
37  import org.apache.maven.plugins.assembly.InvalidAssemblerConfigurationException;
38  import org.apache.maven.plugins.assembly.archive.ArchiveCreationException;
39  import org.apache.maven.plugins.assembly.artifact.DependencyResolutionException;
40  import org.apache.maven.plugins.assembly.artifact.DependencyResolver;
41  import org.apache.maven.plugins.assembly.format.AssemblyFormattingException;
42  import org.apache.maven.plugins.assembly.model.Assembly;
43  import org.apache.maven.plugins.assembly.model.DependencySet;
44  import org.apache.maven.project.MavenProject;
45  import org.junit.Before;
46  import org.junit.Test;
47  import org.junit.runner.RunWith;
48  import org.mockito.junit.MockitoJUnitRunner;
49  
50  @RunWith( MockitoJUnitRunner.class )
51  public class DependencySetAssemblyPhaseTest
52  {
53      private DependencySetAssemblyPhase phase;
54      
55      private DependencyResolver dependencyResolver;
56      
57      @Before
58      public void setUp()
59      {
60          this.dependencyResolver = mock( DependencyResolver.class );
61          
62          this.phase = new DependencySetAssemblyPhase( null, dependencyResolver, null );
63      }
64  
65      @Test
66      public void testExecute_ShouldAddOneDependencyFromProject()
67          throws AssemblyFormattingException, ArchiveCreationException, IOException,
68          InvalidAssemblerConfigurationException, DependencyResolutionException
69      {
70          final String outputLocation = "/out";
71  
72          final MavenProject project = newMavenProject( "group", "project", "0" );
73  
74          Artifact artifact = mock( Artifact.class );
75          project.setArtifact( artifact );
76  
77          final DependencySet ds = new DependencySet();
78          ds.setUseProjectArtifact( false );
79          ds.setOutputDirectory( outputLocation );
80          ds.setOutputFileNameMapping( "${artifact.artifactId}" );
81          ds.setUnpack( false );
82          ds.setScope( Artifact.SCOPE_COMPILE );
83          ds.setFileMode( Integer.toString( 10, 8 ) );
84  
85          final Assembly assembly = new Assembly();
86  
87          assembly.setId( "test" );
88          assembly.setIncludeBaseDirectory( false );
89          assembly.addDependencySet( ds );
90  
91          project.setArtifacts( Collections.singleton( artifact ) );
92  
93          when( dependencyResolver.resolveDependencySets( eq( assembly ),
94                                                          isNull(AssemblerConfigurationSource.class),
95                                                          anyListOf( DependencySet.class ) ) ).thenReturn( new LinkedHashMap<DependencySet, Set<Artifact>>() );
96          
97          this.phase.execute( assembly, null, null );
98  
99          // result of easymock migration, should be assert of expected result instead of verifying methodcalls
100         verify( dependencyResolver ).resolveDependencySets( eq( assembly ),
101                                                             isNull(AssemblerConfigurationSource.class),
102                                                             anyListOf( DependencySet.class ) );
103     }
104 
105     @Test
106     public void testExecute_ShouldNotAddDependenciesWhenProjectHasNone()
107         throws Exception
108     {
109         final Assembly assembly = new Assembly();
110         assembly.setId( "test" );
111         assembly.setIncludeBaseDirectory( false );
112         
113         when( dependencyResolver.resolveDependencySets( eq( assembly ), 
114                                                         isNull( AssemblerConfigurationSource.class ),
115                                                         anyListOf( DependencySet.class ) ) ).thenReturn( new LinkedHashMap<DependencySet, Set<Artifact>>() );
116 
117         this.phase.execute( assembly, null, null );
118 
119         // result of easymock migration, should be assert of expected result instead of verifying methodcalls
120         verify( dependencyResolver ).resolveDependencySets( eq( assembly ),
121                                                             isNull( AssemblerConfigurationSource.class ),
122                                                             anyListOf( DependencySet.class ) );        
123     }
124     
125     private MavenProject newMavenProject( final String groupId, final String artifactId, final String version )
126     {
127         final Model model = new Model();
128         model.setGroupId( groupId );
129         model.setArtifactId( artifactId );
130         model.setVersion( version );
131 
132         return new MavenProject( model );
133     }
134 }