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.Mockito.any;
23  import static org.mockito.Mockito.atLeastOnce;
24  import static org.mockito.Mockito.mock;
25  import static org.mockito.Mockito.verify;
26  import static org.mockito.Mockito.verifyZeroInteractions;
27  import static org.mockito.Mockito.when;
28  
29  import java.io.File;
30  
31  import org.apache.maven.model.Model;
32  import org.apache.maven.plugins.assembly.AssemblerConfigurationSource;
33  import org.apache.maven.plugins.assembly.model.Assembly;
34  import org.apache.maven.plugins.assembly.model.Repository;
35  import org.apache.maven.plugins.assembly.repository.RepositoryAssembler;
36  import org.apache.maven.plugins.assembly.repository.RepositoryBuilderConfigSource;
37  import org.apache.maven.plugins.assembly.repository.model.RepositoryInfo;
38  import org.apache.maven.plugins.assembly.utils.TypeConversionUtils;
39  import org.apache.maven.project.MavenProject;
40  import org.codehaus.plexus.archiver.Archiver;
41  import org.codehaus.plexus.archiver.FileSet;
42  import org.codehaus.plexus.interpolation.fixed.FixedStringSearchInterpolator;
43  import org.codehaus.plexus.logging.Logger;
44  import org.codehaus.plexus.logging.console.ConsoleLogger;
45  import org.junit.Before;
46  import org.junit.Rule;
47  import org.junit.Test;
48  import org.junit.rules.TemporaryFolder;
49  import org.junit.runner.RunWith;
50  import org.mockito.junit.MockitoJUnitRunner;
51  
52  @RunWith( MockitoJUnitRunner.class )
53  public class RepositoryAssemblyPhaseTest
54  {
55      @Rule
56      public TemporaryFolder temporaryFolder = new TemporaryFolder();
57      
58      private RepositoryAssemblyPhase phase;
59      
60      private RepositoryAssembler repositoryAssembler;
61      
62      @Before
63      public void setUp()
64      {
65          this.repositoryAssembler = mock( RepositoryAssembler.class );
66          this.phase = new RepositoryAssemblyPhase( repositoryAssembler );
67          this.phase.enableLogging( mock( Logger.class ) );
68      }
69      
70      @Test
71      public void testExecute_ShouldNotIncludeRepositoryIfNonSpecifiedInAssembly()
72          throws Exception
73      {
74          final AssemblerConfigurationSource configSource = mock( AssemblerConfigurationSource.class );
75          when( configSource.getTemporaryRootDirectory() ).thenReturn( temporaryFolder.getRoot() );
76  
77          final Assembly assembly = new Assembly();
78          assembly.setId( "test" );
79  
80          this.phase.execute( assembly, null, configSource );
81          
82          // result of easymock migration, should be assert of expected result instead of verifying methodcalls
83          verify( configSource, atLeastOnce() ).getTemporaryRootDirectory();
84          
85          verifyZeroInteractions( repositoryAssembler );
86      }
87  
88      @Test
89      public void testExecute_ShouldIncludeOneRepository()
90          throws Exception
91      {
92          final File tempRoot = temporaryFolder.getRoot();
93  
94          final AssemblerConfigurationSource configSource = mock( AssemblerConfigurationSource.class );
95          when( configSource.getCommandLinePropsInterpolator() ).thenReturn( FixedStringSearchInterpolator.empty() );
96          when( configSource.getEnvInterpolator() ).thenReturn( FixedStringSearchInterpolator.empty() );
97          when( configSource.getFinalName() ).thenReturn( "final-name" );
98          when( configSource.getMainProjectInterpolator() ).thenReturn( FixedStringSearchInterpolator.empty() );
99          when( configSource.getProject() ).thenReturn( new MavenProject( new Model() ) );
100         when( configSource.getTemporaryRootDirectory() ).thenReturn( tempRoot );
101 
102         final Assembly assembly = new Assembly();
103         assembly.setId( "test" );
104 
105         final Repository repo = new Repository();
106         repo.setOutputDirectory( "out" );
107         repo.setDirectoryMode( "777" );
108         repo.setFileMode( "777" );
109         assembly.addRepository( repo );
110 
111         final int mode = TypeConversionUtils.modeToInt( "777", new ConsoleLogger( Logger.LEVEL_DEBUG, "test" ) );
112 
113         final int defaultDirMode = -1;
114         final int defaultFileMode = -1;
115 
116         final Archiver archiver = mock( Archiver.class );
117         when( archiver.getOverrideDirectoryMode() ).thenReturn( defaultDirMode );
118         when( archiver.getOverrideFileMode() ).thenReturn( defaultFileMode );
119 
120         this.phase.execute( assembly, archiver, configSource );
121 
122         // result of easymock migration, should be assert of expected result instead of verifying methodcalls
123         verify( configSource ).getCommandLinePropsInterpolator();
124         verify( configSource ).getEnvInterpolator();
125         verify( configSource, atLeastOnce() ).getFinalName();
126         verify( configSource ).getMainProjectInterpolator();
127         verify( configSource ).getMavenSession();
128         verify( configSource, atLeastOnce() ).getProject();
129         verify( configSource, atLeastOnce() ).getTemporaryRootDirectory();
130         
131         verify( archiver ).getOverrideDirectoryMode();
132         verify( archiver ).getOverrideFileMode();
133         verify( archiver ).setDirectoryMode( mode );
134         verify( archiver ).setFileMode( mode );
135         verify( archiver ).setDirectoryMode( defaultDirMode );
136         verify( archiver ).setFileMode( defaultFileMode );
137         verify( archiver ).addFileSet( any( FileSet.class ) );        
138 
139         verify( repositoryAssembler ).buildRemoteRepository( any( File.class ), any( RepositoryInfo.class ),
140                                                              any( RepositoryBuilderConfigSource.class ) );
141     }
142 }