Skip to content

Commit

Permalink
Add new directory assertions for file and path objects.
Browse files Browse the repository at this point in the history
- isDirectoryContaining / isDirectoryNotContaining
- isEmptyDirectory / isNotEmptyDirectory
  • Loading branch information
valery1707 authored and joel-costigliola committed May 26, 2019
1 parent 85f180c commit f4a6b96
Show file tree
Hide file tree
Showing 62 changed files with 3,521 additions and 191 deletions.
244 changes: 244 additions & 0 deletions src/main/java/org/assertj/core/api/AbstractFileAssert.java
Expand Up @@ -18,7 +18,9 @@
import java.io.File;
import java.io.UncheckedIOException;
import java.nio.charset.Charset;
import java.nio.file.FileSystem;
import java.security.MessageDigest;
import java.util.function.Predicate;

import org.assertj.core.internal.Files;
import org.assertj.core.util.CheckReturnValue;
Expand All @@ -38,6 +40,7 @@
* @author Olivier Demeijer
* @author Mikhail Mazursky
* @author Jean-Christophe Gay
* @author Valeriy Vyrva
*/
public abstract class AbstractFileAssert<SELF extends AbstractFileAssert<SELF>> extends AbstractAssert<SELF, File> {

Expand Down Expand Up @@ -722,4 +725,245 @@ public SELF hasDigest(String algorithm, String expected) {
files.assertHasDigest(info, actual, algorithm, expected);
return myself;
}

/**
* Verify that the actual {@code File} is a directory containing at least one file matching the given {@code Predicate<File>}.
* <p>
* Note that the actual {@link File} must exist and be a directory.
* <p>
* Examples:
* <pre><code class="java"> // Let's assume we have the following directory structure:
* // [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]
*
* File root = new File("root");
*
* // The following assertions succeed:
* assertThat(root).isDirectoryContaining(file -&gt; file.getName().startsWith("sub-dir"))
* .isDirectoryContaining(file -&gt; file.getName().startsWith("sub-file"))
* .isDirectoryContaining(file -&gt; file.getName().endsWith(".ext"))
* .isDirectoryContaining(File::isDirectory);
*
* // The following assertions fail:
* assertThat(root).isDirectoryContaining(file -&gt; file.getName().startsWith("dir"));
* assertThat(root).isDirectoryContaining(file -&gt; file.getName().endsWith(".bin")); </code></pre>
*
* @param filter the filter for files located inside {@code actual}'s directory.
* @return {@code this} assertion object.
* @throws NullPointerException if the given filter is {@code null}.
* @throws AssertionError if actual is {@code null}.
* @throws AssertionError if actual does not exist.
* @throws AssertionError if actual is not a directory.
* @throws AssertionError if actual does not contain any files matching the given predicate.
* @since 3.13.0
*/
public SELF isDirectoryContaining(Predicate<File> filter) {
files.assertIsDirectoryContaining(info, actual, filter);
return myself;
}

/**
* Verify that the actual {@code File} is a directory containing at least one file matching the given {@code String}
* interpreted as a path matcher (as per {@link FileSystem#getPathMatcher(String)}).
* <p>
* Note that the actual {@link File} must exist and be a directory.
* <p>
* Examples:
* <pre><code class="java"> // Let's assume we have the following directory structure:
* // [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]
*
* File root = new File("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}"); </code></pre>
*
* @param syntaxAndPattern the syntax and pattern for {@link java.nio.file.PathMatcher} as described in {@link FileSystem#getPathMatcher(String)}.
* @return {@code this} assertion object.
* @throws NullPointerException if the given syntaxAndPattern is {@code null}.
* @throws AssertionError if actual is {@code null}.
* @throws AssertionError if actual does not exist.
* @throws AssertionError if actual is not a directory.
* @throws AssertionError if actual does not contain any files matching the given path matcher.
* @see FileSystem#getPathMatcher(String)
* @since 3.13.0
*/
public SELF isDirectoryContaining(String syntaxAndPattern) {
files.assertIsDirectoryContaining(info, actual, syntaxAndPattern);
return myself;
}

/**
* Verify that the actual {@code File} is a directory that does not contain any files matching the given {@code Predicate<File>}.
* <p>
* Note that the actual {@link File} must exist and be a directory.
* <p>
* Examples:
* <pre><code class="java"> // Let's assume we have the following directory structure:
* // [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]
*
* File root = new File("root");
*
* // The following assertions succeed:
* assertThat(root).isDirectoryNotContaining(file -&gt; file.getName().startsWith("dir"))
* .isDirectoryNotContaining(file -&gt; file.getName().endsWith(".bin"));
*
* // The following assertions fail:
* assertThat(root).isDirectoryNotContaining(file -&gt; file.getName().startsWith("sub-dir"));
* assertThat(root).isDirectoryNotContaining(file -&gt; file.getName().startsWith("sub-file"));
* assertThat(root).isDirectoryNotContaining(file -&gt; file.getName().endsWith(".ext"));
* assertThat(root).isDirectoryNotContaining(File::isDirectory); </code></pre>
*
* @param filter the filter for files located inside {@code actual}'s directory.
* @return {@code this} assertion object.
* @throws NullPointerException if the given filter is {@code null}.
* @throws AssertionError if actual is {@code null}.
* @throws AssertionError if actual does not exist.
* @throws AssertionError if actual is not a directory.
* @throws AssertionError if actual contains a file matching the given predicate.
* @since 3.13.0
*/
public SELF isDirectoryNotContaining(Predicate<File> filter) {
files.assertIsDirectoryNotContaining(info, actual, filter);
return myself;
}

/**
* Verify that the actual {@code File} is a directory that does not contain any files matching the given {@code String}
* interpreted as a path matcher (as per {@link FileSystem#getPathMatcher(String)}).
* <p>
* Note that the actual {@link File} must exist and be a directory.
* <p>
* Examples:
* <pre><code class="java"> // Let's assume we have the following directory structure:
* // [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]
*
* File root = new File("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"); </code></pre>
*
* @param syntaxAndPattern the syntax and pattern for {@link java.nio.file.PathMatcher} as described in {@link FileSystem#getPathMatcher(String)}.
* @return {@code this} assertion object.
* @throws NullPointerException if the given syntaxAndPattern is {@code null}.
* @throws AssertionError if actual is {@code null}.
* @throws AssertionError if actual does not exist.
* @throws AssertionError if actual is not a directory.
* @throws AssertionError if actual contains a file matching the given path matcher.
* @see FileSystem#getPathMatcher(String)
* @since 3.13.0
*/
public SELF isDirectoryNotContaining(String syntaxAndPattern) {
files.assertIsDirectoryNotContaining(info, actual, syntaxAndPattern);
return myself;
}

/**
* Verify that the actual {@code File} is an empty directory.
* <p>
* Note that the actual {@link File} must exist and be a directory.
* <p>
* Examples:
* <pre><code class="java"> // Let's assume we have the following directory structure:
* // [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]
*
* File root = new File("root");
*
* // The following assertion succeeds:
* assertThat(new File(root, "sub-dir-2")).isEmptyDirectory();
*
* // The following assertions fail:
* assertThat(root).isEmptyDirectory();
* assertThat(new File(root, "sub-dir-1")).isEmptyDirectory(); </code></pre>
*
* @return {@code this} assertion object.
* @throws AssertionError if actual is {@code null}.
* @throws AssertionError if actual does not exist.
* @throws AssertionError if actual is not a directory.
* @throws AssertionError if actual is not empty.
* @since 3.13.0
*/
public SELF isEmptyDirectory() {
files.assertIsEmptyDirectory(info, actual);
return myself;
}

/**
* Verify that the actual {@code File} is a non empty directory.
* <p>
* Note that the actual {@link File} must exist and be a directory.
* <p>
* Examples:
* <pre><code class="java"> // assume that we have such directory structure:
* // [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]
*
* File root = new File("root");
*
* // The following assertions succeed:
* assertThat(root).isNotEmptyDirectory();
* assertThat(new File(root, "sub-dir-1")).isNotEmptyDirectory();
*
* // The following assertions fail:
* assertThat(new File(root, "sub-dir-2")).isNotEmptyDirectory(); </code></pre>
*
* @return {@code this} assertion object.
* @throws AssertionError if actual is {@code null}.
* @throws AssertionError if actual does not exist.
* @throws AssertionError if actual is not a directory.
* @throws AssertionError if actual is empty.
* @since 3.13.0
*/
public SELF isNotEmptyDirectory() {
files.assertIsNotEmptyDirectory(info, actual);
return myself;
}
}

0 comments on commit f4a6b96

Please sign in to comment.