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.provider.ftp;
18  
19  import static org.mockito.Mockito.spy;
20  import static org.mockito.Mockito.when;
21  
22  import java.io.IOException;
23  import java.time.Instant;
24  import java.util.concurrent.ThreadLocalRandom;
25  
26  import org.apache.commons.vfs2.FileContent;
27  import org.apache.commons.vfs2.FileObject;
28  import org.apache.commons.vfs2.FileSystemException;
29  import org.apache.commons.vfs2.LastModifiedTests;
30  import org.junit.Test;
31  import org.junit.jupiter.api.Assertions;
32  
33  public class FtpMdtmOnRefreshLastModifiedTests extends LastModifiedTests {
34  
35      private void returnsCorrectMdtmValue(final FtpFileObject fileObject) throws IOException {
36          final String relPath = fileObject.getRelPath();
37          final FtpClient ftpClient = spyClient(fileObject);
38  
39          final long expected = ThreadLocalRandom.current().nextLong(Instant.now().toEpochMilli());
40          when(ftpClient.mdtmInstant(relPath)).thenReturn(Instant.ofEpochMilli(expected));
41  
42          final long lastModTIme = fileObject.getContent().getLastModifiedTime();
43  
44          if (expected != lastModTIme) {
45              Assertions.fail(String.format("%s returned epoch %s not expected: %s.",
46                      FtpFileObject.class.getSimpleName(), lastModTIme, expected));
47          }
48      }
49  
50      private FtpClient spyClient(final FtpFileObject fileObject) throws FileSystemException {
51          final FtpFileSystem fileSystem = (FtpFileSystem) fileObject.getFileSystem();
52          final FtpClient ftpClientSpy = spy(fileSystem.getClient());
53          fileSystem.putClient(ftpClientSpy);
54          return ftpClientSpy;
55      }
56  
57      /**
58       * Tests {@link FileContent#getLastModifiedTime()} re-calls {@link FtpClient#mdtmInstant(String)} after refresh.
59       */
60      @Test
61      public void testGetLastModifiedFileExactMatchRefresh() throws IOException {
62          final String fileName = "file1.txt";
63          final FileObject readFolder = getReadFolder();
64          final FtpFileObject fileObject = (FtpFileObject) readFolder.resolveFile(fileName);
65  
66          returnsCorrectMdtmValue(fileObject);
67          fileObject.refresh();
68          returnsCorrectMdtmValue(fileObject);
69      }
70  
71  }