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