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