View Javadoc
1   package org.codehaus.plexus.compiler.util.scan;
2   
3   /*
4    * Copyright 2001-2005 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import java.io.File;
20  import java.util.Collections;
21  import java.util.Set;
22  
23  import org.codehaus.plexus.compiler.util.scan.mapping.SingleTargetSourceMapping;
24  import org.codehaus.plexus.compiler.util.scan.mapping.SourceMapping;
25  import org.codehaus.plexus.compiler.util.scan.mapping.SuffixMapping;
26  
27  /**
28   * @author jdcasey
29   * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
30   */
31  public class StaleSourceScannerTest
32      extends AbstractSourceInclusionScannerTest
33  {
34  
35      protected void setUp()
36          throws Exception
37      {
38          super.setUp();
39          scanner = new StaleSourceScanner();
40      }
41  
42      // test 1.
43      public void testWithDefaultConstructorShouldFindOneStaleSource()
44          throws Exception
45      {
46          File base = new File( getTestBaseDir(), "test1" );
47  
48          long now = System.currentTimeMillis();
49  
50          File targetFile = new File( base, "file.xml" );
51  
52          writeFile( targetFile );
53  
54          targetFile.setLastModified( now - 60000 );
55  
56          File sourceFile = new File( base, "file.java" );
57  
58          writeFile( sourceFile );
59  
60          sourceFile.setLastModified( now );
61  
62          SuffixMapping mapping = new SuffixMapping( ".java", ".xml" );
63  
64          scanner.addSourceMapping( mapping );
65  
66          Set<File> result = scanner.getIncludedSources( base, base );
67  
68          assertEquals( "wrong number of stale sources returned.", 1, result.size() );
69  
70          assertTrue( "expected stale source file not found in result", result.contains( sourceFile ) );
71      }
72  
73      // test 2.
74      public void testWithDefaultConstructorShouldNotFindStaleSources()
75          throws Exception
76      {
77          File base = new File( getTestBaseDir(), "test2" );
78  
79          long now = System.currentTimeMillis();
80  
81          File sourceFile = new File( base, "file.java" );
82  
83          writeFile( sourceFile );
84  
85          sourceFile.setLastModified( now - 60000 );
86  
87          File targetFile = new File( base, "file.xml" );
88  
89          writeFile( targetFile );
90  
91          targetFile.setLastModified( now );
92  
93          SuffixMapping mapping = new SuffixMapping( ".java", ".xml" );
94  
95          scanner.addSourceMapping( mapping );
96  
97          Set<File> result = scanner.getIncludedSources( base, base );
98  
99          assertEquals( "wrong number of stale sources returned.", 0, result.size() );
100 
101         assertFalse( "expected stale source file not found in result", result.contains( sourceFile ) );
102     }
103 
104     // test 3.
105     public void testWithDefaultConstructorShouldFindStaleSourcesBecauseOfMissingTargetFile()
106         throws Exception
107     {
108         File base = new File( getTestBaseDir(), "test3" );
109 
110         File sourceFile = new File( base, "file.java" );
111 
112         writeFile( sourceFile );
113 
114         SuffixMapping mapping = new SuffixMapping( ".java", ".xml" );
115 
116         scanner.addSourceMapping( mapping );
117 
118         Set<File> result = scanner.getIncludedSources( base, base );
119 
120         assertEquals( "wrong number of stale sources returned.", 1, result.size() );
121 
122         assertTrue( "expected stale source file not found in result", result.contains( sourceFile ) );
123     }
124 
125     // test 4.
126     public void testWithDefaultConstructorShouldFindStaleSourcesOneBecauseOfMissingTargetAndOneBecauseOfStaleTarget()
127         throws Exception
128     {
129         File base = new File( getTestBaseDir(), "test4" );
130 
131         long now = System.currentTimeMillis();
132 
133         File targetFile = new File( base, "file2.xml" );
134 
135         writeFile( targetFile );
136 
137         targetFile.setLastModified( now - 60000 );
138 
139         File sourceFile = new File( base, "file.java" );
140 
141         writeFile( sourceFile );
142 
143         File sourceFile2 = new File( base, "file2.java" );
144 
145         writeFile( sourceFile2 );
146 
147         sourceFile2.setLastModified( now );
148 
149         SuffixMapping mapping = new SuffixMapping( ".java", ".xml" );
150 
151         scanner.addSourceMapping( mapping );
152 
153         Set<File> result = scanner.getIncludedSources( base, base );
154 
155         assertEquals( "wrong number of stale sources returned.", 2, result.size() );
156 
157         assertTrue( "expected stale source file not found in result", result.contains( sourceFile ) );
158 
159         assertTrue( "expected stale source file not found in result", result.contains( sourceFile2 ) );
160     }
161 
162     // test 5.
163     public void testWithDefaultConstructorShouldFindOneStaleSourcesWithStaleTargetAndOmitUpToDateSource()
164         throws Exception
165     {
166         File base = new File( getTestBaseDir(), "test5" );
167 
168         long now = System.currentTimeMillis();
169 
170         // target/source (1) should result in source being included.
171 
172         // write the target file first, and set the lastmod to some time in the
173         // past to ensure this.
174         File targetFile = new File( base, "file.xml" );
175 
176         writeFile( targetFile );
177 
178         targetFile.setLastModified( now - 60000 );
179 
180         // now write the source file, and set the lastmod to now.
181         File sourceFile = new File( base, "file.java" );
182 
183         writeFile( sourceFile );
184 
185         sourceFile.setLastModified( now );
186 
187         // target/source (2) should result in source being omitted.
188 
189         // write the source file first, and set the lastmod to some time in the
190         // past to ensure this.
191         File sourceFile2 = new File( base, "file2.java" );
192 
193         writeFile( sourceFile2 );
194 
195         sourceFile2.setLastModified( now - 60000 );
196 
197         // now write the target file, with lastmod of now.
198         File targetFile2 = new File( base, "file2.xml" );
199 
200         writeFile( targetFile2 );
201 
202         targetFile2.setLastModified( now );
203 
204         SuffixMapping mapping = new SuffixMapping( ".java", ".xml" );
205 
206         scanner.addSourceMapping( mapping );
207 
208         Set<File> result = scanner.getIncludedSources( base, base );
209 
210         assertEquals( "wrong number of stale sources returned.", 1, result.size() );
211 
212         assertTrue( "expected stale source file not found in result", result.contains( sourceFile ) );
213     }
214 
215     // test 6.
216     public void testConstructedWithMsecsShouldReturnOneSourceFileOfTwoDueToLastMod()
217         throws Exception
218     {
219         File base = new File( getTestBaseDir(), "test6" );
220 
221         long now = System.currentTimeMillis();
222 
223         File targetFile = new File( base, "file.xml" );
224 
225         writeFile( targetFile );
226 
227         // should be within the threshold of lastMod for stale sources.
228         targetFile.setLastModified( now - 8000 );
229 
230         File sourceFile = new File( base, "file.java" );
231 
232         writeFile( sourceFile );
233 
234         // modified 'now' for comparison with the above target file.
235         sourceFile.setLastModified( now );
236 
237         File targetFile2 = new File( base, "file2.xml" );
238 
239         writeFile( targetFile2 );
240 
241         targetFile2.setLastModified( now - 12000 );
242 
243         File sourceFile2 = new File( base, "file2.java" );
244 
245         writeFile( sourceFile2 );
246 
247         // modified 'now' for comparison to above target file.
248         sourceFile2.setLastModified( now );
249 
250         SuffixMapping mapping = new SuffixMapping( ".java", ".xml" );
251 
252         scanner = new StaleSourceScanner( 10000 );
253 
254         scanner.addSourceMapping( mapping );
255 
256         Set<File> result = scanner.getIncludedSources( base, base );
257 
258         assertEquals( "wrong number of stale sources returned.", 1, result.size() );
259 
260         assertTrue( "expected stale source file not found in result", result.contains( sourceFile2 ) );
261 
262         assertFalse( "expected stale source file not found in result", result.contains( sourceFile ) );
263     }
264 
265     // test 7.
266     public void testConstructedWithMsecsIncludesAndExcludesShouldReturnOneSourceFileOfThreeDueToIncludePattern()
267         throws Exception
268     {
269         File base = new File( getTestBaseDir(), "test7" );
270 
271         long now = System.currentTimeMillis();
272 
273         File targetFile = new File( base, "file.xml" );
274 
275         writeFile( targetFile );
276 
277         // should be within the threshold of lastMod for stale sources.
278         targetFile.setLastModified( now - 12000 );
279 
280         File sourceFile = new File( base, "file.java" );
281 
282         writeFile( sourceFile );
283 
284         // modified 'now' for comparison with the above target file.
285         sourceFile.setLastModified( now );
286 
287         File targetFile2 = new File( base, "file2.xml" );
288 
289         writeFile( targetFile2 );
290 
291         targetFile2.setLastModified( now - 12000 );
292 
293         File sourceFile2 = new File( base, "file2.java" );
294 
295         writeFile( sourceFile2 );
296 
297         // modified 'now' for comparison to above target file.
298         sourceFile2.setLastModified( now );
299 
300         File targetFile3 = new File( base, "file3.xml" );
301 
302         writeFile( targetFile3 );
303 
304         targetFile3.setLastModified( now - 12000 );
305 
306         File sourceFile3 = new File( base, "file3.java" );
307 
308         writeFile( sourceFile3 );
309 
310         // modified 'now' for comparison to above target file.
311         sourceFile3.setLastModified( now );
312 
313         SuffixMapping mapping = new SuffixMapping( ".java", ".xml" );
314 
315         scanner = new StaleSourceScanner( 0, Collections.singleton( "*3.java" ), Collections.<String>emptySet() );
316 
317         scanner.addSourceMapping( mapping );
318 
319         Set<File> result = scanner.getIncludedSources( base, base );
320 
321         assertEquals( "wrong number of stale sources returned.", 1, result.size() );
322 
323         assertFalse( "expected stale source file not found in result", result.contains( sourceFile ) );
324 
325         assertFalse( "unexpected stale source file found in result", result.contains( sourceFile2 ) );
326 
327         assertTrue( "unexpected stale source file found in result", result.contains( sourceFile3 ) );
328     }
329 
330     // test 8.
331     public void testConstructedWithMsecsIncludesAndExcludesShouldReturnTwoSourceFilesOfThreeDueToExcludePattern()
332         throws Exception
333     {
334         File base = new File( getTestBaseDir(), "test8" );
335 
336         long now = System.currentTimeMillis();
337 
338         File targetFile = new File( base, "fileX.xml" );
339 
340         writeFile( targetFile );
341 
342         // should be within the threshold of lastMod for stale sources.
343         targetFile.setLastModified( now - 12000 );
344 
345         File sourceFile = new File( base, "fileX.java" );
346 
347         writeFile( sourceFile );
348 
349         // modified 'now' for comparison with the above target file.
350         sourceFile.setLastModified( now );
351 
352         File targetFile2 = new File( base, "file2.xml" );
353 
354         writeFile( targetFile2 );
355 
356         targetFile2.setLastModified( now - 12000 );
357 
358         File sourceFile2 = new File( base, "file2.java" );
359 
360         writeFile( sourceFile2 );
361 
362         // modified 'now' for comparison to above target file.
363         sourceFile2.setLastModified( now );
364 
365         File targetFile3 = new File( base, "file3.xml" );
366 
367         writeFile( targetFile3 );
368 
369         targetFile3.setLastModified( now - 12000 );
370 
371         File sourceFile3 = new File( base, "file3.java" );
372 
373         writeFile( sourceFile3 );
374 
375         // modified 'now' for comparison to above target file.
376         sourceFile3.setLastModified( now );
377 
378         SuffixMapping mapping = new SuffixMapping( ".java", ".xml" );
379 
380         scanner = new StaleSourceScanner( 0, Collections.singleton( "**/*" ), Collections.singleton( "*X.*" ) );
381 
382         scanner.addSourceMapping( mapping );
383 
384         Set<File> result = scanner.getIncludedSources( base, base );
385 
386         assertEquals( "wrong number of stale sources returned.", 2, result.size() );
387 
388         assertFalse( "unexpected stale source file found in result", result.contains( sourceFile ) );
389 
390         assertTrue( "expected stale source not file found in result", result.contains( sourceFile2 ) );
391 
392         assertTrue( "expected stale source not file found in result", result.contains( sourceFile3 ) );
393     }
394 
395     // test 9.
396     public void testSingleFileSourceMapping()
397         throws Exception
398     {
399         File src = new File( getTestBaseDir(), "test9-src" );
400 
401         File target = new File( getTestBaseDir(), "test9-target" );
402 
403         long now = System.currentTimeMillis();
404 
405         // ----------------------------------------------------------------------
406         // The output file is missing
407         // ----------------------------------------------------------------------
408 
409         File fooCs = new File( src, "Foo.cs" );
410 
411         writeFile( fooCs );
412 
413         fooCs.setLastModified( now - 10000 );
414 
415         SourceMapping mapping = new SingleTargetSourceMapping( ".cs", "Application.exe" );
416 
417         scanner = new StaleSourceScanner( 0 );
418 
419         scanner.addSourceMapping( mapping );
420 
421         Set<File> result = scanner.getIncludedSources( src, target );
422 
423         assertEquals( 1, result.size() );
424 
425         assertTrue( result.contains( fooCs ) );
426 
427         // ----------------------------------------------------------------------
428         // Add another source file
429         // ----------------------------------------------------------------------
430 
431         File barCs = new File( src, "Bar.cs" );
432 
433         writeFile( barCs );
434 
435         barCs.setLastModified( now - 20000 );
436 
437         result = scanner.getIncludedSources( src, target );
438 
439         assertEquals( 2, result.size() );
440 
441         assertTrue( result.contains( fooCs ) );
442 
443         assertTrue( result.contains( barCs ) );
444 
445         // ----------------------------------------------------------------------
446         // Now add the result file
447         // ----------------------------------------------------------------------
448 
449         File applicationExe = new File( target, "Application.exe" );
450 
451         writeFile( applicationExe );
452 
453         applicationExe.setLastModified( now );
454 
455         result = scanner.getIncludedSources( src, target );
456 
457         assertEquals( 0, result.size() );
458 
459         // ----------------------------------------------------------------------
460         // Make Application.exe older than the Foo.cs
461         // ----------------------------------------------------------------------
462 
463         applicationExe.setLastModified( now - 15000 );
464 
465         result = scanner.getIncludedSources( src, target );
466 
467         assertEquals( 1, result.size() );
468 
469         assertTrue( result.contains( fooCs ) );
470     }
471 
472 }