Skip to content

Commit

Permalink
Refactor copyResourceToTempFile method
Browse files Browse the repository at this point in the history
  • Loading branch information
ml-james committed Oct 24, 2024
1 parent 180041d commit 51a3716
Showing 1 changed file with 12 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@
import org.testcontainers.images.builder.ImageFromDockerfile;

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.time.Duration;
import java.time.temporal.ChronoUnit;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

public abstract class IntegrationTestBase
{
Expand Down Expand Up @@ -77,13 +75,19 @@ public abstract class IntegrationTestBase

private static Path copyResourceToTempFile(final Path parent, final String name) throws IOException
{
final List<String> subDirectoryFullRoute = Arrays.asList(name.split("/"));
final String subDirectoryRouteMinusFileName = String.join("/", subDirectoryFullRoute.subList(0, subDirectoryFullRoute.size() - 1));
final Path fullSubDirectoryPath = parent.resolve(name);

Files.createDirectories(parent.resolve(subDirectoryRouteMinusFileName));
Files.createDirectories(fullSubDirectoryPath.getParent());
final Path tempFile = Files.createFile(fullSubDirectoryPath);

final Path tempFile = Files.createFile(parent.resolve(name));
Files.copy(Objects.requireNonNull(IntegrationTestBase.class.getResourceAsStream("/testcontainers/" + name)), tempFile, StandardCopyOption.REPLACE_EXISTING);
try (InputStream resourceStream = IntegrationTestBase.class.getResourceAsStream("/testcontainers/" + name))
{
if (resourceStream == null)
{
throw new IOException("Resource not found: /testcontainers/" + name);
}
Files.copy(resourceStream, tempFile, StandardCopyOption.REPLACE_EXISTING);
}
return tempFile;
}

Expand Down

0 comments on commit 51a3716

Please sign in to comment.