
org.apache.tinkerpop.gremlin.TestHelper Maven / Gradle / Ivy
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.tinkerpop.gremlin;
import org.apache.tinkerpop.gremlin.structure.Direction;
import org.apache.tinkerpop.gremlin.structure.Edge;
import org.apache.tinkerpop.gremlin.structure.Graph;
import org.apache.tinkerpop.gremlin.structure.Property;
import org.apache.tinkerpop.gremlin.structure.T;
import org.apache.tinkerpop.gremlin.structure.Vertex;
import org.apache.tinkerpop.gremlin.structure.VertexProperty;
import org.apache.tinkerpop.gremlin.structure.util.Comparators;
import org.apache.tinkerpop.gremlin.util.iterator.IteratorUtils;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
/**
* Utility methods for test development.
*
* @author Stephen Mallette (http://stephen.genoprime.com)
*/
public final class TestHelper {
private static final String SEP = File.separator;
private static final char URL_SEP = '/';
public static final String TEST_DATA_RELATIVE_DIR = "test-case-data";
private TestHelper() {
}
/**
* Creates a {@link File} reference that points to a directory relative to the supplied class in the
* {@code /target} directory. Each {@code childPath} passed introduces a new sub-directory and all are placed
* below the {@link #TEST_DATA_RELATIVE_DIR}. For example, calling this method with "a", "b", and "c" as the
* {@code childPath} arguments would yield a relative directory like: {@code test-case-data/clazz/a/b/c}. It is
* a good idea to use the test class for the {@code clazz} argument so that it's easy to find the data if
* necessary after test execution.
*/
public static File makeTestDataPath(final Class clazz, final String... childPath) {
final File root = getRootOfBuildDirectory(clazz);
final List cleanedPaths = Stream.of(childPath).map(TestHelper::cleanPathSegment).collect(Collectors.toList());
// use the class name in the directory structure
cleanedPaths.add(0, cleanPathSegment(clazz.getSimpleName()));
final File f = new File(root, TEST_DATA_RELATIVE_DIR + SEP + String.join(SEP, cleanedPaths));
if (!f.exists()) f.mkdirs();
return f;
}
public static String convertToRelative(final Class clazz, final File f) {
final File root = TestHelper.getRootOfBuildDirectory(clazz).getParentFile().getAbsoluteFile();
return root.toURI().relativize(f.getAbsoluteFile().toURI()).toString();
}
/**
* Internally calls {@link #makeTestDataPath(Class, String...)} but returns the path as a string with the system
* separator appended to the end.
*/
public static String makeTestDataDirectory(final Class clazz, final String... childPath) {
return makeTestDataPath(clazz, childPath).getAbsolutePath() + SEP;
}
/**
* Gets and/or creates the root of the test data directory. This method is here as a convenience and should not
* be used to store test data. Use {@link #makeTestDataPath(Class, String...)} instead.
*/
public static File getRootOfBuildDirectory(final Class clazz) {
// 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);
if (!root.exists()) root.mkdirs();
return root;
}
private static String computePath(final Class clazz) {
final String clsUri = clazz.getName().replace('.', URL_SEP) + ".class";
final URL url = clazz.getClassLoader().getResource(clsUri);
String clsPath;
try {
clsPath = new File(url.toURI()).getAbsolutePath();
} catch (URISyntaxException e) {
throw new RuntimeException("Unable to computePath for " + clazz, e);
}
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;
}
/**
* Takes a class and converts its package name to a path that can be used to access a resource in that package.
*/
public static String convertPackageToResourcePath(final Class clazz) {
final String packageName = clazz.getPackage().getName();
return String.format("/%s/", packageName.replaceAll("\\.", "\\/"));
}
/**
* 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;
}
///////////////
public static void validateVertexEquality(final Vertex originalVertex, final Vertex otherVertex, boolean testEdges) {
assertEquals(originalVertex, otherVertex);
assertEquals(otherVertex, originalVertex);
assertEquals(originalVertex.id(), otherVertex.id());
assertEquals(originalVertex.label(), otherVertex.label());
assertEquals(originalVertex.keys().size(), otherVertex.keys().size());
for (final String key : originalVertex.keys()) {
final List> originalVertexProperties = IteratorUtils.list(originalVertex.properties(key));
final List> otherVertexProperties = IteratorUtils.list(otherVertex.properties(key));
assertEquals(originalVertexProperties.size(), otherVertexProperties.size());
for (VertexProperty
© 2015 - 2025 Weber Informatics LLC | Privacy Policy