Class Files


  • public class Files
    extends java.lang.Object
    Reusable assertions for Files.
    • Field Detail

      • UNABLE_TO_COMPARE_FILE_CONTENTS

        private static final java.lang.String UNABLE_TO_COMPARE_FILE_CONTENTS
        See Also:
        Constant Field Values
      • INSTANCE

        private static final Files INSTANCE
      • ANY

        private static final java.io.FileFilter ANY
    • Constructor Detail

      • Files

        Files()
    • Method Detail

      • instance

        public static Files instance()
        Returns the singleton instance of this class.
        Returns:
        the singleton instance of this class.
      • assertSameContentAs

        public void assertSameContentAs​(AssertionInfo info,
                                        java.io.File actual,
                                        java.nio.charset.Charset actualCharset,
                                        java.io.File expected,
                                        java.nio.charset.Charset expectedCharset)
        Asserts that the given files have same content. Adapted from FileAssert (from JUnit-addons.)
        Parameters:
        info - contains information about the assertion.
        actual - the "actual" file.
        actualCharset - Charset of the "actual" file.
        expected - the "expected" file.
        expectedCharset - Charset of the "actual" file.
        Throws:
        java.lang.NullPointerException - if expected is null.
        java.lang.IllegalArgumentException - if expected is not an existing file.
        java.lang.AssertionError - if actual is null.
        java.lang.AssertionError - if actual is not an existing file.
        java.io.UncheckedIOException - if an I/O error occurs.
        java.lang.AssertionError - if the given files do not have same content.
      • assertSameBinaryContentAs

        public void assertSameBinaryContentAs​(AssertionInfo info,
                                              java.io.File actual,
                                              java.io.File expected)
        Asserts that the given files have the same binary content.
        Parameters:
        info - contains information about the assertion.
        actual - the "actual" file.
        expected - the "expected" file.
        Throws:
        java.lang.NullPointerException - if expected is null.
        java.lang.IllegalArgumentException - if expected is not an existing file.
        java.lang.AssertionError - if actual is null.
        java.lang.AssertionError - if actual is not an existing file.
        java.io.UncheckedIOException - if an I/O error occurs.
        java.lang.AssertionError - if the given files do not have same content.
      • assertHasBinaryContent

        public void assertHasBinaryContent​(AssertionInfo info,
                                           java.io.File actual,
                                           byte[] expected)
        Asserts that the given file has the given binary content.
        Parameters:
        info - contains information about the assertion.
        actual - the "actual" file.
        expected - the "expected" binary content.
        Throws:
        java.lang.NullPointerException - if expected is null.
        java.lang.AssertionError - if actual is null.
        java.lang.AssertionError - if actual is not an existing file.
        java.io.UncheckedIOException - if an I/O error occurs.
        java.lang.AssertionError - if the file does not have the binary content.
      • assertHasSizeInBytes

        public void assertHasSizeInBytes​(AssertionInfo info,
                                         java.io.File actual,
                                         long expectedSizeInBytes)
        Asserts that the given file has the given size in bytes.
        Parameters:
        info - contains information about the assertion.
        actual - the "actual" file.
        expectedSizeInBytes - the "expected" file size.
        Throws:
        java.lang.AssertionError - if actual is null.
        java.lang.AssertionError - if actual is not an existing file.
      • assertHasContent

        public void assertHasContent​(AssertionInfo info,
                                     java.io.File actual,
                                     java.lang.String expected,
                                     java.nio.charset.Charset charset)
        Asserts that the given file has the given text content.
        Parameters:
        info - contains information about the assertion.
        actual - the "actual" file.
        expected - the "expected" text content.
        charset - the charset to use to read the file.
        Throws:
        java.lang.NullPointerException - if expected is null.
        java.lang.AssertionError - if actual is null.
        java.lang.AssertionError - if actual is not an existing file.
        java.io.UncheckedIOException - if an I/O error occurs.
        java.lang.AssertionError - if the file does not have the text content.
      • assertIsFile

        public void assertIsFile​(AssertionInfo info,
                                 java.io.File actual)
        Asserts that the given file is an existing file.
        Parameters:
        info - contains information about the assertion.
        actual - the given file.
        Throws:
        java.lang.AssertionError - if the given file is null.
        java.lang.AssertionError - if the given file is not an existing file.
      • assertIsDirectory

        public void assertIsDirectory​(AssertionInfo info,
                                      java.io.File actual)
        Asserts that the given file is an existing directory.
        Parameters:
        info - contains information about the assertion.
        actual - the given file.
        Throws:
        java.lang.AssertionError - if the given file is null.
        java.lang.AssertionError - if the given file is not an existing directory.
      • assertIsAbsolute

        public void assertIsAbsolute​(AssertionInfo info,
                                     java.io.File actual)
        Asserts that the given file is an absolute path.
        Parameters:
        info - contains information about the assertion.
        actual - the given file.
        Throws:
        java.lang.AssertionError - if the given file is null.
        java.lang.AssertionError - if the given file is not an absolute path.
      • assertIsRelative

        public void assertIsRelative​(AssertionInfo info,
                                     java.io.File actual)
        Asserts that the given file is a relative path.
        Parameters:
        info - contains information about the assertion.
        actual - the given file.
        Throws:
        java.lang.AssertionError - if the given file is null.
        java.lang.AssertionError - if the given file is not a relative path.
      • assertExists

        public void assertExists​(AssertionInfo info,
                                 java.io.File actual)
        Asserts that the given file exists, regardless it's a file or directory.
        Parameters:
        info - contains information about the assertion.
        actual - the given file.
        Throws:
        java.lang.AssertionError - if the given file is null.
        java.lang.AssertionError - if the given file does not exist.
      • assertDoesNotExist

        public void assertDoesNotExist​(AssertionInfo info,
                                       java.io.File actual)
        Asserts that the given file does not exist.
        Parameters:
        info - contains information about the assertion.
        actual - the given file.
        Throws:
        java.lang.AssertionError - if the given file is null.
        java.lang.AssertionError - if the given file exists.
      • assertCanWrite

        public void assertCanWrite​(AssertionInfo info,
                                   java.io.File actual)
        Asserts that the given file can be modified by the application.
        Parameters:
        info - contains information about the assertion.
        actual - the given file.
        Throws:
        java.lang.AssertionError - if the given file is null.
        java.lang.AssertionError - if the given file can not be modified.
      • assertIsEmptyFile

        public void assertIsEmptyFile​(AssertionInfo info,
                                      java.io.File actual)
        Asserts that the given File is empty (i.e. size is equal to zero bytes).
        Parameters:
        info - contains information about the assertion.
        actual - the given file.
        Throws:
        java.lang.AssertionError - if the given File is null.
        java.lang.AssertionError - if the given File does not exist.
        java.lang.AssertionError - if the given File is not empty.
      • assertIsNotEmptyFile

        public void assertIsNotEmptyFile​(AssertionInfo info,
                                         java.io.File actual)
        Asserts that the given File is not empty (i.e. size is greater than zero bytes).
        Parameters:
        info - contains information about the assertion.
        actual - the given file.
        Throws:
        java.lang.AssertionError - if the given File is null.
        java.lang.AssertionError - if the given File does not exist.
        java.lang.AssertionError - if the given File is empty.
      • assertCanRead

        public void assertCanRead​(AssertionInfo info,
                                  java.io.File actual)
        Asserts that the given file can be read by the application.
        Parameters:
        info - contains information about the assertion.
        actual - the given file.
        Throws:
        java.lang.AssertionError - if the given file is null.
        java.lang.AssertionError - if the given file can not be modified.
      • assertIsExecutable

        public void assertIsExecutable​(AssertionInfo info,
                                       java.io.File actual)
        Asserts that the given file can be executed by the application.
        Parameters:
        info - contains information about the assertion.
        actual - the given file.
        Throws:
        java.lang.AssertionError - if the given file is null.
        java.lang.AssertionError - if the given file can not be executed.
      • assertHasParent

        public void assertHasParent​(AssertionInfo info,
                                    java.io.File actual,
                                    java.io.File expected)
        Asserts that the given File has the given parent.
        Parameters:
        info - contains information about the assertion.
        actual - the given file.
        expected - the expected parent File.
        Throws:
        java.lang.NullPointerException - if the expected parent File is null.
        java.io.UncheckedIOException - if an I/O error occurs.
        java.lang.AssertionError - if the given File is null.
        java.lang.AssertionError - if the given File does not have a parent.
        java.lang.AssertionError - if the given File parent is not equal to the expected one.
      • assertHasExtension

        public void assertHasExtension​(AssertionInfo info,
                                       java.io.File actual,
                                       java.lang.String expected)
        Asserts that the given File has the given extension.
        Parameters:
        info - contains information about the assertion.
        actual - the given file.
        expected - the expected extension, it does not contains the '.'
        Throws:
        java.lang.NullPointerException - if the expected extension is null.
        java.lang.AssertionError - if the actual File is null.
        java.lang.AssertionError - if the actual File is not a file (ie a directory).
        java.lang.AssertionError - if the actual File does not have the expected extension.
      • assertHasNoExtension

        public void assertHasNoExtension​(AssertionInfo info,
                                         java.io.File actual)
      • assertHasName

        public void assertHasName​(AssertionInfo info,
                                  java.io.File actual,
                                  java.lang.String expected)
        Asserts that the given File has the given name.
        Parameters:
        info - contains information about the assertion.
        actual - the given file.
        expected - the expected file name.
        Throws:
        java.lang.NullPointerException - if the expected name is null.
        java.lang.AssertionError - if the actual File is null.
        java.lang.AssertionError - if the actual File does not have the expected name.
      • assertHasNoParent

        public void assertHasNoParent​(AssertionInfo info,
                                      java.io.File actual)
        Asserts that the given File does not have a parent.
        Parameters:
        info - contains information about the assertion.
        actual - the given file.
        Throws:
        java.lang.AssertionError - if the actual File is null.
        java.lang.AssertionError - if the actual File has a parent.
      • assertHasDigest

        public void assertHasDigest​(AssertionInfo info,
                                    java.io.File actual,
                                    java.security.MessageDigest digest,
                                    byte[] expected)
      • assertHasDigest

        public void assertHasDigest​(AssertionInfo info,
                                    java.io.File actual,
                                    java.security.MessageDigest digest,
                                    java.lang.String expected)
      • assertHasDigest

        public void assertHasDigest​(AssertionInfo info,
                                    java.io.File actual,
                                    java.lang.String algorithm,
                                    byte[] expected)
      • assertHasDigest

        public void assertHasDigest​(AssertionInfo info,
                                    java.io.File actual,
                                    java.lang.String algorithm,
                                    java.lang.String expected)
      • assertIsEmptyDirectory

        public void assertIsEmptyDirectory​(AssertionInfo info,
                                           java.io.File actual)
      • assertIsNotEmptyDirectory

        public void assertIsNotEmptyDirectory​(AssertionInfo info,
                                              java.io.File actual)
      • assertIsDirectoryContaining

        public void assertIsDirectoryContaining​(AssertionInfo info,
                                                java.io.File actual,
                                                java.util.function.Predicate<java.io.File> filter)
      • assertIsDirectoryContaining

        public void assertIsDirectoryContaining​(AssertionInfo info,
                                                java.io.File actual,
                                                java.lang.String syntaxAndPattern)
      • assertIsDirectoryRecursivelyContaining

        public void assertIsDirectoryRecursivelyContaining​(AssertionInfo info,
                                                           java.io.File actual,
                                                           java.lang.String syntaxAndPattern)
      • assertIsDirectoryRecursivelyContaining

        public void assertIsDirectoryRecursivelyContaining​(AssertionInfo info,
                                                           java.io.File actual,
                                                           java.util.function.Predicate<java.io.File> filter)
      • assertIsDirectoryNotContaining

        public void assertIsDirectoryNotContaining​(AssertionInfo info,
                                                   java.io.File actual,
                                                   java.util.function.Predicate<java.io.File> filter)
      • assertIsDirectoryNotContaining

        public void assertIsDirectoryNotContaining​(AssertionInfo info,
                                                   java.io.File actual,
                                                   java.lang.String syntaxAndPattern)
      • filterDirectory

        private java.util.List<java.io.File> filterDirectory​(AssertionInfo info,
                                                             java.io.File actual,
                                                             java.io.FileFilter filter)
      • directoryContent

        private java.util.List<java.io.File> directoryContent​(AssertionInfo info,
                                                              java.io.File actual)
      • assertIsDirectoryContaining

        private void assertIsDirectoryContaining​(AssertionInfo info,
                                                 java.io.File actual,
                                                 java.io.FileFilter filter,
                                                 java.lang.String filterPresentation)
      • assertIsDirectoryNotContaining

        private void assertIsDirectoryNotContaining​(AssertionInfo info,
                                                    java.io.File actual,
                                                    java.io.FileFilter filter,
                                                    java.lang.String filterPresentation)
      • isDirectoryRecursivelyContaining

        private boolean isDirectoryRecursivelyContaining​(AssertionInfo info,
                                                         java.io.File actual,
                                                         java.util.function.Predicate<java.io.File> filter)
      • sortedRecursiveContent

        private java.util.List<java.io.File> sortedRecursiveContent​(java.io.File directory)
      • recursiveContentOf

        private java.util.stream.Stream<java.io.File> recursiveContentOf​(java.io.File directory)
      • assertIsDirectoryRecursivelyContaining

        private void assertIsDirectoryRecursivelyContaining​(AssertionInfo info,
                                                            java.io.File actual,
                                                            java.util.function.Predicate<java.io.File> filter,
                                                            java.lang.String filterPresentation)
      • fileFilter

        private static java.io.FileFilter fileFilter​(AssertionInfo info,
                                                     java.io.File actual,
                                                     java.lang.String syntaxAndPattern)
      • assertNotNull

        private static void assertNotNull​(AssertionInfo info,
                                          java.io.File actual)
      • getFileExtension

        private java.util.Optional<java.lang.String> getFileExtension​(java.io.File file)
      • verifyIsFile

        private void verifyIsFile​(java.io.File expected)
      • isAbsolutePath

        private boolean isAbsolutePath​(AssertionInfo info,
                                       java.io.File actual)