View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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   * Tests {@link LastModifiedFileComparator}.
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  }