
com.tinkerpop.gremlin.TestHelper Maven / Gradle / Ivy
The newest version!
package com.tinkerpop.gremlin;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
/**
* @author Stephen Mallette (http://stephen.genoprime.com)
*/
public final class TestHelper {
/**
* Creates a {@link File} reference that points to a directory relative to the supplied class in the
* {@code /target} directory.
*/
public static File makeTestDataPath(final Class clazz, final String childPath) {
// build.dir gets sets during runs of tests with maven via the surefire configuration in the pom.xml
// if that is not set as an environment variable, then the path is computed based on the location of the
// requested class. the computed version at least as far as intellij is concerned comes drops it into
// /target/test-classes. the build.dir had to be added because maven doesn't seem to like a computed path
// as it likes to find that path in the .m2 directory and other weird places......
final String buildDirectory = System.getProperty("build.dir");
final File root = null == buildDirectory ? new File(computePath(clazz)).getParentFile() : new File(buildDirectory);
return new File(root, cleanPathSegment(childPath));
}
private static String computePath(final Class clazz) {
final String clsUri = clazz.getName().replace('.', '/') + ".class";
final URL url = clazz.getClassLoader().getResource(clsUri);
final String clsPath = url.getPath();
return clsPath.substring(0, clsPath.length() - clsUri.length());
}
/**
* Creates a {@link File} reference in the path returned from {@link TestHelper#makeTestDataPath} in a subdirectory
* called {@code temp}.
*/
public static File generateTempFile(final Class clazz, final String fileName, final String fileNameSuffix) throws IOException {
final File path = makeTestDataPath(clazz, "temp");
if (!path.exists()) path.mkdirs();
return File.createTempFile(fileName, fileNameSuffix, path);
}
/**
* Copies a file stored as part of a resource to the file system in the path returned from
* {@link TestHelper#makeTestDataPath} in a subdirectory called {@code temp/resources}.
*/
public static File generateTempFileFromResource(final Class resourceClass, final String resourceName, final String extension) throws IOException {
final File temp = makeTestDataPath(resourceClass, "resources");
if (!temp.exists()) temp.mkdirs();
final File tempFile = new File(temp, resourceName + extension);
final FileOutputStream outputStream = new FileOutputStream(tempFile);
int data;
final InputStream inputStream = resourceClass.getResourceAsStream(resourceName);
while ((data = inputStream.read()) != -1) {
outputStream.write(data);
}
outputStream.close();
inputStream.close();
return tempFile;
}
/**
* Removes characters that aren't acceptable in a file path (mostly for windows).
*/
public static String cleanPathSegment(final String toClean) {
final String cleaned = toClean.replaceAll("[.\\\\/:*?\"<>|\\[\\]\\(\\)]", "");
if (cleaned.length() == 0)
throw new IllegalStateException("Path segment " + toClean + " has not valid characters and is thus empty");
return cleaned;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy