2020import static org .junit .jupiter .api .Assertions .assertArrayEquals ;
2121import static org .junit .jupiter .api .Assertions .assertEquals ;
2222import static org .junit .jupiter .api .Assertions .assertFalse ;
23+ import static org .junit .jupiter .api .Assertions .assertNotEquals ;
2324import static org .junit .jupiter .api .Assertions .assertNull ;
25+ import static org .junit .jupiter .api .Assertions .assertThrows ;
2426import static org .junit .jupiter .api .Assertions .assertThrowsExactly ;
2527import static org .junit .jupiter .api .Assertions .assertTrue ;
2628import static org .junit .jupiter .api .Assumptions .assumeFalse ;
3941import java .nio .file .Path ;
4042import java .nio .file .Paths ;
4143import java .nio .file .attribute .DosFileAttributeView ;
44+ import java .nio .file .attribute .FileTime ;
4245import java .nio .file .attribute .PosixFileAttributes ;
46+ import java .util .GregorianCalendar ;
4347import java .util .HashMap ;
4448import java .util .Iterator ;
4549import java .util .Map ;
4650
51+ import org .apache .commons .io .FileUtils ;
4752import org .apache .commons .io .filefilter .NameFileFilter ;
4853import org .apache .commons .io .test .TestUtils ;
4954import org .apache .commons .lang3 .ArrayUtils ;
@@ -84,6 +89,10 @@ private Path createTempSymlinkedRelativeDir() throws IOException {
8489 return symlinkDir ;
8590 }
8691
92+ private Long getLastModifiedMillis (final Path file ) throws IOException {
93+ return Files .getLastModifiedTime (file ).toMillis ();
94+ }
95+
8796 private FileSystem openArchive (final Path p , final boolean createNew ) throws IOException {
8897 if (createNew ) {
8998 final Map <String , String > env = new HashMap <>();
@@ -95,6 +104,10 @@ private FileSystem openArchive(final Path p, final boolean createNew) throws IOE
95104 return FileSystems .newFileSystem (p , (ClassLoader ) null );
96105 }
97106
107+ private void setLastModifiedMillis (final Path file , final long millis ) throws IOException {
108+ Files .setLastModifiedTime (file , FileTime .fromMillis (millis ));
109+ }
110+
98111 @ Test
99112 public void testCopyDirectoryForDifferentFilesystemsWithAbsolutePath () throws IOException {
100113 final Path archivePath = Paths .get (TEST_JAR_PATH );
@@ -407,6 +420,32 @@ public void testSetReadOnlyFile() throws IOException {
407420 PathUtils .deleteFile (resolved );
408421 }
409422
423+ @ Test
424+ public void testTouch () throws IOException {
425+ assertThrows (NullPointerException .class , () -> FileUtils .touch (null ));
426+
427+ final Path file = managedTempDirPath .resolve ("touch.txt" );
428+ Files .deleteIfExists (file );
429+ assertFalse (Files .exists (file ), "Bad test: test file still exists" );
430+ PathUtils .touch (file );
431+ assertTrue (Files .exists (file ), "touch() created file" );
432+ try (final OutputStream out = Files .newOutputStream (file )) {
433+ assertEquals (0 , Files .size (file ), "Created empty file." );
434+ out .write (0 );
435+ }
436+ assertEquals (1 , Files .size (file ), "Wrote one byte to file" );
437+ final long y2k = new GregorianCalendar (2000 , 0 , 1 ).getTime ().getTime ();
438+ setLastModifiedMillis (file , y2k ); // 0L fails on Win98
439+ assertEquals (y2k , getLastModifiedMillis (file ), "Bad test: set lastModified set incorrect value" );
440+ final long nowMillis = System .currentTimeMillis ();
441+ PathUtils .touch (file );
442+ assertEquals (1 , Files .size (file ), "FileUtils.touch() didn't empty the file." );
443+ assertNotEquals (y2k , getLastModifiedMillis (file ), "FileUtils.touch() changed lastModified" );
444+ final int delta = 3000 ;
445+ assertTrue (getLastModifiedMillis (file ) >= nowMillis - delta , "FileUtils.touch() changed lastModified to more than now-3s" );
446+ assertTrue (getLastModifiedMillis (file ) <= nowMillis + delta , "FileUtils.touch() changed lastModified to less than now+3s" );
447+ }
448+
410449 @ Test
411450 public void testWriteStringToFile1 () throws Exception {
412451 final Path file = tempDirPath .resolve ("write.txt" );
0 commit comments