From 0800a08b7b77773900adfbec90dc5d91fb9a1823 Mon Sep 17 00:00:00 2001 From: Peter Streef Date: Mon, 25 Sep 2023 14:38:10 +0200 Subject: [PATCH] avoid creating an empty file if nothing will be written to it. (#3561) * avoid creating an empty file if nothing will be written to it. * add test * license --- .../maven/cache/LocalMavenArtifactCache.java | 21 ++--- .../cache/LocalMavenArtifactCacheTest.java | 90 +++++++++++++++++++ 2 files changed, 101 insertions(+), 10 deletions(-) create mode 100644 rewrite-maven/src/test/java/org/openrewrite/maven/cache/LocalMavenArtifactCacheTest.java diff --git a/rewrite-maven/src/main/java/org/openrewrite/maven/cache/LocalMavenArtifactCache.java b/rewrite-maven/src/main/java/org/openrewrite/maven/cache/LocalMavenArtifactCache.java index 74d36c65b65..f0ed18ccfd7 100644 --- a/rewrite-maven/src/main/java/org/openrewrite/maven/cache/LocalMavenArtifactCache.java +++ b/rewrite-maven/src/main/java/org/openrewrite/maven/cache/LocalMavenArtifactCache.java @@ -35,7 +35,7 @@ public class LocalMavenArtifactCache implements MavenArtifactCache { private final Path cache; public LocalMavenArtifactCache(Path cache) { - if(!cache.toFile().exists() && !cache.toFile().mkdirs()) { + if (!cache.toFile().exists() && !cache.toFile().mkdirs()) { throw new IllegalStateException("Unable to find or create maven artifact cache at " + cache); } this.cache = cache; @@ -51,15 +51,16 @@ public Path getArtifact(ResolvedDependency dependency) { @Override @Nullable public Path putArtifact(ResolvedDependency dependency, InputStream artifactInputStream, Consumer onError) { + if (artifactInputStream == null) { + return null; + } Path path = dependencyPath(dependency); try (InputStream is = artifactInputStream; OutputStream out = Files.newOutputStream(path)) { - if (is != null) { - byte[] buffer = new byte[1024]; - int read; - while ((read = is.read(buffer, 0, 1024)) >= 0) { - out.write(buffer, 0, read); - } + byte[] buffer = new byte[1024]; + int read; + while ((read = is.read(buffer, 0, 1024)) >= 0) { + out.write(buffer, 0, read); } } catch (Throwable t) { onError.accept(t); @@ -85,8 +86,8 @@ private Path dependencyPath(ResolvedDependency dependency) { } return resolvedPath.resolve(dependency.getArtifactId() + "-" + - (dependency.getDatedSnapshotVersion() == null ? dependency.getVersion() : dependency.getDatedSnapshotVersion()) + - (dependency.getRequested().getClassifier() == null ? "" : dependency.getRequested().getClassifier()) + - ".jar"); + (dependency.getDatedSnapshotVersion() == null ? dependency.getVersion() : dependency.getDatedSnapshotVersion()) + + (dependency.getRequested().getClassifier() == null ? "" : dependency.getRequested().getClassifier()) + + ".jar"); } } diff --git a/rewrite-maven/src/test/java/org/openrewrite/maven/cache/LocalMavenArtifactCacheTest.java b/rewrite-maven/src/test/java/org/openrewrite/maven/cache/LocalMavenArtifactCacheTest.java new file mode 100644 index 00000000000..4301b243c95 --- /dev/null +++ b/rewrite-maven/src/test/java/org/openrewrite/maven/cache/LocalMavenArtifactCacheTest.java @@ -0,0 +1,90 @@ +/* + * Copyright 2020 the original author or authors. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * https://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.openrewrite.maven.cache; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; +import org.openrewrite.ExecutionContext; +import org.openrewrite.InMemoryExecutionContext; +import org.openrewrite.SourceFile; +import org.openrewrite.maven.MavenParser; +import org.openrewrite.maven.tree.*; + +import java.io.ByteArrayInputStream; +import java.nio.file.Path; +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; + +class LocalMavenArtifactCacheTest { + + LocalMavenArtifactCache cache; + + @BeforeEach + void setup() { + + } + + @Test + void saveFileWhenInputStringExists(@TempDir Path tempDir) { + cache = new LocalMavenArtifactCache(tempDir); + Path output = cache.putArtifact(findDependency(), new ByteArrayInputStream("hi".getBytes()), Throwable::printStackTrace); + assertThat(output).exists(); + } + + @Test + void dontCreateFileWhenInputStringNull(@TempDir Path tempDir) { + cache = new LocalMavenArtifactCache(tempDir); + Path output = cache.putArtifact(findDependency(), null, Throwable::printStackTrace); + assertThat(output).isNull(); + assertThat(tempDir).isEmptyDirectory(); + } + + private static ResolvedDependency findDependency() { + ExecutionContext ctx = new InMemoryExecutionContext(Throwable::printStackTrace); + ResolvedGroupArtifactVersion recipeGav = new ResolvedGroupArtifactVersion( + "https://repo1.maven.org/maven2", + "org.openrewrite.recipe", + "rewrite-testing-frameworks", + "1.6.0", null); + + MavenParser mavenParser = MavenParser.builder().build(); + SourceFile parsed = mavenParser.parse(ctx, + String.format( + //language=xml + """ + + org.openrewrite + maven-downloader-test + 1 + + + %s + %s + %s + + + + """.formatted(recipeGav.getGroupId(), recipeGav.getArtifactId(), recipeGav.getVersion())) + ).findFirst().orElseThrow(() -> new IllegalArgumentException("Could not parse as XML")); + + MavenResolutionResult mavenModel = parsed.getMarkers().findFirst(MavenResolutionResult.class).orElseThrow(); + assertThat(mavenModel.getDependencies()).isNotEmpty(); + List runtimeDependencies = mavenModel.getDependencies().get(Scope.Runtime); + return runtimeDependencies.get(0); + } +} \ No newline at end of file