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..e09fc8454f5 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"); 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/java/org/openrewrite/remote/RemoteFileTest.java b/rewrite-core/src/test/java/org/openrewrite/remote/RemoteFileTest.java index 6e335a1eee3..de1b23476c8 100644 --- a/rewrite-core/src/test/java/org/openrewrite/remote/RemoteFileTest.java +++ b/rewrite-core/src/test/java/org/openrewrite/remote/RemoteFileTest.java @@ -55,10 +55,8 @@ void gradleWrapperProperties() throws Exception { @Test void gradleWrapperDownloadFails() throws Exception { - URL distributionUrl = requireNonNull(RemoteFileTest.class.getClassLoader().getResource("gradle-wrapper.properties")); + 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)); diff --git a/rewrite-core/src/test/resources/zipfile.zip b/rewrite-core/src/test/resources/zipfile.zip new file mode 100644 index 00000000000..028830c420e Binary files /dev/null and b/rewrite-core/src/test/resources/zipfile.zip differ