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