Class TestingDir

java.lang.Object
org.eclipse.jetty.toolchain.test.TestingDir
All Implemented Interfaces:
org.junit.rules.TestRule

public class TestingDir extends Object implements org.junit.rules.TestRule
A junit 4.x Rule to provide a common, easy to use, testing directory that is unique per unit test method.

Similar in scope to the TemporaryFolder rule, as it creates a directory, when asked (via getPath() or getEmptyPathDir()) in the maven project familiar and friendly maven location of ${basedir}/target/tests/${testclass}/${testmethod}.

Note: MavenTestingUtils will keep the directory name short for the sake of windows users.

This is best suited for Tests that will benefit from a unit directory per test method. If you desire to have a test directory per test class, with all test methods sharing the same test directory, then consider using the MavenTestingUtils.getTargetTestingDir(String)

Example use:

 public class TestingDirTest
 {
     @Rule
     public TestingDir testingdir = new TestingDir();
 
     @Test
     public void testUseDirectory() throws IOException
     {
         Path appDir = testingdir.getPathFile("app");
         Path tmpDir = testingdir.getPathFile("tmp");

         FS.ensureEmpty(appDir);
         FS.ensureEmpty(tmpDir);

         Path index = appDir.resolve("index.html");
         try(BufferedWriter writer = Files.newBufferedWriter(index, StandardCharsets.UTF_8))
         {
             writer.write("Hello World");
         }

         Server server = new Server();
         server.setTmpDir(tmpDir);
         server.addWebApp(appDir);
         server.start();

         Client client = new Client();
         String response = client.request("http://localhost/app/");

         Assert.assertThat(response, containsString("Hello World"));
     }
 
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private Path
     
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    org.junit.runners.model.Statement
    apply(org.junit.runners.model.Statement statement, org.junit.runner.Description description)
     
    void
    Ensure that the test directory is empty.
    Get the unique testing directory while ensuring that it is empty (if not).
    Get the test specific directory to use for testing work directory.
    Get a Path file reference for content inside of the test specific test directory.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • dir

      private Path dir
  • Constructor Details

    • TestingDir

      public TestingDir()
  • Method Details

    • apply

      public org.junit.runners.model.Statement apply(org.junit.runners.model.Statement statement, org.junit.runner.Description description)
      Specified by:
      apply in interface org.junit.rules.TestRule
    • getPath

      public Path getPath()
      Get the test specific directory to use for testing work directory.

      Name is derived from the test classname & method name.

      Returns:
      the test specific directory.
    • getPathFile

      public Path getPathFile(String name)
      Get a Path file reference for content inside of the test specific test directory.

      Note: No assertions are made if the file exists or not.

      Parameters:
      name - the path name of the file (supports deep paths)
      Returns:
      the file reference.
    • ensureEmpty

      public void ensureEmpty()
      Ensure that the test directory is empty.

      Useful for repeated testing without using the maven clean goal (such as within Eclipse).

    • getEmptyPathDir

      public Path getEmptyPathDir()
      Get the unique testing directory while ensuring that it is empty (if not).
      Returns:
      the unique testing directory, created, and empty.