Skip to content

Commit

Permalink
Remove commons-io
Browse files Browse the repository at this point in the history
  • Loading branch information
onigoetz committed Feb 8, 2024
1 parent 907ee45 commit d02ffe2
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 27 deletions.
5 changes: 0 additions & 5 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,6 @@
<artifactId>maven-model</artifactId>
<version>${maven.version}</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.15.1</version>
</dependency>
<dependency>
<groupId>net.lingala.zip4j</groupId>
<artifactId>zip4j</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Path> walk = Files.walk(Paths.get(sourceDirectoryLocation))) {
for (Path source : (Iterable<Path>) 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 :<br>
* resourcesRoot/expanded-jars/module1/...<br>
Expand Down Expand Up @@ -87,8 +97,9 @@ public void extractResources(List<CarnotzetModule> 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());
}
}
}
Expand Down Expand Up @@ -138,7 +149,7 @@ private void copyOwnResources(List<CarnotzetModule> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,43 @@
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 {

@Rule
public TemporaryFolder temp = new TemporaryFolder();

private static void copyDirectory(String sourceDirectoryLocation, String destinationDirectoryLocation)
throws IOException {
try (Stream<Path> walk = Files.walk(Paths.get(sourceDirectoryLocation))) {
for (Path source : (Iterable<Path>) 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<CarnotzetModule> modules = Arrays.asList(
CarnotzetModule.builder().name("service3").serviceId("service3").build(),
Expand Down Expand Up @@ -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<CarnotzetModule> modules = Arrays.asList(
CarnotzetModule.builder().name("service3").serviceId("service3").build(),
Expand Down Expand Up @@ -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<CarnotzetModule> modules = Arrays.asList(
CarnotzetModule.builder().name("service2").serviceId("service2").build(),
Expand All @@ -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<CarnotzetModule> modules = Arrays.asList(
CarnotzetModule.builder().name("service2").serviceId("service2").build(),
Expand All @@ -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<CarnotzetModule> modules = Arrays.asList(
CarnotzetModule.builder().name("service1-variant2").serviceId("service1").build(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
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;
import static org.junit.Assert.assertTrue;

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;
Expand All @@ -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, ".");
}

Expand Down Expand Up @@ -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\"]"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<Path> walk = Files.walk(Paths.get(sourceDirectoryLocation))) {
for (Path source : (Iterable<Path>) 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<CarnotzetModule> modules = Arrays.asList(
CarnotzetModule.builder().name("service3").serviceId("service3").build(),
Expand Down

0 comments on commit d02ffe2

Please sign in to comment.