org.eclipse.jetty.toolchain.test.MavenTestingUtils Maven / Gradle / Ivy
Show all versions of jetty-test-helper Show documentation
//
// ========================================================================
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//
package org.eclipse.jetty.toolchain.test;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.nio.file.FileSystem;
import java.nio.file.Path;
import org.junit.jupiter.api.TestInfo;
import static org.eclipse.jetty.toolchain.test.PathMatchers.exists;
import static org.eclipse.jetty.toolchain.test.PathMatchers.isDirectory;
import static org.eclipse.jetty.toolchain.test.PathMatchers.isRegularFile;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.fail;
/**
* Common utility methods for working with JUnit tests cases in a maven friendly way.
*/
public final class MavenTestingUtils
{
private static Path basePath;
private static Path testResourcesPath;
private static Path targetPath;
private MavenTestingUtils()
{
/* prevent instantiation */
}
/**
* Obtain a {@link File} reference to the maven ${basedir} for the module.
*
* Convenience method for MavenTestingUtils.getBasePath().toFile()
*
* @return the equivalent to the maven ${basedir} property.
* @see #getBasePath()
* @deprecated use {@link #getBasePath()} instead
*/
@Deprecated(forRemoval = true, since = "6.0")
public static File getBaseDir()
{
return getBasePath().toFile();
}
/**
* Obtain a {@link Path} reference to the maven ${basedir} for the module.
*
* Note: while running in maven, the ${basedir} is populated by maven and used by the surefire-plugin.
* While running in eclipse, the ${basedir} property is unset, resulting in this method falling back to ${user.dir}
* equivalent use.
*
* @return the equivalent to the maven ${basedir} property.
*/
public static Path getBasePath()
{
if (basePath == null)
{
String cwd = System.getProperty("basedir");
if (cwd == null)
{
cwd = System.getProperty("user.dir");
}
try
{
basePath = new File(cwd).toPath().toRealPath();
}
catch (IOException e)
{
// if toRealPath() fails, fallback to as detected version.
basePath = new File(cwd).getAbsoluteFile().toPath();
}
}
return basePath;
}
/**
* Get the Basedir for the project as a URI
*
* @return the URI for the project basedir
*/
public static URI getBaseURI()
{
return getBasePath().toUri();
}
/**
* Get the {@link File} reference to the /target
directory for this project.
*
* Convenience method for MavenTestingUtils.getTargetPath().toFile()
*
* @return the directory path to the target directory.
* @see #getTargetPath()
* @deprecated use {@link #getTargetPath()} instead
*/
@Deprecated(forRemoval = true, since = "6.0")
public static File getTargetDir()
{
return getTargetPath().toFile();
}
/**
* Get the {@link Path} reference to the /target
directory for this project.
*
* This is roughly equivalent to the ${project.build.directory}
property.
*
* Note: this implementation does not inspect the pom.xml
for non-standard locations
* of the ${project.build.directory}
property. (it always assumes /target
)
*
* @return the directory path to the /target
directory.
*/
public static Path getTargetPath()
{
if (targetPath == null)
{
targetPath = getBasePath().resolve("target");
assertThat("Target Dir", targetPath, isDirectory());
}
return targetPath;
}
/**
* Create a {@link File} object for a path in the /target directory.
*
* Convenience method for MavenTestingUtils.getTargetPath("foo").toFile()
*
* @param path the path desired, no validation of existence is performed.
* @return the File to the path.
* @see #getTargetPath(String)
* @deprecated use {@link #getTargetPath(String)} instead
*/
@Deprecated(forRemoval = true, since = "6.0")
public static File getTargetFile(String path)
{
return getTargetPath(path).toFile();
}
/**
* Create a {@link Path} object for a path in the /target directory.
*
* @param path the path desired, no validation of existence is performed.
* @return the File to the path.
*/
public static Path getTargetPath(String path)
{
Path targetPath = getTargetPath();
FileSystem fs = targetPath.getFileSystem();
return fs.getPath(targetPath.toString(), path);
}
/**
* Get a {@link File} reference to the maven ${basedir}/target/tests/
directory.
*
* Convenience method for MavenTestingUtils.getTargetTestingPath().toFile()
*
* @return the maven ${basedir}/target/tests/
directory.
* Note: will not validate that the directory exists, or create the directory)
* @deprecated use {@link #getTargetTestingPath()} instead
*/
@Deprecated(forRemoval = true, since = "6.0")
public static File getTargetTestingDir()
{
return getTargetTestingPath().toFile();
}
/**
* Get a {@link Path} reference to the maven ${basedir}/target/tests/
path.
*
* @return the maven ${basedir}/target/tests/
directory.
* Note: will not validate that the directory exists, or create the directory)
*/
public static Path getTargetTestingPath()
{
return getTargetPath().resolve("tests");
}
/**
* Get a {@link File} reference to the maven ${basedir}/target/tests/test-${testname}
using
* the supplied testname
*
* Convenience method for MavenTestingUtils.getTargetTestingPath(testname).toFile()
*
* @param testname the testname to create directory against.
* @return the maven ${basedir}/target/tests/test-${testname}
directory
* @deprecated use {@link #getTargetTestingPath(String)} instead
*/
@Deprecated(forRemoval = true, since = "6.0")
public static File getTargetTestingDir(String testname)
{
return getTargetTestingPath(testname).toFile();
}
/**
* Get a {@link Path} reference to the maven ${basedir}/target/tests/test-${testname}
using
* the supplied testname
*
* @param testname the testname to create directory against.
* @return the maven ${basedir}/target/tests/test-${testname}
directory
*/
public static Path getTargetTestingPath(String testname)
{
return getTargetTestingPath().resolve("test-" + testname);
}
/**
* Get a {@link File} reference to the ${basedir}/target/tests/test-${testname}
directory.
* Uses the JUnit 5.x {@link TestInfo#getDisplayName()} to make a unique directory name per test.
*
* Convenience method for MavenTestingUtils.getTargetTestingPath(TestCase.getName()).toFile()
*
* @param testInfo the junit 5.x testcase to base this new directory on.
* @return the maven ${basedir}/target/tests/test-${testname}
directory.
* @deprecated use {@link #getTargetTestingPath(TestInfo)} instead
*/
@Deprecated(forRemoval = true, since = "6.0")
public static File getTargetTestingDir(TestInfo testInfo)
{
return getTargetTestingPath(testInfo.getDisplayName()).toFile();
}
/**
* Get a {@link Path} reference to the ${basedir}/target/tests/test-${testname}
directory.
* Uses the JUnit 5.x {@link TestInfo#getDisplayName()} to make a unique directory name per test.
*
* Convenience method for MavenTestingUtils.getTargetTestingPath(TestCase.getName())
*
* @param testInfo the junit 5.x testcase to base this new directory on.
* @return the maven ${basedir}/target/tests/test-${testname}
directory.
* @see #getTargetTestingPath(String)
*/
public static Path getTargetTestingPath(TestInfo testInfo)
{
return getTargetTestingPath(testInfo.getDisplayName());
}
/**
* Get a URI reference to a path (File or Dir) within the maven "${basedir}/target" directory.
*
* Convenience method for MavenTestingUtils.getTargetPath(path).toUri()
*
* @param path the relative path to use
* @return the URI reference to the target path
*/
public static URI getTargetURI(String path)
{
return getTargetPath(path).toUri();
}
/**
* Get a URL reference to a path (File or Dir) within the maven "${basedir}/target" directory.
*
* Convenience method for MavenTestingUtils.getTargetURI(path).toURL()
*
* @param path the relative path to use
* @return the URL reference to the target path
* @throws MalformedURLException if unable to create a new target url due to URL error.
*/
public static URL getTargetURL(String path) throws MalformedURLException
{
return getTargetURI(path).toURL();
}
/**
* Obtain a testing directory reference in maven
* ${basedir}/target/tests/${condensed-classname}/${methodname}
path that uses an condensed directory
* name based on the testclass and subdirectory based on the testmethod being run.
*
* @param testclass the class for the test case
* @param testmethodname the test method name
* @return the File path to the testname specific testing directory underneath the
* ${basedir}/target/tests/
sub directory
* @see FS
*/
public static Path getTargetTestingPath(final Class> testclass, final String testmethodname)
{
String methodname = testmethodname;
if (org.junit.jupiter.api.condition.OS.WINDOWS.isCurrentOs())
{
/* Condense the directory names to make them more friendly for the
* 255 character pathname limitations that exist on windows.
*/
methodname = StringMangler.maxStringLength(30, methodname);
}
Path testdir = getTargetTestingPath().resolve(methodname);
FS.ensureDirExists(testdir);
return testdir;
}
private static class TestID
{
public String classname;
public String methodname;
}
/**
* Get a {@link File} reference to a required file in the project module path, based on relative
* path references from maven ${basedir}.
*
* Note: will throw assertion error if path does point to an existing file
*
* Convenience method for MavenTestingUtils.getProjectFilePath(path).toFile()
*
* @param path the relative path to reference
* @return the file reference (must exist)
* @deprecated use {@link #getProjectFilePath(String)} instead
*/
@Deprecated(forRemoval = true, since = "6.0")
public static File getProjectFile(String path)
{
return getProjectFilePath(path).toFile();
}
/**
* Get a {@link Path} reference to a required file in the project module path, based on relative
* path references from maven ${basedir}.
*
* Note: will throw assertion error if path does point to an existing file
*
* @param path the relative path to reference
* @return the file reference (must exist)
*/
public static Path getProjectFilePath(String path)
{
Path file = getBasePath().resolve(path);
assertThat("Project File", file, isRegularFile());
return file;
}
/**
* Get a directory reference to a required directory in the project module path, based on relative
* path references from maven ${basedir}.
*
* Note: will throw assertion error if path does point to an existing directory
*
* Convenience method for MavenTestingUtils.getProjectDirPath(path).toFile()
*
* @param path the relative path to reference
* @return the directory reference (must exist)
* @deprecated use {@link #getProjectDirPath(String)} instead
*/
@Deprecated(forRemoval = true, since = "6.0")
public static File getProjectDir(String path)
{
return getProjectDirPath(path).toFile();
}
/**
* Get a {@link Path} reference to a required directory in the project module path, based on relative
* path references from maven ${basedir}.
*
* Note: will throw assertion error if path does point to an existing directory
*
* @param path the relative path to reference
* @return the directory reference (must exist)
*/
public static Path getProjectDirPath(String path)
{
Path dir = getBasePath().resolve(path);
assertThat("Project Dir", dir, isDirectory());
return dir;
}
/**
* Using junit 3.x naming standards for unit tests and test method names, attempt to discover the unit test name
* from the execution stack.
*
* @return the unit test id found via execution stack and junit 3.8 naming conventions.
* @deprecated use Junit5 {@link TestInfo} instead
*/
@Deprecated(forRemoval = true, since = "6.0")
private static TestID getTestID()
{
StackTraceElement stacked[] = new Throwable().getStackTrace();
for (StackTraceElement stack : stacked)
{
if (stack.getClassName().endsWith("Test"))
{
if (stack.getMethodName().startsWith("test"))
{
TestID testid = new TestID();
testid.classname = stack.getClassName();
testid.methodname = stack.getMethodName();
return testid;
}
}
}
// If we have reached this point, we have failed to find the test id
String LN = System.getProperty("line.separator");
StringBuilder err = new StringBuilder();
err.append("Unable to find a TestID from a testcase that ");
err.append("doesn't follow the standard naming rules.");
err.append(LN);
err.append("Test class name must end in \"*Test\".");
err.append(LN);
err.append("Test method name must start in \"test*\".");
err.append(LN);
err.append("Call to ").append(MavenTestingUtils.class.getSimpleName());
err.append(".getTestID(), must occur from within stack frame of ");
err.append("test method, not @Before, @After, @BeforeClass, ");
err.append("@AfterClass, or Constructors of test case.");
fail(err.toString());
return null;
}
/**
* Get the {@link File} reference to the maven ${basedir}/src/test/resources
directory
*
* Convenience method for MavenTestingUtils.getTestResourcesPath().toFile()
*
* @return the directory {@link File} to the maven ${basedir}/src/test/resources
directory
* @deprecated use {@link #getTestResourcesPath()} instead
*/
@Deprecated(forRemoval = true, since = "6.0")
public static File getTestResourcesDir()
{
return getTestResourcesPath().toFile();
}
/**
* Get the {@link Path} reference to the maven ${basedir}/src/test/resources
directory
*
* @return the directory {@link Path} to the maven ${basedir}/src/test/resources
directory
*/
public static Path getTestResourcesPath()
{
if (testResourcesPath == null)
{
testResourcesPath = getBasePath().resolve("src/test/resources");
assertThat("Test Resource Dir", testResourcesPath, isDirectory());
}
return testResourcesPath;
}
/**
* Get a dir from the maven ${basedir}/src/test/resource
directory.
*
* Note: will throw assertion error if path does point to an existing directory
*
* Convenience method for MavenTestingUtils.getTestResourcesPathDir(name).toFile()
*
* @param name the name of the path to get (it must exist as a dir)
* @return the dir in the maven ${basedir}/src/test/resource
path
* @deprecated use {@link #getTestResourcePathDir(String)} instead
*/
@Deprecated(forRemoval = true, since = "6.0")
public static File getTestResourceDir(String name)
{
return getTestResourcePathDir(name).toFile();
}
/**
* Get a dir from the maven ${basedir}/src/test/resource
directory.
*
* Note: will throw assertion error if path does point to an existing directory
*
* @param name the name of the path to get (it must exist as a dir)
* @return the dir in the maven ${basedir}/src/test/resource
path
*/
public static Path getTestResourcePathDir(String name)
{
Path dir = getTestResourcesPath().resolve(name);
assertThat("Test Resources Dir", dir, isDirectory());
return dir;
}
/**
* Get a file from the maven ${basedir}/src/test/resource
directory.
*
* Note: will throw assertion error if path does point to an existing file
*
* @param name the name of the path to get (it must exist as a file)
* @return the file in maven ${basedir}/src/test/resource
* @deprecated use {@link #getTestResourcePathFile(String)} instead
*/
@Deprecated(forRemoval = true, since = "6.0")
public static File getTestResourceFile(String name)
{
File file = new File(getTestResourcesDir(), FS.separators(name));
assertThat("Test Resource File", file.toPath(), isRegularFile());
return file;
}
/**
* Get a file from the maven ${basedir}/src/test/resource
directory.
*
* Note: will throw assertion error if path does point to an existing file
*
* @param name the name of the path to get (it must exist as a file)
* @return the file in maven ${basedir}/src/test/resource
*/
public static Path getTestResourcePathFile(String name)
{
Path file = getTestResourcesPath().resolve(name);
assertThat("Test Resource File", file, isRegularFile());
return file;
}
/**
* Get a path resource (File or Dir) from the maven ${basedir}/src/test/resource
directory.
*
* @param name the name of the path to get (it must exist)
* @return the path in maven ${basedir}/src/test/resource
*/
public static Path getTestResourcePath(String name)
{
Path path = getTestResourcesPath().resolve(name);
assertThat("Test Resource Path", path, exists());
return path;
}
}