|
11 | 11 | import java.io.IOException; |
12 | 12 | import java.nio.charset.StandardCharsets; |
13 | 13 | import java.nio.file.Files; |
| 14 | +import java.time.Duration; |
14 | 15 | import java.util.ArrayList; |
| 16 | +import java.util.Date; |
15 | 17 | import java.util.List; |
| 18 | +import java.util.function.Consumer; |
16 | 19 | import org.junit.jupiter.api.Test; |
17 | 20 | import software.amazon.smithy.build.model.MavenRepository; |
18 | 21 | import software.amazon.smithy.utils.IoUtils; |
@@ -48,38 +51,113 @@ public void ignoresAndDeletesEmptyCacheFiles() throws IOException { |
48 | 51 | } |
49 | 52 |
|
50 | 53 | @Test |
51 | | - public void loadsCacheFromDelegateWhenCacheMissingAndSaves() throws IOException { |
| 54 | + public void invalidatesCacheWhenArtifactDeleted() throws IOException { |
| 55 | + // Delete the "JAR" to invalidate the cache. |
| 56 | + validateCacheScenario(File::delete); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void invalidatesCacheWhenArtifactIsNewerThanCache() throws IOException { |
| 61 | + // Set the last modified time of the "JAR" to the future to ensure the cache is invalidated. |
| 62 | + validateCacheScenario(jar -> jar.setLastModified(new Date().getTime() + Duration.parse("P1D").toMillis())); |
| 63 | + } |
| 64 | + |
| 65 | + private void validateCacheScenario(Consumer<File> jarFileMutation) throws IOException { |
52 | 66 | File cache = File.createTempFile("classpath", ".json"); |
53 | | - File jar = File.createTempFile("foo", ".json"); |
| 67 | + File jar = File.createTempFile("foo", ".jar"); |
54 | 68 | Files.write(jar.toPath(), "{}".getBytes(StandardCharsets.UTF_8)); |
55 | 69 |
|
56 | 70 | ResolvedArtifact artifact = ResolvedArtifact.fromCoordinates(jar.toPath(), "com.foo:bar:1.0.0"); |
57 | 71 | List<ResolvedArtifact> result = new ArrayList<>(); |
58 | 72 | result.add(artifact); |
59 | 73 |
|
60 | 74 | Mock mock = new Mock(result); |
61 | | - DependencyResolver resolver = new FileCacheResolver(cache, jar.lastModified(), mock); |
62 | | - List<ResolvedArtifact> resolved = resolver.resolve(); |
| 75 | + DependencyResolver cachingResolver = new FileCacheResolver(cache, jar.lastModified(), mock); |
| 76 | + List<ResolvedArtifact> resolved = cachingResolver.resolve(); |
63 | 77 |
|
| 78 | + // Make sure artifacts were cached as expected. |
64 | 79 | assertThat(resolved, contains(artifact)); |
65 | 80 | assertThat(IoUtils.readUtf8File(cache.toPath()), containsString("com.foo:bar:1.0.0")); |
66 | 81 |
|
67 | | - // Remove the canned entry from the mock to ensure the cache is working before delegating. |
| 82 | + // Remove the canned entry from the mock so that when the cache is invalidated, we get a different result. |
68 | 83 | result.clear(); |
69 | 84 |
|
70 | | - // Calling it again will load from the cached file. |
71 | | - assertThat(resolver.resolve(), contains(artifact)); |
| 85 | + // Calling it again will load from the cached file and not from the delegate mock that's now empty. |
| 86 | + assertThat(cachingResolver.resolve(), contains(artifact)); |
72 | 87 |
|
73 | 88 | // The cache should still be there. |
74 | 89 | assertThat(IoUtils.readUtf8File(cache.toPath()), containsString("com.foo:bar:1.0.0")); |
75 | 90 |
|
76 | | - // Removing the cache artifact invalidates the cache. |
77 | | - assertThat(jar.delete(), is(true)); |
| 91 | + // Mutate the JAR using the provided method. This method should invalidate the cache. |
| 92 | + jarFileMutation.accept(jar); |
78 | 93 |
|
79 | | - assertThat(resolver.resolve(), empty()); |
| 94 | + // Resolving here skips the cache (which contains artifacts) and calls the delegate (which is now empty). |
| 95 | + assertThat(cachingResolver.resolve(), empty()); |
| 96 | + |
| 97 | + // The caching resolver should now write an empty cache file. |
80 | 98 | assertThat(IoUtils.readUtf8File(cache.toPath()), containsString("{}")); |
81 | 99 | } |
82 | 100 |
|
| 101 | + @Test |
| 102 | + public void invalidatesCacheWhenConfigIsNewerThanCache() throws IOException { |
| 103 | + File cache = File.createTempFile("classpath", ".json"); |
| 104 | + File jar = File.createTempFile("foo", ".jar"); |
| 105 | + Files.write(jar.toPath(), "{}".getBytes(StandardCharsets.UTF_8)); |
| 106 | + |
| 107 | + ResolvedArtifact artifact = ResolvedArtifact.fromCoordinates(jar.toPath(), "com.foo:bar:1.0.0"); |
| 108 | + List<ResolvedArtifact> result = new ArrayList<>(); |
| 109 | + result.add(artifact); |
| 110 | + |
| 111 | + Mock mock = new Mock(result); |
| 112 | + // Set the "config" last modified to a future date to ensure it's newer than the "JAR" file. |
| 113 | + DependencyResolver cachingResolver = new FileCacheResolver( |
| 114 | + cache, |
| 115 | + jar.lastModified() + Duration.parse("P1D").toMillis(), |
| 116 | + mock |
| 117 | + ); |
| 118 | + List<ResolvedArtifact> resolved = cachingResolver.resolve(); |
| 119 | + |
| 120 | + // Make sure artifacts were cached as expected. |
| 121 | + assertThat(resolved, contains(artifact)); |
| 122 | + assertThat(IoUtils.readUtf8File(cache.toPath()), containsString("com.foo:bar:1.0.0")); |
| 123 | + |
| 124 | + // Remove the canned entry from the mock so that when the cache is invalidated, we get a different result. |
| 125 | + result.clear(); |
| 126 | + |
| 127 | + // The cache will be invalidated here and reloaded from source, resulting in an empty result. |
| 128 | + assertThat(cachingResolver.resolve(), empty()); |
| 129 | + } |
| 130 | + |
| 131 | + @Test |
| 132 | + public void invalidatesCacheWhenCacheExceedsTTL() throws IOException { |
| 133 | + long tenDaysAgo = new Date().getTime() - Duration.parse("P10D").toMillis(); |
| 134 | + File cache = File.createTempFile("classpath", ".json"); |
| 135 | + File jar = File.createTempFile("foo", ".jar"); |
| 136 | + Files.write(jar.toPath(), "{}".getBytes(StandardCharsets.UTF_8)); |
| 137 | + |
| 138 | + ResolvedArtifact artifact = ResolvedArtifact.fromCoordinates(jar.toPath(), "com.foo:bar:1.0.0"); |
| 139 | + List<ResolvedArtifact> result = new ArrayList<>(); |
| 140 | + result.add(artifact); |
| 141 | + |
| 142 | + Mock mock = new Mock(result); |
| 143 | + // Make sure the config is set to 10 days ago too, so that config date checking doesn't invalidate. |
| 144 | + DependencyResolver cachingResolver = new FileCacheResolver(cache, tenDaysAgo, mock); |
| 145 | + List<ResolvedArtifact> resolved = cachingResolver.resolve(); |
| 146 | + |
| 147 | + // Make sure artifacts were cached as expected. |
| 148 | + assertThat(resolved, contains(artifact)); |
| 149 | + assertThat(IoUtils.readUtf8File(cache.toPath()), containsString("com.foo:bar:1.0.0")); |
| 150 | + |
| 151 | + // Remove the canned entry from the mock so that when the cache is invalidated, we get a different result. |
| 152 | + result.clear(); |
| 153 | + |
| 154 | + // Change the last modified of the cache to a date in the distant past to invalidate the cache. |
| 155 | + assertThat(cache.setLastModified(tenDaysAgo), is(true)); |
| 156 | + |
| 157 | + // The cache will be invalidated here and reloaded from source, resulting in an empty result. |
| 158 | + assertThat(cachingResolver.resolve(), empty()); |
| 159 | + } |
| 160 | + |
83 | 161 | private static final class Mock implements DependencyResolver { |
84 | 162 | final List<ResolvedArtifact> artifacts; |
85 | 163 | final List<MavenRepository> repositories = new ArrayList<>(); |
|
0 commit comments