Class TemporaryFolder

  • All Implemented Interfaces:
    TestRule

    public class TemporaryFolder
    extends ExternalResource
    The TemporaryFolder Rule allows creation of files and folders that should be deleted when the test method finishes (whether it passes or fails). Whether the deletion is successful or not is not checked by this rule. No exception will be thrown in case the deletion fails.

    Example of usage:

     public static class HasTempFolder {
      @Rule
      public TemporaryFolder folder= new TemporaryFolder();
    
      @Test
      public void testUsingTempFolder() throws IOException {
          File createdFile= folder.newFile("myfile.txt");
          File createdFolder= folder.newFolder("subfolder");
          // ...
         }
     }
     
    Since:
    4.7
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.io.File folder  
      private java.io.File parentFolder  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      protected void after()
      Override to tear down your specific external resource.
      protected void before()
      Override to set up your specific external resource.
      void create()
      for testing purposes only.
      private java.io.File createTemporaryFolderIn​(java.io.File parentFolder)  
      void delete()
      Delete all files and folders under the temporary folder.
      java.io.File getRoot()  
      private boolean isLastElementInArray​(int index, java.lang.String[] array)  
      java.io.File newFile()
      Returns a new fresh file with a random name under the temporary folder.
      java.io.File newFile​(java.lang.String fileName)
      Returns a new fresh file with the given name under the temporary folder.
      java.io.File newFolder()
      Returns a new fresh folder with a random name under the temporary folder.
      java.io.File newFolder​(java.lang.String folder)
      Returns a new fresh folder with the given name under the temporary folder.
      java.io.File newFolder​(java.lang.String... folderNames)
      Returns a new fresh folder with the given name(s) under the temporary folder.
      private void recursiveDelete​(java.io.File file)  
      private void validateFolderName​(java.lang.String folderName)
      Validates if multiple path components were used while creating a folder.
      • Methods inherited from class java.lang.Object

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

      • parentFolder

        private final java.io.File parentFolder
      • folder

        private java.io.File folder
    • Constructor Detail

      • TemporaryFolder

        public TemporaryFolder()
      • TemporaryFolder

        public TemporaryFolder​(java.io.File parentFolder)
    • Method Detail

      • before

        protected void before()
                       throws java.lang.Throwable
        Description copied from class: ExternalResource
        Override to set up your specific external resource.
        Overrides:
        before in class ExternalResource
        Throws:
        java.lang.Throwable - if setup fails (which will disable after
      • create

        public void create()
                    throws java.io.IOException
        for testing purposes only. Do not use.
        Throws:
        java.io.IOException
      • newFile

        public java.io.File newFile​(java.lang.String fileName)
                             throws java.io.IOException
        Returns a new fresh file with the given name under the temporary folder.
        Throws:
        java.io.IOException
      • newFile

        public java.io.File newFile()
                             throws java.io.IOException
        Returns a new fresh file with a random name under the temporary folder.
        Throws:
        java.io.IOException
      • newFolder

        public java.io.File newFolder​(java.lang.String folder)
                               throws java.io.IOException
        Returns a new fresh folder with the given name under the temporary folder.
        Throws:
        java.io.IOException
      • newFolder

        public java.io.File newFolder​(java.lang.String... folderNames)
                               throws java.io.IOException
        Returns a new fresh folder with the given name(s) under the temporary folder.
        Throws:
        java.io.IOException
      • validateFolderName

        private void validateFolderName​(java.lang.String folderName)
                                 throws java.io.IOException
        Validates if multiple path components were used while creating a folder.
        Parameters:
        folderName - Name of the folder being created
        Throws:
        java.io.IOException
      • isLastElementInArray

        private boolean isLastElementInArray​(int index,
                                             java.lang.String[] array)
      • newFolder

        public java.io.File newFolder()
                               throws java.io.IOException
        Returns a new fresh folder with a random name under the temporary folder.
        Throws:
        java.io.IOException
      • createTemporaryFolderIn

        private java.io.File createTemporaryFolderIn​(java.io.File parentFolder)
                                              throws java.io.IOException
        Throws:
        java.io.IOException
      • getRoot

        public java.io.File getRoot()
        Returns:
        the location of this temporary folder.
      • delete

        public void delete()
        Delete all files and folders under the temporary folder. Usually not called directly, since it is automatically applied by the Rule
      • recursiveDelete

        private void recursiveDelete​(java.io.File file)