public SELF exists() { files.assertExists(info, actual); return myself; }
/** * Verifies that the actual {@code File} is an existing file. * <p> * Example: * <pre><code class='java'> 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();</code></pre> * * @return {@code this} assertion object. * @throws AssertionError if the actual {@code File} is {@code null}. * @throws AssertionError if the actual {@code File} is not an existing file. */ public SELF isFile() { files.assertIsFile(info, actual); return myself; }
/** * Verifies that the actual {@code File} can be read by the application. * <p> * Example: * <pre><code class='java'> 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();</code></pre> * * @return {@code this} assertion object. * @throws AssertionError if the actual {@code File} is {@code null}. * @throws AssertionError if the actual {@code File} can not be read by the application. */ public SELF canRead() { files.assertCanRead(info, actual); return myself; }
public void assertHasDigest(AssertionInfo info, File actual, MessageDigest digest, byte[] expected) { checkNotNull(digest, "The message digest algorithm should not be null"); checkNotNull(expected, "The binary representation of digest to compare to should not be null"); assertExists(info, actual); assertIsFile(info, actual); assertCanRead(info, actual); try (InputStream actualStream = nioFilesWrapper.newInputStream(actual.toPath())) { DigestDiff digestDiff = digestDiff(actualStream, digest, expected); if (digestDiff.digestsDiffer()) throw failures.failure(info, shouldHaveDigest(actual, digestDiff)); } catch (IOException e) { throw new UncheckedIOException(format("Unable to calculate digest of path:<%s>", actual), e); } }
/** * Asserts that the given {@code File} has the given extension. * * @param info contains information about the assertion. * @param actual the given file. * @param expected the expected extension, it does not contains the {@code '.'} * @throws NullPointerException if the expected extension is {@code null}. * @throws AssertionError if the actual {@code File} is {@code null}. * @throws AssertionError if the actual {@code File} is not a file (ie a directory). * @throws AssertionError if the actual {@code File} does not have the expected extension. */ public void assertHasExtension(AssertionInfo info, File actual, String expected) { checkNotNull(expected, "The expected extension should not be null."); assertIsFile(info, actual); String actualExtension = getFileExtension(actual); if (expected.equals(actualExtension)) return; throw failures.failure(info, shouldHaveExtension(actual, actualExtension, expected)); }
verifyIsFile(expected); assertIsFile(info, actual); try { List<Delta<String>> diffs = diff.diff(actual, actualCharset, expected, expectedCharset);
private boolean commonCheckThatLogFileAssertionSucceeds(AssertionInfo info, File actual, Object[] sequence) { checkIsNotNull(sequence); assertNotNull(info, actual); files.assertIsFile(info, actual); files.assertExists(info, actual); // if both actual and values are empty, then assertion passes. if (FileUtils.sizeOf(actual) == 0 && sequence.length == 0) { return true; } failIfEmptySinceActualIsNotEmpty(sequence); return false; }
/** * Verifies that the actual {@code File} does not have a parent. * * <p> * Example: * <pre><code class='java'> File xFile = new File("somewhere/xFile.java"); * File xDirectory = new File("xDirectory"); * * // assertion will pass * assertThat(xDirectory).hasNoParent(); * * // assertion will fail * assertThat(xFile).hasNoParent();</code></pre> * * @return {@code this} assertion object. * * @throws AssertionError if the actual {@code File} is {@code null}. * @throws AssertionError if the actual {@code File} has a parent. */ public SELF hasNoParent() { files.assertHasNoParent(info, actual); return myself; }
/** * Verifies that the actual {@code File} can be modified by the application. * <p> * Example: * <pre><code class='java'> 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();</code></pre> * * @return {@code this} assertion object. * @throws AssertionError if the actual {@code File} is {@code null}. * @throws AssertionError if the actual {@code File} can not be modified by the application. */ public SELF canWrite() { files.assertCanWrite(info, actual); return myself; }
/** * Verifies that the binary content of the actual {@code File} is <b>exactly</b> equal to the given one. * <p> * Example: * <pre><code class='java'> 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});</code></pre> * * @param expected the expected binary content to compare the actual {@code File}'s content to. * @return {@code this} assertion object. * @throws NullPointerException if the given content is {@code null}. * @throws AssertionError if the actual {@code File} is {@code null}. * @throws AssertionError if the actual {@code File} is not an existing file. * @throws UncheckedIOException if an I/O error occurs. * @throws AssertionError if the content of the actual {@code File} is not equal to the given binary content. */ public SELF hasBinaryContent(byte[] expected) { files.assertHasBinaryContent(info, actual, expected); return myself; }
/** * Verifies that the actual {@code File} has given extension. * * <p> * Example: * <pre><code class='java'> File xFile = new File("xFile.java"); * * // assertion will pass * assertThat(xFile).hasExtension("java"); * * // assertion will fail * assertThat(xFile).hasExtension("png");</code></pre> * * @param expected the expected extension, it does not contains the {@code '.'} * @return {@code this} assertion object. * @throws NullPointerException if the expected extension is {@code null}. * @throws AssertionError if the actual {@code File} is {@code null}. * @throws AssertionError if the actual {@code File} is not a file (ie a directory). * @throws AssertionError if the actual {@code File} does not have the expected extension. * * @see <a href="http://en.wikipedia.org/wiki/Filename_extension">Filename extension</a> */ public SELF hasExtension(String expected) { files.assertHasExtension(info, actual, expected); return myself; }
/** * Verifies that the actual {@code File} has given name. * * <p> * Example: * <pre><code class='java'> 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");</code></pre> * * @param expected the expected {@code File} name. * @return {@code this} assertion object. * @throws NullPointerException if the expected name is {@code null}. * @throws AssertionError if the actual {@code File} is {@code null}. * @throws AssertionError if the actual {@code File} does not have the expected name. * * @see java.io.File#getName() name definition. */ public SELF hasName(String expected) { files.assertHasName(info, actual, expected); return myself; }
/** * Verifies that the actual {@code File} does not exist. * <p> * Example: * <pre><code class='java'> 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();</code></pre> * * @return {@code this} assertion object. * @throws AssertionError if the actual {@code File} is {@code null}. * @throws AssertionError if the actual {@code File} exists. */ public SELF doesNotExist() { files.assertDoesNotExist(info, actual); return myself; }
files.assertHasContent(info, actual, expected, charset); return myself;
public void assertHasDigest(AssertionInfo info, File actual, MessageDigest digest, byte[] expected) { checkNotNull(digest, "The message digest algorithm should not be null"); checkNotNull(expected, "The binary representation of digest to compare to should not be null"); assertExists(info, actual); assertIsFile(info, actual); assertCanRead(info, actual); try (InputStream actualStream = nioFilesWrapper.newInputStream(actual.toPath())) { DigestDiff digestDiff = digestDiff(actualStream, digest, expected); if (digestDiff.digestsDiffer()) throw failures.failure(info, shouldHaveDigest(actual, digestDiff)); } catch (IOException e) { throw new UncheckedIOException(format("Unable to calculate digest of path:<%s>", actual), e); } }
/** * Asserts that the given {@code File} has the given extension. * * @param info contains information about the assertion. * @param actual the given file. * @param expected the expected extension, it does not contains the {@code '.'} * @throws NullPointerException if the expected extension is {@code null}. * @throws AssertionError if the actual {@code File} is {@code null}. * @throws AssertionError if the actual {@code File} is not a file (ie a directory). * @throws AssertionError if the actual {@code File} does not have the expected extension. */ public void assertHasExtension(AssertionInfo info, File actual, String expected) { checkNotNull(expected, "The expected extension should not be null."); assertIsFile(info, actual); String actualExtension = getFileExtension(actual); if (expected.equals(actualExtension)) return; throw failures.failure(info, shouldHaveExtension(actual, actualExtension, expected)); }
verifyIsFile(expected); assertIsFile(info, actual); try { List<Delta<String>> diffs = diff.diff(actual, actualCharset, expected, expectedCharset);
/** * Verifies that the actual {@code File} does not have a parent. * * <p> * Example: * <pre><code class='java'> File xFile = new File("somewhere/xFile.java"); * File xDirectory = new File("xDirectory"); * * // assertion will pass * assertThat(xDirectory).hasNoParent(); * * // assertion will fail * assertThat(xFile).hasNoParent();</code></pre> * * @return {@code this} assertion object. * * @throws AssertionError if the actual {@code File} is {@code null}. * @throws AssertionError if the actual {@code File} has a parent. */ public SELF hasNoParent() { files.assertHasNoParent(info, actual); return myself; }
/** * Verifies that the actual {@code File} can be modified by the application. * <p> * Example: * <pre><code class='java'> 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();</code></pre> * * @return {@code this} assertion object. * @throws AssertionError if the actual {@code File} is {@code null}. * @throws AssertionError if the actual {@code File} can not be modified by the application. */ public SELF canWrite() { files.assertCanWrite(info, actual); return myself; }
/** * Verifies that the binary content of the actual {@code File} is <b>exactly</b> equal to the given one. * <p> * Example: * <pre><code class='java'> 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});</code></pre> * * @param expected the expected binary content to compare the actual {@code File}'s content to. * @return {@code this} assertion object. * @throws NullPointerException if the given content is {@code null}. * @throws AssertionError if the actual {@code File} is {@code null}. * @throws AssertionError if the actual {@code File} is not an existing file. * @throws UncheckedIOException if an I/O error occurs. * @throws AssertionError if the content of the actual {@code File} is not equal to the given binary content. */ public SELF hasBinaryContent(byte[] expected) { files.assertHasBinaryContent(info, actual, expected); return myself; }