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.execution.MavenSession;
23  import org.apache.maven.plugin.assembly.AssemblerConfigurationSource;
24  import org.apache.maven.plugin.assembly.testutils.MockManager;
25  import org.apache.maven.plugin.assembly.testutils.TestFileManager;
26  import org.apache.maven.project.MavenProject;
27  import org.codehaus.plexus.archiver.Archiver;
28  import org.codehaus.plexus.archiver.ArchiverException;
29  import org.easymock.MockControl;
30  
31  import java.io.File;
32  
33  import junit.framework.Assert;
34  
35  public class MockAndControlForAddFileSetsTask
36  {
37  
38      public AssemblerConfigurationSource configSource;
39  
40      public MockControl configSourceCtl;
41  
42      public Archiver archiver;
43  
44      public MockControl archiverCtl;
45  
46      public TestFileManager fileManager;
47  
48      public File archiveBaseDir;
49  
50      public MockAndControlForAddFileSetsTask( MockManager mockManager, TestFileManager fileManager )
51      {
52          this.fileManager = fileManager;
53  
54          configSourceCtl = MockControl.createControl( AssemblerConfigurationSource.class );
55          mockManager.add( configSourceCtl );
56  
57          configSource = (AssemblerConfigurationSource) configSourceCtl.getMock();
58  
59          archiverCtl = MockControl.createControl( Archiver.class );
60          mockManager.add( archiverCtl );
61  
62          archiver = (Archiver) archiverCtl.getMock();
63  
64          archiveBaseDir = fileManager.createTempDir();
65  
66          configSource.getMavenSession();
67          configSourceCtl.setReturnValue( null, MockControl.ZERO_OR_MORE );
68      }
69  
70      public void expectGetArchiveBaseDirectory()
71      {
72          configSource.getArchiveBaseDirectory();
73          configSourceCtl.setReturnValue( archiveBaseDir, MockControl.ONE_OR_MORE );
74      }
75  
76      public void expectGetBasedir( File basedir )
77      {
78          configSource.getBasedir();
79          configSourceCtl.setReturnValue( basedir, MockControl.ONE_OR_MORE );
80      }
81  
82      public void expectModeChanges( int[] modes, int modeChangeCount )
83      {
84          archiver.getOverrideDirectoryMode();
85          archiverCtl.setReturnValue( modes[0] );
86  
87          archiver.getOverrideFileMode();
88          archiverCtl.setReturnValue( modes[1] );
89  
90          if ( modeChangeCount > 1 )
91          {
92              for ( int i = 1; i < modeChangeCount; i++ )
93              {
94                  if ( modes[2] > -1 )
95                  {
96                      archiver.setDirectoryMode( modes[2] );
97                  }
98                  
99                  if ( modes[3] > -1 )
100                 {
101                     archiver.setFileMode( modes[3] );
102                 }
103             }
104         }
105 
106         if ( modes[2] > -1 )
107         {
108             archiver.setDirectoryMode( modes[0] );
109         }
110         
111         if ( modes[3] > -1 )
112         {
113             archiver.setFileMode( modes[1] );
114         }
115     }
116 
117     public void expectAdditionOfSingleFileSet( MavenProject project, File basedir, String finalName,
118                                                boolean shouldAddDir, int[] modes, int modeChangeCount,
119                                                boolean isDebugEnabled )
120     {
121         expectAdditionOfSingleFileSet( project, basedir, finalName, shouldAddDir, modes, modeChangeCount,
122                                        isDebugEnabled, true );
123 
124     }
125 
126     public void expectAdditionOfSingleFileSet( MavenProject project, File basedir, String finalName,
127                                                boolean shouldAddDir, int[] modes, int modeChangeCount,
128                                                boolean isDebugEnabled, boolean isProjectUsed )
129     {
130         // the logger sends a debug message with this info inside the addFileSet(..) method..
131         if ( isDebugEnabled )
132         {
133             archiver.getOverrideDirectoryMode();
134             archiverCtl.setReturnValue( modes[0] );
135 
136             archiver.getOverrideFileMode();
137             archiverCtl.setReturnValue( modes[1] );
138         }
139 
140         if ( isProjectUsed )
141         {
142             configSource.getProject();
143             configSourceCtl.setReturnValue( project, MockControl.ONE_OR_MORE );
144         }
145 
146         configSource.getFinalName();
147         configSourceCtl.setReturnValue( finalName, MockControl.ONE_OR_MORE );
148 
149         if ( shouldAddDir )
150         {
151             expectModeChanges( modes, modeChangeCount );
152 
153             try
154             {
155                 archiver.addFileSet( null );
156                 archiverCtl.setMatcher( MockControl.ALWAYS_MATCHER );
157                 archiverCtl.setVoidCallable( MockControl.ONE_OR_MORE );
158             }
159             catch ( ArchiverException e )
160             {
161                 Assert.fail( "Should never happen." );
162             }
163         }
164 
165     }
166 
167     public void expectGetProject( MavenProject project )
168     {
169         configSource.getProject();
170         configSourceCtl.setReturnValue( project, MockControl.ONE_OR_MORE );
171     }
172 
173     public void expectGetSession( MavenSession session )
174     {
175         configSource.getMavenSession();
176         configSourceCtl.setReturnValue( session, MockControl.ONE_OR_MORE );
177     }
178 
179     public void expectGetFinalName( String finalName )
180     {
181         configSource.getFinalName();
182         configSourceCtl.setReturnValue( finalName, MockControl.ONE_OR_MORE );
183     }
184 
185 }