View Javadoc

1   package org.apache.maven.shared.utils;
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.commons.lang3.exception.ExceptionUtils;
23  import org.apache.maven.shared.utils.io.FileUtils;
24  import org.junit.Rule;
25  import org.junit.Test;
26  import org.junit.Assert;
27  import org.junit.rules.TemporaryFolder;
28  
29  import java.io.File;
30  import java.io.FileNotFoundException;
31  import java.net.URL;
32  
33  import static org.hamcrest.CoreMatchers.*;
34  
35  /**
36   * This will test the plexus utility class {@link Expand}.
37   *
38   * Most of this stuff will be obsolete because java-1.4.2
39   * introduced a java.util.zip package which works like a charm.
40   *
41   * We of course need to implement this class due to compatibility
42   * reasons.
43   *
44   * @author <a href="mailto:struberg@yahoo.de">Mark Struberg</a>
45   */
46  public class ExpandTest
47      extends Assert
48  {
49  
50      private static final String TEST_ZIP_LOCATION = "/expand/expand_test.zip";
51      private static final String TEST_ZIP_TARGET_FOLDER = "expand_test_target/";
52  
53      private static final String TEST_UNZIPPED_FILE = "expand_test/test_file.txt";
54      private static final String TEST_UNZIPPED_CONTENT = "TestContent";
55  
56  
57      @Rule
58      public TemporaryFolder tempFolder = new TemporaryFolder();
59  
60      private File getSourceFile()
61      {
62          URL zipFileUrl = getClass().getResource( TEST_ZIP_LOCATION );
63  
64          assertNotNull( zipFileUrl );
65  
66          return new File( zipFileUrl.getFile() );
67      }
68  
69      /**
70       * Create a clean target directory for unzipping.
71       * If it did exist, then clean it first.
72       *
73       * @return
74       */
75      private File getTestTargetDir()
76      {
77          return tempFolder.newFolder( TEST_ZIP_TARGET_FOLDER );
78      }
79  
80      @Test
81      public void testSetDest_No_NPE()
82      {
83          Expand expand = new Expand();
84          expand.setDest( null );
85      }
86  
87      @Test
88      public void testSetSrc_No_NPE()
89      {
90          Expand expand = new Expand();
91          expand.setSrc( null );
92      }
93  
94      @Test
95      public void testExecute()
96          throws Exception
97      {
98          Expand expand = new Expand();
99  
100         File source = getSourceFile();
101         expand.setSrc( source );
102 
103         File targetDir = getTestTargetDir();
104         expand.setDest( targetDir );
105 
106         expand.execute();
107 
108         verifyExpandedFileAndContent( targetDir, TEST_UNZIPPED_CONTENT );
109     }
110 
111     @Test
112     public void testExecuteIntoNonexistingDirectory()
113         throws Exception
114     {
115         Expand expand = new Expand();
116 
117         File source = getSourceFile();
118         expand.setSrc( source );
119 
120         File nonexisingDir = new File( getTestTargetDir(), "nonexisting_dir" );
121 
122         if ( nonexisingDir.exists() )
123         {
124             FileUtils.deleteDirectory( nonexisingDir );
125         }
126 
127         expand.setDest( nonexisingDir );
128 
129         expand.execute();
130 
131         verifyExpandedFileAndContent( nonexisingDir, TEST_UNZIPPED_CONTENT );
132     }
133 
134     @Test
135     public void testExecuteNonexistingSource()
136         throws Exception
137     {
138         Expand expand = new Expand();
139 
140         File nonexistingSource = new File( "target/expand_test_target/nonexisting_source_file.nixda" );
141         expand.setSrc( nonexistingSource );
142 
143         File targetDir = getTestTargetDir();
144         expand.setDest( targetDir );
145 
146         try
147         {
148             expand.execute();
149             fail( "expand with notexiting source must throw Exception!" );
150         }
151         catch ( Exception e )
152         {
153             Throwable cause = ExceptionUtils.getRootCause( e );
154             if ( cause == null )
155             {
156                 cause = e;
157             }
158 
159             assertTrue( "cause must be a FileNotFoundException", cause instanceof FileNotFoundException );
160         }
161 
162     }
163 
164     @Test( expected = NullPointerException.class )
165     public void testExecute_NullSource()
166         throws Exception
167     {
168         Expand expand = new Expand();
169         expand.setSrc( null );
170 
171         File targetDir = getTestTargetDir();
172         expand.setDest( targetDir );
173 
174         expand.execute();
175     }
176 
177     @Test
178     public void testExecute_NullDest()
179         throws Exception
180     {
181         Expand expand = new Expand();
182         expand.setSrc( getSourceFile() );
183 
184         // execute without a dest directory seems to
185         // expand all the archive into the current working directory
186         expand.setDest( null );
187 
188         String oldWorkingDirectory = System.getProperty( "user.dir" );
189 
190         try
191         {
192             File targetDir = getTestTargetDir();
193             System.setProperty( "user.dir", targetDir.getAbsolutePath() );
194 
195             expand.execute();
196 
197             verifyExpandedFileAndContent( targetDir, TEST_UNZIPPED_CONTENT );
198         }
199         finally
200         {
201             System.setProperty( "user.dir", oldWorkingDirectory );
202         }
203     }
204 
205     @Test
206     public void testExecute_Overwrite()
207         throws Exception
208     {
209         File targetDir = getTestTargetDir();
210         File expandedFile = null;
211 
212         {
213             // part1: expand
214 
215             Expand expand = new Expand();
216 
217             File source = getSourceFile();
218             expand.setSrc( source );
219 
220             expand.setDest( targetDir );
221 
222             expand.execute();
223 
224             expandedFile = verifyExpandedFile( targetDir );
225         }
226 
227         // turn the clock back 10 seconds
228         long time = System.currentTimeMillis() - 10000L;
229 
230         // round down to 1s;
231         time = time - time % 1000L;
232 
233         expandedFile.setLastModified( time );
234         assertEquals( time, expandedFile.lastModified() );
235 
236         {
237             // part2: expand in non-overwrite mode
238 
239             Expand expand = new Expand();
240 
241             File source = getSourceFile();
242             expand.setSrc( source );
243             expand.setDest( targetDir );
244 
245             expand.setOverwrite( false );
246 
247             expand.execute();
248 
249             expandedFile = verifyExpandedFile( targetDir );
250 
251             assertEquals( "file must still have the old lastModified timestamp"
252                         , time, expandedFile.lastModified() );
253 
254         }
255 
256         {
257             // part3: expand in overwrite mode but local file is still newer than the one in the archive
258 
259             Expand expand = new Expand();
260 
261             File source = getSourceFile();
262             expand.setSrc( source );
263             expand.setDest( targetDir );
264 
265             expand.setOverwrite( true );
266 
267             expand.execute();
268 
269             expandedFile = verifyExpandedFile( targetDir );
270 
271             // obviously the file will be overwritten anyway
272             assertTrue( "file must now have the original old lastModified timestamp, but was: time=" + time
273                         + " expandedFile.lastModified()= " + expandedFile.lastModified()
274                         , time > expandedFile.lastModified() );
275         }
276 
277         // turn the clock back a loooong time!
278         time = 100000000L;
279 
280         expandedFile.setLastModified( time );
281         assertEquals( time, expandedFile.lastModified() );
282 
283         {
284             // part3: expand in overwrite mode but local file is now older than the one in the archive
285 
286             Expand expand = new Expand();
287 
288             File source = getSourceFile();
289             expand.setSrc( source );
290             expand.setDest( targetDir );
291 
292             expand.setOverwrite( true );
293 
294             expand.execute();
295 
296             expandedFile = verifyExpandedFile( targetDir );
297 
298             assertTrue( "file must now have newer lastModified timestamp, but was: time=" + time
299                         + " expandedFile.lastModified()= " + expandedFile.lastModified()
300                         , time < expandedFile.lastModified() );
301 
302         }
303     }
304 
305 
306     private File verifyExpandedFile( File targetDir )
307     {
308         assertThat( "target directory must exist"
309                   , targetDir.exists()
310                   , is( true ) );
311 
312         File expandedFile = new File( targetDir, TEST_UNZIPPED_FILE );
313 
314         assertThat( "expanded file must exist: " + expandedFile.getAbsolutePath()
315                   , expandedFile.exists()
316                   , is( true ) );
317 
318         return expandedFile;
319     }
320 
321     private void verifyExpandedFileAndContent( File targetDir, String expectedContent )
322             throws FileNotFoundException
323     {
324         File expandedFile = verifyExpandedFile( targetDir );
325 
326         assertNotNull( expandedFile );
327 
328         java.util.Scanner scanner = new java.util.Scanner( expandedFile ).useDelimiter( "\n" );
329         String text = scanner.next();
330 
331         assertThat( "expanded file content must match"
332                   , text
333                   , is( expectedContent ) );
334     }
335 }