24
24
package org .codehaus .plexus .archiver ;
25
25
26
26
import java .io .File ;
27
+ import java .io .IOException ;
28
+ import java .nio .file .Files ;
29
+ import java .nio .file .attribute .FileTime ;
30
+
27
31
import org .codehaus .plexus .PlexusTestCase ;
28
32
import org .codehaus .plexus .util .FileUtils ;
29
33
@@ -35,31 +39,54 @@ public abstract class BasePlexusArchiverTest extends PlexusTestCase
35
39
{
36
40
37
41
/**
38
- * Ensure that when a new file is created at the specified location that the timestamp of
39
- * that file will be greater than the one specified as a reference.
40
- *
41
- * Warning: Runs in a busy loop creating a file until the output file is newer than the reference timestamp.
42
- * This should be better than sleeping for a race condition time out value.
42
+ * Ensure that the last modified timestamp of a file will be greater
43
+ * than the one specified as a reference.
43
44
*
44
- * @param outputFile the file to be created
45
- * @param timestampReference the created file will have a newer timestamp than this reference timestamp.
45
+ * @param outputFile the file
46
+ * @param timestampReference the file will have a newer timestamp
47
+ * than this reference timestamp.
46
48
*
47
- * @throws Exception failures
49
+ * @throws IOException if the timestamp could not be modified
48
50
*/
49
- protected void waitUntilNewTimestamp ( File outputFile , long timestampReference ) throws Exception
51
+ protected void waitUntilNewTimestamp ( File outputFile , long timestampReference )
52
+ throws IOException
50
53
{
51
- File tmpFile = File .createTempFile ( "ZipArchiverTest.waitUntilNewTimestamp" , null );
52
- // slurp the file into a temp file and then copy the temp back over the top until it is newer.
53
- FileUtils .copyFile ( outputFile , tmpFile );
54
+ long startTime = System .currentTimeMillis ();
55
+ File tmpFile = File .createTempFile (
56
+ "BasePlexusArchiverTest.waitUntilNewTimestamp" , null );
57
+ long newTimestamp ;
54
58
55
- FileUtils .copyFile ( tmpFile , outputFile );
56
- while ( timestampReference >= outputFile .lastModified () )
59
+ // We could easily just set the last modified time using
60
+ // Files.setLastModifiedTime and System.currentTimeMillis(),
61
+ // but the problem is that tests are using this method to verify that
62
+ // the force flag is working. To ensure that modified or
63
+ // newly created files will have timestamp newer than
64
+ // `timestampReference`, we need to modify a file ourself.
65
+ // Otherwise the build may fail because when the test overrides
66
+ // `outputFile` it will have timestamp that is equal
67
+ // to `timestampReference`.
68
+ do
57
69
{
58
- FileUtils .copyFile ( tmpFile , outputFile );
70
+ FileUtils .fileWrite ( tmpFile , "waitUntilNewTimestamp" );
71
+ newTimestamp = tmpFile .lastModified ();
59
72
Thread .yield ();
60
73
}
74
+ while ( timestampReference >= newTimestamp
75
+ // A simple guard to ensure that we'll not do this forever.
76
+ // If the last modified timestamp is not changed to
77
+ // a newer value after 10 seconds, probably it never will.
78
+ && System .currentTimeMillis () - startTime < 10_000 );
61
79
62
80
tmpFile .delete ();
81
+
82
+ if ( timestampReference >= newTimestamp )
83
+ {
84
+ throw new IOException ("Could not modify the last modified timestamp "
85
+ + "to newer than the refence value." );
86
+ }
87
+
88
+ FileTime newTimestampTime = FileTime .fromMillis ( newTimestamp );
89
+ Files .setLastModifiedTime ( outputFile .toPath (), newTimestampTime );
63
90
}
64
91
65
92
/**
0 commit comments