View Javadoc

1   package org.apache.maven.plugin.assembly.archive.task.testutils;
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.repository.ArtifactRepository;
23  import org.apache.maven.execution.MavenSession;
24  import org.apache.maven.plugin.assembly.AssemblerConfigurationSource;
25  import org.apache.maven.plugin.assembly.testutils.MockManager;
26  import org.apache.maven.project.MavenProject;
27  import org.apache.maven.project.MavenProjectBuilder;
28  import org.apache.maven.project.ProjectBuildingException;
29  import org.codehaus.plexus.archiver.Archiver;
30  import org.codehaus.plexus.archiver.ArchiverException;
31  import org.codehaus.plexus.archiver.manager.ArchiverManager;
32  import org.easymock.MockControl;
33  
34  import java.io.File;
35  import java.util.List;
36  
37  import junit.framework.Assert;
38  
39  public class MockAndControlForAddDependencySetsTask
40  {
41  
42      public Archiver archiver;
43  
44      public MockControl archiverCtl;
45  
46      public AssemblerConfigurationSource configSource;
47  
48      public MockControl configSourceCtl;
49  
50      public MockControl dependencyResolverCtl;
51  
52      public MavenProjectBuilder projectBuilder;
53  
54      public MockControl archiverManagerCtl;
55  
56      public ArchiverManager archiverManager;
57  
58      public MockControl projectBuilderCtl;
59  
60      private final MavenProject project;
61  
62      public MockAndControlForAddDependencySetsTask( final MockManager mockManager )
63      {
64          this( mockManager, null );
65      }
66  
67      public MockAndControlForAddDependencySetsTask( final MockManager mockManager, final MavenProject project )
68      {
69          this.project = project;
70  
71          archiverCtl = MockControl.createControl( Archiver.class );
72          mockManager.add( archiverCtl );
73  
74          archiver = (Archiver) archiverCtl.getMock();
75  
76          configSourceCtl = MockControl.createControl( AssemblerConfigurationSource.class );
77          mockManager.add( configSourceCtl );
78  
79          configSource = (AssemblerConfigurationSource) configSourceCtl.getMock();
80  
81          projectBuilderCtl = MockControl.createControl( MavenProjectBuilder.class );
82          mockManager.add( projectBuilderCtl );
83  
84          projectBuilder = (MavenProjectBuilder) projectBuilderCtl.getMock();
85  
86          archiverManagerCtl = MockControl.createControl( ArchiverManager.class );
87          mockManager.add( archiverManagerCtl );
88  
89          archiverManager = (ArchiverManager) archiverManagerCtl.getMock();
90  
91          enableDefaultExpectations();
92      }
93  
94      private void enableDefaultExpectations()
95      {
96          configSource.getProject();
97          configSourceCtl.setReturnValue( project, MockControl.ZERO_OR_MORE );
98      }
99  
100     public void expectAddArchivedFileSet( final File file, final String outputLocation, final String[] includes,
101                                           final String[] excludes )
102     {
103         try
104         {
105             archiver.addArchivedFileSet( file, outputLocation, includes, excludes );
106 
107             if ( ( includes != null ) || ( excludes != null ) )
108             {
109                 archiverCtl.setMatcher( MockControl.ARRAY_MATCHER );
110             }
111 
112             archiverCtl.setVoidCallable( MockControl.ONE_OR_MORE );
113         }
114         catch ( final ArchiverException e )
115         {
116             Assert.fail( "Should never happen." );
117         }
118     }
119 
120     public void expectModeChange( final int originalDirMode, final int originalFileMode, final int dirMode,
121                                   final int fileMode, final int numberOfChanges )
122     {
123         archiver.getOverrideDirectoryMode();
124         archiverCtl.setReturnValue( originalDirMode );
125 
126         archiver.getOverrideFileMode();
127         archiverCtl.setReturnValue( originalFileMode );
128 
129         // one of the changes will occur below, when we restore the original mode.
130         if ( numberOfChanges > 1 )
131         {
132             for ( int i = 1; i < numberOfChanges; i++ )
133             {
134                 archiver.setDirectoryMode( dirMode );
135                 archiver.setFileMode( fileMode );
136             }
137         }
138 
139         archiver.setDirectoryMode( originalDirMode );
140         archiver.setFileMode( originalFileMode );
141     }
142 
143     public void expectAddFile( final File file, final String outputLocation )
144     {
145         try
146         {
147             archiver.addFile( file, outputLocation );
148         }
149         catch ( final ArchiverException e )
150         {
151             Assert.fail( "Should never happen." );
152         }
153     }
154 
155     public void expectAddFile( final File file, final String outputLocation, final int fileMode )
156     {
157         try
158         {
159             archiver.addFile( file, outputLocation, fileMode );
160         }
161         catch ( final ArchiverException e )
162         {
163             Assert.fail( "Should never happen." );
164         }
165     }
166 
167     public void expectGetReactorProjects( final List<MavenProject> projects )
168     {
169         configSource.getReactorProjects();
170         configSourceCtl.setReturnValue( projects, MockControl.ONE_OR_MORE );
171     }
172 
173     public void expectCSGetFinalName( final String finalName )
174     {
175         configSource.getFinalName();
176         configSourceCtl.setReturnValue( finalName, MockControl.ONE_OR_MORE );
177     }
178 
179     public void expectGetDestFile( final File destFile )
180     {
181         archiver.getDestFile();
182         archiverCtl.setReturnValue( destFile, MockControl.ZERO_OR_MORE );
183     }
184 
185     public void expectCSGetRepositories( final ArtifactRepository localRepo, final List<ArtifactRepository> remoteRepos )
186     {
187         configSource.getLocalRepository();
188         configSourceCtl.setReturnValue( localRepo, MockControl.ONE_OR_MORE );
189 
190         configSource.getRemoteRepositories();
191         configSourceCtl.setReturnValue( remoteRepos, MockControl.ONE_OR_MORE );
192     }
193 
194     public void expectBuildFromRepository( final ProjectBuildingException error )
195     {
196         try
197         {
198             projectBuilder.buildFromRepository( null, null, null );
199             projectBuilderCtl.setMatcher( MockControl.ALWAYS_MATCHER );
200             projectBuilderCtl.setThrowable( error, MockControl.ONE_OR_MORE );
201         }
202         catch ( final ProjectBuildingException e )
203         {
204             Assert.fail( "should never happen" );
205         }
206     }
207 
208     public void expectBuildFromRepository( final MavenProject project )
209     {
210         try
211         {
212             projectBuilder.buildFromRepository( null, null, null );
213             projectBuilderCtl.setMatcher( MockControl.ALWAYS_MATCHER );
214             projectBuilderCtl.setReturnValue( project, MockControl.ONE_OR_MORE );
215         }
216         catch ( final ProjectBuildingException e )
217         {
218             Assert.fail( "should never happen" );
219         }
220     }
221 
222     public void expectGetSession( final MavenSession session )
223     {
224         configSource.getMavenSession();
225         configSourceCtl.setReturnValue( session, MockControl.ZERO_OR_MORE );
226     }
227 
228 }