1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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 }