View Javadoc

1   package org.apache.maven.plugin.assembly.archive.task;
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 java.io.File;
23  import java.io.IOException;
24  import java.util.Collections;
25  
26  import junit.framework.TestCase;
27  
28  import org.apache.maven.plugin.assembly.archive.ArchiveCreationException;
29  import org.apache.maven.plugin.assembly.testutils.MockManager;
30  import org.apache.maven.plugin.assembly.testutils.TestFileManager;
31  import org.codehaus.plexus.archiver.Archiver;
32  import org.codehaus.plexus.archiver.ArchiverException;
33  import org.easymock.MockControl;
34  
35  public class AddDirectoryTaskTest
36      extends TestCase
37  {
38  
39      private MockManager mockManager;
40  
41      private TestFileManager fileManager;
42  
43      private Archiver archiver;
44  
45      private MockControl archiverControl;
46  
47      public void setUp()
48      {
49          fileManager = new TestFileManager( "ArchiveAssemblyUtils.test.", "" );
50  
51          mockManager = new MockManager();
52  
53          archiverControl = MockControl.createControl( Archiver.class );
54          mockManager.add( archiverControl );
55  
56          archiver = (Archiver) archiverControl.getMock();
57      }
58  
59      public void tearDown()
60          throws IOException
61      {
62          fileManager.cleanUp();
63      }
64  
65      public void testAddDirectory_ShouldNotAddDirectoryIfNonExistent()
66          throws ArchiveCreationException
67      {
68          File dir = new File( System.getProperty( "java.io.tmpdir" ), "non-existent." + System.currentTimeMillis() );
69  
70          configureModeExpectations( -1, -1, -1, -1, false );
71  
72          mockManager.replayAll();
73  
74          AddDirectoryTask task = new AddDirectoryTask( dir );
75  
76          task.execute( archiver, null );
77  
78          mockManager.verifyAll();
79      }
80  
81      public void testAddDirectory_ShouldAddDirectory()
82          throws ArchiveCreationException
83      {
84          File dir = fileManager.createTempDir();
85  
86          try
87          {
88              archiver.addFileSet( null );
89              archiverControl.setMatcher( MockControl.ALWAYS_MATCHER );
90          }
91          catch ( ArchiverException e )
92          {
93              fail( "Should never happen." );
94          }
95  
96          configureModeExpectations( -1, -1, -1, -1, false );
97  
98          mockManager.replayAll();
99  
100         AddDirectoryTask task = new AddDirectoryTask( dir );
101 
102         task.setOutputDirectory( "dir" );
103 
104         task.execute( archiver, null );
105 
106         mockManager.verifyAll();
107     }
108 
109     public void testAddDirectory_ShouldAddDirectoryWithDirMode()
110         throws ArchiveCreationException
111     {
112         File dir = fileManager.createTempDir();
113 
114         try
115         {
116             archiver.addFileSet( null );
117             archiverControl.setMatcher( MockControl.ALWAYS_MATCHER );
118         }
119         catch ( ArchiverException e )
120         {
121             fail( "Should never happen." );
122         }
123 
124         int dirMode = Integer.parseInt( "777", 8 );
125         int fileMode = Integer.parseInt( "777", 8 );
126 
127         configureModeExpectations( -1, -1, dirMode, fileMode, true );
128 
129         mockManager.replayAll();
130 
131         AddDirectoryTask task = new AddDirectoryTask( dir );
132 
133         task.setDirectoryMode( dirMode );
134         task.setFileMode( fileMode );
135         task.setOutputDirectory( "dir" );
136 
137         task.execute( archiver, null );
138 
139         mockManager.verifyAll();
140     }
141 
142     public void testAddDirectory_ShouldAddDirectoryWithIncludesAndExcludes()
143         throws ArchiveCreationException
144     {
145         File dir = fileManager.createTempDir();
146 
147         try
148         {
149             archiver.addFileSet( null );
150             archiverControl.setMatcher( MockControl.ALWAYS_MATCHER );
151         }
152         catch ( ArchiverException e )
153         {
154             fail( "Should never happen." );
155         }
156 
157         configureModeExpectations( -1, -1, -1, -1, false );
158 
159         mockManager.replayAll();
160 
161         AddDirectoryTask task = new AddDirectoryTask( dir );
162 
163         task.setIncludes( Collections.singletonList( "**/*.txt" ) );
164         task.setExcludes( Collections.singletonList( "**/README.txt" ) );
165         task.setOutputDirectory( "dir" );
166 
167         task.execute( archiver, null );
168 
169         mockManager.verifyAll();
170     }
171 
172     private void configureModeExpectations( int defaultDirMode, int defaultFileMode, int dirMode, int fileMode,
173                                             boolean expectTwoSets )
174     {
175         archiver.getOverrideDirectoryMode();
176         archiverControl.setReturnValue( defaultDirMode );
177 
178         archiver.getOverrideFileMode();
179         archiverControl.setReturnValue( defaultFileMode );
180 
181         if ( expectTwoSets )
182         {
183             if ( dirMode > -1 )
184             {
185                 archiver.setDirectoryMode( dirMode );
186             }
187             
188             if ( fileMode > -1 )
189             {
190                 archiver.setFileMode( fileMode );
191             }
192         }
193 
194         if ( dirMode > -1 )
195         {
196             archiver.setDirectoryMode( defaultDirMode );
197         }
198         
199         if ( fileMode > -1 )
200         {
201             archiver.setFileMode( defaultFileMode );
202         }
203     }
204 
205 }