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.vfs2;
18  
19  import static org.junit.jupiter.api.Assertions.assertNotEquals;
20  
21  import java.time.Duration;
22  import java.time.Instant;
23  import java.util.Date;
24  
25  import org.junit.Test;
26  import org.junit.jupiter.api.Assertions;
27  
28  /**
29   * Test cases for getting and setting file last modified time.
30   */
31  public class LastModifiedTests extends AbstractProviderTestCase {
32  
33      protected static final Duration ONE_DAY = Duration.ofDays(1);
34  
35      protected void assertDeltaMillis(final String message, final long expectedMillis, final long actualMillis, final long deltaMillis) {
36          if (expectedMillis == actualMillis) {
37              return;
38          }
39          // getLastModTimeAccuracy() is not accurate
40          final long actualDelta = Math.abs(expectedMillis - actualMillis);
41          if (actualDelta > Math.max(deltaMillis, 1000)) {
42              Assertions.fail(String.format("%s expected=%,d (%s), actual=%,d (%s), expected delta=%,d millis, actual delta=%,d millis", message,
43                      Long.valueOf(expectedMillis), new Date(expectedMillis).toString(), Long.valueOf(actualMillis), new Date(actualMillis).toString(),
44                      Long.valueOf(deltaMillis), Long.valueOf(actualDelta)));
45          }
46      }
47  
48      protected void assertEqualMillis(final String message, final long expectedMillis, final long actualMillis) {
49          if (expectedMillis != actualMillis) {
50              final long delta = Math.abs(expectedMillis - actualMillis);
51              Assertions.fail(String.format("%s expected=%,d (%s), actual=%,d (%s), delta=%,d millis", message, Long.valueOf(expectedMillis),
52                      new Date(expectedMillis).toString(), Long.valueOf(actualMillis), new Date(actualMillis).toString(), delta));
53          }
54      }
55  
56      /**
57       * Returns the capabilities required by the tests of this test case.
58       */
59      @Override
60      protected Capability[] getRequiredCapabilities() {
61          return new Capability[] {Capability.GET_LAST_MODIFIED};
62      }
63  
64      /**
65       * Tests FileSystem#getLastModTimeAccuracy for sane values.
66       *
67       * @throws FileSystemException if error occurred
68       */
69      @Test
70      public void testGetAccuracy() throws FileSystemException {
71          final FileObject file = getReadFolder().resolveFile("file1.txt");
72          final long lastModTimeAccuracyMillis = (long) file.getFileSystem().getLastModTimeAccuracy();
73          // System.out.println("Accuracy on " + file.getFileSystem().getRootURI() + " is " + lastModTimeAccuracy + " as
74          // told by " + file.getFileSystem().getClass().getCanonicalName());
75          assertTrue("Accuracy must be positive", lastModTimeAccuracyMillis >= 0);
76          // just any sane limit
77          assertTrue("Accuracy must be < 2m", lastModTimeAccuracyMillis < Duration.ofMinutes(2).toMillis());
78      }
79  
80      /**
81       * Tests getting the last modified time of a file.
82       *
83       * @throws FileSystemException if error occurred
84       */
85      @Test
86      public void testGetLastModifiedFile() throws FileSystemException {
87          final FileObject file = getReadFolder().resolveFile("file1.txt");
88          assertNotEquals(0L, file.getContent().getLastModifiedTime());
89      }
90  
91      /**
92       * Tests getting the last modified time of a folder.
93       *
94       * @throws FileSystemException if error occurred
95       */
96      @Test
97      public void testGetLastModifiedFolder() throws FileSystemException {
98          final FileObject file = getReadFolder().resolveFile("dir1");
99          assertNotEquals(0L, file.getContent().getLastModifiedTime());
100     }
101 
102     /**
103      * Tests setting the last modified time of file.
104      *
105      * @throws FileSystemException if error occurred
106      */
107     @Test
108     public void testSetLastModifiedFile() throws FileSystemException {
109         final long yesterdayMillis = Instant.now().minus(ONE_DAY).toEpochMilli();
110 
111         if (getReadFolder().getFileSystem().hasCapability(Capability.SET_LAST_MODIFIED_FILE)) {
112             // Try a file
113             final FileObject file = getReadFolder().resolveFile("file1.txt");
114             file.getContent().setLastModifiedTime(yesterdayMillis);
115             final long lastModTimeAccuracyMillis = (long) file.getFileSystem().getLastModTimeAccuracy();
116             // folder.refresh(); TODO: does not work with SSH VFS-563
117             final long lastModifiedTime = file.getContent().getLastModifiedTime();
118             assertDeltaMillis("set/getLastModified on File", yesterdayMillis, lastModifiedTime,
119                 lastModTimeAccuracyMillis);
120         }
121     }
122 
123     /**
124      * Tests setting the last modified time of a folder.
125      *
126      * @throws FileSystemException if error occurred
127      */
128     @Test
129     public void testSetLastModifiedFolder() throws FileSystemException {
130         final long yesterdayMillis = Instant.now().minus(ONE_DAY).toEpochMilli();
131 
132         if (getReadFolder().getFileSystem().hasCapability(Capability.SET_LAST_MODIFIED_FOLDER)) {
133             // Try a folder
134             final FileObject folder = getReadFolder().resolveFile("dir1");
135             folder.getContent().setLastModifiedTime(yesterdayMillis);
136             final long lastModTimeAccuracyMillis = (long) folder.getFileSystem().getLastModTimeAccuracy();
137             // folder.refresh(); TODO: does not work with SSH VFS-563
138             final long lastModifiedTime = folder.getContent().getLastModifiedTime();
139             assertDeltaMillis("set/getLastModified on Folder", yesterdayMillis, lastModifiedTime,
140                 lastModTimeAccuracyMillis);
141         }
142     }
143 
144 }