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 junit.framework.Assert;
23  import junit.framework.TestCase;
24  import org.apache.maven.model.Model;
25  import org.apache.maven.plugins.assembly.AssemblerConfigurationSource;
26  import org.apache.maven.plugins.assembly.InvalidAssemblerConfigurationException;
27  import org.apache.maven.plugins.assembly.archive.ArchiveCreationException;
28  import org.apache.maven.plugins.assembly.format.AssemblyFormattingException;
29  import org.apache.maven.plugins.assembly.model.Assembly;
30  import org.apache.maven.plugins.assembly.model.Repository;
31  import org.apache.maven.plugins.assembly.repository.RepositoryAssembler;
32  import org.apache.maven.plugins.assembly.repository.RepositoryAssemblyException;
33  import org.apache.maven.plugins.assembly.repository.RepositoryBuilderConfigSource;
34  import org.apache.maven.plugins.assembly.repository.model.RepositoryInfo;
35  import org.apache.maven.plugins.assembly.testutils.TestFileManager;
36  import org.apache.maven.plugins.assembly.utils.TypeConversionUtils;
37  import org.apache.maven.project.MavenProject;
38  import org.codehaus.plexus.archiver.Archiver;
39  import org.codehaus.plexus.archiver.ArchiverException;
40  import org.codehaus.plexus.archiver.FileSet;
41  import org.codehaus.plexus.archiver.util.DefaultFileSet;
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.easymock.classextension.EasyMock;
46  import org.easymock.classextension.EasyMockSupport;
47  
48  import java.io.File;
49  import java.io.IOException;
50  
51  import static org.easymock.EasyMock.anyObject;
52  import static org.easymock.EasyMock.expect;
53  
54  public class RepositoryAssemblyPhaseTest
55      extends TestCase
56  {
57  
58      private final TestFileManager fileManager = new TestFileManager( "repository-phase.test.", "" );
59  
60      @Override
61      public void tearDown()
62          throws IOException
63      {
64          fileManager.cleanUp();
65      }
66  
67      public void testExecute_ShouldNotIncludeRepositoryIfNonSpecifiedInAssembly()
68          throws ArchiveCreationException, AssemblyFormattingException, InvalidAssemblerConfigurationException
69      {
70          final EasyMockSupport mm = new EasyMockSupport();
71  
72          final MockAndControlForRepositoryAssembler macRepo = new MockAndControlForRepositoryAssembler( mm );
73          final MockAndControlForArchiver macArchiver = new MockAndControlForArchiver( mm );
74          final MockAndControlForConfigSource macCS = new MockAndControlForConfigSource( mm );
75  
76          final File tempRoot = fileManager.createTempDir();
77  
78          macCS.expectGetTemporaryRootDirectory( tempRoot );
79  
80          final Assembly assembly = new Assembly();
81  
82          assembly.setId( "test" );
83  
84          mm.replayAll();
85  
86          createPhase( macRepo.repositoryAssembler, new ConsoleLogger( Logger.LEVEL_DEBUG, "test" ) ).execute( assembly,
87                                                                                                               macArchiver.archiver,
88                                                                                                               macCS
89                                                                                                                   .configSource );
90  
91          mm.verifyAll();
92      }
93  
94      public void testExecute_ShouldIncludeOneRepository()
95          throws ArchiveCreationException, AssemblyFormattingException, InvalidAssemblerConfigurationException
96      {
97          final EasyMockSupport mm = new EasyMockSupport();
98  
99          final MockAndControlForRepositoryAssembler macRepo = new MockAndControlForRepositoryAssembler( mm );
100         final MockAndControlForArchiver macArchiver = new MockAndControlForArchiver( mm );
101         final MockAndControlForConfigSource macCS = new MockAndControlForConfigSource( mm );
102 
103         final File tempRoot = fileManager.createTempDir();
104 
105         macCS.expectGetTemporaryRootDirectory( tempRoot );
106         macCS.expectGetProject( new MavenProject( new Model() ) );
107         macCS.expectGetFinalName( "final-name" );
108         macCS.expectInterpolators();
109 
110         final Assembly assembly = new Assembly();
111 
112         assembly.setId( "test" );
113 
114         final Repository repo = new Repository();
115 
116         repo.setOutputDirectory( "out" );
117         repo.setDirectoryMode( "777" );
118         repo.setFileMode( "777" );
119 
120         final int mode = TypeConversionUtils.modeToInt( "777", new ConsoleLogger( Logger.LEVEL_DEBUG, "test" ) );
121 
122         final File outDir = new File( tempRoot, "out" );
123 
124         macArchiver.expectModeChange( -1, -1, mode, mode, true );
125         macArchiver.expectAddDirectory( outDir, "out/", null, null );
126 
127         macRepo.expectAssemble();
128 
129         assembly.addRepository( repo );
130 
131         mm.replayAll();
132 
133         createPhase( macRepo.repositoryAssembler, new ConsoleLogger( Logger.LEVEL_DEBUG, "test" ) ).execute( assembly,
134                                                                                                              macArchiver.archiver,
135                                                                                                              macCS
136                                                                                                                  .configSource );
137 
138         mm.verifyAll();
139     }
140 
141     private RepositoryAssemblyPhase createPhase( final RepositoryAssembler repositoryAssembler, final Logger logger )
142     {
143         final RepositoryAssemblyPhase phase = new RepositoryAssemblyPhase( repositoryAssembler );
144         phase.enableLogging( logger );
145 
146         return phase;
147     }
148 
149     private final class MockAndControlForArchiver
150     {
151         final Archiver archiver;
152 
153         public MockAndControlForArchiver( final EasyMockSupport mockManager )
154         {
155 
156             archiver = mockManager.createMock( Archiver.class );
157         }
158 
159         public void expectAddDirectory( final File outDir, final String location, final String[] includes,
160                                         final String[] excludes )
161         {
162             try
163             {
164                 final DefaultFileSet fs = new DefaultFileSet();
165                 fs.setDirectory( outDir );
166                 fs.setPrefix( location );
167                 fs.setIncludes( includes );
168                 fs.setExcludes( excludes );
169 
170                 archiver.addFileSet( (FileSet) anyObject() );
171             }
172             catch ( final ArchiverException e )
173             {
174                 Assert.fail( "Should never happen." );
175             }
176 
177             EasyMock.expectLastCall().atLeastOnce();
178         }
179 
180         void expectModeChange( final int defaultDirMode, final int defaultFileMode, final int dirMode,
181                                final int fileMode, final boolean expectTwoSets )
182         {
183             expect( archiver.getOverrideDirectoryMode() ).andReturn( defaultDirMode );
184 
185             expect( archiver.getOverrideFileMode() ).andReturn( defaultFileMode );
186 
187             if ( expectTwoSets )
188             {
189                 archiver.setDirectoryMode( dirMode );
190                 archiver.setFileMode( fileMode );
191             }
192 
193             archiver.setDirectoryMode( defaultDirMode );
194             archiver.setFileMode( defaultFileMode );
195         }
196 
197         // public void expectAddFile( File file, String outputLocation, int fileMode )
198         // {
199         // try
200         // {
201         // archiver.addFile( file, outputLocation, fileMode );
202         // }
203         // catch ( ArchiverException e )
204         // {
205         // Assert.fail( "Should never happen." );
206         // }
207         // }
208     }
209 
210     private final class MockAndControlForConfigSource
211     {
212         final AssemblerConfigurationSource configSource;
213 
214         public MockAndControlForConfigSource( final EasyMockSupport mockManager )
215         {
216             configSource = mockManager.createMock( AssemblerConfigurationSource.class );
217 
218             expect( configSource.getMavenSession() ).andReturn( null ).anyTimes();
219         }
220 
221         public void expectGetProject( final MavenProject project )
222         {
223             expect( configSource.getProject() ).andReturn( project ).atLeastOnce();
224         }
225 
226         public void expectGetFinalName( final String finalName )
227         {
228             expect( configSource.getFinalName() ).andReturn( finalName ).atLeastOnce();
229         }
230 
231         public void expectInterpolators()
232         {
233             expect( configSource.getCommandLinePropsInterpolator() ).andReturn(
234                 FixedStringSearchInterpolator.empty() ).anyTimes();
235             expect( configSource.getEnvInterpolator() ).andReturn( FixedStringSearchInterpolator.empty() ).anyTimes();
236             expect( configSource.getMainProjectInterpolator() ).andReturn(
237                 FixedStringSearchInterpolator.empty() ).anyTimes();
238         }
239 
240         public void expectGetTemporaryRootDirectory( final File tempRoot )
241         {
242             expect( configSource.getTemporaryRootDirectory() ).andReturn( tempRoot ).atLeastOnce();
243         }
244 
245         //
246         // public void expectGetBasedir( File basedir )
247         // {
248         // configSource.getBasedir();
249         // control.setReturnValue( basedir, MockControl.ONE_OR_MORE );
250         // }
251     }
252 
253     private final class MockAndControlForRepositoryAssembler
254     {
255         final RepositoryAssembler repositoryAssembler;
256 
257         MockAndControlForRepositoryAssembler( final EasyMockSupport mockManager )
258         {
259             repositoryAssembler = mockManager.createMock( RepositoryAssembler.class );
260         }
261 
262         public void expectAssemble()
263         {
264             try
265             {
266                 repositoryAssembler.buildRemoteRepository( (File) anyObject(), (RepositoryInfo) anyObject(),
267                                                            (RepositoryBuilderConfigSource) anyObject() );
268                 EasyMock.expectLastCall().atLeastOnce();
269             }
270             catch ( final RepositoryAssemblyException e )
271             {
272                 Assert.fail( "Should never happen" );
273             }
274 
275         }
276     }
277 
278 }