Package org.assertj.core.api
Class AbstractFileAssert<SELF extends AbstractFileAssert<SELF>>
- java.lang.Object
-
- org.assertj.core.api.AbstractAssert<SELF,java.io.File>
-
- org.assertj.core.api.AbstractFileAssert<SELF>
-
- Type Parameters:
SELF
- the "self" type of this assertion class. Please read "Emulating 'self types' using Java Generics to simplify fluent API implementation" for more details.
- All Implemented Interfaces:
Assert<SELF,java.io.File>
,Descriptable<SELF>
,ExtensionPoints<SELF,java.io.File>
- Direct Known Subclasses:
FileAssert
public abstract class AbstractFileAssert<SELF extends AbstractFileAssert<SELF>> extends AbstractAssert<SELF,java.io.File>
Base class for all implementations of assertions forFile
s.
-
-
Field Summary
Fields Modifier and Type Field Description (package private) java.nio.charset.Charset
charset
(package private) Files
files
-
Fields inherited from class org.assertj.core.api.AbstractAssert
actual, conditions, info, myself, objects
-
-
Constructor Summary
Constructors Constructor Description AbstractFileAssert(java.io.File actual, java.lang.Class<?> selfType)
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description SELF
canRead()
Verifies that the actualFile
can be read by the application.SELF
canWrite()
Verifies that the actualFile
can be modified by the application.SELF
doesNotExist()
Verifies that the actualFile
does not exist.SELF
exists()
Verifies that the actualFile
exists, regardless it's a file or directory.SELF
hasBinaryContent(byte[] expected)
Verifies that the binary content of the actualFile
is exactly equal to the given one.SELF
hasContent(java.lang.String expected)
Verifies that the text content of the actualFile
is exactly equal to the given one.
The charset to use when reading the file 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.SELF
hasContentEqualTo(java.io.File expected)
Deprecated.usehasSameContentAs(File)
insteadSELF
hasExtension(java.lang.String expected)
Verifies that the actualFile
has given extension.SELF
hasName(java.lang.String expected)
Verifies that the actualFile
has given name.SELF
hasNoParent()
Verifies that the actualFile
does not have a parent.SELF
hasParent(java.io.File expected)
Verifies that the actualFile
has given parent.SELF
hasParent(java.lang.String expected)
SELF
hasSameContentAs(java.io.File expected)
Verifies that the content of the actualFile
is equal to the content of the given one.SELF
hasSameContentAs(java.io.File expected, java.nio.charset.Charset expectedCharset)
Verifies that the content of the actualFile
is the same as the expected one, the expectedFile
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.SELF
isAbsolute()
Verifies that the actualFile
is an absolute path.SELF
isDirectory()
Verifies that the actualFile
is an existing directory.SELF
isFile()
Verifies that the actualFile
is an existing file.SELF
isRelative()
Verifies that the actualFile
is a relative path.SELF
usingCharset(java.lang.String charsetName)
Specifies the name of the charset to use for text-based assertions on the file's contents.SELF
usingCharset(java.nio.charset.Charset charset)
Specifies the charset to use for text-based assertions on the file's contents.-
Methods inherited from class org.assertj.core.api.AbstractAssert
as, as, asList, asString, describedAs, describedAs, descriptionText, doesNotHave, doesNotHaveSameClassAs, equals, failWithMessage, getWritableAssertionInfo, has, hashCode, hasSameClassAs, hasToString, inBinary, inHexadecimal, is, isEqualTo, isExactlyInstanceOf, isIn, isIn, isInstanceOf, isInstanceOfAny, isInstanceOfSatisfying, isNot, isNotEqualTo, isNotExactlyInstanceOf, isNotIn, isNotIn, isNotInstanceOf, isNotInstanceOfAny, isNotNull, isNotOfAnyClassIn, isNotSameAs, isNull, isOfAnyClassIn, isSameAs, matches, matches, overridingErrorMessage, satisfies, setCustomRepresentation, throwAssertionError, usingComparator, usingDefaultComparator, withFailMessage, withRepresentation, withThreadDumpOnError
-
-
-
-
Field Detail
-
files
Files files
-
charset
java.nio.charset.Charset charset
-
-
Method Detail
-
exists
public SELF exists()
Verifies that the actualFile
exists, regardless it's a file or directory.Example:
File tmpFile = File.createTempFile("tmp", "txt"); File tmpDir = Files.createTempDirectory("tmpDir").toFile(); // assertions will pass assertThat(tmpFile).exists(); assertThat(tmpDir).exists(); tmpFile.delete(); tmpDir.delete(); // assertions will fail assertThat(tmpFile).exists(); assertThat(tmpDir).exists();
- Returns:
this
assertion object.- Throws:
java.lang.AssertionError
- if the actualFile
isnull
.java.lang.AssertionError
- if the actualFile
does not exist.
-
doesNotExist
public SELF doesNotExist()
Verifies that the actualFile
does not exist.Example:
File parentDir = Files.createTempDirectory("tmpDir").toFile(); File tmpDir = new File(parentDir, "subDir"); File tmpFile = new File(parentDir, "a.txt"); // assertions will pass assertThat(tmpDir).doesNotExist(); assertThat(tmpFile).doesNotExist(); tmpDir.mkdir(); tmpFile.createNewFile(); // assertions will fail assertThat(tmpFile).doesNotExist(); assertThat(tmpDir).doesNotExist();
- Returns:
this
assertion object.- Throws:
java.lang.AssertionError
- if the actualFile
isnull
.java.lang.AssertionError
- if the actualFile
exists.
-
isFile
public SELF isFile()
Verifies that the actualFile
is an existing file.Example:
File tmpFile = File.createTempFile("tmp", "txt"); // assertion will pass assertThat(tmpFile).isFile(); tmpFile.delete(); File tmpDir = Files.createTempDirectory("tmpDir").toFile(); // assertions will fail assertThat(tmpFile).isFile(); assertThat(tmpDir).isFile();
- Returns:
this
assertion object.- Throws:
java.lang.AssertionError
- if the actualFile
isnull
.java.lang.AssertionError
- if the actualFile
is not an existing file.
-
isDirectory
public SELF isDirectory()
Verifies that the actualFile
is an existing directory.Example:
File tmpDir = Files.createTempDirectory("tmpDir").toFile(); // assertion will pass assertThat(tmpDir).isDirectory(); tmpDir.delete(); File tmpFile = File.createTempFile("tmp", "txt"); // assertions will fail assertThat(tmpFile).isDirectory(); assertThat(tmpDir).isDirectory();
- Returns:
this
assertion object.- Throws:
java.lang.AssertionError
- if the actualFile
isnull
.java.lang.AssertionError
- if the actualFile
is not an existing file.
-
isAbsolute
public SELF isAbsolute()
Verifies that the actualFile
is an absolute path.Example:
File absoluteFile = File.createTempFile("tmp", "txt"); // assertions will pass assertThat(absoluteFile).isAbsolute(); File relativeFile = new File("./test"); // assertion will fail assertThat(relativeFile).isAbsolute();
- Returns:
this
assertion object.- Throws:
java.lang.AssertionError
- if the actualFile
isnull
.java.lang.AssertionError
- if the actualFile
is not an absolute path.
-
isRelative
public SELF isRelative()
Verifies that the actualFile
is a relative path.Example:
File relativeFile = new File("./test"); // assertion will pass assertThat(relativeFile).isRelative(); File absoluteFile = File.createTempFile("tmp", "txt"); // assertion will fail assertThat(absoluteFile).isRelative();
- Returns:
this
assertion object.- Throws:
java.lang.AssertionError
- if the actualFile
isnull
.java.lang.AssertionError
- if the actualFile
is not a relative path.
-
hasContentEqualTo
@Deprecated public SELF hasContentEqualTo(java.io.File expected)
Deprecated.usehasSameContentAs(File)
insteadVerifies that the content of the actualFile
is equal to the content of the given one. The charset to use when reading the actual file 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 File xFile = Files.write(Paths.get("xfile.txt"), "The Truth Is Out There".getBytes()).toFile(); File xFileClone = Files.write(Paths.get("xfile-clone.txt"), "The Truth Is Out There".getBytes()).toFile(); File xFileFrench = Files.write(Paths.get("xfile-french.txt"), "La Vérité Est Ailleurs".getBytes()).toFile(); // use UTF-8 charset File xFileUTF8 = Files.write(Paths.get("xfile-clone.txt"), Arrays.asList("The Truth Is Out There"), Charset.forName("UTF-8")).toFile(); // 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 givenFile
to compare the actualFile
to.- Returns:
this
assertion object.- Throws:
java.lang.NullPointerException
- if the givenFile
isnull
.java.lang.IllegalArgumentException
- if the givenFile
is not an existing file.java.lang.AssertionError
- if the actualFile
isnull
.java.lang.AssertionError
- if the actualFile
is not an existing file.RuntimeIOException
- if an I/O error occurs.java.lang.AssertionError
- if the content of the actualFile
is not equal to the content of the given one.
-
hasSameContentAs
public SELF hasSameContentAs(java.io.File expected)
Verifies that the content of the actualFile
is equal to the content of the given one. The charset to use when reading the actual file 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 File xFile = Files.write(Paths.get("xfile.txt"), "The Truth Is Out There".getBytes()).toFile(); File xFileClone = Files.write(Paths.get("xfile-clone.txt"), "The Truth Is Out There".getBytes()).toFile(); File xFileFrench = Files.write(Paths.get("xfile-french.txt"), "La Vérité Est Ailleurs".getBytes()).toFile(); // use UTF-8 charset File xFileUTF8 = Files.write(Paths.get("xfile-clone.txt"), Arrays.asList("The Truth Is Out There"), Charset.forName("UTF-8")).toFile(); // 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 givenFile
to compare the actualFile
to.- Returns:
this
assertion object.- Throws:
java.lang.NullPointerException
- if the givenFile
isnull
.java.lang.IllegalArgumentException
- if the givenFile
is not an existing file.java.lang.AssertionError
- if the actualFile
isnull
.java.lang.AssertionError
- if the actualFile
is not an existing file.RuntimeIOException
- if an I/O error occurs.java.lang.AssertionError
- if the content of the actualFile
is not equal to the content of the given one.
-
hasSameContentAs
public SELF hasSameContentAs(java.io.File expected, java.nio.charset.Charset expectedCharset)
Verifies that the content of the actualFile
is the same as the expected one, the expectedFile
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:
File fileUTF8 = Files.write(Paths.get("actual"), Collections.singleton("Gerçek"), StandardCharsets.UTF_8).toFile(); Charset turkishCharset = Charset.forName("windows-1254"); File fileTurkischCharset = Files.write(Paths.get("expected"), Collections.singleton("Gerçek"), turkishCharset).toFile(); // The following assertion succeeds: assertThat(fileUTF8).usingCharset(StandardCharsets.UTF_8).hasSameContentAs(fileTurkischCharset, turkishCharset); // The following assertion fails: assertThat(fileUTF8).usingCharset(StandardCharsets.UTF_8).hasSameContentAs(fileTurkischCharset, StandardCharsets.UTF_8);
- Parameters:
expected
- the givenFile
to compare the actualFile
to.expectedCharset
- theCharset
used to read the content of the expected file.- Returns:
this
assertion object.- Throws:
java.lang.NullPointerException
- if the givenFile
isnull
.java.lang.IllegalArgumentException
- if the givenFile
is not an existing file.java.lang.AssertionError
- if the actualFile
isnull
.java.lang.AssertionError
- if the actualFile
is not an existing file.RuntimeIOException
- if an I/O error occurs.java.lang.AssertionError
- if the content of the actualFile
is not equal to the content of the given one.
-
hasBinaryContent
public SELF hasBinaryContent(byte[] expected)
Verifies that the binary content of the actualFile
is exactly equal to the given one.Example:
File bin = File.createTempFile("tmp", "bin"); Files.write(bin.toPath(), new byte[] {1, 1}); // assertion will pass assertThat(bin).hasBinaryContent(new byte[] {1, 1}); // assertions will fail assertThat(bin).hasBinaryContent(new byte[] { }); assertThat(bin).hasBinaryContent(new byte[] {0, 0});
- Parameters:
expected
- the expected binary content to compare the actualFile
's content to.- Returns:
this
assertion object.- Throws:
java.lang.NullPointerException
- if the given content isnull
.java.lang.AssertionError
- if the actualFile
isnull
.java.lang.AssertionError
- if the actualFile
is not an existing file.RuntimeIOException
- if an I/O error occurs.java.lang.AssertionError
- if the content of the actualFile
is not equal to the given binary content.
-
usingCharset
public SELF usingCharset(java.lang.String charsetName)
Specifies the name of the charset to use for text-based assertions on the file's contents.- Parameters:
charsetName
- the name of the charset to use.- Returns:
this
assertion object.- Throws:
java.lang.IllegalArgumentException
- if the given encoding is not supported on this platform.
-
usingCharset
public SELF usingCharset(java.nio.charset.Charset charset)
Specifies the charset to use for text-based assertions on the file's contents.- Parameters:
charset
- the charset to use.- Returns:
this
assertion object.- Throws:
java.lang.NullPointerException
- if the given charset isnull
.
-
hasContent
public SELF hasContent(java.lang.String expected)
Verifies that the text content of the actualFile
is exactly equal to the given one.
The charset to use when reading the file 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.Example:
// use the default charset File xFile = Files.write(Paths.get("xfile.txt"), "The Truth Is Out There".getBytes()).toFile(); // 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"); File xFileTurkish = Files.write(Paths.get("xfile.turk"), Collections.singleton("Gerçek"), turkishCharset).toFile(); // The following assertion succeeds: assertThat(xFileTurkish).usingCharset(turkishCharset).hasContent("Gerçek"); // The following assertion fails : assertThat(xFileTurkish).usingCharset(StandardCharsets.UTF_8).hasContent("Gerçek");
- Parameters:
expected
- the expected text content to compare the actualFile
's content to.- Returns:
this
assertion object.- Throws:
java.lang.NullPointerException
- if the given content isnull
.java.lang.AssertionError
- if the actualFile
isnull
.java.lang.AssertionError
- if the actualFile
is not an existing file.RuntimeIOException
- if an I/O error occurs.java.lang.AssertionError
- if the content of the actualFile
is not equal to the given content.
-
canWrite
public SELF canWrite()
Verifies that the actualFile
can be modified by the application.Example:
File tmpFile = File.createTempFile("tmp", "txt"); File tmpDir = Files.createTempDirectory("tmp").toFile(); // assertions will pass assertThat(tmpFile).canWrite(); assertThat(tmpDir).canWrite(); tmpFile.setReadOnly(); tmpDir.setReadOnly(); // assertions will fail assertThat(tmpFile).canWrite(); assertThat(tmpDir).canWrite();
- Returns:
this
assertion object.- Throws:
java.lang.AssertionError
- if the actualFile
isnull
.java.lang.AssertionError
- if the actualFile
can not be modified by the application.
-
canRead
public SELF canRead()
Verifies that the actualFile
can be read by the application.Example:
File tmpFile = File.createTempFile("tmp", "txt"); File tmpDir = Files.createTempDirectory("tmp").toFile(); // assertions will pass assertThat(tmpFile).canRead(); assertThat(tmpDir).canRead(); tmpFile.setReadable(false); tmpDir.setReadable(false); // assertions will fail assertThat(tmpFile).canRead(); assertThat(tmpDir).canRead();
- Returns:
this
assertion object.- Throws:
java.lang.AssertionError
- if the actualFile
isnull
.java.lang.AssertionError
- if the actualFile
can not be read by the application.
-
hasParent
public SELF hasParent(java.io.File expected)
Verifies that the actualFile
has given parent.Example:
File xFile = new File("mulder/xFile"); // assertion will pass assertThat(xFile).hasParent(new File("mulder")); // assertion will fail assertThat(xFile).hasParent(new File("scully"));
- Parameters:
expected
- the expected parentFile
.- Returns:
this
assertion object.- Throws:
java.lang.NullPointerException
- if the expected parentFile
isnull
.RuntimeIOException
- if an I/O error occurs.java.lang.AssertionError
- if the actualFile
isnull
.java.lang.AssertionError
- if the actualFile
parent is not equal to the expected one.- See Also:
parent definition.
-
hasParent
public SELF hasParent(java.lang.String expected)
Same ashasParent(java.io.File)
but takes care of converting givenString
asFile
for youExample:
File xFile = new File("mulder/xFile"); // assertion will pass assertThat(xFile).hasParent("mulder"); // assertion will fail assertThat(xFile).hasParent("scully");
-
hasExtension
public SELF hasExtension(java.lang.String expected)
Verifies that the actualFile
has given extension.Example:
File xFile = new File("xFile.java"); // assertion will pass assertThat(xFile).hasExtension("java"); // assertion will fail assertThat(xFile).hasExtension("png");
- Parameters:
expected
- the expected extension, it does not contains the'.'
- Returns:
this
assertion object.- Throws:
java.lang.NullPointerException
- if the expected extension isnull
.java.lang.AssertionError
- if the actualFile
isnull
.java.lang.AssertionError
- if the actualFile
is not a file (ie a directory).java.lang.AssertionError
- if the actualFile
does not have the expected extension.- See Also:
- Filename extension
-
hasName
public SELF hasName(java.lang.String expected)
Verifies that the actualFile
has given name.Example:
File xFile = new File("somewhere/xFile.java"); File xDirectory = new File("somewhere/xDirectory"); // assertion will pass assertThat(xFile).hasName("xFile.java"); assertThat(xDirectory).hasName("xDirectory"); // assertion will fail assertThat(xFile).hasName("xFile"); assertThat(xDirectory).hasName("somewhere");
- Parameters:
expected
- the expectedFile
name.- Returns:
this
assertion object.- Throws:
java.lang.NullPointerException
- if the expected name isnull
.java.lang.AssertionError
- if the actualFile
isnull
.java.lang.AssertionError
- if the actualFile
does not have the expected name.- See Also:
name definition.
-
hasNoParent
public SELF hasNoParent()
Verifies that the actualFile
does not have a parent.Example:
File xFile = new File("somewhere/xFile.java"); File xDirectory = new File("xDirectory"); // assertion will pass assertThat(xDirectory).hasNoParent(); // assertion will fail assertThat(xFile).hasNoParent();
- Returns:
this
assertion object.- Throws:
java.lang.AssertionError
- if the actualFile
isnull
.java.lang.AssertionError
- if the actualFile
has a parent.
-
-