Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Combine db tests #242

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,12 @@ public void addEnv(String key, String value) {
*/
@Override
public void addFileSystemBind(String hostPath, String containerPath, BindMode mode) {

if (hostPath.contains(".jar!")) {
// the host file is inside a JAR resource - copy to a temporary location that Docker can read
hostPath = PathUtils.extractClassPathResourceToTempLocation(hostPath);
}

if (SystemUtils.IS_OS_WINDOWS) {
hostPath = PathUtils.createMinGWPath(hostPath);
}
Expand Down
106 changes: 102 additions & 4 deletions core/src/main/java/org/testcontainers/utility/PathUtils.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
package org.testcontainers.utility;

import lombok.NonNull;
import org.slf4j.Logger;

import java.io.File;
import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.io.InputStream;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

import static org.slf4j.LoggerFactory.getLogger;

/**
* Filesystem operation utility methods.
*/
public class PathUtils {

private static final Logger log = getLogger(PathUtils.class);

/**
* Recursively delete a directory and all its subdirectories and files.
* @param directory path to the directory to delete.
Expand Down Expand Up @@ -67,4 +74,95 @@ public static String createMinGWPath(String path) {
mingwPath = mingwPath.replace(":","");
return mingwPath;
}

/**
* Extract a file or directory tree from a JAR file to a temporary location.
* This allows Docker to mount classpath resources as files.
* @param hostPath the path on the host, expected to be of the format 'file:/path/to/some.jar!/classpath/path/to/resource'
* @return the path of the temporary file/directory
*/
public static String extractClassPathResourceToTempLocation(final String hostPath) {
File tmpLocation = new File(".testcontainers-tmp-" + Base58.randomString(5));
//noinspection ResultOfMethodCallIgnored
tmpLocation.delete();

String jarPath = hostPath.replaceFirst("file:", "").replaceAll("!.*", "");
String internalPath = hostPath.replaceAll("[^!]*!/", "");

try (JarFile jarFile = new JarFile(jarPath)) {
Enumeration<JarEntry> entries = jarFile.entries();

while (entries.hasMoreElements()) {
JarEntry entry = entries.nextElement();
final String name = entry.getName();
if (name.startsWith(internalPath)) {
log.debug("Copying classpath resource(s) from {} to {} to permit Docker to bind",
hostPath,
tmpLocation);
copyFromJarToLocation(jarFile, entry, internalPath, tmpLocation);
}
}

} catch (IOException e) {
throw new RuntimeException("Failed to process JAR file when extracting classpath resource: " + hostPath, e);
}

// Mark temporary files/dirs for deletion at JVM shutdown
deleteOnExit(tmpLocation.toPath());

return tmpLocation.getAbsolutePath();
}

@SuppressWarnings("ResultOfMethodCallIgnored")
private static void copyFromJarToLocation(final JarFile jarFile,
final JarEntry entry,
final String fromRoot,
final File toRoot) {

String destinationName = entry.getName().replaceFirst(fromRoot, "");
File newFile = new File(toRoot, destinationName);

if (!entry.isDirectory()) {
// Create parent directories
newFile.mkdirs();
newFile.delete();
newFile.deleteOnExit();

try (InputStream is = jarFile.getInputStream(entry)) {
Files.copy(is, newFile.toPath());
} catch (IOException e) {
throw new RuntimeException("Failed to extract classpath resource " + entry.getName() + " from JAR file " + jarFile.getName(), e);
}
}
}

public static void deleteOnExit(final Path path) {
Runtime.getRuntime().addShutdownHook(new Thread(() -> deleteRecursively(path)));
}


public static void deleteRecursively(final Path path) {
if (! Files.exists(path)) {
return;
}

try {
Files.walkFileTree(path, new SimpleFileVisitor<Path>() {

@Override
public FileVisitResult postVisitDirectory(final Path dir, final IOException exc) throws IOException {
Files.deleteIfExists(dir);
return super.postVisitDirectory(dir, exc);
}

@Override
public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) throws IOException {
Files.deleteIfExists(file);
return super.visitFile(file, attrs);
}

});
} catch (IOException ignored) {
}
}
}
124 changes: 124 additions & 0 deletions modules/jdbc-test/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers-parent</artifactId>
<version>1.1.7-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
</parent>

<artifactId>jdbc-test</artifactId>
<name>TestContainers :: JDBC :: Tests</name>

<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>mysql</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>postgresql</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>mariadb</artifactId>
<version>${project.version}</version>
</dependency>

<!-- Database drivers for testing -->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.3-1101-jdbc41</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.35</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mariadb.jdbc</groupId>
<artifactId>mariadb-java-client</artifactId>
<version>1.4.6</version>
<scope>test</scope>
</dependency>

<!-- Database connection pools for testing -->
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP-java6</artifactId>
<version>2.3.8</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jdbc</artifactId>
<version>8.5.4</version>
</dependency>
<dependency>
<groupId>org.vibur</groupId>
<artifactId>vibur-dbcp</artifactId>
<version>9.0</version>
</dependency>

<dependency>
<groupId>commons-dbutils</groupId>
<artifactId>commons-dbutils</artifactId>
<version>1.6</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.googlecode.junit-toolbox</groupId>
<artifactId>junit-toolbox</artifactId>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it used?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will remove

<version>2.2</version>
<scope>test</scope>
</dependency>
</dependencies>

<profiles>
<profile>
<id>proprietary-deps</id>
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>oracle-xe</artifactId>
<version>${project.version}</version>
</dependency>

<!-- Oracle JDBC Driver -->
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.4</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<executions>
<execution>
<id>test-proprietary-deps</id>
<goals>
<goal>test</goal>
</goals>
<configuration>
<testSourceDirectory>src/testProprietary/java</testSourceDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
Loading