Class TestingDir

  • All Implemented Interfaces:
    org.junit.rules.TestRule

    public class TestingDir
    extends java.lang.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 java.nio.file.Path dir  
    • Constructor Summary

      Constructors 
      Constructor Description
      TestingDir()  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      org.junit.runners.model.Statement apply​(org.junit.runners.model.Statement statement, org.junit.runner.Description description)  
      void ensureEmpty()
      Ensure that the test directory is empty.
      java.nio.file.Path getEmptyPathDir()
      Get the unique testing directory while ensuring that it is empty (if not).
      java.nio.file.Path getPath()
      Get the test specific directory to use for testing work directory.
      java.nio.file.Path getPathFile​(java.lang.String name)
      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 Detail

      • dir

        private java.nio.file.Path dir
    • Constructor Detail

      • TestingDir

        public TestingDir()
    • Method Detail

      • 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 java.nio.file.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 java.nio.file.Path getPathFile​(java.lang.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 java.nio.file.Path getEmptyPathDir()
        Get the unique testing directory while ensuring that it is empty (if not).
        Returns:
        the unique testing directory, created, and empty.