SELF
- the "self" type of this assertion class. Please read "Emulating 'self types' using Java Generics to simplify fluent API implementation"
for more details.public abstract class AbstractFileAssert<SELF extends AbstractFileAssert<SELF>> extends AbstractAssert<SELF,File>
File
s.actual, info, myself, throwUnsupportedExceptionOnEquals
Constructor and Description |
---|
AbstractFileAssert(File actual,
Class<?> selfType) |
Modifier and Type | Method and Description |
---|---|
SELF |
canRead()
Verifies that the actual
File can be read by the application. |
SELF |
canWrite()
Verifies that the actual
File can be modified by the application. |
SELF |
doesNotExist()
Verifies that the actual
File does not exist. |
SELF |
exists()
Verifies that the actual
File exists, regardless it's a file or directory. |
SELF |
hasBinaryContent(byte[] expected)
Verifies that the binary content of the actual
File is exactly equal to the given one. |
SELF |
hasContent(String expected)
Verifies that the text content of the actual
File is exactly equal to the given one.The charset to use when reading the file should be provided with usingCharset(Charset) or
usingCharset(String) prior to calling this method; if not, the platform's default charset (as returned by
Charset.defaultCharset() ) will be used. |
SELF |
hasContentEqualTo(File expected)
Deprecated.
use
hasSameContentAs(File) instead |
SELF |
hasDigest(MessageDigest digest,
byte[] expected)
Verifies that the tested
File digest (calculated with the specified MessageDigest ) is equal to the given one. |
SELF |
hasDigest(MessageDigest digest,
String expected)
Verifies that the tested
File digest (calculated with the specified MessageDigest ) is equal to the given one. |
SELF |
hasDigest(String algorithm,
byte[] expected)
Verifies that the tested
File digest (calculated with the specified algorithm) is equal to the given one. |
SELF |
hasDigest(String algorithm,
String expected)
Verifies that the tested
File digest (calculated with the specified algorithm) is equal to the given one. |
SELF |
hasExtension(String expected)
Verifies that the actual
File has given extension. |
SELF |
hasName(String expected)
Verifies that the actual
File has given name. |
SELF |
hasNoParent()
Verifies that the actual
File does not have a parent. |
SELF |
hasParent(File expected)
Verifies that the actual
File has given parent. |
SELF |
hasParent(String expected)
|
SELF |
hasSameContentAs(File expected)
Verifies that the content of the actual
File is equal to the content of the given one. |
SELF |
hasSameContentAs(File expected,
Charset expectedCharset)
Verifies that the content of the actual
File is the same as the expected one, the expected File being read with the given charset while
the charset used to read the actual path can be provided with usingCharset(Charset) or
usingCharset(String) prior to calling this method; if not, the platform's default charset (as returned by
Charset.defaultCharset() ) will be used. |
SELF |
isAbsolute()
Verifies that the actual
File is an absolute path. |
SELF |
isDirectory()
Verifies that the actual
File is an existing directory. |
SELF |
isFile()
Verifies that the actual
File is an existing file. |
SELF |
isRelative()
Verifies that the actual
File is a relative path. |
SELF |
usingCharset(Charset charset)
Specifies the charset to use for text-based assertions on the file's contents.
|
SELF |
usingCharset(String charsetName)
Specifies the name of the charset to use for text-based assertions on the file's contents.
|
as, as, asList, asString, describedAs, describedAs, descriptionText, doesNotHave, doesNotHaveSameClassAs, equals, failWithMessage, getWritableAssertionInfo, has, hashCode, hasSameClassAs, hasSameHashCodeAs, 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, newListAssertInstance, overridingErrorMessage, satisfies, satisfies, satisfiesAnyOf, satisfiesAnyOf, setCustomRepresentation, throwAssertionError, usingComparator, usingComparator, usingDefaultComparator, withFailMessage, withRepresentation, withThreadDumpOnError
public SELF exists()
File
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();
this
assertion object.AssertionError
- if the actual File
is null
.AssertionError
- if the actual File
does not exist.public SELF doesNotExist()
File
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();
this
assertion object.AssertionError
- if the actual File
is null
.AssertionError
- if the actual File
exists.public SELF isFile()
File
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();
this
assertion object.AssertionError
- if the actual File
is null
.AssertionError
- if the actual File
is not an existing file.public SELF isDirectory()
File
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();
this
assertion object.AssertionError
- if the actual File
is null
.AssertionError
- if the actual File
is not an existing file.public SELF isAbsolute()
File
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();
this
assertion object.AssertionError
- if the actual File
is null
.AssertionError
- if the actual File
is not an absolute path.public SELF isRelative()
File
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();
this
assertion object.AssertionError
- if the actual File
is null
.AssertionError
- if the actual File
is not a relative path.@Deprecated public SELF hasContentEqualTo(File expected)
hasSameContentAs(File)
insteadFile
is equal to the content of the given one.
The charset to use when reading the actual file can be provided with usingCharset(Charset)
or
usingCharset(String)
prior to calling this method; if not, the platform's default charset (as returned by
Charset.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"), StandardCharsets.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);
expected
- the given File
to compare the actual File
to.this
assertion object.NullPointerException
- if the given File
is null
.IllegalArgumentException
- if the given File
is not an existing file.AssertionError
- if the actual File
is null
.AssertionError
- if the actual File
is not an existing file.UncheckedIOException
- if an I/O error occurs.AssertionError
- if the content of the actual File
is not equal to the content of the given one.public SELF hasSameContentAs(File expected)
File
is equal to the content of the given one.
The charset to use when reading the actual file can be provided with usingCharset(Charset)
or
usingCharset(String)
prior to calling this method; if not, the platform's default charset (as returned by
Charset.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"), StandardCharsets.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);
expected
- the given File
to compare the actual File
to.this
assertion object.NullPointerException
- if the given File
is null
.IllegalArgumentException
- if the given File
is not an existing file.AssertionError
- if the actual File
is null
.AssertionError
- if the actual File
is not an existing file.UncheckedIOException
- if an I/O error occurs.AssertionError
- if the content of the actual File
is not equal to the content of the given one.public SELF hasSameContentAs(File expected, Charset expectedCharset)
File
is the same as the expected one, the expected File
being read with the given charset while
the charset used to read the actual path can be provided with usingCharset(Charset)
or
usingCharset(String)
prior to calling this method; if not, the platform's default charset (as returned by
Charset.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);
expected
- the given File
to compare the actual File
to.expectedCharset
- the Charset
used to read the content of the expected file.this
assertion object.NullPointerException
- if the given File
is null
.IllegalArgumentException
- if the given File
is not an existing file.AssertionError
- if the actual File
is null
.AssertionError
- if the actual File
is not an existing file.UncheckedIOException
- if an I/O error occurs.AssertionError
- if the content of the actual File
is not equal to the content of the given one.public SELF hasBinaryContent(byte[] expected)
File
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});
expected
- the expected binary content to compare the actual File
's content to.this
assertion object.NullPointerException
- if the given content is null
.AssertionError
- if the actual File
is null
.AssertionError
- if the actual File
is not an existing file.UncheckedIOException
- if an I/O error occurs.AssertionError
- if the content of the actual File
is not equal to the given binary content.public SELF usingCharset(String charsetName)
charsetName
- the name of the charset to use.this
assertion object.IllegalArgumentException
- if the given encoding is not supported on this platform.public SELF usingCharset(Charset charset)
charset
- the charset to use.this
assertion object.NullPointerException
- if the given charset is null
.public SELF hasContent(String expected)
File
is exactly equal to the given one.usingCharset(Charset)
or
usingCharset(String)
prior to calling this method; if not, the platform's default charset (as returned by
Charset.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");
expected
- the expected text content to compare the actual File
's content to.this
assertion object.NullPointerException
- if the given content is null
.AssertionError
- if the actual File
is null
.AssertionError
- if the actual File
is not an existing file.UncheckedIOException
- if an I/O error occurs.AssertionError
- if the content of the actual File
is not equal to the given content.public SELF canWrite()
File
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();
this
assertion object.AssertionError
- if the actual File
is null
.AssertionError
- if the actual File
can not be modified by the application.public SELF canRead()
File
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();
this
assertion object.AssertionError
- if the actual File
is null
.AssertionError
- if the actual File
can not be read by the application.public SELF hasParent(File expected)
File
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"));
expected
- the expected parent File
.this
assertion object.NullPointerException
- if the expected parent File
is null
.UncheckedIOException
- if an I/O error occurs.AssertionError
- if the actual File
is null
.AssertionError
- if the actual File
parent is not equal to the expected one.parent definition.
public SELF hasParent(String expected)
hasParent(java.io.File)
but takes care of converting given String
as File
for you
Example:
File xFile = new File("mulder/xFile");
// assertion will pass
assertThat(xFile).hasParent("mulder");
// assertion will fail
assertThat(xFile).hasParent("scully");
expected
- the expected parent file path.this
assertion object.public SELF hasExtension(String expected)
File
has given extension.
Example:
File xFile = new File("xFile.java");
// assertion will pass
assertThat(xFile).hasExtension("java");
// assertion will fail
assertThat(xFile).hasExtension("png");
expected
- the expected extension, it does not contains the '.'
this
assertion object.NullPointerException
- if the expected extension is null
.AssertionError
- if the actual File
is null
.AssertionError
- if the actual File
is not a file (ie a directory).AssertionError
- if the actual File
does not have the expected extension.public SELF hasName(String expected)
File
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");
expected
- the expected File
name.this
assertion object.NullPointerException
- if the expected name is null
.AssertionError
- if the actual File
is null
.AssertionError
- if the actual File
does not have the expected name.name definition.
public SELF hasNoParent()
File
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();
this
assertion object.AssertionError
- if the actual File
is null
.AssertionError
- if the actual File
has a parent.public SELF hasDigest(MessageDigest digest, byte[] expected)
File
digest (calculated with the specified MessageDigest
) is equal to the given one.
Note that the File
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
File tested = new File("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());
digest
- the MessageDigest used to calculate the digests.expected
- the expected binary content to compare the actual File
's content to.this
assertion object.NullPointerException
- if the given algorithm is null
.NullPointerException
- if the given digest is null
.AssertionError
- if the actual File
is null
.AssertionError
- if the actual File
does not exist.AssertionError
- if the actual File
is not an file.AssertionError
- if the actual File
is not readable.UncheckedIOException
- if an I/O error occurs.AssertionError
- if the content of the tested File
's digest is not equal to the given one.public SELF hasDigest(MessageDigest digest, String expected)
File
digest (calculated with the specified MessageDigest
) is equal to the given one.
Note that the File
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
File tested = new File("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");
digest
- the MessageDigest used to calculate the digests.expected
- the expected binary content to compare the actual File
's content to.this
assertion object.NullPointerException
- if the given algorithm is null
.NullPointerException
- if the given digest is null
.AssertionError
- if the actual File
is null
.AssertionError
- if the actual File
does not exist.AssertionError
- if the actual File
is not an file.AssertionError
- if the actual File
is not readable.UncheckedIOException
- if an I/O error occurs.AssertionError
- if the content of the tested File
's digest is not equal to the given one.public SELF hasDigest(String algorithm, byte[] expected)
File
digest (calculated with the specified algorithm) is equal to the given one.
Note that the File
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
File tested = new File("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());
algorithm
- the algorithm used to calculate the digests.expected
- the expected digest to compare the actual File
's digest to.this
assertion object.NullPointerException
- if the given algorithm is null
.NullPointerException
- if the given digest is null
.AssertionError
- if the actual File
is null
.AssertionError
- if the actual File
does not exist.AssertionError
- if the actual File
is not an file.AssertionError
- if the actual File
is not readable.UncheckedIOException
- if any I/O error occurs.AssertionError
- if the content of the tested File
's digest is not equal to the given one.public SELF hasDigest(String algorithm, String expected)
File
digest (calculated with the specified algorithm) is equal to the given one.
Note that the File
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
File tested = new File("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");
algorithm
- the algorithm used to calculate the digests.expected
- the expected digest to compare the actual File
's digest to.this
assertion object.NullPointerException
- if the given algorithm is null
.NullPointerException
- if the given digest is null
.AssertionError
- if the actual File
is null
.AssertionError
- if the actual File
does not exist.AssertionError
- if the actual File
is not an file.AssertionError
- if the actual File
is not readable.UncheckedIOException
- if any I/O error occurs.AssertionError
- if the content of the tested File
's digest is not equal to the given one.Copyright © 2014–2019 AssertJ. All rights reserved.