View Javadoc

1   package org.apache.maven.plugin.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 org.apache.maven.model.Model;
23  import org.apache.maven.plugin.assembly.AssemblerConfigurationSource;
24  import org.apache.maven.plugin.assembly.DefaultAssemblyContext;
25  import org.apache.maven.plugin.assembly.InvalidAssemblerConfigurationException;
26  import org.apache.maven.plugin.assembly.archive.ArchiveCreationException;
27  import org.apache.maven.plugin.assembly.archive.phase.wrappers.RepoBuilderConfigSourceWrapper;
28  import org.apache.maven.plugin.assembly.archive.phase.wrappers.RepoInfoWrapper;
29  import org.apache.maven.plugin.assembly.format.AssemblyFormattingException;
30  import org.apache.maven.plugin.assembly.model.Assembly;
31  import org.apache.maven.plugin.assembly.model.Repository;
32  import org.apache.maven.plugin.assembly.testutils.MockManager;
33  import org.apache.maven.plugin.assembly.testutils.TestFileManager;
34  import org.apache.maven.plugin.assembly.utils.TypeConversionUtils;
35  import org.apache.maven.project.MavenProject;
36  import org.apache.maven.shared.repository.RepositoryAssembler;
37  import org.apache.maven.shared.repository.RepositoryAssemblyException;
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.logging.Logger;
43  import org.codehaus.plexus.logging.console.ConsoleLogger;
44  import org.codehaus.plexus.util.StringUtils;
45  import org.easymock.AbstractMatcher;
46  import org.easymock.MockControl;
47  
48  import java.io.File;
49  import java.io.IOException;
50  import java.util.Arrays;
51  
52  import junit.framework.Assert;
53  import junit.framework.TestCase;
54  
55  public class RepositoryAssemblyPhaseTest
56      extends TestCase
57  {
58  
59      private final TestFileManager fileManager = new TestFileManager( "repository-phase.test.", "" );
60  
61      @Override
62      public void tearDown()
63          throws IOException
64      {
65          fileManager.cleanUp();
66      }
67  
68      public void testExecute_ShouldNotIncludeRepositoryIfNonSpecifiedInAssembly()
69          throws ArchiveCreationException, AssemblyFormattingException, InvalidAssemblerConfigurationException
70      {
71          final MockManager mm = new MockManager();
72  
73          final MockAndControlForRepositoryAssembler macRepo = new MockAndControlForRepositoryAssembler( mm );
74          final MockAndControlForArchiver macArchiver = new MockAndControlForArchiver( mm );
75          final MockAndControlForConfigSource macCS = new MockAndControlForConfigSource( mm );
76  
77          final File tempRoot = fileManager.createTempDir();
78  
79          macCS.expectGetTemporaryRootDirectory( tempRoot );
80  
81          final Assembly assembly = new Assembly();
82  
83          assembly.setId( "test" );
84  
85          mm.replayAll();
86  
87          createPhase( macRepo.repositoryAssembler, new ConsoleLogger( Logger.LEVEL_DEBUG, "test" ) ).execute( assembly,
88                                                                                                               macArchiver.archiver,
89                                                                                                               macCS.configSource,
90                                                                                                               new DefaultAssemblyContext() );
91  
92          mm.verifyAll();
93      }
94  
95      public void testExecute_ShouldIncludeOneRepository()
96          throws ArchiveCreationException, AssemblyFormattingException, InvalidAssemblerConfigurationException
97      {
98          final MockManager mm = new MockManager();
99  
100         final MockAndControlForRepositoryAssembler macRepo = new MockAndControlForRepositoryAssembler( mm );
101         final MockAndControlForArchiver macArchiver = new MockAndControlForArchiver( mm );
102         final MockAndControlForConfigSource macCS = new MockAndControlForConfigSource( mm );
103 
104         final File tempRoot = fileManager.createTempDir();
105 
106         macCS.expectGetTemporaryRootDirectory( tempRoot );
107         macCS.expectGetProject( new MavenProject( new Model() ) );
108         macCS.expectGetFinalName( "final-name" );
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( outDir, repo, macCS.configSource );
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.configSource,
136                                                                                                              new DefaultAssemblyContext() );
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         Archiver archiver;
152 
153         MockControl control;
154 
155         public MockAndControlForArchiver( final MockManager mockManager )
156         {
157             control = MockControl.createControl( Archiver.class );
158             mockManager.add( control );
159 
160             archiver = (Archiver) control.getMock();
161         }
162 
163         public void expectAddDirectory( final File outDir, final String location, final String[] includes,
164                                         final String[] excludes )
165         {
166             try
167             {
168                 final DefaultFileSet fs = new DefaultFileSet();
169                 fs.setDirectory( outDir );
170                 fs.setPrefix( location );
171                 fs.setIncludes( includes );
172                 fs.setExcludes( excludes );
173 
174                 archiver.addFileSet( fs );
175             }
176             catch ( final ArchiverException e )
177             {
178                 Assert.fail( "Should never happen." );
179             }
180 
181             control.setMatcher( new AbstractMatcher()
182             {
183 
184                 @Override
185                 protected boolean argumentMatches( final Object expected, final Object actual )
186                 {
187                     final FileSet e = (FileSet) expected;
188                     final FileSet a = (FileSet) actual;
189 
190                     if ( !eq( e.getDirectory(), a.getDirectory() ) )
191                     {
192                         System.out.println( "FileSet directory expected: " + e.getDirectory() + "\nActual: "
193                                         + a.getDirectory() );
194 
195                         return false;
196                     }
197 
198                     if ( !eq( e.getPrefix(), a.getPrefix() ) )
199                     {
200                         System.out.println( "FileSet prefix expected: " + e.getPrefix() + "\nActual: " + a.getPrefix() );
201 
202                         return false;
203                     }
204 
205                     if ( !areq( e.getIncludes(), a.getIncludes() ) )
206                     {
207                         System.out.println( "FileSet includes expected: " + arToStr( e.getIncludes() ) + "\nActual: "
208                                         + arToStr( a.getIncludes() ) );
209 
210                         return false;
211                     }
212 
213                     if ( !areq( e.getExcludes(), a.getExcludes() ) )
214                     {
215                         System.out.println( "FileSet excludes expected: " + arToStr( e.getExcludes() ) + "\nActual: "
216                                         + arToStr( a.getExcludes() ) );
217 
218                         return false;
219                     }
220 
221                     return true;
222                 }
223 
224                 @Override
225                 protected String argumentToString( final Object argument )
226                 {
227                     final FileSet a = (FileSet) argument;
228 
229                     return argument == null ? "Null FileSet" : "FileSet:[dir=" + a.getDirectory() + ", prefix: "
230                                     + a.getPrefix() + "\nincludes:\n" + arToStr( a.getIncludes() ) + "\nexcludes:\n"
231                                     + arToStr( a.getExcludes() ) + "]";
232                 }
233 
234                 private String arToStr( final String[] array )
235                 {
236                     return array == null ? "-EMPTY-" : StringUtils.join( array, "\n\t" );
237                 }
238 
239                 private boolean areq( final String[] first, final String[] second )
240                 {
241                     if ( ( first == null || first.length == 0 ) && ( second == null || second.length == 0 ) )
242                     {
243                         return true;
244                     }
245                     else if ( first == null && second != null )
246                     {
247                         return false;
248                     }
249                     else if ( first != null && second == null )
250                     {
251                         return false;
252                     }
253                     else
254                     {
255                         return Arrays.equals( first, second );
256                     }
257                 }
258 
259                 private boolean eq( final Object first, final Object second )
260                 {
261                     if ( first == null && second == null )
262                     {
263                         return true;
264                     }
265                     else if ( first == null && second != null )
266                     {
267                         return false;
268                     }
269                     else if ( first != null && second == null )
270                     {
271                         return false;
272                     }
273                     else
274                     {
275                         return first.equals( second );
276                     }
277                 }
278 
279             } );
280 
281             control.setVoidCallable( MockControl.ONE_OR_MORE );
282         }
283 
284         void expectModeChange( final int defaultDirMode, final int defaultFileMode, final int dirMode,
285                                final int fileMode, final boolean expectTwoSets )
286         {
287             archiver.getOverrideDirectoryMode();
288             control.setReturnValue( defaultDirMode );
289 
290             archiver.getOverrideFileMode();
291             control.setReturnValue( defaultFileMode );
292 
293             if ( expectTwoSets )
294             {
295                 archiver.setDirectoryMode( dirMode );
296                 archiver.setFileMode( fileMode );
297             }
298 
299             archiver.setDirectoryMode( defaultDirMode );
300             archiver.setFileMode( defaultFileMode );
301         }
302 
303         // public void expectAddFile( File file, String outputLocation, int fileMode )
304         // {
305         // try
306         // {
307         // archiver.addFile( file, outputLocation, fileMode );
308         // }
309         // catch ( ArchiverException e )
310         // {
311         // Assert.fail( "Should never happen." );
312         // }
313         // }
314     }
315 
316     private final class MockAndControlForConfigSource
317     {
318         AssemblerConfigurationSource configSource;
319 
320         MockControl control;
321 
322         public MockAndControlForConfigSource( final MockManager mockManager )
323         {
324             control = MockControl.createControl( AssemblerConfigurationSource.class );
325             mockManager.add( control );
326 
327             configSource = (AssemblerConfigurationSource) control.getMock();
328 
329             configSource.getMavenSession();
330             control.setReturnValue( null, MockControl.ZERO_OR_MORE );
331         }
332 
333         public void expectGetProject( final MavenProject project )
334         {
335             configSource.getProject();
336             control.setReturnValue( project, MockControl.ONE_OR_MORE );
337         }
338 
339         public void expectGetFinalName( final String finalName )
340         {
341             configSource.getFinalName();
342             control.setReturnValue( finalName, MockControl.ONE_OR_MORE );
343         }
344 
345         public void expectGetTemporaryRootDirectory( final File tempRoot )
346         {
347             configSource.getTemporaryRootDirectory();
348             control.setReturnValue( tempRoot, MockControl.ONE_OR_MORE );
349         }
350 
351         //
352         // public void expectGetBasedir( File basedir )
353         // {
354         // configSource.getBasedir();
355         // control.setReturnValue( basedir, MockControl.ONE_OR_MORE );
356         // }
357     }
358 
359     private final class MockAndControlForRepositoryAssembler
360     {
361         RepositoryAssembler repositoryAssembler;
362 
363         MockControl control;
364 
365         MockAndControlForRepositoryAssembler( final MockManager mockManager )
366         {
367             control = MockControl.createControl( RepositoryAssembler.class );
368             mockManager.add( control );
369 
370             repositoryAssembler = (RepositoryAssembler) control.getMock();
371         }
372 
373         public void expectAssemble( final File dir, final Repository repo,
374                                     final AssemblerConfigurationSource configSource )
375         {
376             try
377             {
378                 repositoryAssembler.buildRemoteRepository( dir, new RepoInfoWrapper( repo ),
379                                                            new RepoBuilderConfigSourceWrapper( configSource ) );
380                 control.setMatcher( MockControl.ALWAYS_MATCHER );
381             }
382             catch ( final RepositoryAssemblyException e )
383             {
384                 Assert.fail( "Should never happen" );
385             }
386 
387             control.setVoidCallable( MockControl.ONE_OR_MORE );
388         }
389     }
390 
391 }