From d02ffe22b27dc453f832d8db7507f8fddc400638 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Goetz?= Date: Thu, 8 Feb 2024 07:23:47 +0100 Subject: [PATCH] Remove commons-io --- core/pom.xml | 5 ---- .../core/maven/ResourcesManager.java | 21 +++++++++++---- .../core/maven/ResourcesManagerTest.java | 27 +++++++++++++------ .../ContainerStartupWrapperUtilsTest.java | 10 ++++--- .../merger/json/ResourcesManagerTest.java | 20 ++++++++++---- 5 files changed, 56 insertions(+), 27 deletions(-) diff --git a/core/pom.xml b/core/pom.xml index 70f95ade..6090dd4f 100644 --- a/core/pom.xml +++ b/core/pom.xml @@ -27,11 +27,6 @@ maven-model ${maven.version} - - commons-io - commons-io - 2.15.1 - net.lingala.zip4j zip4j diff --git a/core/src/main/java/com/github/swissquote/carnotzet/core/maven/ResourcesManager.java b/core/src/main/java/com/github/swissquote/carnotzet/core/maven/ResourcesManager.java index 856739ee..b9d889d5 100644 --- a/core/src/main/java/com/github/swissquote/carnotzet/core/maven/ResourcesManager.java +++ b/core/src/main/java/com/github/swissquote/carnotzet/core/maven/ResourcesManager.java @@ -16,8 +16,7 @@ import java.util.List; import java.util.ServiceLoader; import java.util.function.BiPredicate; - -import org.apache.commons.io.FileUtils; +import java.util.stream.Stream; import com.github.swissquote.carnotzet.core.CarnotzetDefinitionException; import com.github.swissquote.carnotzet.core.CarnotzetModule; @@ -58,6 +57,17 @@ public Path getOwnModuleResourcesPath(CarnotzetModule module) { return expandedJars.resolve(module.getName()); } + private static void copyDirectory(String sourceDirectoryLocation, String destinationDirectoryLocation) + throws IOException { + try (Stream walk = Files.walk(Paths.get(sourceDirectoryLocation))) { + for (Path source : (Iterable) walk::iterator) { + Path destination = Paths.get(destinationDirectoryLocation, source.toString() + .substring(sourceDirectoryLocation.length())); + Files.copy(source, destination, StandardCopyOption.REPLACE_EXISTING); + } + } + } + /** * Extract all jar resources to a single directory with the following structure :
* resourcesRoot/expanded-jars/module1/...
@@ -87,8 +97,9 @@ public void extractResources(List modules) { if (module.getName().equals(topLevelModuleName) && topLevelModuleResourcesPath != null && topLevelModuleResourcesPath.toFile().exists()) { - FileUtils.copyDirectory(topLevelModuleResourcesPath.toFile(), - expandedJars.resolve(topLevelModuleName).toFile()); + + copyDirectory(topLevelModuleResourcesPath.toString(), + expandedJars.resolve(topLevelModuleName).toString()); } } } @@ -138,7 +149,7 @@ private void copyOwnResources(List processedModules, CarnotzetM if (Files.isRegularFile(source)) { Files.copy(source, resolvedModulePath.resolve(source.getFileName()), StandardCopyOption.REPLACE_EXISTING); } else if (Files.isDirectory(source)) { - FileUtils.copyDirectory(source.toFile(), resolvedModulePath.resolve(source.getFileName()).toFile()); + copyDirectory(source.toString(), resolvedModulePath.resolve(source.getFileName()).toString()); } } catch (IOException e) { diff --git a/core/src/test/java/com/github/swissquote/carnotzet/core/maven/ResourcesManagerTest.java b/core/src/test/java/com/github/swissquote/carnotzet/core/maven/ResourcesManagerTest.java index e579f683..3cf12def 100644 --- a/core/src/test/java/com/github/swissquote/carnotzet/core/maven/ResourcesManagerTest.java +++ b/core/src/test/java/com/github/swissquote/carnotzet/core/maven/ResourcesManagerTest.java @@ -9,18 +9,18 @@ import java.net.URL; import java.nio.file.Files; import java.nio.file.Path; +import java.nio.file.Paths; +import java.nio.file.StandardCopyOption; import java.util.Arrays; import java.util.List; import java.util.Properties; +import java.util.stream.Stream; -import org.apache.commons.io.FileUtils; import org.junit.Assert; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; import com.github.swissquote.carnotzet.core.CarnotzetModule; public class ResourcesManagerTest { @@ -28,13 +28,24 @@ public class ResourcesManagerTest { @Rule public TemporaryFolder temp = new TemporaryFolder(); + private static void copyDirectory(String sourceDirectoryLocation, String destinationDirectoryLocation) + throws IOException { + try (Stream walk = Files.walk(Paths.get(sourceDirectoryLocation))) { + for (Path source : (Iterable) walk::iterator) { + Path destination = Paths.get(destinationDirectoryLocation, source.toString() + .substring(sourceDirectoryLocation.length())); + Files.copy(source, destination, StandardCopyOption.REPLACE_EXISTING); + } + } + } + @Test public void override_file() throws IOException { // Given URL url = Thread.currentThread().getContextClassLoader().getResource("example_override"); File example = new File(url.getPath()); Path resources = temp.newFolder().toPath(); - FileUtils.copyDirectory(example, resources.toFile()); + copyDirectory(example.toString(), resources.toString()); ResourcesManager manager = new ResourcesManager(resources, null); List modules = Arrays.asList( CarnotzetModule.builder().name("service3").serviceId("service3").build(), @@ -67,7 +78,7 @@ public void merge_files() throws IOException { URL url = Thread.currentThread().getContextClassLoader().getResource("example_merge"); File example = new File(url.getPath()); Path resources = temp.newFolder().toPath(); - FileUtils.copyDirectory(example, resources.toFile()); + copyDirectory(example.toString(), resources.toString()); ResourcesManager manager = new ResourcesManager(resources, null); List modules = Arrays.asList( CarnotzetModule.builder().name("service3").serviceId("service3").build(), @@ -105,7 +116,7 @@ public void copy_own_resources() throws IOException { URL url = Thread.currentThread().getContextClassLoader().getResource("example_copy_own_resources"); File example = new File(url.getPath()); Path resources = temp.newFolder().toPath(); - FileUtils.copyDirectory(example, resources.toFile()); + copyDirectory(example.toString(), resources.toString()); ResourcesManager manager = new ResourcesManager(resources, null); List modules = Arrays.asList( CarnotzetModule.builder().name("service2").serviceId("service2").build(), @@ -129,7 +140,7 @@ public void config_variant() throws IOException { URL url = Thread.currentThread().getContextClassLoader().getResource("example_config_variant"); File example = new File(url.getPath()); Path resources = temp.newFolder().toPath(); - FileUtils.copyDirectory(example, resources.toFile()); + copyDirectory(example.toString(), resources.toString()); ResourcesManager manager = new ResourcesManager(resources, null); List modules = Arrays.asList( CarnotzetModule.builder().name("service2").serviceId("service2").build(), @@ -153,7 +164,7 @@ public void multiple_variants_for_same_service_id() throws IOException { URL url = Thread.currentThread().getContextClassLoader().getResource("example_multiple_variants_for_same_service_id"); File example = new File(url.getPath()); Path resources = temp.newFolder().toPath(); - FileUtils.copyDirectory(example, resources.toFile()); + copyDirectory(example.toString(), resources.toString()); ResourcesManager manager = new ResourcesManager(resources, null); List modules = Arrays.asList( CarnotzetModule.builder().name("service1-variant2").serviceId("service1").build(), diff --git a/core/src/test/java/com/github/swissquote/carnotzet/core/util/ContainerStartupWrapperUtilsTest.java b/core/src/test/java/com/github/swissquote/carnotzet/core/util/ContainerStartupWrapperUtilsTest.java index 10768cdf..e3d216ab 100644 --- a/core/src/test/java/com/github/swissquote/carnotzet/core/util/ContainerStartupWrapperUtilsTest.java +++ b/core/src/test/java/com/github/swissquote/carnotzet/core/util/ContainerStartupWrapperUtilsTest.java @@ -1,7 +1,6 @@ package com.github.swissquote.carnotzet.core.util; import static com.github.swissquote.carnotzet.core.runtime.DefaultCommandRunner.INSTANCE; -import static org.apache.commons.io.FileUtils.writeStringToFile; import static org.hamcrest.core.Is.is; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertThat; @@ -9,10 +8,12 @@ import java.io.File; import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; import java.nio.file.Paths; import java.util.HashSet; -import org.apache.commons.io.FileUtils; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; @@ -37,7 +38,7 @@ private void createDockerImage(String tag, String entrypoint, String cmd) throws sb.append(cmd); sb.append("\n"); } - writeStringToFile(temp.newFile("Dockerfile"), sb.toString()); + Files.write(temp.newFile("Dockerfile").toPath(), sb.toString().getBytes(StandardCharsets.UTF_8)); INSTANCE.runCommand(temp.getRoot(), "docker", "build", "-t", tag, "."); } @@ -69,7 +70,8 @@ public void exec_entrypoint_no_cmd() throws IOException { CarnotzetModule wrapped = getWrappedModule(imageName); - String wrapperContent = FileUtils.readFileToString(new File("/tmp/startup-wrappers/test-wrapper_my-service.sh")); + Path wrapperPath = new File("/tmp/startup-wrappers/test-wrapper_my-service.sh").toPath(); + String wrapperContent =new String(Files.readAllBytes(wrapperPath), StandardCharsets.UTF_8); assertThat(wrapperContent, is("#!/bin/sh\necho hi!\nexec \"$@\"\n")); assertTrue(wrapped.getDockerVolumes().contains("/tmp/startup-wrappers/test-wrapper_my-service.sh:/test-wrapper_my-service.sh")); assertThat(wrapped.getDockerEntrypoint(), is("[\"/test-wrapper_my-service.sh\",\"/container_entrypoint\"]")); diff --git a/file-merger-json/src/test/java/com/github/swissquote/carnotzet/file/merger/json/ResourcesManagerTest.java b/file-merger-json/src/test/java/com/github/swissquote/carnotzet/file/merger/json/ResourcesManagerTest.java index 75e04196..882ec2b8 100644 --- a/file-merger-json/src/test/java/com/github/swissquote/carnotzet/file/merger/json/ResourcesManagerTest.java +++ b/file-merger-json/src/test/java/com/github/swissquote/carnotzet/file/merger/json/ResourcesManagerTest.java @@ -2,19 +2,18 @@ import static org.hamcrest.core.Is.is; import static org.junit.Assert.assertThat; -import static org.junit.Assert.assertTrue; import java.io.File; import java.io.IOException; import java.net.URL; import java.nio.file.Files; import java.nio.file.Path; +import java.nio.file.Paths; +import java.nio.file.StandardCopyOption; import java.util.Arrays; import java.util.List; -import java.util.Properties; +import java.util.stream.Stream; -import org.apache.commons.io.FileUtils; -import org.junit.Assert; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; @@ -29,13 +28,24 @@ public class ResourcesManagerTest { @Rule public TemporaryFolder temp = new TemporaryFolder(); + private static void copyDirectory(String sourceDirectoryLocation, String destinationDirectoryLocation) + throws IOException { + try (Stream walk = Files.walk(Paths.get(sourceDirectoryLocation))) { + for (Path source : (Iterable) walk::iterator) { + Path destination = Paths.get(destinationDirectoryLocation, source.toString() + .substring(sourceDirectoryLocation.length())); + Files.copy(source, destination, StandardCopyOption.REPLACE_EXISTING); + } + } + } + @Test public void merge_files() throws IOException { // Given URL url = Thread.currentThread().getContextClassLoader().getResource("example_merge"); File example = new File(url.getPath()); Path resources = temp.newFolder().toPath(); - FileUtils.copyDirectory(example, resources.toFile()); + copyDirectory(example.toString(), resources.toString()); ResourcesManager manager = new ResourcesManager(resources, null); List modules = Arrays.asList( CarnotzetModule.builder().name("service3").serviceId("service3").build(),