View Javadoc
1   package org.apache.maven.plugins.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 junit.framework.Assert;
23  import org.apache.maven.plugins.assembly.AssemblerConfigurationSource;
24  import org.apache.maven.plugins.assembly.testutils.TestFileManager;
25  import org.apache.maven.project.MavenProject;
26  import org.codehaus.plexus.archiver.Archiver;
27  import org.codehaus.plexus.archiver.ArchiverException;
28  import org.codehaus.plexus.archiver.FileSet;
29  import org.easymock.EasyMock;
30  import org.easymock.classextension.EasyMockSupport;
31  
32  import java.io.File;
33  
34  import static org.easymock.EasyMock.anyObject;
35  import static org.easymock.EasyMock.expect;
36  
37  public class MockAndControlForAddFileSetsTask
38  {
39  
40      public final AssemblerConfigurationSource configSource;
41  
42  
43      public final Archiver archiver;
44  
45      public File archiveBaseDir;
46  
47      public MockAndControlForAddFileSetsTask( EasyMockSupport mockManager, TestFileManager fileManager )
48      {
49          configSource = mockManager.createMock( AssemblerConfigurationSource.class );
50          archiver = mockManager.createMock( Archiver.class );
51          archiveBaseDir = fileManager.createTempDir();
52  
53          expect( configSource.getMavenSession() ).andReturn( null ).anyTimes();
54      }
55  
56      public void expectGetArchiveBaseDirectory()
57      {
58          expect( configSource.getArchiveBaseDirectory() ).andReturn( archiveBaseDir ).anyTimes();
59      }
60  
61      void expectModeChanges( int[] modes, int modeChangeCount )
62      {
63          expect( archiver.getOverrideDirectoryMode() ).andReturn( modes[0] );
64          expect( archiver.getOverrideFileMode() ).andReturn( modes[1] );
65  
66          if ( modeChangeCount > 1 )
67          {
68              for ( int i = 1; i < modeChangeCount; i++ )
69              {
70                  if ( modes[2] > -1 )
71                  {
72                      archiver.setDirectoryMode( modes[2] );
73                  }
74  
75                  if ( modes[3] > -1 )
76                  {
77                      archiver.setFileMode( modes[3] );
78                  }
79              }
80          }
81  
82          if ( modes[2] > -1 )
83          {
84              archiver.setDirectoryMode( modes[0] );
85          }
86  
87          if ( modes[3] > -1 )
88          {
89              archiver.setFileMode( modes[1] );
90          }
91      }
92  
93      public void expectAdditionOfSingleFileSet( MavenProject project, String finalName, boolean shouldAddDir,
94                                                 int[] modes, int modeChangeCount, boolean isDebugEnabled )
95      {
96          expectAdditionOfSingleFileSet( project, finalName, shouldAddDir, modes, modeChangeCount, isDebugEnabled, true );
97  
98      }
99  
100     public void expectAdditionOfSingleFileSet( MavenProject project, String finalName, boolean shouldAddDir,
101                                                int[] modes, int modeChangeCount, boolean isDebugEnabled,
102                                                boolean isProjectUsed )
103     {
104         // the logger sends a debug message with this info inside the addFileSet(..) method..
105         if ( isDebugEnabled )
106         {
107             expect( archiver.getOverrideDirectoryMode() ).andReturn( modes[0] );
108             expect( archiver.getOverrideFileMode() ).andReturn( modes[1] );
109         }
110 
111         if ( isProjectUsed )
112         {
113             expect( configSource.getProject() ).andReturn( project ).atLeastOnce();
114         }
115 
116         expect( configSource.getFinalName() ).andReturn( finalName ).atLeastOnce();
117 
118         if ( shouldAddDir )
119         {
120             expectModeChanges( modes, modeChangeCount );
121 
122             try
123             {
124                 archiver.addFileSet( (FileSet) anyObject() );
125                 EasyMock.expectLastCall().atLeastOnce();
126             }
127             catch ( ArchiverException e )
128             {
129                 Assert.fail( "Should never happen." );
130             }
131         }
132 
133     }
134 
135     public void expectGetProject( MavenProject project )
136     {
137         expect( configSource.getProject() ).andReturn( project ).atLeastOnce();
138     }
139 
140     public void expectGetFinalName( String finalName )
141     {
142         expect( configSource.getFinalName() ).andReturn( finalName ).atLeastOnce();
143     }
144 
145 }