1 package org.apache.maven.shared.utils;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
37
38
39
40
41
42
43
44
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
71
72
73
74
75 private File getTestTargetDir()
76 throws Exception
77 {
78 return tempFolder.newFolder( TEST_ZIP_TARGET_FOLDER );
79 }
80
81 @Test
82 public void testSetDest_No_NPE()
83 {
84 Expand expand = new Expand();
85 expand.setDest( null );
86 }
87
88 @Test
89 public void testSetSrc_No_NPE()
90 {
91 Expand expand = new Expand();
92 expand.setSrc( null );
93 }
94
95 @Test
96 public void testExecute()
97 throws Exception
98 {
99 Expand expand = new Expand();
100
101 File source = getSourceFile();
102 expand.setSrc( source );
103
104 File targetDir = getTestTargetDir();
105 expand.setDest( targetDir );
106
107 expand.execute();
108
109 verifyExpandedFileAndContent( targetDir, TEST_UNZIPPED_CONTENT );
110 }
111
112 @Test
113 public void testExecuteIntoNonexistingDirectory()
114 throws Exception
115 {
116 Expand expand = new Expand();
117
118 File source = getSourceFile();
119 expand.setSrc( source );
120
121 File nonexisingDir = new File( getTestTargetDir(), "nonexisting_dir" );
122
123 if ( nonexisingDir.exists() )
124 {
125 FileUtils.deleteDirectory( nonexisingDir );
126 }
127
128 expand.setDest( nonexisingDir );
129
130 expand.execute();
131
132 verifyExpandedFileAndContent( nonexisingDir, TEST_UNZIPPED_CONTENT );
133 }
134
135 @Test
136 public void testExecuteNonexistingSource()
137 throws Exception
138 {
139 Expand expand = new Expand();
140
141 File nonexistingSource = new File( "target/expand_test_target/nonexisting_source_file.nixda" );
142 expand.setSrc( nonexistingSource );
143
144 File targetDir = getTestTargetDir();
145 expand.setDest( targetDir );
146
147 try
148 {
149 expand.execute();
150 fail( "expand with notexiting source must throw Exception!" );
151 }
152 catch ( Exception e )
153 {
154 Throwable cause = ExceptionUtils.getRootCause( e );
155 if ( cause == null )
156 {
157 cause = e;
158 }
159
160 assertTrue( "cause must be a FileNotFoundException", cause instanceof FileNotFoundException );
161 }
162
163 }
164
165 @Test( expected = NullPointerException.class )
166 public void testExecute_NullSource()
167 throws Exception
168 {
169 Expand expand = new Expand();
170 expand.setSrc( null );
171
172 File targetDir = getTestTargetDir();
173 expand.setDest( targetDir );
174
175 expand.execute();
176 }
177
178 @Test
179 public void testExecute_NullDest()
180 throws Exception
181 {
182 Expand expand = new Expand();
183 expand.setSrc( getSourceFile() );
184
185
186
187 expand.setDest( null );
188
189 String oldWorkingDirectory = System.getProperty( "user.dir" );
190
191 try
192 {
193 File targetDir = getTestTargetDir();
194 System.setProperty( "user.dir", targetDir.getAbsolutePath() );
195
196 expand.execute();
197
198 verifyExpandedFileAndContent( targetDir, TEST_UNZIPPED_CONTENT );
199 }
200 finally
201 {
202 System.setProperty( "user.dir", oldWorkingDirectory );
203 }
204 }
205
206 @Test
207 public void testExecute_Overwrite()
208 throws Exception
209 {
210 File targetDir = getTestTargetDir();
211 File expandedFile = null;
212
213 {
214
215
216 Expand expand = new Expand();
217
218 File source = getSourceFile();
219 expand.setSrc( source );
220
221 expand.setDest( targetDir );
222
223 expand.execute();
224
225 expandedFile = verifyExpandedFile( targetDir );
226 }
227
228
229 long time = System.currentTimeMillis() - 10000L;
230
231
232 time = time - time % 1000L;
233
234 expandedFile.setLastModified( time );
235 assertEquals( time, expandedFile.lastModified() );
236
237 {
238
239
240 Expand expand = new Expand();
241
242 File source = getSourceFile();
243 expand.setSrc( source );
244 expand.setDest( targetDir );
245
246 expand.setOverwrite( false );
247
248 expand.execute();
249
250 expandedFile = verifyExpandedFile( targetDir );
251
252 assertEquals( "file must still have the old lastModified timestamp"
253 , time, expandedFile.lastModified() );
254
255 }
256
257 {
258
259
260 Expand expand = new Expand();
261
262 File source = getSourceFile();
263 expand.setSrc( source );
264 expand.setDest( targetDir );
265
266 expand.setOverwrite( true );
267
268 expand.execute();
269
270 expandedFile = verifyExpandedFile( targetDir );
271
272
273 assertTrue( "file must now have the original old lastModified timestamp, but was: time=" + time
274 + " expandedFile.lastModified()= " + expandedFile.lastModified()
275 , time > expandedFile.lastModified() );
276 }
277
278
279 time = 100000000L;
280
281 expandedFile.setLastModified( time );
282 assertEquals( time, expandedFile.lastModified() );
283
284 {
285
286
287 Expand expand = new Expand();
288
289 File source = getSourceFile();
290 expand.setSrc( source );
291 expand.setDest( targetDir );
292
293 expand.setOverwrite( true );
294
295 expand.execute();
296
297 expandedFile = verifyExpandedFile( targetDir );
298
299 assertTrue( "file must now have newer lastModified timestamp, but was: time=" + time
300 + " expandedFile.lastModified()= " + expandedFile.lastModified()
301 , time < expandedFile.lastModified() );
302
303 }
304 }
305
306
307 private File verifyExpandedFile( File targetDir )
308 {
309 assertThat( "target directory must exist"
310 , targetDir.exists()
311 , is( true ) );
312
313 File expandedFile = new File( targetDir, TEST_UNZIPPED_FILE );
314
315 assertThat( "expanded file must exist: " + expandedFile.getAbsolutePath()
316 , expandedFile.exists()
317 , is( true ) );
318
319 return expandedFile;
320 }
321
322 private void verifyExpandedFileAndContent( File targetDir, String expectedContent )
323 throws FileNotFoundException
324 {
325 File expandedFile = verifyExpandedFile( targetDir );
326
327 assertNotNull( expandedFile );
328
329 java.util.Scanner scanner = new java.util.Scanner( expandedFile ).useDelimiter( "\n" );
330 String text = scanner.next();
331
332 assertThat( "expanded file content must match"
333 , text
334 , is( expectedContent ) );
335 }
336 }