2424package org .codehaus .plexus .archiver ;
2525
2626import java .io .File ;
27+ import java .io .IOException ;
28+ import java .nio .file .Files ;
29+ import java .nio .file .attribute .FileTime ;
30+
2731import org .codehaus .plexus .PlexusTestCase ;
2832import org .codehaus .plexus .util .FileUtils ;
2933
@@ -35,31 +39,54 @@ public abstract class BasePlexusArchiverTest extends PlexusTestCase
3539{
3640
3741 /**
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.
4344 *
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.
4648 *
47- * @throws Exception failures
49+ * @throws IOException if the timestamp could not be modified
4850 */
49- protected void waitUntilNewTimestamp ( File outputFile , long timestampReference ) throws Exception
51+ protected void waitUntilNewTimestamp ( File outputFile , long timestampReference )
52+ throws IOException
5053 {
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 ;
5458
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
5769 {
58- FileUtils .copyFile ( tmpFile , outputFile );
70+ FileUtils .fileWrite ( tmpFile , "waitUntilNewTimestamp" );
71+ newTimestamp = tmpFile .lastModified ();
5972 Thread .yield ();
6073 }
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 );
6179
6280 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 );
6390 }
6491
6592 /**
0 commit comments