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.TestFileManager;
30  import org.codehaus.plexus.archiver.Archiver;
31  import org.codehaus.plexus.archiver.ArchiverException;
32  import org.codehaus.plexus.archiver.FileSet;
33  import org.easymock.classextension.EasyMockSupport;
34  
35  import static org.easymock.EasyMock.anyObject;
36  import static org.easymock.EasyMock.expect;
37  
38  public class AddDirectoryTaskTest
39      extends TestCase
40  {
41  
42      private EasyMockSupport mockManager;
43  
44      private TestFileManager fileManager;
45  
46      private Archiver archiver;
47  
48  
49      public void setUp()
50      {
51          fileManager = new TestFileManager( "ArchiveAssemblyUtils.test.", "" );
52  
53          mockManager = new EasyMockSupport();
54  
55  
56          archiver = mockManager.createMock(Archiver.class);
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 );
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( (FileSet) anyObject() );
89          }
90          catch ( ArchiverException e )
91          {
92              fail( "Should never happen." );
93          }
94  
95          configureModeExpectations( -1, -1, -1, -1, false );
96  
97          mockManager.replayAll();
98  
99          AddDirectoryTask task = new AddDirectoryTask( dir );
100 
101         task.setOutputDirectory( "dir" );
102 
103         task.execute( archiver );
104 
105         mockManager.verifyAll();
106     }
107 
108     public void testAddDirectory_ShouldAddDirectoryWithDirMode()
109         throws ArchiveCreationException
110     {
111         File dir = fileManager.createTempDir();
112 
113         try
114         {
115             archiver.addFileSet( (FileSet) anyObject() );
116         }
117         catch ( ArchiverException e )
118         {
119             fail( "Should never happen." );
120         }
121 
122         int dirMode = Integer.parseInt( "777", 8 );
123         int fileMode = Integer.parseInt( "777", 8 );
124 
125         configureModeExpectations( -1, -1, dirMode, fileMode, true );
126 
127         mockManager.replayAll();
128 
129         AddDirectoryTask task = new AddDirectoryTask( dir );
130 
131         task.setDirectoryMode( dirMode );
132         task.setFileMode( fileMode );
133         task.setOutputDirectory( "dir" );
134 
135         task.execute( archiver );
136 
137         mockManager.verifyAll();
138     }
139 
140     public void testAddDirectory_ShouldAddDirectoryWithIncludesAndExcludes()
141         throws ArchiveCreationException
142     {
143         File dir = fileManager.createTempDir();
144 
145         try
146         {
147             archiver.addFileSet( (FileSet) anyObject() );
148         }
149         catch ( ArchiverException e )
150         {
151             fail( "Should never happen." );
152         }
153 
154         configureModeExpectations( -1, -1, -1, -1, false );
155 
156         mockManager.replayAll();
157 
158         AddDirectoryTask task = new AddDirectoryTask( dir );
159 
160         task.setIncludes( Collections.singletonList( "**/*.txt" ) );
161         task.setExcludes( Collections.singletonList( "**/README.txt" ) );
162         task.setOutputDirectory( "dir" );
163 
164         task.execute( archiver );
165 
166         mockManager.verifyAll();
167     }
168 
169     private void configureModeExpectations( int defaultDirMode, int defaultFileMode, int dirMode, int fileMode,
170                                             boolean expectTwoSets )
171     {
172         expect(archiver.getOverrideDirectoryMode()).andReturn( defaultDirMode );
173         expect(archiver.getOverrideFileMode()).andReturn( defaultFileMode );
174 
175         if ( expectTwoSets )
176         {
177             if ( dirMode > -1 )
178             {
179                 archiver.setDirectoryMode( dirMode );
180             }
181             
182             if ( fileMode > -1 )
183             {
184                 archiver.setFileMode( fileMode );
185             }
186         }
187 
188         if ( dirMode > -1 )
189         {
190             archiver.setDirectoryMode( defaultDirMode );
191         }
192         
193         if ( fileMode > -1 )
194         {
195             archiver.setFileMode( defaultFileMode );
196         }
197     }
198 
199 }