|
| 1 | +/* |
| 2 | + * Copyright 2025 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.integration.sftp.filters; |
| 18 | + |
| 19 | +import java.nio.file.attribute.FileTime; |
| 20 | +import java.time.Duration; |
| 21 | +import java.time.Instant; |
| 22 | + |
| 23 | +import org.apache.sshd.sftp.client.SftpClient; |
| 24 | +import org.junit.jupiter.api.Test; |
| 25 | + |
| 26 | +import static org.assertj.core.api.Assertions.assertThat; |
| 27 | + |
| 28 | +/** |
| 29 | + * @author Artem Bilan |
| 30 | + * |
| 31 | + * @since 6.5 |
| 32 | + */ |
| 33 | +public class SftpRecentFileListFilterTests { |
| 34 | + |
| 35 | + @Test |
| 36 | + public void testAge() { |
| 37 | + SftpRecentFileListFilter filter = new SftpRecentFileListFilter(Duration.ofHours(20)); |
| 38 | + SftpClient.Attributes attributes1 = new SftpClient.Attributes(); |
| 39 | + attributes1.setModifyTime(FileTime.from(Instant.now())); |
| 40 | + SftpClient.Attributes attributes2 = new SftpClient.Attributes(); |
| 41 | + attributes2.setModifyTime(FileTime.from(Instant.now())); |
| 42 | + |
| 43 | + SftpClient.DirEntry sftpFile1 = new SftpClient.DirEntry("foo", "foo", attributes1); |
| 44 | + SftpClient.DirEntry sftpFile2 = new SftpClient.DirEntry("bar", "bar", attributes2); |
| 45 | + |
| 46 | + SftpClient.DirEntry[] files = new SftpClient.DirEntry[] {sftpFile1, sftpFile2}; |
| 47 | + |
| 48 | + assertThat(filter.filterFiles(files)).hasSize(2); |
| 49 | + assertThat(filter.accept(sftpFile1)).isTrue(); |
| 50 | + assertThat(filter.accept(sftpFile2)).isTrue(); |
| 51 | + |
| 52 | + FileTime fileTime = FileTime.from(Instant.now().minus(Duration.ofDays(1))); |
| 53 | + sftpFile2.getAttributes().setModifyTime(fileTime); |
| 54 | + assertThat(filter.filterFiles(files)).hasSize(1); |
| 55 | + assertThat(filter.accept(sftpFile1)).isTrue(); |
| 56 | + assertThat(filter.accept(sftpFile2)).isFalse(); |
| 57 | + } |
| 58 | + |
| 59 | +} |
0 commit comments