Class AbstractPathAssert<SELF extends AbstractPathAssert<SELF>>
- Type Parameters:
SELF
- self type
- All Implemented Interfaces:
Assert<SELF,
,Path> ComparableAssert<SELF,
,Path> Descriptable<SELF>
,ExtensionPoints<SELF,
Path>
- Direct Known Subclasses:
PathAssert
Path
objects
Note that some assertions have two versions: a normal one and a "raw" one (for instance, hasParent()
and
hasParentRaw()
. The difference is that normal assertions will canonicalize
or normalize
the tested path and, where applicable, the path argument, before
performing the actual test. Canonicalization includes normalization.
Canonicalization may lead to an I/O error if a path does not exist, in which case the given assertions will fail with
a PathsException
. Also note that symbolic links
will be followed if the
filesystem supports them. Finally, if a path is not absolute
, canonicalization will
resolve the path against the process' current working directory.
These assertions are filesystem independent. You may use them on Path
instances issued from the default
filesystem (ie, instances you get when using Paths.get(String, String...)
) or from other
filesystems. For more information, see the javadoc for FileSystem
.
Furthermore:
- Unless otherwise noted, assertions which accept arguments will not accept
null
arguments; if a null argument is passed, these assertions will throw aNullPointerException
. - It is the caller's responsibility to ensure that paths used in assertions are issued from valid filesystems which
are not
closed
. If a filesystems is closed, assertions will throw aClosedFileSystemException
. - Some assertions take another
Path
as an argument. If this path is not issued from the sameprovider
as the tested path, assertions will throw aProviderMismatchException
. - Some assertions may need to perform I/O on the path's underlying filesystem; if an I/O error occurs when
accessing the filesystem, these assertions will throw a
PathsException
.
- Author:
- Valeriy Vyrva
- See Also:
-
Field Summary
Fields inherited from class org.assertj.core.api.AbstractAssert
actual, info, myself, objects, throwUnsupportedExceptionOnEquals
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionReturnsByteArray
assertions on the content of the actualPath
read.content()
Returns String assertions on the content of the actualPath
read with thedefault charset
.Returns String assertions on the content of the actualPath
read with the givenCharset
.Assert that the testedPath
does not exist.Assert that the testedPath
ends with the given path.endsWithRaw
(Path other) Assert that the testedPath
ends with the given path.exists()
Assert that the testedPath
exists according toFiles#exists(Path)
)Assert that the testedPath
exists, not following symbolic links, by callingFiles#exists(Path, LinkOption.NOFOLLOW_LINKS)
).hasBinaryContent
(byte[] expected) Verifies that the binary content of the actualPath
is exactly equal to the given one.hasContent
(String expected) Verifies that the text content of the actualPath
(which must be a readable file) is exactly equal to the given one.
The charset to use when reading the actual path should be provided withusingCharset(Charset)
orusingCharset(String)
prior to calling this method; if not, the platform's default charset (as returned byCharset.defaultCharset()
) will be used.Verifies that the testedPath
digest (calculated with the specified algorithm) is equal to the given one.Verifies that the testedPath
digest (calculated with the specified algorithm) is equal to the given one.hasDigest
(MessageDigest digest, byte[] expected) Verifies that the testedPath
digest (calculated with the specifiedMessageDigest
) is equal to the given one.hasDigest
(MessageDigest digest, String expected) Verifies that the testedPath
digest (calculated with the specifiedMessageDigest
) is equal to the given one.hasExtension
(String expectedExtension) Verifies that the actualPath
has given extension.hasFileName
(String fileName) Assert that the testedPath
last element String representation is equal to the given filename.hasFileSystem
(FileSystem expected) Verifies that the path has the given file system.Verifies that the actualPath
has no extension.Assert that the testedPath
has no parent.Assert that the testedPath
has no parent.Assert that the testedPath
has the expected parent path.hasParentRaw
(Path expected) Assert that the testedPath
has the expected parent path.hasSameBinaryContentAs
(Path expected) Verifies that the content of the actualPath
is equal to the content of the given one, the comparison is done at the binary level.
For text files, usehasSameTextualContentAs(Path)
orhasSameTextualContentAs(Path, Charset)
.hasSameContentAs
(Path expected) Deprecated.hasSameContentAs
(Path expected, Charset expectedCharset) Deprecated.usehasSameTextualContentAs(Path, Charset)
insteadhasSameFileSystemAs
(Path expected) Verifies that the path file system is the same as the given path file system.hasSameTextualContentAs
(Path expected) Verifies that the content of the actualPath
is the same as the given one (both paths must be a readable files).hasSameTextualContentAs
(Path expected, Charset expectedCharset) Verifies that the content of the actualPath
is the same as the expected one, the expectedPath
being read with the given charset while the charset used to read the actual path can be provided withusingCharset(Charset)
orusingCharset(String)
prior to calling this method; if not, the platform's default charset (as returned byCharset.defaultCharset()
) will be used.hasSize
(long expectedSize) Assert that the testedPath
has the given size in bytes.Assert that the testedPath
is absolute (the path does not have to exist).Assert that the testedPath
is a directory.isDirectoryContaining
(String syntaxAndPattern) Verify that the actualPath
is a directory containing at least one file matching the givenString
interpreted as a path matcher (as perFileSystem.getPathMatcher(String)
).isDirectoryContaining
(Predicate<Path> filter) Verify that the actualPath
is a directory containing at least one file matching the givenPredicate<Path>
.isDirectoryNotContaining
(String syntaxAndPattern) Verify that the actualPath
is a directory that does not contain any files matching the givenString
interpreted as a path matcher (as perFileSystem.getPathMatcher(String)
).isDirectoryNotContaining
(Predicate<Path> filter) Verify that the actualPath
is a directory that does not contain any files matching the givenPredicate<Path>
.isDirectoryRecursivelyContaining
(String syntaxAndPattern) Verify that the actualPath
directory or any of its subdirectories (recursively) contains at least one file matching the givenString
interpreted as a path matcher (as perFileSystem.getPathMatcher(String)
).Verify that the actualPath
directory or any of its subdirectories (recursively) contains at least one file matching the givenPredicate<Path>
.Verify that the actualPath
is an empty directory.Verify that the actualPath
is an empty regular file.Assert that the testedPath
is an executable file, it checks that the file exists (according toFiles.exists(Path, LinkOption...)
) and that it is executable(according toFiles.isExecutable(Path)
).Assert that the testedPath
is normalized.Verify that the actualPath
is a non empty directory.Verify that the actualPath
is a non-empty regular file.Assert that the testedPath
is a readable file, it checks that the file exists (according toFiles.exists(Path, LinkOption...)
) and that it is readable(according toFiles.isReadable(Path)
).Assert that the testedPath
is a regular file.Assert that the testedPath
is relative (opposite toPath.isAbsolute()
).Assert that the testedPath
is a symbolic link.Assert that the testedPath
is a writable file, it checks that the file exists (according toFiles.exists(Path, LinkOption...)
) and that it is writable(according toFiles.isWritable(Path)
).startsWith
(Path other) Assert that the testedPath
starts with the given path.startsWithRaw
(Path other) Assert that the testedPath
starts with the given path.usingCharset
(String charsetName) Specifies the name of the charset to use for text-based assertions on the path's contents (path must be a readable file).usingCharset
(Charset charset) Specifies the charset to use for text-based assertions on the path's contents (path must be a readable file).Methods inherited from class org.assertj.core.api.AbstractComparableAssert
inBinary, inHexadecimal, isBetween, isEqualByComparingTo, isGreaterThan, isGreaterThanOrEqualTo, isLessThan, isLessThanOrEqualTo, isNotEqualByComparingTo, isStrictlyBetween, usingComparator, usingComparator, usingDefaultComparator
Methods inherited from class org.assertj.core.api.AbstractObjectAssert
as, as, doesNotReturn, extracting, extracting, extracting, extracting, extracting, extracting, extractingForProxy, getComparatorsByType, hasAllNullFieldsOrProperties, hasAllNullFieldsOrPropertiesExcept, hasFieldOrProperty, hasFieldOrPropertyWithValue, hasNoNullFieldsOrProperties, hasNoNullFieldsOrPropertiesExcept, hasOnlyFields, isEqualToComparingFieldByField, isEqualToComparingFieldByFieldRecursively, isEqualToComparingOnlyGivenFields, isEqualToIgnoringGivenFields, isEqualToIgnoringNullFields, newObjectAssert, returns, usingComparatorForFields, usingComparatorForType, usingRecursiveAssertion, usingRecursiveAssertion, usingRecursiveComparison, usingRecursiveComparison
Methods inherited from class org.assertj.core.api.AbstractAssert
areEqual, asInstanceOf, asList, assertionError, asString, describedAs, descriptionText, doesNotHave, doesNotHaveSameClassAs, doesNotHaveSameHashCodeAs, doesNotHaveToString, doesNotHaveToString, equals, extracting, extracting, failure, failureWithActualExpected, failWithActualExpectedAndMessage, failWithMessage, getWritableAssertionInfo, has, hashCode, hasSameClassAs, hasSameHashCodeAs, hasToString, hasToString, is, isElementOfCustomAssert, isEqualTo, isExactlyInstanceOf, isIn, isIn, isInstanceOf, isInstanceOfAny, isInstanceOfSatisfying, isNot, isNotEqualTo, isNotExactlyInstanceOf, isNotIn, isNotIn, isNotInstanceOf, isNotInstanceOfAny, isNotNull, isNotOfAnyClassIn, isNotSameAs, isNull, isOfAnyClassIn, isSameAs, matches, matches, newListAssertInstance, overridingErrorMessage, overridingErrorMessage, satisfies, satisfies, satisfies, satisfiesAnyOf, satisfiesAnyOf, satisfiesAnyOfForProxy, satisfiesForProxy, setCustomRepresentation, setDescriptionConsumer, setPrintAssertionsDescription, throwAssertionError, withFailMessage, withFailMessage, withRepresentation, withThreadDumpOnError
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.assertj.core.api.Descriptable
as, describedAs, describedAs
-
Field Details
-
paths
protected org.assertj.core.internal.Paths paths
-
-
Constructor Details
-
AbstractPathAssert
-
-
Method Details
-
hasSameContentAs
Deprecated.usehasSameTextualContentAs(Path)
insteadVerifies that the content of the actual
Path
is the same as the given one (both paths must be a readable files). The charset to use when reading the actual path can be provided withusingCharset(Charset)
orusingCharset(String)
prior to calling this method; if not, the platform's default charset (as returned byCharset.defaultCharset()
) will be used. Examples:// use the default charset Path xFile = Files.write(Paths.get("xfile.txt"), "The Truth Is Out There".getBytes()); Path xFileUTF8 = Files.write(Paths.get("xfile-clone.txt"), "The Truth Is Out There".getBytes("UTF-8")); Path xFileClone = Files.write(Paths.get("xfile-clone.txt"), "The Truth Is Out There".getBytes()); Path xFileFrench = Files.write(Paths.get("xfile-french.txt"), "La Vérité Est Ailleurs".getBytes()); // The following assertion succeeds (default charset is used): assertThat(xFile).hasSameContentAs(xFileClone); // The following assertion succeeds (UTF-8 charset is used to read xFile): assertThat(xFileUTF8).usingCharset("UTF-8").hasContent(xFileClone); // The following assertion fails: assertThat(xFile).hasSameContentAs(xFileFrench);
- Parameters:
expected
- the givenPath
to compare the actualPath
to.- Returns:
this
assertion object.- Throws:
NullPointerException
- if the givenPath
isnull
.AssertionError
- if the actual or givenPath
is not an existing readable file.AssertionError
- if the actualPath
isnull
.AssertionError
- if the content of the actualPath
is not equal to the content of the given one.PathsException
- if an I/O error occurs.
-
hasSameTextualContentAs
Verifies that the content of the actualPath
is the same as the given one (both paths must be a readable files). The charset to use when reading the actual path can be provided withusingCharset(Charset)
orusingCharset(String)
prior to calling this method; if not, the platform's default charset (as returned byCharset.defaultCharset()
) will be used.Examples:
// use the default charset Path xFile = Files.write(Paths.get("xfile.txt"), "The Truth Is Out There".getBytes()); Path xFileUTF8 = Files.write(Paths.get("xfile-clone.txt"), "The Truth Is Out There".getBytes("UTF-8")); Path xFileClone = Files.write(Paths.get("xfile-clone.txt"), "The Truth Is Out There".getBytes()); Path xFileFrench = Files.write(Paths.get("xfile-french.txt"), "La Vérité Est Ailleurs".getBytes()); // The following assertion succeeds (default charset is used): assertThat(xFile).hasSameTextualContentAs(xFileClone); // The following assertion succeeds (UTF-8 charset is used to read xFile): assertThat(xFileUTF8).usingCharset("UTF-8").hasContent(xFileClone); // The following assertion fails: assertThat(xFile).hasSameTextualContentAs(xFileFrench);
- Parameters:
expected
- the givenPath
to compare the actualPath
to.- Returns:
this
assertion object.- Throws:
NullPointerException
- if the givenPath
isnull
.AssertionError
- if the actual or givenPath
is not an existing readable file.AssertionError
- if the actualPath
isnull
.AssertionError
- if the content of the actualPath
is not equal to the content of the given one.PathsException
- if an I/O error occurs.- Since:
- 3.15
-
hasSameTextualContentAs
Verifies that the content of the actualPath
is the same as the expected one, the expectedPath
being read with the given charset while the charset used to read the actual path can be provided withusingCharset(Charset)
orusingCharset(String)
prior to calling this method; if not, the platform's default charset (as returned byCharset.defaultCharset()
) will be used.Examples:
Path fileUTF8Charset = Files.write(Paths.get("actual"), Collections.singleton("Gerçek"), StandardCharsets.UTF_8); Charset turkishCharset = Charset.forName("windows-1254"); Path fileTurkischCharset = Files.write(Paths.get("expected"), Collections.singleton("Gerçek"), turkishCharset); // The following assertion succeeds: assertThat(fileUTF8Charset).usingCharset(StandardCharsets.UTF_8).hasSameTextualContentAs(fileTurkischCharset, turkishCharset); // The following assertion fails: assertThat(fileUTF8Charset).usingCharset(StandardCharsets.UTF_8).hasSameTextualContentAs(fileTurkischCharset, StandardCharsets.UTF_8);
- Parameters:
expected
- the givenPath
to compare the actualPath
to.expectedCharset
- theCharset
used to read the content of the expected Path.- Returns:
this
assertion object.- Throws:
NullPointerException
- if the givenPath
isnull
.AssertionError
- if the actual or givenPath
is not an existing readable file.AssertionError
- if the actualPath
isnull
.AssertionError
- if the content of the actualPath
is not equal to the content of the given one.PathsException
- if an I/O error occurs.- Since:
- 3.15
-
hasSameContentAs
Deprecated.usehasSameTextualContentAs(Path, Charset)
insteadVerifies that the content of the actual
Path
is the same as the expected one, the expectedPath
being read with the given charset while the charset used to read the actual path can be provided withusingCharset(Charset)
orusingCharset(String)
prior to calling this method; if not, the platform's default charset (as returned byCharset.defaultCharset()
) will be used.Examples:
Path fileUTF8Charset = Files.write(Paths.get("actual"), Collections.singleton("Gerçek"), StandardCharsets.UTF_8); Charset turkishCharset = Charset.forName("windows-1254"); Path fileTurkischCharset = Files.write(Paths.get("expected"), Collections.singleton("Gerçek"), turkishCharset); // The following assertion succeeds: assertThat(fileUTF8Charset).usingCharset(StandardCharsets.UTF_8).hasSameContentAs(fileTurkischCharset, turkishCharset); // The following assertion fails: assertThat(fileUTF8Charset).usingCharset(StandardCharsets.UTF_8).hasSameContentAs(fileTurkischCharset, StandardCharsets.UTF_8);
- Parameters:
expected
- the givenPath
to compare the actualPath
to.expectedCharset
- theCharset
used to read the content of the expected Path.- Returns:
this
assertion object.- Throws:
NullPointerException
- if the givenPath
isnull
.AssertionError
- if the actual or givenPath
is not an existing readable file.AssertionError
- if the actualPath
isnull
.AssertionError
- if the content of the actualPath
is not equal to the content of the given one.PathsException
- if an I/O error occurs.
-
hasBinaryContent
Verifies that the binary content of the actualPath
is exactly equal to the given one. Examples:// using the default charset, the following assertion succeeds: Path xFile = Files.write(Paths.get("xfile.txt"), "The Truth Is Out There".getBytes()); assertThat(xFile).hasBinaryContent("The Truth Is Out There".getBytes()); // using a specific charset Charset turkishCharset = Charset.forName("windows-1254"); Path xFileTurkish = Files.write(Paths.get("xfile.turk"), Collections.singleton("Gerçek Başka bir yerde mi"), turkishCharset); // The following assertion succeeds: String expectedContent = "Gerçek Başka bir yerde mi" + org.assertj.core.util.Compatibility.System.lineSeparator(); byte[] binaryContent = expectedContent.getBytes(turkishCharset.name()); assertThat(xFileTurkish).hasBinaryContent(binaryContent); // The following assertion fails ... unless you are in Turkey ;-): assertThat(xFileTurkish).hasBinaryContent("Gerçek Başka bir yerde mi".getBytes());
- Parameters:
expected
- the expected binary content to compare the actualPath
's content to.- Returns:
this
assertion object.- Throws:
NullPointerException
- if the given content isnull
.AssertionError
- if the actualPath
isnull
.AssertionError
- if the actualPath
is not an existing file.UncheckedIOException
- if an I/O error occurs.AssertionError
- if the content of the actualPath
is not equal to the given binary content.
-
hasSameBinaryContentAs
Verifies that the content of the actualPath
is equal to the content of the given one, the comparison is done at the binary level.
For text files, usehasSameTextualContentAs(Path)
orhasSameTextualContentAs(Path, Charset)
.Examples:
// The first two paths have the same contents, the third does not Path aPath = Files.write(Paths.get("a-file.bin"), new byte[] { 42 }); Path bPath = Files.write(Paths.get("b-file.bin"), new byte[] { 42 }); Path cPath = Files.write(Paths.get("c-file.bin"), new byte[] { 24 }); // The following assertion succeeds: assertThat(aPath).hasSameBinaryContentAs(bPath); // The following assertion fails: assertThat(aPath).hasSameBinaryContent(cPath);
- Parameters:
expected
- the givenPath
to compare the actualPath
to.- Returns:
this
assertion object.- Throws:
NullPointerException
- if the givenPath
isnull
.IllegalArgumentException
- if the givenPath
is not an existing file.AssertionError
- if the actualPath
isnull
.AssertionError
- if the actualPath
is not an existing file.UncheckedIOException
- if any I/O error occurs.AssertionError
- if the content of the actualPath
is not equal to the content of the given one.- Since:
- 3.15
-
usingCharset
Specifies the name of the charset to use for text-based assertions on the path's contents (path must be a readable file). Examples:Charset turkishCharset = Charset.forName("windows-1254"); Path xFileTurkish = Files.write(Paths.get("xfile.turk"), Collections.singleton("Gerçek Başka bir yerde mi"), turkishCharset); // The following assertion succeeds: assertThat(xFileTurkish).usingCharset("windows-1254").hasContent("Gerçek Başka bir yerde mi");
- Parameters:
charsetName
- the name of the charset to use.- Returns:
this
assertion object.- Throws:
IllegalArgumentException
- if the given encoding is not supported on this platform.
-
usingCharset
Specifies the charset to use for text-based assertions on the path's contents (path must be a readable file). Examples:Charset turkishCharset = Charset.forName("windows-1254"); Path xFileTurkish = Files.write(Paths.get("xfile.turk"), Collections.singleton("Gerçek Başka bir yerde mi"), turkishCharset); // The following assertion succeeds: assertThat(xFileTurkish).usingCharset(turkishCharset).hasContent("Gerçek Başka bir yerde mi");
- Parameters:
charset
- the charset to use.- Returns:
this
assertion object.- Throws:
NullPointerException
- if the given charset isnull
.
-
hasContent
Verifies that the text content of the actualPath
(which must be a readable file) is exactly equal to the given one.
The charset to use when reading the actual path should be provided withusingCharset(Charset)
orusingCharset(String)
prior to calling this method; if not, the platform's default charset (as returned byCharset.defaultCharset()
) will be used. Examples:// use the default charset Path xFile = Files.write(Paths.get("xfile.txt"), "The Truth Is Out There".getBytes()); // The following assertion succeeds (default charset is used): assertThat(xFile).hasContent("The Truth Is Out There"); // The following assertion fails: assertThat(xFile).hasContent("La Vérité Est Ailleurs"); // using a specific charset Charset turkishCharset = Charset.forName("windows-1254"); Path xFileTurkish = Files.write(Paths.get("xfile.turk"), Collections.singleton("Gerçek Başka bir yerde mi"), turkishCharset); // The following assertion succeeds: assertThat(xFileTurkish).usingCharset(turkishCharset).hasContent("Gerçek Başka bir yerde mi"); // The following assertion fails ... unless you are in Turkey ;-): assertThat(xFileTurkish).hasContent("Gerçek Başka bir yerde mi");
- Parameters:
expected
- the expected text content to compare the actualPath
's content to.- Returns:
this
assertion object.- Throws:
NullPointerException
- if the given content isnull
.UncheckedIOException
- if an I/O error occurs.AssertionError
- if the actualPath
isnull
.AssertionError
- if the actualPath
is not areadable
file.AssertionError
- if the content of the actualPath
is not equal to the given content.
-
hasFileSystem
Verifies that the path has the given file system.Examples:
Path jarFile = Paths.get("assertj-core.jar"); FileSystem mainFileSystem = jarFile.getFileSystem(); try (FileSystem fs = FileSystems.newFileSystem(jarFile, (ClassLoader) null)) { Path manifestFile = fs.getPath("META-INF", "MANIFEST.MF"); // Succeeds assertThat(manifestFile).hasFileSystem(fs); // Fails assertThat(manifestFile).hasFileSystem(mainFileSystem); }
- Parameters:
expected
- the givenFileSystem
to compare the actualPath
file system to.- Returns:
this
assertion object.- Throws:
NullPointerException
- if the givenFileSystem
isnull
.AssertionError
- if the actualFileSystem
isnull
.- Since:
- 3.23.0
-
hasSameFileSystemAs
Verifies that the path file system is the same as the given path file system. Examples:Path jarFile = Paths.get("assertj-core.jar"); try (FileSystem fs = FileSystems.newFileSystem(jarFile, (ClassLoader) null)) { Path manifestFile = fs.getPath("META-INF", "MANIFEST.MF"); Path abstractPathAssertFile = fs.getPath("org", "assertj", "core", "api", "AbstractPathAssert.class"); // Succeeds assertThat(manifestFile).hasSameFileSystemAs(abstractPathAssertFile); // Fails assertThat(manifestFile).hasSameFileSystemAs(jarFile); }
- Parameters:
expected
- the givenPath
to compare the actualPath
file system to.- Returns:
this
assertion object.- Throws:
NullPointerException
- if the givenPath
isnull
.AssertionError
- if the actualPath
isnull
.- Since:
- 3.23.0
-
isReadable
Assert that the testedPath
is a readable file, it checks that the file exists (according toFiles.exists(Path, LinkOption...)
) and that it is readable(according toFiles.isReadable(Path)
). Examples:// Create a file and set permissions to be readable by all. Path readableFile = Paths.get("readableFile"); Set<PosixFilePermission> perms = PosixFilePermissions.fromString("r--r--r--"); Files.createFile(readableFile, PosixFilePermissions.asFileAttribute(perms)); final Path symlinkToReadableFile = FileSystems.getDefault().getPath("symlinkToReadableFile"); Files.createSymbolicLink(symlinkToReadableFile, readableFile); // Create a file and set permissions not to be readable. Path nonReadableFile = Paths.get("nonReadableFile"); Set<PosixFilePermission> notReadablePerms = PosixFilePermissions.fromString("-wx------"); Files.createFile(nonReadableFile, PosixFilePermissions.asFileAttribute(notReadablePerms)); final Path nonExistentPath = FileSystems.getDefault().getPath("nonexistent"); // The following assertions succeed: assertThat(readableFile).isReadable(); assertThat(symlinkToReadableFile).isReadable(); // The following assertions fail: assertThat(nonReadableFile).isReadable(); assertThat(nonExistentPath).isReadable();
- Returns:
- self
- See Also:
-
isWritable
Assert that the testedPath
is a writable file, it checks that the file exists (according toFiles.exists(Path, LinkOption...)
) and that it is writable(according toFiles.isWritable(Path)
). Examples:Create a file and set permissions to be writable by all. Path writableFile = Paths.get("writableFile"); Set<PosixFilePermission> perms = PosixFilePermissions.fromString("rw-rw-rw-"); Files.createFile(writableFile, PosixFilePermissions.asFileAttribute(perms)); final Path symlinkToWritableFile = FileSystems.getDefault().getPath("symlinkToWritableFile"); Files.createSymbolicLink(symlinkToWritableFile, writableFile); // Create a file and set permissions not to be writable. Path nonWritableFile = Paths.get("nonWritableFile"); perms = PosixFilePermissions.fromString("r--r--r--"); Files.createFile(nonWritableFile, PosixFilePermissions.asFileAttribute(perms)); final Path nonExistentPath = FileSystems.getDefault().getPath("nonexistent"); // The following assertions succeed: assertThat(writableFile).isWritable(); assertThat(symlinkToWritableFile).isWritable(); // The following assertions fail: assertThat(nonWritableFile).isWritable(); assertThat(nonExistentPath).isWritable();
- Returns:
- self
- See Also:
-
isExecutable
Assert that the testedPath
is an executable file, it checks that the file exists (according toFiles.exists(Path, LinkOption...)
) and that it is executable(according toFiles.isExecutable(Path)
). Examples:// Create a file and set permissions to be executable by all. Path executableFile = Paths.get("executableFile"); Set<PosixFilePermission> perms = PosixFilePermissions.fromString("r-xr-xr-x"); Files.createFile(executableFile, PosixFilePermissions.asFileAttribute(perms)); final Path symlinkToExecutableFile = FileSystems.getDefault().getPath("symlinkToExecutableFile"); Files.createSymbolicLink(symlinkToExecutableFile, executableFile); // Create a file and set permissions not to be executable. Path nonExecutableFile = Paths.get("nonExecutableFile"); perms = PosixFilePermissions.fromString("rw-------"); Files.createFile(nonExecutableFile, PosixFilePermissions.asFileAttribute(perms)); final Path nonExistentPath = FileSystems.getDefault().getPath("nonexistent"); // The following assertions succeed: assertThat(executableFile).isExecutable(); assertThat(symlinkToExecutableFile).isExecutable(); // The following assertions fail: assertThat(nonExecutableFile).isExecutable(); assertThat(nonExistentPath).isExecutable();
- Returns:
- self
- See Also:
-
exists
Assert that the testedPath
exists according toFiles#exists(Path)
)Note that this assertion will follow symbolic links before asserting the path's existence.
On Windows system, this has no influence. On Unix systems, this means the assertion result will fail if the path is a symbolic link whose target does not exist. If you want to assert the existence of the symbolic link itself, use
Examples:existsNoFollowLinks()
instead.// fs is a Unix filesystem // Create a regular file, and a symbolic link pointing to it final Path existingFile = fs.getPath("somefile"); Files.createFile(existingFile); final Path symlinkToExistingFile = fs.getPath("symlinkToExistingFile"); Files.createSymbolicLink(symlinkToExistingFile, existingFile); // Create a symbolic link whose target does not exist final Path nonExistentPath = fs.getPath("nonexistent"); final Path symlinkToNonExistentPath = fs.getPath("symlinkToNonExistentPath"); Files.createSymbolicLink(symlinkToNonExistentPath, nonExistentPath); // The following assertions succeed: assertThat(existingFile).exists(); assertThat(symlinkToExistingFile).exists(); // The following assertions fail: assertThat(nonExistentPath).exists(); assertThat(symlinkToNonExistentPath).exists();
- Returns:
- self
- See Also:
-
existsNoFollowLinks
Assert that the testedPath
exists, not following symbolic links, by callingFiles#exists(Path, LinkOption.NOFOLLOW_LINKS)
).This assertion behaves like
Examples:exists()
, with the difference that it can be used to assert the existence of a symbolic link even if its target is invalid.// fs is a Unix filesystem // Create a regular file, and a symbolic link pointing to it final Path existingFile = fs.getPath("somefile"); Files.createFile(existingFile); final Path symlinkToExistingFile = fs.getPath("symlink"); Files.createSymbolicLink(symlinkToExistingFile, existingFile); // Create a symbolic link whose target does not exist final Path nonExistentPath = fs.getPath("nonexistent"); final Path symlinkToNonExistentPath = fs.getPath("symlinkToNonExistentPath"); Files.createSymbolicLink(symlinkToNonExistentPath, nonExistentPath); // The following assertions succeed assertThat(existingFile).existsNoFollowLinks(); assertThat(symlinkToExistingFile).existsNoFollowLinks(); assertThat(symlinkToNonExistentPath).existsNoFollowLinks(); // The following assertion fails assertThat(nonExistentPath).existsNoFollowLinks();
- Returns:
- self
- See Also:
-
doesNotExist
Assert that the testedPath
does not exist.IMPORTANT NOTE: this method will NOT follow symbolic links (provided that the underlying
FileSystem
of this path supports symbolic links at all).This means that even if the link exists this assertion will fail even if the link's target does not exists - note that this is unlike the default behavior of
exists()
.If you are a Windows user, the above does not apply to you; if you are a Unix user however, this is important. Consider the following:
// fs is a FileSystem // Create a regular file, and a symbolic link pointing to it final Path existingFile = fs.getPath("somefile"); Files.createFile(existingFile); final Path symlinkToExistingFile = fs.getPath("symlink"); Files.createSymbolicLink(symlinkToExistingFile, existingFile); // Create a symbolic link to a nonexistent target file. final Path nonExistentPath = fs.getPath("nonExistentPath"); final Path symlinkToNonExistentPath = fs.getPath("symlinkToNonExistentPath"); Files.createSymbolicLink(symlinkToNonExistentPath, nonExistentPath); // The following assertion succeeds assertThat(nonExistentPath).doesNotExist(); // The following assertions fail: assertThat(existingFile).doesNotExist(); assertThat(symlinkToExistingFile).doesNotExist(); // fail because symlinkToNonExistentPath exists even though its target does not. assertThat(symlinkToNonExistentPath).doesNotExist();
- Returns:
- self
- See Also:
-
isRegularFile
Assert that the testedPath
is a regular file.Note that this method will follow symbolic links. If you are a Unix user and wish to assert that a path is a symbolic link instead, use
isSymbolicLink()
.This assertion first asserts the existence of the path (using
Examples:exists()
) then checks whether the path is a regular file.// fs is a Unix filesystem // Create a regular file, and a symbolic link to that regular file final Path existingFile = fs.getPath("existingFile"); final Path symlinkToExistingFile = fs.getPath("symlinkToExistingFile"); Files.createFile(existingFile); Files.createSymbolicLink(symlinkToExistingFile, existingFile); // Create a directory, and a symbolic link to that directory final Path dir = fs.getPath("dir"); final Path dirSymlink = fs.getPath("dirSymlink"); Files.createDirectories(dir); Files.createSymbolicLink(dirSymlink, dir); // Create a nonexistent entry, and a symbolic link to that entry final Path nonExistentPath = fs.getPath("nonexistent"); final Path symlinkToNonExistentPath = fs.getPath("symlinkToNonExistentPath"); Files.createSymbolicLink(symlinkToNonExistentPath, nonExistentPath); // the following assertions succeed: assertThat(existingFile).isRegularFile(); assertThat(symlinkToExistingFile).isRegularFile(); // the following assertions fail because paths do not exist: assertThat(nonExistentPath).isRegularFile(); assertThat(symlinkToNonExistentPath).isRegularFile(); // the following assertions fail because paths exist but are not regular files: assertThat(dir).isRegularFile(); assertThat(dirSymlink).isRegularFile();
- Returns:
- self
-
isDirectory
Assert that the testedPath
is a directory.Note that this method will follow symbolic links. If you are a Unix user and wish to assert that a path is a symbolic link instead, use
isSymbolicLink()
.This assertion first asserts the existence of the path (using
Examples:exists()
) then checks whether the path is a directory.// fs is a Unix filesystem // Create a regular file, and a symbolic link to that regular file final Path existingFile = fs.getPath("existingFile"); final Path symlinkToExistingFile = fs.getPath("symlinkToExistingFile"); Files.createFile(existingFile); Files.createSymbolicLink(symlinkToExistingFile, existingFile); // Create a directory, and a symbolic link to that directory final Path dir = fs.getPath("dir"); final Path dirSymlink = fs.getPath("dirSymlink"); Files.createDirectories(dir); Files.createSymbolicLink(dirSymlink, dir); // Create a nonexistent entry, and a symbolic link to that entry final Path nonExistentPath = fs.getPath("nonexistent"); final Path symlinkToNonExistentPath = fs.getPath("symlinkToNonExistentPath"); Files.createSymbolicLink(symlinkToNonExistentPath, nonExistentPath); // the following assertions succeed: assertThat(dir).isDirectory(); assertThat(dirSymlink).isDirectory(); // the following assertions fail because paths do not exist: assertThat(nonExistentPath).isDirectory(); assertThat(symlinkToNonExistentPath).isDirectory(); // the following assertions fail because paths exist but are not directories: assertThat(existingFile).isDirectory(); assertThat(symlinkToExistingFile).isDirectory();
- Returns:
- self
-
isSymbolicLink
Assert that the testedPath
is a symbolic link.This assertion first asserts the existence of the path (using
Examples:existsNoFollowLinks()
) then checks whether the path is a symbolic link.// fs is a Unix filesystem // Create a regular file, and a symbolic link to that regular file final Path existingFile = fs.getPath("existingFile"); final Path symlinkToExistingFile = fs.getPath("symlinkToExistingFile"); Files.createFile(existingFile); Files.createSymbolicLink(symlinkToExistingFile, existingFile); // Create a directory, and a symbolic link to that directory final Path dir = fs.getPath("dir"); final Path dirSymlink = fs.getPath("dirSymlink"); Files.createDirectories(dir); Files.createSymbolicLink(dirSymlink, dir); // Create a nonexistent entry, and a symbolic link to that entry final Path nonExistentPath = fs.getPath("nonexistent"); final Path symlinkToNonExistentPath = fs.getPath("symlinkToNonExistentPath"); Files.createSymbolicLink(symlinkToNonExistentPath, nonExistentPath); // the following assertions succeed: assertThat(dirSymlink).isSymbolicLink(); assertThat(symlinkToExistingFile).isSymbolicLink(); assertThat(symlinkToNonExistentPath).isSymbolicLink(); // the following assertion fails because the path does not exist: assertThat(nonExistentPath).isSymbolicLink(); // the following assertions fail because paths exist but are not symbolic links assertThat(existingFile).isSymbolicLink(); assertThat(dir).isSymbolicLink();
- Returns:
- self
-
isAbsolute
Assert that the testedPath
is absolute (the path does not have to exist).Note that the fact that a path is absolute does not mean that it is
Examples:normalized
:/foo/..
is absolute, for instance, but it is not normalized.// unixFs is a Unix FileSystem // The following assertion succeeds: assertThat(unixFs.getPath("/foo/bar")).isAbsolute(); // The following assertion fails: assertThat(unixFs.getPath("foo/bar")).isAbsolute(); // windowsFs is a Windows FileSystem // The following assertion succeeds: assertThat(windowsFs.getPath("c:\\foo")).isAbsolute(); // The following assertions fail: assertThat(windowsFs.getPath("foo\\bar")).isAbsolute(); assertThat(windowsFs.getPath("c:foo")).isAbsolute(); assertThat(windowsFs.getPath("\\foo\\bar")).isAbsolute();
- Returns:
- self
- See Also:
-
isRelative
Assert that the testedPath
is relative (opposite toPath.isAbsolute()
). Examples:// unixFs is a Unix FileSystem // The following assertions succeed: assertThat(unixFs.getPath("./foo/bar")).isRelative(); assertThat(unixFs.getPath("foo/bar")).isRelative(); // The following assertion fails: assertThat(unixFs.getPath("/foo/bar")).isRelative(); // windowsFs is a Windows FileSystem // The following assertion succeeds: assertThat(windowsFs.getPath("foo\\bar")).isRelative(); assertThat(windowsFs.getPath("c:foo")).isRelative(); assertThat(windowsFs.getPath("\\foo\\bar")).isRelative(); // The following assertions fail: assertThat(windowsFs.getPath("c:\\foo")).isRelative();
- Returns:
- self
- See Also:
-
isNormalized
Assert that the testedPath
is normalized.A path is normalized if it has no redundant components; typically, on both Unix and Windows, this means that the path has no "self" components (
Examples:.
) and that its only parent components (..
), if any, are at the beginning of the path.// fs is a Unix filesystem // the following assertions succeed: assertThat(fs.getPath("/usr/lib")).isNormalized(); assertThat(fs.getPath("a/b/c")).isNormalized(); assertThat(fs.getPath("../d")).isNormalized(); // the following assertions fail: assertThat(fs.getPath("/a/./b")).isNormalized(); assertThat(fs.getPath("c/b/..")).isNormalized(); assertThat(fs.getPath("/../../e")).isNormalized();
- Returns:
- self
-
isCanonical
Assert that the testedPath
is canonical by comparing it to itsreal path
.For Windows users, this assertion is no different than
Examples:isAbsolute()
expect that the file must exist. For Unix users, this assertion ensures that the tested path is the actual file system resource, that is, it is not asymbolic link
to the actual resource, even if the path is absolute.// fs is a Unix filesystem // Create a directory final Path basedir = fs.getPath("/tmp/foo"); Files.createDirectories(basedir); // Create a file in this directory final Path existingFile = basedir.resolve("existingFile"); Files.createFile(existingFile); // Create a symbolic link to that file final Path symlinkToExistingFile = basedir.resolve("symlinkToExistingFile"); Files.createSymbolicLink(symlinkToExistingFile, existingFile); // The following assertion succeeds: assertThat(existingFile).isCanonical(); // The following assertion fails: assertThat(symlinkToExistingFile).isCanonical();
- Returns:
- self
- Throws:
PathsException
- an I/O error occurred while evaluating the path- See Also:
-
hasFileName
Assert that the testedPath
last element String representation is equal to the given filename.Note that the path does not need to exist to check its file name.
Examples:// fs is a Unix filesystem final Path file = fs.getPath("/foo/foo.txt"); final Path symlink = fs.getPath("/home/symlink-to-foo"); Files.createSymbolicLink(symlink, file); // the following assertions succeed: assertThat(fs.getPath("/dir1/file.txt")).hasFileName("file.txt"); assertThat(fs.getPath("/dir1/dir2")).hasFileName("dir2"); // you can check file name on non existent paths assertThat(file).hasFileName("foo.txt"); assertThat(symlink).hasFileName("symlink-to-foo"); // the following assertions fail: assertThat(fs.getPath("/dir1/file.txt").hasFileName("other.txt"); // fail because, last element is "." assertThat(fs.getPath("/dir1/.")).hasFileName("dir1"); // fail because a link filename is not the same as its target filename assertThat(symlink).hasFileName("file.txt");
- Parameters:
fileName
- the expected filename- Returns:
- self
- Throws:
NullPointerException
- if the given fileName is null.- See Also:
-
hasParent
Assert that the testedPath
has the expected parent path.This assertion will perform canonicalization of the tested path and of the given argument before performing the test; see the class description for more details. If this is not what you want, use
hasParentRaw(Path)
instead.Checks that the tested path has the given parent. This assertion will fail both if the tested path has no parent, or has a different parent than what is expected.
Examples:// fs is a Unix filesystem final Path actual = fs.getPath("/dir1/dir2/file"); // the following assertion succeeds: assertThat(actual).hasParent(fs.getPath("/dir1/dir2/.")); // this one too as this path will be normalized to "/dir1/dir2": assertThat(actual).hasParent(fs.getPath("/dir1/dir3/../dir2/.")); // the following assertion fails: assertThat(actual).hasParent(fs.getPath("/dir1"));
- Parameters:
expected
- the expected parent path- Returns:
- self
- Throws:
NullPointerException
- if the given parent path is null.PathsException
- failed to canonicalize the tested path or the path given as an argument- See Also:
-
hasParentRaw
Assert that the testedPath
has the expected parent path.This assertion will not perform any canonicalization of either the tested path or the path given as an argument; see class description for more details. If this is not what you want, use
hasParent(Path)
instead.This assertion uses
Path.getParent()
with no modification, which means the only criterion for this assertion's success is the path's components (its root and its name elements).This may lead to surprising results if the tested path and the path given as an argument are not normalized. For instance, if the tested path is
/home/foo/../bar
and the argument is/home
, the assertion will fail since the parent of the tested path is not/home
but.../home/foo/..
.While this may seem counterintuitive, it has to be recalled here that it is not required for a
Examples:FileSystem
to consider that.
and..
are name elements for respectively the current directory and the parent directory respectively. In fact, it is not even required that aFileSystem
be hierarchical at all.// fs is a Unix filesystem final Path actual = fs.getPath("/dir1/dir2/file"); // the following assertion succeeds: assertThat(actual).hasParentRaw(fs.getPath("/dir1/dir2")); // the following assertions fails: assertThat(actual).hasParent(fs.getPath("/dir1")); // ... and this one too as expected path is not canonicalized. assertThat(actual).hasParentRaw(fs.getPath("/dir1/dir3/../dir2"));
- Parameters:
expected
- the expected parent path- Returns:
- self
- Throws:
NullPointerException
- if the given parent path is null.- See Also:
-
hasNoParent
Assert that the testedPath
has no parent.This assertion will first canonicalize the tested path before performing the test; if this is not what you want, use
hasNoParentRaw()
instead.Check that the tested path, after canonicalization, has no parent. See the class description for more information about canonicalization.
Examples:// fs is a Unix filesystem // the following assertion succeeds: assertThat(fs.getPath("/")).hasNoParent(); // this one too as path will be normalized to "/" assertThat(fs.getPath("/usr/..")).hasNoParent(); // the following assertions fail: assertThat(fs.getPath("/usr/lib")).hasNoParent(); assertThat(fs.getPath("/usr")).hasNoParent();
- Returns:
- self
- Throws:
PathsException
- failed to canonicalize the tested path- See Also:
-
hasNoParentRaw
Assert that the testedPath
has no parent.This assertion will not canonicalize the tested path before performing the test; if this is not what you want, use
hasNoParent()
instead.As canonicalization is not performed, this means the only criterion for this assertion's success is the path's components (its root and its name elements).
This may lead to surprising results. For instance, path
Examples:/usr/..
does have a parent, and this parent is/usr
.// fs is a Unix filesystem // the following assertions succeed: assertThat(fs.getPath("/")).hasNoParentRaw(); assertThat(fs.getPath("foo")).hasNoParentRaw(); // the following assertions fail: assertThat(fs.getPath("/usr/lib")).hasNoParentRaw(); assertThat(fs.getPath("/usr")).hasNoParentRaw(); // this one fails as canonicalization is not performed, leading to parent being /usr assertThat(fs.getPath("/usr/..")).hasNoParent();
- Returns:
- self
- See Also:
-
hasSize
Assert that the testedPath
has the given size in bytes.Note that the actual
Examples:Path
must exist and be a regular file.Path foxPath = Files.write(Paths.get("/fox.txt"), "The Quick Brown Fox.".getBytes()); // this assertion succeeds assertThat(foxPath).hasSize(20); // this assertion fails assertThat(foxPath).hasSize(3);
- Parameters:
expectedSize
- the expectedPath
file size in bytes- Returns:
this
assertion object- Throws:
AssertionError
- is the actualPath
isnull
.AssertionError
- if the actualPath
does not exist.AssertionError
- if the actualPath
is not a regular file.AssertionError
- if the actualPath
file size is not equal to the expected size.UncheckedIOException
- if any I/O error occurs.- Since:
- 3.21.0
-
startsWith
Assert that the testedPath
starts with the given path.This assertion will perform canonicalization of both the tested path and the path given as an argument; see class description for more details. If this is not what you want, use
startsWithRaw(Path)
instead.Checks that the given
Examples:Path
starts with another path. Note that the name components matter, not the string representation; this means that, for example,/home/foobar/baz
does not start with/home/foo
.// fs is a Unix filesystem final Path tested = fs.getPath("/home/joe/myfile"); // the following assertion succeeds: assertThat(tested).startsWith(fs.getPath("/home")); assertThat(tested).startsWith(fs.getPath("/home/")); assertThat(tested).startsWith(fs.getPath("/home/.")); // assertion succeeds because this path will be canonicalized to "/home/joe" assertThat(tested).startsWith(fs.getPath("/home/jane/../joe/.")); // the following assertion fails: assertThat(tested).startsWith(fs.getPath("/home/harry"));
- Parameters:
other
- the other path- Returns:
- self
- Throws:
NullPointerException
- if the given path is null.PathsException
- failed to canonicalize the tested path or the path given as an argument- See Also:
-
startsWithRaw
Assert that the testedPath
starts with the given path.This assertions does not perform canonicalization on either the tested path or the path given as an argument; see class description for more details. If this is not what you want, use
startsWith(Path)
instead.Checks that the given
Path
starts with another path, without performing canonicalization on its arguments. This means that the only criterion to determine whether a path starts with another is the tested path's, and the argument's, name elements.This may lead to some surprising results: for instance, path
Examples:/../home/foo
does not start with/home
since the first name element of the former (..
) is different from the first name element of the latter (home
).// fs is a Unix filesystem final Path tested = fs.getPath("/home/joe/myfile"); // the following assertion succeeds: assertThat(tested).startsWithRaw(fs.getPath("/home/joe")); // the following assertion fails: assertThat(tested).startsWithRaw(fs.getPath("/home/harry")); // .... and this one too as given path is not canonicalized assertThat(tested).startsWithRaw(fs.getPath("/home/joe/.."));
- Parameters:
other
- the other path- Returns:
- self
- Throws:
NullPointerException
- if the given path is null.- See Also:
-
endsWith
Assert that the testedPath
ends with the given path.This assertion will attempt to canonicalize the tested path and normalize the path given as an argument before performing the actual test.
Note that the criterion to determine success is determined by the path's name elements; therefore,
Examples:/home/foobar/baz
does not end withbar/baz
.// fs is a Unix filesystem. // the current directory is supposed to be /home. final Path tested = fs.getPath("/home/joe/myfile"); // as tested will be canonicalized, it could have been written: /home/jane/../joe/myfile // the following assertion succeeds: assertThat(tested).endsWith(fs.getPath("joe/myfile")); // the following assertions fail: assertThat(tested).endsWith(fs.getPath("joe/otherfile")); // this path will be normalized to joe/otherfile assertThat(tested).endsWith(fs.getPath("joe/myfile/../otherfile"));
- Parameters:
other
- the other path- Returns:
- self
- Throws:
NullPointerException
- if the given path is null.PathsException
- failed to canonicalize the tested path (see class description)- See Also:
-
endsWithRaw
Assert that the testedPath
ends with the given path.This assertion will not perform any canonicalization (on the tested path) or normalization (on the path given as an argument); see the class description for more details. If this is not what you want, use
endsWith(Path)
instead.This may lead to some surprising results; for instance, path
Examples:/home/foo
does not end withfoo/.
since the last name element of the former (foo
) is different from the last name element of the latter (.
).// fs is a Unix filesystem // the current directory is supposed to be /home. final Path tested = fs.getPath("/home/joe/myfile"); // The following assertion succeeds: assertThat(tested).endsWithRaw(fs.getPath("joe/myfile")); // But the following assertion fails: assertThat(tested).endsWithRaw(fs.getPath("harry/myfile")); // and this one too as the given path is not normalized assertThat(tested).endsWithRaw(fs.getPath("harry/../joe/myfile"));
- Parameters:
other
- the other path- Returns:
- self
- Throws:
NullPointerException
- if the given path is null.- See Also:
-
hasDigest
Verifies that the testedPath
digest (calculated with the specifiedMessageDigest
) is equal to the given one.Note that the
Path
must be readable.Examples:
// fs is a filesystem // assume that the current directory contains https://repo1.maven.org/maven2/org/assertj/assertj-core/2.9.0/assertj-core-2.9.0.jar. Path tested = Paths.get("assertj-core-2.9.0.jar"); // The following assertions succeed: assertThat(tested).hasDigest(MessageDigest.getInstance("SHA1"), new byte[]{92, 90, -28, 91, 88, -15, 32, 35, -127, 122, -66, 73, 36, 71, -51, -57, -111, 44, 26, 44}); assertThat(tested).hasDigest(MessageDigest.getInstance("MD5"), new byte[]{-36, -77, 1, 92, -46, -124, 71, 100, 76, -127, 10, -13, 82, -125, 44, 25}); // The following assertions fail: assertThat(tested).hasDigest(MessageDigest.getInstance("SHA1"), "93b9ced2ee5b3f0f4c8e640e77470dab031d4cad".getBytes()); assertThat(tested).hasDigest(MessageDigest.getInstance("MD5"), "3735dff8e1f9df0492a34ef075205b8f".getBytes());
- Parameters:
digest
- the MessageDigest used to calculate the digests.expected
- the expected binary content to compare the actualPath
's content to.- Returns:
this
assertion object.- Throws:
NullPointerException
- if the given algorithm isnull
.NullPointerException
- if the given digest isnull
.AssertionError
- if the actualPath
isnull
.AssertionError
- if the actualPath
does not exist.AssertionError
- if the actualPath
is not an file.AssertionError
- if the actualPath
is not readable.UncheckedIOException
- if any I/O error occurs.AssertionError
- if the content of the testedPath
's digest is not equal to the given one.- Since:
- 3.11.0
-
hasDigest
Verifies that the testedPath
digest (calculated with the specifiedMessageDigest
) is equal to the given one.Note that the
Path
must be readable.Examples:
// fs is a filesystem // assume that the current directory contains https://repo1.maven.org/maven2/org/assertj/assertj-core/2.9.0/assertj-core-2.9.0.jar. Path tested = Paths.get("assertj-core-2.9.0.jar"); // The following assertions succeed: assertThat(tested).hasDigest(MessageDigest.getInstance("SHA1"), "5c5ae45b58f12023817abe492447cdc7912c1a2c"); assertThat(tested).hasDigest(MessageDigest.getInstance("MD5"), "dcb3015cd28447644c810af352832c19"); // The following assertions fail: assertThat(tested).hasDigest(MessageDigest.getInstance("SHA1"), "93b9ced2ee5b3f0f4c8e640e77470dab031d4cad"); assertThat(tested).hasDigest(MessageDigest.getInstance("MD5"), "3735dff8e1f9df0492a34ef075205b8f");
- Parameters:
digest
- the MessageDigest used to calculate the digests.expected
- the expected binary content to compare the actualPath
's content to.- Returns:
this
assertion object.- Throws:
NullPointerException
- if the given algorithm isnull
.NullPointerException
- if the given digest isnull
.AssertionError
- if the actualPath
isnull
.AssertionError
- if the actualPath
does not exist.AssertionError
- if the actualPath
is not an file.AssertionError
- if the actualPath
is not readable.UncheckedIOException
- if any I/O error occurs.AssertionError
- if the content of the testedPath
's digest is not equal to the given one.- Since:
- 3.11.0
-
hasDigest
Verifies that the testedPath
digest (calculated with the specified algorithm) is equal to the given one.Note that the
Path
must be readable.Examples:
// fs is a filesystem // assume that the current directory contains https://repo1.maven.org/maven2/org/assertj/assertj-core/2.9.0/assertj-core-2.9.0.jar. Path tested = Paths.get("assertj-core-2.9.0.jar"); // The following assertions succeed: assertThat(tested).hasDigest("SHA1", new byte[]{92, 90, -28, 91, 88, -15, 32, 35, -127, 122, -66, 73, 36, 71, -51, -57, -111, 44, 26, 44}); assertThat(tested).hasDigest("MD5", new byte[]{-36, -77, 1, 92, -46, -124, 71, 100, 76, -127, 10, -13, 82, -125, 44, 25}); // The following assertions fail: assertThat(tested).hasDigest("SHA1", "93b9ced2ee5b3f0f4c8e640e77470dab031d4cad".getBytes()); assertThat(tested).hasDigest("MD5", "3735dff8e1f9df0492a34ef075205b8f".getBytes());
- Parameters:
algorithm
- the algorithm used to calculate the digests to compare.expected
- the expected binary content to compare the actualPath
's content to.- Returns:
this
assertion object.- Throws:
NullPointerException
- if the given algorithm isnull
.NullPointerException
- if the given digest isnull
.AssertionError
- if the actualPath
isnull
.AssertionError
- if the actualPath
does not exist.AssertionError
- if the actualPath
is not an file.AssertionError
- if the actualPath
is not readable.UncheckedIOException
- if any I/O error occurs.AssertionError
- if the content of the testedPath
's digest is not equal to the given one.- Since:
- 3.11.0
-
hasDigest
Verifies that the testedPath
digest (calculated with the specified algorithm) is equal to the given one.Note that the
Path
must be readable.Examples:
// assume that assertj-core-2.9.0.jar was downloaded from https://repo1.maven.org/maven2/org/assertj/assertj-core/2.9.0/assertj-core-2.9.0.jar Path tested = Paths.get("assertj-core-2.9.0.jar"); // The following assertions succeed: assertThat(tested).hasDigest("SHA1", "5c5ae45b58f12023817abe492447cdc7912c1a2c"); assertThat(tested).hasDigest("MD5", "dcb3015cd28447644c810af352832c19"); // The following assertions fail: assertThat(tested).hasDigest("SHA1", "93b9ced2ee5b3f0f4c8e640e77470dab031d4cad"); assertThat(tested).hasDigest("MD5", "3735dff8e1f9df0492a34ef075205b8f");
- Parameters:
algorithm
- the algorithm used to calculate the digests.expected
- the expected digest to compare the actualPath
's digest to.- Returns:
this
assertion object.- Throws:
NullPointerException
- if the given algorithm isnull
.NullPointerException
- if the given digest isnull
.AssertionError
- if the actualPath
isnull
.AssertionError
- if the actualPath
does not exist.AssertionError
- if the actualPath
is not an file.AssertionError
- if the actualPath
is not readable.UncheckedIOException
- if any I/O error occurs.AssertionError
- if the content of the testedPath
's digest is not equal to the given one.- Since:
- 3.11.0
-
isDirectoryContaining
Verify that the actualPath
is a directory containing at least one file matching the givenPredicate<Path>
.Note that the actual
Path
must exist and be a directory.Given the following directory structure:
Here are some assertions examples:/root/ /root/sub-dir-1/ /root/sub-dir-1/file-1.ext /root/sub-dir-1/file-2.ext /root/sub-file-1.ext /root/sub-file-2.ext
Path root = Paths.get("root"); // The following assertions succeed: assertThat(root).isDirectoryContaining(path -> path.getFileName().toString().startsWith("sub-dir")) .isDirectoryContaining(path -> path.getFileName().toString().startsWith("sub-file")) .isDirectoryContaining(path -> path.getFileName().toString().endsWith(".ext")) .isDirectoryContaining(Files::isDirectory); // The following assertions fail: assertThat(root).isDirectoryContaining(file -> file.getFileName().toString().startsWith("dir")); assertThat(root).isDirectoryContaining(file -> file.getFileName().toString().endsWith(".bin"));
- Parameters:
filter
- the filter for files located insideactual
's directory.- Returns:
this
assertion object.- Throws:
NullPointerException
- if the given filter isnull
.AssertionError
- if actual isnull
.AssertionError
- if actual does not exist.AssertionError
- if actual is not a directory.AssertionError
- if actual does not contain any files matching the given predicate.- Since:
- 3.13.0
-
isDirectoryContaining
Verify that the actualPath
is a directory containing at least one file matching the givenString
interpreted as a path matcher (as perFileSystem.getPathMatcher(String)
).Note that the actual
Path
must exist and be a directory.Given the following directory structure:
Here are some assertions examples:/root/ /root/sub-dir-1/ /root/sub-dir-1/file-1.ext /root/sub-dir-1/file-2.ext /root/sub-file-1.ext /root/sub-file-2.ext
Path root = Paths.get("root"); // The following assertions succeed: assertThat(root).isDirectoryContaining("glob:**sub-dir*") .isDirectoryContaining("glob:**sub-file*") .isDirectoryContaining("glob:**.ext") .isDirectoryContaining("regex:.*ext") .isDirectoryContaining("glob:**.{ext,bin"); // The following assertions fail: assertThat(root).isDirectoryContaining("glob:**dir"); assertThat(root).isDirectoryContaining("glob:**.bin"); assertThat(root).isDirectoryContaining("glob:**.{java,class}");
- Parameters:
syntaxAndPattern
- the syntax and pattern forPathMatcher
as described inFileSystem.getPathMatcher(String)
.- Returns:
this
assertion object.- Throws:
NullPointerException
- if the given syntaxAndPattern isnull
.AssertionError
- if actual isnull
.AssertionError
- if actual does not exist.AssertionError
- if actual is not a directory.AssertionError
- if actual does not contain any files matching the given path matcher.- Since:
- 3.13.0
- See Also:
-
isDirectoryRecursivelyContaining
Verify that the actualPath
directory or any of its subdirectories (recursively) contains at least one file matching the givenString
interpreted as a path matcher (as perFileSystem.getPathMatcher(String)
).That methods performs the same assertion as
isDirectoryContaining(String syntaxAndPattern)
but recursively.Note that the actual
Path
must exist and be a directory.Examples given the following directory structure:
root |—— foo | |—— foobar | |—— foo-file-1.ext |—— foo-file-2.ext
Path root = Paths.get("root"); // The following assertions succeed: assertThat(root).isDirectoryRecursivelyContaining("glob:**foo") .isDirectoryRecursivelyContaining("glob:**ooba*") .isDirectoryRecursivelyContaining("glob:**file-1.ext") .isDirectoryRecursivelyContaining("regex:.*file-2.*") .isDirectoryRecursivelyContaining("glob:**.{ext,dummy}"); // The following assertions fail: assertThat(root).isDirectoryRecursivelyContaining("glob:**fooba"); assertThat(root).isDirectoryRecursivelyContaining("glob:**.bin"); assertThat(root).isDirectoryRecursivelyContaining("glob:**.{java,class}");
- Parameters:
syntaxAndPattern
- the syntax and pattern forPathMatcher
as described inFileSystem.getPathMatcher(String)
.- Returns:
this
assertion object.- Throws:
NullPointerException
- if the given syntaxAndPattern isnull
.AssertionError
- if actual isnull
.AssertionError
- if actual does not exist.AssertionError
- if actual is not a directory.AssertionError
- if actual does not contain recursively any files matching the given path matcher.- Since:
- 3.16.0
- See Also:
-
isDirectoryRecursivelyContaining
Verify that the actualPath
directory or any of its subdirectories (recursively) contains at least one file matching the givenPredicate<Path>
.That methods performs the same assertion as
isDirectoryContaining(Predicate filter)
but recursively.Note that the actual
Path
must exist and be a directory.Examples given the following directory structure:
Here are some assertions examples:root |—— foo | |—— foobar | |—— foo-file-1.ext |—— foo-file-2.ext
Path root = Paths.get("root"); // The following assertions succeed: assertThat(root).isDirectoryRecursivelyContaining(file -> file.getName().startsWith("foo-file-1")) .isDirectoryRecursivelyContaining(file -> file.getName().endsWith("file-2.ext")) .isDirectoryRecursivelyContaining(file -> file.getName().equals("foo")) .isDirectoryRecursivelyContaining(file -> file.getParentFile().getName().equals("foo")) // The following assertions fail: assertThat(root).isDirectoryRecursivelyContaining(file -> file.getName().equals("foo-file-1")) assertThat(root).isDirectoryRecursivelyContaining(file -> file.getName().equals("foo/foobar"));
- Parameters:
filter
- the filter for files located insideactual
's directory.- Returns:
this
assertion object.- Throws:
NullPointerException
- if the given filter isnull
.AssertionError
- if actual isnull
.AssertionError
- if actual does not exist.AssertionError
- if actual is not a directory.AssertionError
- if actual does not contain recursively any files matching the given predicate.- Since:
- 3.16.0
-
isDirectoryNotContaining
Verify that the actualPath
is a directory that does not contain any files matching the givenPredicate<Path>
.Note that the actual
Path
must exist and be a directory.Given the following directory structure:
Here are some assertions examples:/root/ /root/sub-dir-1/ /root/sub-dir-1/file-1.ext /root/sub-dir-1/file-2.ext /root/sub-file-1.ext /root/sub-file-2.ext
Path root = Paths.get("root"); // The following assertions succeed: assertThat(root).isDirectoryNotContaining(file -> file.getFileName().toString().startsWith("dir")) .isDirectoryNotContaining(file -> file.getFileName().toString().endsWith(".bin")); // The following assertions fail: assertThat(root).isDirectoryNotContaining(path -> path.getFileName().toString().startsWith("sub-dir")); assertThat(root).isDirectoryNotContaining(path -> path.getFileName().toString().startsWith("sub-file")); assertThat(root).isDirectoryNotContaining(path -> path.getFileName().toString().endsWith(".ext")); assertThat(root).isDirectoryNotContaining(Files::isDirectory);
- Parameters:
filter
- the filter for files located insideactual
's directory.- Returns:
this
assertion object.- Throws:
NullPointerException
- if the given filter isnull
.AssertionError
- if actual isnull
.AssertionError
- if actual does not exist.AssertionError
- if actual is not a directory.AssertionError
- if actual contains a file matching the given predicate.- Since:
- 3.13.0
-
isDirectoryNotContaining
Verify that the actualPath
is a directory that does not contain any files matching the givenString
interpreted as a path matcher (as perFileSystem.getPathMatcher(String)
).Note that the actual
Path
must exist and be a directory.Given the following directory structure:
Here are some assertions examples:/root/ /root/sub-dir-1/ /root/sub-dir-1/file-1.ext /root/sub-dir-1/file-2.ext /root/sub-file-1.ext /root/sub-file-2.ext
Path root = Paths.get("root"); // The following assertions succeed: assertThat(root).isDirectoryNotContaining("glob:**dir") .isDirectoryNotContaining("glob:**.bin") .isDirectoryNotContaining("regex:.*bin") .isDirectoryNotContaining("glob:**.{java,class}"); // The following assertions fail: assertThat(root).isDirectoryNotContaining("glob:**sub-dir*"); assertThat(root).isDirectoryNotContaining("glob:**sub-file*"); assertThat(root).isDirectoryNotContaining("glob:**.ext"); assertThat(root).isDirectoryNotContaining("regex:.*ext"); assertThat(root).isDirectoryNotContaining("glob:**.{ext,bin");
- Parameters:
syntaxAndPattern
- the syntax and pattern forPathMatcher
as described inFileSystem.getPathMatcher(String)
.- Returns:
this
assertion object.- Throws:
NullPointerException
- if the given syntaxAndPattern isnull
.AssertionError
- if actual isnull
.AssertionError
- if actual does not exist.AssertionError
- if actual is not a directory.AssertionError
- if actual contains a file matching the given path matcher.- Since:
- 3.13.0
- See Also:
-
isEmptyDirectory
Verify that the actualPath
is an empty directory.Note that the actual
Path
must exist and be a directory.Given the following directory structure:
Here are some assertions examples:/root/ /root/sub-dir-1/ /root/sub-dir-1/file-1.ext /root/sub-dir-1/file-2.ext /root/sub-dir-2/ /root/sub-file-1.ext /root/sub-file-2.ext
Path root = Paths.get("root"); // The following assertion succeeds: assertThat(root.resolve("sub-dir-2")).isEmptyDirectory(); // The following assertions fail: assertThat(root).isEmptyDirectory(); assertThat(root.resolve("sub-dir-1")).isEmptyDirectory();
- Returns:
this
assertion object.- Throws:
AssertionError
- if actual isnull
.AssertionError
- if actual does not exist.AssertionError
- if actual is not a directory.AssertionError
- if actual is not empty.- Since:
- 3.13.0
-
isNotEmptyDirectory
Verify that the actualPath
is a non empty directory.Note that the actual
Path
must exist and be a directory.Given the following directory structure:
Here are some assertions examples:/root/ /root/sub-dir-1/ /root/sub-dir-1/file-1.ext /root/sub-dir-1/file-2.ext /root/sub-dir-2/ /root/sub-file-1.ext /root/sub-file-2.ext
Path root = Paths.get("root"); // The following assertions succeed: assertThat(root).isNotEmptyDirectory(); assertThat(root.resolve("sub-dir-1")).isNotEmptyDirectory(); // The following assertion fails: assertThat(root.resolve("sub-dir-2")).isNotEmptyDirectory();
- Returns:
this
assertion object.- Throws:
AssertionError
- if actual isnull
.AssertionError
- if actual does not exist.AssertionError
- if actual is not a directory.AssertionError
- if actual is empty.- Since:
- 3.13.0
-
isEmptyFile
Verify that the actualPath
is an empty regular file.Note that the actual
Path
must exist and be a regular file.Given the following path structure:
Here are some assertions examples:/root/sub-dir-1/file-1.ext (no content) /root/sub-dir-1/file-2.ext (content)
Path withoutContent = Paths.get("/root/sub-dir-1/file-1.ext"); Path withContent = Paths.get("/root/sub-dir-1/file-2.ext"); // The following assertion succeeds: assertThat(withoutContent).isEmptyFile(); // The following assertion fails: assertThat(withContent).isEmptyFile();
- Returns:
this
assertion object.- Throws:
AssertionError
- if actual isnull
.AssertionError
- if actual does not exist.AssertionError
- if actual is not empty.- Since:
- 3.19.0
-
isNotEmptyFile
Verify that the actualPath
is a non-empty regular file.Note that the actual
Path
must exist and be a regular file.Given the following path structure:
Here are some assertions examples:/root/sub-dir-1/file-1.ext (no content) /root/sub-dir-1/file-2.ext (content)
Path withoutContent = Paths.get("/root/sub-dir-1/file-1.ext"); Path withContent = Paths.get("/root/sub-dir-1/file-2.ext"); // The following assertion succeeds: assertThat(withContent).isNotEmptyFile(); // The following assertion fails: assertThat(withoutContent).isNotEmptyFile();
- Returns:
this
assertion object.- Throws:
AssertionError
- if actual isnull
.AssertionError
- if actual does not exist.AssertionError
- if actual is empty.- Since:
- 3.19.0
-
binaryContent
ReturnsByteArray
assertions on the content of the actualPath
read.Example:
Path xFile = Files.write(Paths.get("xfile.txt"), "The Truth Is Out There".getBytes()); // assertion succeeds assertThat(xFile).binaryContent().isEqualTo("The Truth Is Out There".getBytes()); // assertion fails: assertThat(xFile).binaryContent().isEqualTo("Elsewhere".getBytes());
- Returns:
- a
AbstractByteArrayAssert
object with the binary content of the path. - Throws:
AssertionError
- if the actualPath
is not readable as perFiles.isReadable(Path)
.UncheckedIOException
- when failing to read the actualPath
.
-
content
Returns String assertions on the content of the actualPath
read with thedefault charset
.Example:
Path xFile = Files.write(Paths.get("xfile.txt"), "The Truth Is Out There".getBytes()); // assertion succeeds (default charset is used to read xFile content): assertThat(xFile).content().startsWith("The Truth Is "); // assertion fails: assertThat(xFile).content().contains("Elsewhere");
- Returns:
- a StringAssert object with the content of the actual
Path
read with the defaultCharset
. - Throws:
AssertionError
- if the actualPath
is not readable as perFiles.isReadable(Path)
.UncheckedIOException
- when failing to read the actualPath
.- Since:
- 3.21.0
-
content
Returns String assertions on the content of the actualPath
read with the givenCharset
.Example:
Path utf8Path = Files.write(Paths.get("utf8.txt"), "é à".getBytes()); // assertion succeeds: assertThat(utf8Path).content(StandardCharsets.UTF_8).endsWith("é à"); // assertion fails: assertThat(utf8Path).content(StandardCharsets.UTF_8).contains("e");
- Parameters:
charset
- theCharset
to use to read the actualPath
.- Returns:
- a
StringAssert
object with the content of the actualPath
read with the defaultCharset
. - Throws:
AssertionError
- if the actualPath
is not readable as perFiles.isReadable(Path)
.UncheckedIOException
- when failing to read the actualPath
.- Since:
- 3.21.0
-
hasExtension
Verifies that the actualPath
has given extension.Example:
Path path = Paths.get("file.java"); // assertion succeeds assertThat(path).hasExtension("java"); // assertion fails assertThat(path).hasExtension("png");
- Parameters:
expectedExtension
- the expected extension, without the'.'
.- Returns:
this
assertion object.- Throws:
NullPointerException
- if the expected extension isnull
.AssertionError
- if the actualPath
isnull
.AssertionError
- if the actualPath
is not a regular file.AssertionError
- if the actualPath
does not have the expected extension.- Since:
- 3.21.0
-
hasNoExtension
Verifies that the actualPath
has no extension.Example:
// assertions succeed assertThat(Paths.get("file")).hasNoExtension(); assertThat(Paths.get("file.")).hasNoExtension(); // assertion fails assertThat(Paths.get("file.txt")).hasNoExtension();
- Returns:
this
assertion object.- Throws:
AssertionError
- if the actualPath
isnull
.AssertionError
- if the actualPath
is not a regular file.AssertionError
- if the actualPath
does have an extension.
-
hasSameTextualContentAs(Path)
instead