|
| 1 | +package aQute.bnd.repository.fileset; |
| 2 | + |
| 3 | +import static org.assertj.core.api.Assertions.assertThatObject; |
| 4 | + |
| 5 | +import java.nio.charset.StandardCharsets; |
| 6 | +import java.nio.file.Files; |
| 7 | +import java.nio.file.Path; |
| 8 | +import java.nio.file.attribute.BasicFileAttributeView; |
| 9 | +import java.nio.file.attribute.BasicFileAttributes; |
| 10 | +import java.nio.file.attribute.FileTime; |
| 11 | +import java.time.Instant; |
| 12 | + |
| 13 | +import org.junit.jupiter.api.Test; |
| 14 | +import org.junit.jupiter.api.condition.DisabledOnOs; |
| 15 | +import org.junit.jupiter.api.condition.OS; |
| 16 | + |
| 17 | +import aQute.bnd.test.jupiter.InjectTemporaryDirectory; |
| 18 | +import aQute.lib.io.IO; |
| 19 | + |
| 20 | +class RepositoryCacheKeyTest { |
| 21 | + |
| 22 | + @Test |
| 23 | + void unchanged(@InjectTemporaryDirectory |
| 24 | + Path tmp) throws Exception { |
| 25 | + Path subject = tmp.resolve("test"); |
| 26 | + IO.store("line1", subject, StandardCharsets.UTF_8); |
| 27 | + ResourceCacheKey key1 = new ResourceCacheKey(subject); |
| 28 | + ResourceCacheKey key2 = new ResourceCacheKey(subject); |
| 29 | + assertThatObject(key1).isEqualTo(key2); |
| 30 | + assertThatObject(key1).hasSameHashCodeAs(key2); |
| 31 | + } |
| 32 | + |
| 33 | + @Test |
| 34 | + void change_modified(@InjectTemporaryDirectory |
| 35 | + Path tmp) throws Exception { |
| 36 | + Path subject = tmp.resolve("test"); |
| 37 | + IO.store("line1", subject, StandardCharsets.UTF_8); |
| 38 | + ResourceCacheKey key1 = new ResourceCacheKey(subject); |
| 39 | + BasicFileAttributes attributes = Files.getFileAttributeView(subject, BasicFileAttributeView.class) |
| 40 | + .readAttributes(); |
| 41 | + FileTime lastModifiedTime = attributes.lastModifiedTime(); |
| 42 | + Instant plusSeconds = lastModifiedTime.toInstant() |
| 43 | + .plusSeconds(10L); |
| 44 | + Files.setLastModifiedTime(subject, FileTime.from(plusSeconds)); |
| 45 | + ResourceCacheKey key2 = new ResourceCacheKey(subject); |
| 46 | + assertThatObject(key1).isNotEqualTo(key2); |
| 47 | + assertThatObject(key1).doesNotHaveSameHashCodeAs(key2); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + void change_size(@InjectTemporaryDirectory |
| 52 | + Path tmp) throws Exception { |
| 53 | + Path subject = tmp.resolve("test"); |
| 54 | + IO.store("line1", subject, StandardCharsets.UTF_8); |
| 55 | + ResourceCacheKey key1 = new ResourceCacheKey(subject); |
| 56 | + BasicFileAttributes attributes = Files.getFileAttributeView(subject, BasicFileAttributeView.class) |
| 57 | + .readAttributes(); |
| 58 | + FileTime lastModifiedTime = attributes.lastModifiedTime(); |
| 59 | + IO.store("line100", subject, StandardCharsets.UTF_8); |
| 60 | + Files.setLastModifiedTime(subject, lastModifiedTime); |
| 61 | + ResourceCacheKey key2 = new ResourceCacheKey(subject); |
| 62 | + assertThatObject(key1).isNotEqualTo(key2); |
| 63 | + assertThatObject(key1).doesNotHaveSameHashCodeAs(key2); |
| 64 | + } |
| 65 | + |
| 66 | + @DisabledOnOs(value = OS.WINDOWS, disabledReason = "Windows FS does not support fileKey") |
| 67 | + @Test |
| 68 | + void change_filekey(@InjectTemporaryDirectory |
| 69 | + Path tmp) throws Exception { |
| 70 | + Path subject = tmp.resolve("test"); |
| 71 | + IO.store("line1", subject, StandardCharsets.UTF_8); |
| 72 | + ResourceCacheKey key1 = new ResourceCacheKey(subject); |
| 73 | + BasicFileAttributes attributes = Files.getFileAttributeView(subject, BasicFileAttributeView.class) |
| 74 | + .readAttributes(); |
| 75 | + assertThatObject(attributes.fileKey()).isNotNull(); |
| 76 | + FileTime lastModifiedTime = attributes.lastModifiedTime(); |
| 77 | + Path subject2 = tmp.resolve("test.tmp"); |
| 78 | + IO.store("line2", subject2, StandardCharsets.UTF_8); |
| 79 | + Files.setLastModifiedTime(subject2, lastModifiedTime); |
| 80 | + IO.rename(subject2, subject); |
| 81 | + ResourceCacheKey key2 = new ResourceCacheKey(subject); |
| 82 | + attributes = Files.getFileAttributeView(subject, BasicFileAttributeView.class) |
| 83 | + .readAttributes(); |
| 84 | + assertThatObject(attributes.fileKey()).isNotNull(); |
| 85 | + assertThatObject(key1).as("key2 not equal") |
| 86 | + .isNotEqualTo(key2); |
| 87 | + assertThatObject(key1).as("key2 different hash") |
| 88 | + .doesNotHaveSameHashCodeAs(key2); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + void change_file_modified(@InjectTemporaryDirectory |
| 93 | + Path tmp) throws Exception { |
| 94 | + Path subject = tmp.resolve("test"); |
| 95 | + IO.store("line1", subject, StandardCharsets.UTF_8); |
| 96 | + ResourceCacheKey key1 = new ResourceCacheKey(subject); |
| 97 | + Path subject2 = tmp.resolve("test.tmp"); |
| 98 | + IO.store("line2", subject2, StandardCharsets.UTF_8); |
| 99 | + BasicFileAttributes attributes = Files.getFileAttributeView(subject2, BasicFileAttributeView.class) |
| 100 | + .readAttributes(); |
| 101 | + FileTime lastModifiedTime = attributes.lastModifiedTime(); |
| 102 | + Instant plusSeconds = lastModifiedTime.toInstant() |
| 103 | + .plusSeconds(10L); |
| 104 | + Files.setLastModifiedTime(subject2, FileTime.from(plusSeconds)); |
| 105 | + IO.rename(subject2, subject); |
| 106 | + ResourceCacheKey key2 = new ResourceCacheKey(subject); |
| 107 | + assertThatObject(key1).as("key2 not equal") |
| 108 | + .isNotEqualTo(key2); |
| 109 | + assertThatObject(key1).as("key2 different hash") |
| 110 | + .doesNotHaveSameHashCodeAs(key2); |
| 111 | + } |
| 112 | + |
| 113 | + @Test |
| 114 | + void different_files(@InjectTemporaryDirectory |
| 115 | + Path tmp) throws Exception { |
| 116 | + Path subject1 = tmp.resolve("test1"); |
| 117 | + IO.store("line1", subject1, StandardCharsets.UTF_8); |
| 118 | + ResourceCacheKey key1 = new ResourceCacheKey(subject1); |
| 119 | + Path subject2 = tmp.resolve("test2"); |
| 120 | + IO.copy(subject1, subject2); |
| 121 | + ResourceCacheKey key2 = new ResourceCacheKey(subject2); |
| 122 | + assertThatObject(key1).isNotEqualTo(key2); |
| 123 | + assertThatObject(key1).doesNotHaveSameHashCodeAs(key2); |
| 124 | + } |
| 125 | + |
| 126 | +} |
0 commit comments