From 107f249a8a5b2f35a873c034f1c46760258b226b Mon Sep 17 00:00:00 2001 From: Peter Streef Date: Mon, 16 Dec 2024 12:27:51 +0100 Subject: [PATCH] Allow file scheme in `RemoteArchive` to simplify testing While it might look a bit controversial, the file scheme can also point to a remote (for instance a mounted network share) file. By allowing the `file://` scheme we can use `RemoteArchive` for those files. As a useful side effect, this makes testing RemoteArchive handling a lot easier. --- .../org/openrewrite/remote/RemoteArchive.java | 4 +++ .../openrewrite/remote/RemoteArchiveTest.java | 26 +++++++++++++----- rewrite-core/src/test/resources/zipfile.zip | Bin 0 -> 193 bytes 3 files changed, 23 insertions(+), 7 deletions(-) create mode 100644 rewrite-core/src/test/resources/zipfile.zip diff --git a/rewrite-core/src/main/java/org/openrewrite/remote/RemoteArchive.java b/rewrite-core/src/main/java/org/openrewrite/remote/RemoteArchive.java index 23de1da8810..6e553fdac4b 100644 --- a/rewrite-core/src/main/java/org/openrewrite/remote/RemoteArchive.java +++ b/rewrite-core/src/main/java/org/openrewrite/remote/RemoteArchive.java @@ -34,6 +34,7 @@ import java.nio.charset.Charset; import java.nio.file.Files; import java.nio.file.Path; +import java.nio.file.Paths; import java.util.List; import java.util.UUID; import java.util.regex.Pattern; @@ -92,6 +93,9 @@ public InputStream getInputStream(ExecutionContext ctx) { try { Path localArchive = cache.compute(uri, () -> { //noinspection resource + if ("file".equals(uri.getScheme())) { + return Files.newInputStream(Paths.get(uri)); + } HttpSender.Response response = httpSender.send(httpSender.get(uri.toString()).build()); if (response.isSuccessful()) { return response.getBody(); diff --git a/rewrite-core/src/test/java/org/openrewrite/remote/RemoteArchiveTest.java b/rewrite-core/src/test/java/org/openrewrite/remote/RemoteArchiveTest.java index f96eea97f00..135c0a1d7c8 100644 --- a/rewrite-core/src/test/java/org/openrewrite/remote/RemoteArchiveTest.java +++ b/rewrite-core/src/test/java/org/openrewrite/remote/RemoteArchiveTest.java @@ -21,10 +21,12 @@ import org.openrewrite.ExecutionContext; import org.openrewrite.HttpSenderExecutionContextView; import org.openrewrite.InMemoryExecutionContext; +import org.openrewrite.PrintOutputCapture; import org.openrewrite.test.MockHttpSender; import java.io.IOException; import java.io.InputStream; +import java.net.URISyntaxException; import java.net.URL; import java.nio.file.Paths; import java.util.concurrent.*; @@ -40,10 +42,6 @@ class RemoteArchiveTest { void gradleWrapper(String version) throws Exception { URL distributionUrl = requireNonNull(RemoteArchiveTest.class.getClassLoader().getResource("gradle-" + version + "-bin.zip")); ExecutionContext ctx = new InMemoryExecutionContext(); - RemoteExecutionContextView.view(ctx).setArtifactCache(new LocalRemoteArtifactCache( - Paths.get(System.getProperty("user.home") + "/.rewrite/remote/gradleWrapper"))); - HttpSenderExecutionContextView.view(ctx) - .setLargeFileHttpSender(new MockHttpSender(distributionUrl::openStream)); RemoteArchive remoteArchive = Remote .builder( @@ -58,10 +56,9 @@ void gradleWrapper(String version) throws Exception { @Test void gradleWrapperDownloadFails() throws Exception { - URL distributionUrl = requireNonNull(RemoteArchiveTest.class.getClassLoader().getResource("gradle-7.4.2-bin.zip")); + URL distributionUrl = new URL("http://example.com"); ExecutionContext ctx = new InMemoryExecutionContext(); - RemoteExecutionContextView.view(ctx).setArtifactCache(new LocalRemoteArtifactCache( - Paths.get(System.getProperty("user.home") + "/.rewrite/remote/gradleWrapperDownloadFails"))); + HttpSenderExecutionContextView.view(ctx) .setLargeFileHttpSender(new MockHttpSender(408)); @@ -116,6 +113,21 @@ void gradleWrapperConcurrent(String version) throws Exception { executorService.shutdown(); } + @Test + void printingRemoteArchive() throws URISyntaxException { + URL zipUrl = requireNonNull(RemoteArchiveTest.class.getClassLoader().getResource("zipfile.zip")); + + RemoteArchive remoteArchive = Remote + .builder( + Paths.get("content.txt"), + zipUrl.toURI() + ) + .build("content.txt"); + + String printed = remoteArchive.printAll(new PrintOutputCapture<>(0, PrintOutputCapture.MarkerPrinter.DEFAULT)); + assertThat(printed).isEqualTo("this is a zipped file"); + } + private Long getInputStreamSize(InputStream is) { BlackHoleOutputStream out = new BlackHoleOutputStream(); try { diff --git a/rewrite-core/src/test/resources/zipfile.zip b/rewrite-core/src/test/resources/zipfile.zip new file mode 100644 index 0000000000000000000000000000000000000000..028830c420ec4023f6e482cbe9dbbf38a606f654 GIT binary patch literal 193 zcmWIWW@h1H0DNs?36d)D(rZ%$(E!Z$>6LW?W`V0JSkN01aSR(g