1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.io.comparator;
18
19 import java.io.BufferedOutputStream;
20 import java.io.File;
21 import java.io.IOException;
22 import java.nio.file.Files;
23
24 import org.apache.commons.io.FileUtils;
25 import org.apache.commons.io.test.TestUtils;
26 import org.junit.jupiter.api.BeforeEach;
27
28
29
30
31 public class LastModifiedFileComparatorTest extends ComparatorAbstractTest {
32
33 @BeforeEach
34 public void setUp() throws Exception {
35 comparator = (AbstractFileComparator) LastModifiedFileComparator.LASTMODIFIED_COMPARATOR;
36 reverse = LastModifiedFileComparator.LASTMODIFIED_REVERSE;
37 final File olderFile = new File(dir, "older.txt");
38 if (!olderFile.getParentFile().exists()) {
39 throw new IOException("Cannot create file " + olderFile + " as the parent directory does not exist");
40 }
41 try (BufferedOutputStream output2 = new BufferedOutputStream(Files.newOutputStream(olderFile.toPath()))) {
42 TestUtils.generateTestData(output2, 0);
43 }
44
45 final File equalFile = new File(dir, "equal.txt");
46 if (!equalFile.getParentFile().exists()) {
47 throw new IOException("Cannot create file " + equalFile + " as the parent directory does not exist");
48 }
49 try (BufferedOutputStream output1 = new BufferedOutputStream(Files.newOutputStream(equalFile.toPath()))) {
50 TestUtils.generateTestData(output1, 0);
51 }
52 do {
53 TestUtils.sleepQuietly(300);
54 equalFile.setLastModified(System.currentTimeMillis());
55 } while (FileUtils.lastModified(olderFile) == FileUtils.lastModified(equalFile));
56
57 final File newerFile = new File(dir, "newer.txt");
58 if (!newerFile.getParentFile().exists()) {
59 throw new IOException("Cannot create file " + newerFile + " as the parent directory does not exist");
60 }
61 try (BufferedOutputStream output = new BufferedOutputStream(Files.newOutputStream(newerFile.toPath()))) {
62 TestUtils.generateTestData(output, 0);
63 }
64 do {
65 TestUtils.sleepQuietly(300);
66 newerFile.setLastModified(System.currentTimeMillis());
67 } while (FileUtils.lastModified(equalFile) == FileUtils.lastModified(newerFile));
68 equalFile1 = equalFile;
69 equalFile2 = equalFile;
70 lessFile = olderFile;
71 moreFile = newerFile;
72 }
73
74 }