From d391e2019a23bbbdfd1ecd4035f9141adcc0b092 Mon Sep 17 00:00:00 2001 From: Stefano Cordio Date: Sun, 15 Aug 2021 12:47:08 +0200 Subject: [PATCH] Remove `MockPathsBaseTest` (#2292) --- .../assertj/core/api/AbstractPathAssert.java | 6 +- .../core/internal/NioFilesWrapper.java | 38 +--- .../java/org/assertj/core/internal/Paths.java | 176 ++++++----------- .../api/path/PathAssert_hasContent_Test.java | 16 +- .../PathAssert_hasSameContentAs_Test.java | 2 +- ...thAssert_hasSameTextualContentAs_Test.java | 4 +- .../assertj/core/internal/PathsBaseTest.java | 19 +- .../internal/paths/MockPathsBaseTest.java | 142 ------------- .../paths/Paths_assertEndsWithRaw_Test.java | 85 +++++--- .../paths/Paths_assertEndsWith_Test.java | 110 +++++++---- .../Paths_assertExistsNoFollowLinks_Test.java | 53 +++-- .../paths/Paths_assertExists_Test.java | 57 ++++-- .../Paths_assertHasBinaryContent_Test.java | 149 +++++++------- .../paths/Paths_assertHasContent_Test.java | 139 ------------- ...s_assertHasDigest_AlgorithmBytes_Test.java | 155 --------------- ..._assertHasDigest_AlgorithmString_Test.java | 155 --------------- ...aths_assertHasDigest_DigestBytes_Test.java | 155 --------------- ...ths_assertHasDigest_DigestString_Test.java | 147 -------------- ...ith_MessageDigest_and_Byte_array_Test.java | 163 +++++++++++++++ ...st_with_MessageDigest_and_String_Test.java | 163 +++++++++++++++ ...igest_with_String_and_Byte_array_Test.java | 178 +++++++++++++++++ ...HasDigest_with_String_and_String_Test.java | 178 +++++++++++++++++ .../paths/Paths_assertHasFileName_Test.java | 16 +- .../Paths_assertHasNoParentRaw_Test.java | 55 ++++-- .../paths/Paths_assertHasNoParent_Test.java | 72 ++++--- .../paths/Paths_assertHasParentRaw_Test.java | 107 ++++++---- .../paths/Paths_assertHasParent_Test.java | 154 ++++++++------- ...ths_assertHasSameBinaryContentAs_Test.java | 153 +++++++------- .../Paths_assertHasSameContentAs_Test.java | 163 --------------- ...hs_assertHasSameTextualContentAs_Test.java | 170 ++++++++++++++++ .../paths/Paths_assertHasSize_Test.java | 20 +- .../Paths_assertHasTextualContent_Test.java | 129 ++++++++++++ .../paths/Paths_assertIsAbsolute_Test.java | 46 +++-- .../paths/Paths_assertIsCanonical_Test.java | 62 +++--- ...tIsDirectoryContaining_Predicate_Test.java | 166 ---------------- ...ctoryContaining_SyntaxAndPattern_Test.java | 187 ------------------ ...rectoryContaining_with_Predicate_Test.java | 135 +++++++++++++ ...sDirectoryContaining_with_String_Test.java | 135 +++++++++++++ ...DirectoryNotContaining_Predicate_Test.java | 171 ---------------- ...ryNotContaining_SyntaxAndPattern_Test.java | 177 ----------------- ...toryNotContaining_with_Predicate_Test.java | 132 +++++++++++++ ...rectoryNotContaining_with_String_Test.java | 131 ++++++++++++ .../paths/Paths_assertIsDirectory_Test.java | 57 +++--- .../Paths_assertIsEmptyDirectory_Test.java | 81 ++++---- .../paths/Paths_assertIsEmptyFile_Test.java | 77 ++++---- .../paths/Paths_assertIsExecutable_Test.java | 60 +++--- .../paths/Paths_assertIsNormalized_Test.java | 78 ++++++-- .../Paths_assertIsNotEmptyDirectory_Test.java | 77 ++++---- .../Paths_assertIsNotEmptyFile_Test.java | 77 ++++---- .../paths/Paths_assertIsReadable_Test.java | 60 +++--- .../paths/Paths_assertIsRegularFile_Test.java | 58 +++--- .../paths/Paths_assertIsRelative_Test.java | 37 ++-- .../Paths_assertIsSymbolicLink_Test.java | 58 +++--- .../paths/Paths_assertIsWritable_Test.java | 57 +++--- .../paths/Paths_assertNotExists_Test.java | 52 +++-- .../paths/Paths_assertStartsWithRaw_Test.java | 86 +++++--- .../paths/Paths_assertStartsWith_Test.java | 131 +++++++----- 57 files changed, 2847 insertions(+), 2870 deletions(-) delete mode 100644 src/test/java/org/assertj/core/internal/paths/MockPathsBaseTest.java delete mode 100644 src/test/java/org/assertj/core/internal/paths/Paths_assertHasContent_Test.java delete mode 100644 src/test/java/org/assertj/core/internal/paths/Paths_assertHasDigest_AlgorithmBytes_Test.java delete mode 100644 src/test/java/org/assertj/core/internal/paths/Paths_assertHasDigest_AlgorithmString_Test.java delete mode 100644 src/test/java/org/assertj/core/internal/paths/Paths_assertHasDigest_DigestBytes_Test.java delete mode 100644 src/test/java/org/assertj/core/internal/paths/Paths_assertHasDigest_DigestString_Test.java create mode 100644 src/test/java/org/assertj/core/internal/paths/Paths_assertHasDigest_with_MessageDigest_and_Byte_array_Test.java create mode 100644 src/test/java/org/assertj/core/internal/paths/Paths_assertHasDigest_with_MessageDigest_and_String_Test.java create mode 100644 src/test/java/org/assertj/core/internal/paths/Paths_assertHasDigest_with_String_and_Byte_array_Test.java create mode 100644 src/test/java/org/assertj/core/internal/paths/Paths_assertHasDigest_with_String_and_String_Test.java delete mode 100644 src/test/java/org/assertj/core/internal/paths/Paths_assertHasSameContentAs_Test.java create mode 100644 src/test/java/org/assertj/core/internal/paths/Paths_assertHasSameTextualContentAs_Test.java create mode 100644 src/test/java/org/assertj/core/internal/paths/Paths_assertHasTextualContent_Test.java delete mode 100644 src/test/java/org/assertj/core/internal/paths/Paths_assertIsDirectoryContaining_Predicate_Test.java delete mode 100644 src/test/java/org/assertj/core/internal/paths/Paths_assertIsDirectoryContaining_SyntaxAndPattern_Test.java create mode 100644 src/test/java/org/assertj/core/internal/paths/Paths_assertIsDirectoryContaining_with_Predicate_Test.java create mode 100644 src/test/java/org/assertj/core/internal/paths/Paths_assertIsDirectoryContaining_with_String_Test.java delete mode 100644 src/test/java/org/assertj/core/internal/paths/Paths_assertIsDirectoryNotContaining_Predicate_Test.java delete mode 100644 src/test/java/org/assertj/core/internal/paths/Paths_assertIsDirectoryNotContaining_SyntaxAndPattern_Test.java create mode 100644 src/test/java/org/assertj/core/internal/paths/Paths_assertIsDirectoryNotContaining_with_Predicate_Test.java create mode 100644 src/test/java/org/assertj/core/internal/paths/Paths_assertIsDirectoryNotContaining_with_String_Test.java diff --git a/src/main/java/org/assertj/core/api/AbstractPathAssert.java b/src/main/java/org/assertj/core/api/AbstractPathAssert.java index 16c017eef4..a6ceeec132 100644 --- a/src/main/java/org/assertj/core/api/AbstractPathAssert.java +++ b/src/main/java/org/assertj/core/api/AbstractPathAssert.java @@ -166,7 +166,7 @@ public SELF hasSameContentAs(Path expected) { * @since 3.15 */ public SELF hasSameTextualContentAs(Path expected) { - paths.assertHasSameContentAs(info, actual, charset, expected, Charset.defaultCharset()); + paths.assertHasSameTextualContentAs(info, actual, charset, expected, Charset.defaultCharset()); return myself; } @@ -198,7 +198,7 @@ public SELF hasSameTextualContentAs(Path expected) { * @since 3.15 */ public SELF hasSameTextualContentAs(Path expected, Charset expectedCharset) { - paths.assertHasSameContentAs(info, actual, charset, expected, expectedCharset); + paths.assertHasSameTextualContentAs(info, actual, charset, expected, expectedCharset); return myself; } @@ -377,7 +377,7 @@ public SELF usingCharset(Charset charset) { * @throws AssertionError if the content of the actual {@code Path} is not equal to the given content. */ public SELF hasContent(String expected) { - paths.assertHasContent(info, actual, expected, charset); + paths.assertHasTextualContent(info, actual, expected, charset); return myself; } diff --git a/src/main/java/org/assertj/core/internal/NioFilesWrapper.java b/src/main/java/org/assertj/core/internal/NioFilesWrapper.java index 8f31c97f01..253cb2802d 100644 --- a/src/main/java/org/assertj/core/internal/NioFilesWrapper.java +++ b/src/main/java/org/assertj/core/internal/NioFilesWrapper.java @@ -22,54 +22,18 @@ import java.util.function.Predicate; /** - * Wrapper for {@link java.nio.file.Files} to test {@link Paths}. + * Wrapper for {@link java.nio.file.Files} to test methods throwing {@link IOException}. */ public class NioFilesWrapper { private static final NioFilesWrapper INSTANCE = new NioFilesWrapper(); - /** - * Returns the singleton instance of this class. - * @return the singleton instance of this class. - */ static NioFilesWrapper instance() { return INSTANCE; } private NioFilesWrapper() {} - public boolean isRegularFile(Path path) { - return Files.isRegularFile(path); - } - - public boolean isSymbolicLink(Path path) { - return Files.isSymbolicLink(path); - } - - public boolean isDirectory(Path path) { - return Files.isDirectory(path); - } - - public boolean exists(Path path, LinkOption... options) { - return Files.exists(path, options); - } - - public boolean notExists(Path path, LinkOption... options) { - return Files.notExists(path, options); - } - - public boolean isReadable(Path path) { - return Files.isReadable(path); - } - - public boolean isWritable(Path path) { - return Files.isWritable(path); - } - - public boolean isExecutable(Path path) { - return Files.isExecutable(path); - } - public InputStream newInputStream(Path path, OpenOption... options) throws IOException { return Files.newInputStream(path, options); } diff --git a/src/main/java/org/assertj/core/internal/Paths.java b/src/main/java/org/assertj/core/internal/Paths.java index 0dbd7e89d1..fc92bcd259 100644 --- a/src/main/java/org/assertj/core/internal/Paths.java +++ b/src/main/java/org/assertj/core/internal/Paths.java @@ -54,6 +54,7 @@ import java.io.UncheckedIOException; import java.nio.charset.Charset; import java.nio.file.DirectoryStream; +import java.nio.file.Files; import java.nio.file.LinkOption; import java.nio.file.Path; import java.nio.file.PathMatcher; @@ -64,7 +65,6 @@ import java.util.stream.Stream; import org.assertj.core.api.AssertionInfo; -import org.assertj.core.api.exception.PathsException; import org.assertj.core.util.VisibleForTesting; import org.assertj.core.util.diff.Delta; @@ -75,12 +75,8 @@ */ public class Paths { - private static final String FAILED_TO_RESOLVE_ARGUMENT_REAL_PATH = "failed to resolve argument real path"; - private static final String FAILED_TO_RESOLVE_ACTUAL_REAL_PATH = "failed to resolve actual real path"; private static final String UNABLE_TO_COMPARE_PATH_CONTENTS = "Unable to compare contents of paths:<%s> and:<%s>"; - public static final String IOERROR_FORMAT = "I/O error attempting to process assertion for path: <%s>"; - private static final Paths INSTANCE = new Paths(); private static final Predicate ANY = any -> true; @@ -90,70 +86,62 @@ public class Paths { BinaryDiff binaryDiff = new BinaryDiff(); @VisibleForTesting Failures failures = Failures.instance(); - - private final NioFilesWrapper nioFilesWrapper; + @VisibleForTesting + NioFilesWrapper nioFilesWrapper = NioFilesWrapper.instance(); public static Paths instance() { return INSTANCE; } - @VisibleForTesting - Paths(NioFilesWrapper nioFilesWrapper) { - this.nioFilesWrapper = nioFilesWrapper; - } - private Paths() { - this(NioFilesWrapper.instance()); } public void assertIsReadable(final AssertionInfo info, final Path actual) { assertNotNull(info, actual); assertExists(info, actual); - if (!nioFilesWrapper.isReadable(actual)) throw failures.failure(info, shouldBeReadable(actual)); + if (!Files.isReadable(actual)) throw failures.failure(info, shouldBeReadable(actual)); } public void assertIsWritable(AssertionInfo info, Path actual) { assertNotNull(info, actual); assertExists(info, actual); - if (!nioFilesWrapper.isWritable(actual)) throw failures.failure(info, shouldBeWritable(actual)); + if (!Files.isWritable(actual)) throw failures.failure(info, shouldBeWritable(actual)); } public void assertIsExecutable(final AssertionInfo info, final Path actual) { assertNotNull(info, actual); assertExists(info, actual); - if (!nioFilesWrapper.isExecutable(actual)) throw failures.failure(info, shouldBeExecutable(actual)); + if (!Files.isExecutable(actual)) throw failures.failure(info, shouldBeExecutable(actual)); } public void assertExists(final AssertionInfo info, final Path actual) { assertNotNull(info, actual); - if (!nioFilesWrapper.exists(actual)) throw failures.failure(info, shouldExist(actual)); + if (!Files.exists(actual)) throw failures.failure(info, shouldExist(actual)); } public void assertExistsNoFollowLinks(final AssertionInfo info, final Path actual) { assertNotNull(info, actual); - if (!nioFilesWrapper.exists(actual, LinkOption.NOFOLLOW_LINKS)) - throw failures.failure(info, shouldExistNoFollowLinks(actual)); + if (!Files.exists(actual, LinkOption.NOFOLLOW_LINKS)) throw failures.failure(info, shouldExistNoFollowLinks(actual)); } public void assertDoesNotExist(final AssertionInfo info, final Path actual) { assertNotNull(info, actual); - if (!nioFilesWrapper.notExists(actual, LinkOption.NOFOLLOW_LINKS)) - throw failures.failure(info, shouldNotExist(actual)); + if (!Files.notExists(actual, LinkOption.NOFOLLOW_LINKS)) throw failures.failure(info, shouldNotExist(actual)); } public void assertIsRegularFile(final AssertionInfo info, final Path actual) { assertExists(info, actual); - if (!nioFilesWrapper.isRegularFile(actual)) throw failures.failure(info, shouldBeRegularFile(actual)); + if (!Files.isRegularFile(actual)) throw failures.failure(info, shouldBeRegularFile(actual)); } public void assertIsDirectory(final AssertionInfo info, final Path actual) { assertExists(info, actual); - if (!nioFilesWrapper.isDirectory(actual)) throw failures.failure(info, shouldBeDirectory(actual)); + if (!Files.isDirectory(actual)) throw failures.failure(info, shouldBeDirectory(actual)); } public void assertIsSymbolicLink(final AssertionInfo info, final Path actual) { assertExistsNoFollowLinks(info, actual); - if (!nioFilesWrapper.isSymbolicLink(actual)) throw failures.failure(info, shouldBeSymbolicLink(actual)); + if (!Files.isSymbolicLink(actual)) throw failures.failure(info, shouldBeSymbolicLink(actual)); } public void assertIsAbsolute(final AssertionInfo info, final Path actual) { @@ -173,55 +161,28 @@ public void assertIsNormalized(final AssertionInfo info, final Path actual) { public void assertIsCanonical(final AssertionInfo info, final Path actual) { assertNotNull(info, actual); - try { - if (!actual.equals(actual.toRealPath())) throw failures.failure(info, shouldBeCanonicalPath(actual)); - } catch (IOException e) { - throw new PathsException(FAILED_TO_RESOLVE_ACTUAL_REAL_PATH, e); - } + if (!actual.equals(toRealPath(actual))) throw failures.failure(info, shouldBeCanonicalPath(actual)); } public void assertHasParent(final AssertionInfo info, final Path actual, final Path expected) { assertNotNull(info, actual); checkExpectedParentPathIsNotNull(expected); - - final Path canonicalActual; - try { - canonicalActual = actual.toRealPath(); - } catch (IOException e) { - throw new PathsException(FAILED_TO_RESOLVE_ACTUAL_REAL_PATH, e); - } - - final Path canonicalExpected; - try { - canonicalExpected = expected.toRealPath(); - } catch (IOException e) { - throw new PathsException(FAILED_TO_RESOLVE_ARGUMENT_REAL_PATH, e); - } - - final Path actualParent = canonicalActual.getParent(); - if (actualParent == null) throw failures.failure(info, shouldHaveParent(actual, expected)); - if (!actualParent.equals(canonicalExpected)) - throw failures.failure(info, shouldHaveParent(actual, actualParent, expected)); + Path parent = toRealPath(actual).getParent(); + if (parent == null) throw failures.failure(info, shouldHaveParent(actual, expected)); + if (!parent.equals(toRealPath(expected))) throw failures.failure(info, shouldHaveParent(actual, parent, expected)); } public void assertHasParentRaw(final AssertionInfo info, final Path actual, final Path expected) { assertNotNull(info, actual); checkExpectedParentPathIsNotNull(expected); - - final Path actualParent = actual.getParent(); - if (actualParent == null) throw failures.failure(info, shouldHaveParent(actual, expected)); - if (!actualParent.equals(expected)) - throw failures.failure(info, shouldHaveParent(actual, actualParent, expected)); + Path parent = actual.getParent(); + if (parent == null) throw failures.failure(info, shouldHaveParent(actual, expected)); + if (!parent.equals(expected)) throw failures.failure(info, shouldHaveParent(actual, parent, expected)); } public void assertHasNoParent(final AssertionInfo info, final Path actual) { assertNotNull(info, actual); - try { - final Path canonicalActual = actual.toRealPath(); - if (canonicalActual.getParent() != null) throw failures.failure(info, shouldHaveNoParent(actual)); - } catch (IOException e) { - throw new PathsException(FAILED_TO_RESOLVE_ACTUAL_REAL_PATH, e); - } + if (toRealPath(actual).getParent() != null) throw failures.failure(info, shouldHaveNoParent(actual)); } public void assertHasNoParentRaw(final AssertionInfo info, final Path actual) { @@ -234,30 +195,15 @@ public void assertHasSize(final AssertionInfo info, final Path actual, long expe try { long actualSize = nioFilesWrapper.size(actual); if (actualSize != expectedSize) throw failures.failure(info, shouldHaveSize(actual, expectedSize)); - } catch(IOException e) { - throw new UncheckedIOException(format("unable to verify the size of the path: <%s>", actual), e); + } catch (IOException e) { + throw new UncheckedIOException(e); } } - public void assertStartsWith(final AssertionInfo info, final Path actual, final Path start) { + public void assertStartsWith(final AssertionInfo info, final Path actual, final Path other) { assertNotNull(info, actual); - assertExpectedStartPathIsNotNull(start); - - final Path canonicalActual; - try { - canonicalActual = actual.toRealPath(); - } catch (IOException e) { - throw new PathsException(FAILED_TO_RESOLVE_ACTUAL_REAL_PATH, e); - } - - final Path canonicalOther; - try { - canonicalOther = start.toRealPath(); - } catch (IOException e) { - throw new PathsException(FAILED_TO_RESOLVE_ARGUMENT_REAL_PATH, e); - } - - if (!canonicalActual.startsWith(canonicalOther)) throw failures.failure(info, shouldStartWith(actual, start)); + assertExpectedStartPathIsNotNull(other); + if (!toRealPath(actual).startsWith(toRealPath(other))) throw failures.failure(info, shouldStartWith(actual, other)); } public void assertStartsWithRaw(final AssertionInfo info, final Path actual, final Path other) { @@ -266,15 +212,10 @@ public void assertStartsWithRaw(final AssertionInfo info, final Path actual, fin if (!actual.startsWith(other)) throw failures.failure(info, shouldStartWith(actual, other)); } - public void assertEndsWith(final AssertionInfo info, final Path actual, final Path end) { + public void assertEndsWith(final AssertionInfo info, final Path actual, final Path other) { assertNotNull(info, actual); - assertExpectedEndPathIsNotNull(end); - try { - final Path canonicalActual = actual.toRealPath(); - if (!canonicalActual.endsWith(end.normalize())) throw failures.failure(info, shouldEndWith(actual, end)); - } catch (IOException e) { - throw new PathsException(FAILED_TO_RESOLVE_ACTUAL_REAL_PATH, e); - } + assertExpectedEndPathIsNotNull(other); + if (!toRealPath(actual).endsWith(other.normalize())) throw failures.failure(info, shouldEndWith(actual, other)); } public void assertEndsWithRaw(final AssertionInfo info, final Path actual, final Path end) { @@ -289,13 +230,12 @@ public void assertHasFileName(final AssertionInfo info, Path actual, String file if (!actual.getFileName().endsWith(fileName)) throw failures.failure(info, shouldHaveName(actual, fileName)); } - public void assertHasContent(final AssertionInfo info, Path actual, String expected, Charset charset) { + public void assertHasTextualContent(final AssertionInfo info, Path actual, String expected, Charset charset) { requireNonNull(expected, "The text to compare to should not be null"); assertIsReadable(info, actual); try { List> diffs = diff.diff(actual, expected, charset); - if (diffs.isEmpty()) return; - throw failures.failure(info, shouldHaveContent(actual, charset, diffs)); + if (!diffs.isEmpty()) throw failures.failure(info, shouldHaveContent(actual, charset, diffs)); } catch (IOException e) { throw new UncheckedIOException(format("Unable to verify text contents of path:<%s>", actual), e); } @@ -306,8 +246,7 @@ public void assertHasBinaryContent(AssertionInfo info, Path actual, byte[] expec assertIsReadable(info, actual); try { BinaryDiffResult diffResult = binaryDiff.diff(actual, expected); - if (diffResult.hasNoDiff()) return; - throw failures.failure(info, shouldHaveBinaryContent(actual, diffResult)); + if (!diffResult.hasNoDiff()) throw failures.failure(info, shouldHaveBinaryContent(actual, diffResult)); } catch (IOException e) { throw new UncheckedIOException(format("Unable to verify binary contents of path:<%s>", actual), e); } @@ -315,10 +254,9 @@ public void assertHasBinaryContent(AssertionInfo info, Path actual, byte[] expec public void assertHasSameBinaryContentAs(AssertionInfo info, Path actual, Path expected) { requireNonNull(expected, "The given Path to compare actual content to should not be null"); + checkArgument(Files.exists(expected), "The given Path <%s> to compare actual content to should exist", expected); + checkArgument(Files.isReadable(expected), "The given Path <%s> to compare actual content to should be readable", expected); assertIsReadable(info, actual); - checkArgument(nioFilesWrapper.exists(expected), "The given Path <%s> to compare actual content to should exist", expected); - checkArgument(nioFilesWrapper.isReadable(expected), "The given Path <%s> to compare actual content to should be readable", - expected); try { BinaryDiffResult binaryDiffResult = binaryDiff.diff(actual, readAllBytes(expected)); if (binaryDiffResult.hasDiff()) throw failures.failure(info, shouldHaveBinaryContent(actual, binaryDiffResult)); @@ -327,17 +265,15 @@ public void assertHasSameBinaryContentAs(AssertionInfo info, Path actual, Path e } } - public void assertHasSameContentAs(AssertionInfo info, Path actual, Charset actualCharset, Path expected, - Charset expectedCharset) { + public void assertHasSameTextualContentAs(AssertionInfo info, Path actual, Charset actualCharset, Path expected, + Charset expectedCharset) { requireNonNull(expected, "The given Path to compare actual content to should not be null"); - checkArgument(nioFilesWrapper.exists(expected), "The given Path <%s> to compare actual content to should exist", expected); - checkArgument(nioFilesWrapper.isReadable(expected), "The given Path <%s> to compare actual content to should be readable", - expected); + checkArgument(Files.exists(expected), "The given Path <%s> to compare actual content to should exist", expected); + checkArgument(Files.isReadable(expected), "The given Path <%s> to compare actual content to should be readable", expected); assertIsReadable(info, actual); try { List> diffs = diff.diff(actual, actualCharset, expected, expectedCharset); - if (diffs.isEmpty()) return; - throw failures.failure(info, shouldHaveSameContent(actual, expected, diffs)); + if (!diffs.isEmpty()) throw failures.failure(info, shouldHaveSameContent(actual, expected, diffs)); } catch (IOException e) { throw new UncheckedIOException(format(UNABLE_TO_COMPARE_PATH_CONTENTS, actual, expected), e); } @@ -416,7 +352,25 @@ public void assertIsEmptyDirectory(AssertionInfo info, Path actual) { public void assertIsNotEmptyDirectory(AssertionInfo info, Path actual) { boolean isEmptyDirectory = directoryContent(info, actual).isEmpty(); - if (isEmptyDirectory) throw failures.failure(info, shouldNotBeEmpty()); + if (isEmptyDirectory) throw failures.failure(info, shouldNotBeEmpty(actual)); + } + + public void assertIsEmptyFile(AssertionInfo info, Path actual) { + assertIsRegularFile(info, actual); + try { + if (nioFilesWrapper.size(actual) > 0) throw failures.failure(info, shouldBeEmpty(actual)); + } catch (IOException e) { + throw new UncheckedIOException(e); + } + } + + public void assertIsNotEmptyFile(AssertionInfo info, Path actual) { + assertIsRegularFile(info, actual); + try { + if (nioFilesWrapper.size(actual) == 0) throw failures.failure(info, shouldNotBeEmpty(actual)); + } catch (IOException e) { + throw new UncheckedIOException(e); + } } public static List toPathNames(List files) { @@ -508,21 +462,11 @@ private static void assertExpectedEndPathIsNotNull(final Path end) { requireNonNull(end, "the expected end path should not be null"); } - public void assertIsEmptyFile(AssertionInfo info, Path actual) { - assertIsRegularFile(info, actual); - try { - if (nioFilesWrapper.size(actual) > 0) throw failures.failure(info, shouldBeEmpty(actual)); - } catch (IOException e) { - throw new PathsException(FAILED_TO_RESOLVE_ACTUAL_REAL_PATH, e); - } - } - - public void assertIsNotEmptyFile(AssertionInfo info, Path actual) { - assertIsRegularFile(info, actual); + private static Path toRealPath(Path path) { try { - if (nioFilesWrapper.size(actual) == 0) throw failures.failure(info, shouldNotBeEmpty(actual)); + return path.toRealPath(); } catch (IOException e) { - throw new PathsException(FAILED_TO_RESOLVE_ACTUAL_REAL_PATH, e); + throw new UncheckedIOException(e); } } diff --git a/src/test/java/org/assertj/core/api/path/PathAssert_hasContent_Test.java b/src/test/java/org/assertj/core/api/path/PathAssert_hasContent_Test.java index 864b322415..8e862ac555 100644 --- a/src/test/java/org/assertj/core/api/path/PathAssert_hasContent_Test.java +++ b/src/test/java/org/assertj/core/api/path/PathAssert_hasContent_Test.java @@ -16,27 +16,17 @@ import org.assertj.core.api.PathAssert; import org.assertj.core.api.PathAssertBaseTest; -import org.junit.jupiter.api.BeforeAll; -/** - * Tests for {@link PathAssert#hasContent(String)}. - */ class PathAssert_hasContent_Test extends PathAssertBaseTest { - private static String expected; - - @BeforeAll - static void beforeOnce() { - expected = "xyz"; - } - @Override protected PathAssert invoke_api_method() { - return assertions.hasContent(expected); + return assertions.hasContent("xyz"); } @Override protected void verify_internal_effects() { - verify(paths).assertHasContent(getInfo(assertions), getActual(assertions), expected, getCharset(assertions)); + verify(paths).assertHasTextualContent(getInfo(assertions), getActual(assertions), "xyz", getCharset(assertions)); } + } diff --git a/src/test/java/org/assertj/core/api/path/PathAssert_hasSameContentAs_Test.java b/src/test/java/org/assertj/core/api/path/PathAssert_hasSameContentAs_Test.java index 65b2852910..22820b330e 100644 --- a/src/test/java/org/assertj/core/api/path/PathAssert_hasSameContentAs_Test.java +++ b/src/test/java/org/assertj/core/api/path/PathAssert_hasSameContentAs_Test.java @@ -42,7 +42,7 @@ protected PathAssert invoke_api_method() { @Override protected void verify_internal_effects() { - verify(paths).assertHasSameContentAs(getInfo(assertions), getActual(assertions), defaultCharset, expected, defaultCharset); + verify(paths).assertHasSameTextualContentAs(getInfo(assertions), getActual(assertions), defaultCharset, expected, defaultCharset); } } diff --git a/src/test/java/org/assertj/core/api/path/PathAssert_hasSameTextualContentAs_Test.java b/src/test/java/org/assertj/core/api/path/PathAssert_hasSameTextualContentAs_Test.java index 29d40eb8ca..ddb5c4beae 100644 --- a/src/test/java/org/assertj/core/api/path/PathAssert_hasSameTextualContentAs_Test.java +++ b/src/test/java/org/assertj/core/api/path/PathAssert_hasSameTextualContentAs_Test.java @@ -23,10 +23,8 @@ import org.assertj.core.api.PathAssert; import org.assertj.core.api.PathAssertBaseTest; import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; -@DisplayName("PathAssert hasSameTextualContentAs") class PathAssert_hasSameTextualContentAs_Test extends PathAssertBaseTest { private static Path expected; @@ -43,7 +41,7 @@ protected PathAssert invoke_api_method() { @Override protected void verify_internal_effects() { - verify(paths).assertHasSameContentAs(getInfo(assertions), getActual(assertions), defaultCharset, expected, defaultCharset); + verify(paths).assertHasSameTextualContentAs(getInfo(assertions), getActual(assertions), defaultCharset, expected, defaultCharset); } @Test diff --git a/src/test/java/org/assertj/core/internal/PathsBaseTest.java b/src/test/java/org/assertj/core/internal/PathsBaseTest.java index 613874ca47..c75315231e 100644 --- a/src/test/java/org/assertj/core/internal/PathsBaseTest.java +++ b/src/test/java/org/assertj/core/internal/PathsBaseTest.java @@ -23,6 +23,7 @@ import org.assertj.core.api.AssertionInfo; import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.io.TempDir; import com.github.marschall.memoryfilesystem.MemoryFileSystemBuilder; @@ -54,6 +55,9 @@ */ public abstract class PathsBaseTest { + @TempDir + protected Path tempDir; + protected Failures failures; protected Paths paths; protected NioFilesWrapper nioFilesWrapper; @@ -64,15 +68,16 @@ public abstract class PathsBaseTest { @BeforeEach public void setUp() { - failures = spy(new Failures()); - nioFilesWrapper = mock(NioFilesWrapper.class); - paths = new Paths(nioFilesWrapper); + paths = Paths.instance(); + nioFilesWrapper = spy(paths.nioFilesWrapper); + paths.nioFilesWrapper = nioFilesWrapper; + failures = spy(paths.failures); paths.failures = failures; - info = someInfo(); - diff = mock(Diff.class); + diff = spy(paths.diff); paths.diff = diff; - binaryDiff = mock(BinaryDiff.class); + binaryDiff = spy(paths.binaryDiff); paths.binaryDiff = binaryDiff; + info = someInfo(); } /** @@ -102,4 +107,4 @@ public void close() { } } } -} \ No newline at end of file +} diff --git a/src/test/java/org/assertj/core/internal/paths/MockPathsBaseTest.java b/src/test/java/org/assertj/core/internal/paths/MockPathsBaseTest.java deleted file mode 100644 index d61e50abb9..0000000000 --- a/src/test/java/org/assertj/core/internal/paths/MockPathsBaseTest.java +++ /dev/null @@ -1,142 +0,0 @@ -/* - * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on - * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - * - * Copyright 2012-2021 the original author or authors. - */ -package org.assertj.core.internal.paths; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.fail; -import static org.assertj.core.test.TestData.someInfo; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.anyInt; -import static org.mockito.ArgumentMatchers.eq; -import static org.mockito.BDDMockito.given; -import static org.mockito.BDDMockito.willAnswer; -import static org.mockito.Mockito.mock; - -import java.io.ByteArrayInputStream; -import java.io.File; -import java.io.IOException; -import java.io.InputStream; -import java.nio.file.DirectoryStream; -import java.nio.file.Path; -import java.util.Arrays; -import java.util.List; -import java.util.Spliterators; -import java.util.function.Predicate; - -import org.assertj.core.api.AssertionInfo; -import org.assertj.core.internal.PathsBaseTest; -import org.assertj.core.util.Strings; -import org.junit.jupiter.api.BeforeEach; - -import com.google.common.collect.Iterators; -import org.junit.jupiter.api.io.TempDir; - -/** - * @deprecated favor tests based on {@link PathsBaseTest} and JUnit 5 {@link TempDir}. - */ -@Deprecated -public class MockPathsBaseTest extends PathsBaseTest { - - static final AssertionInfo INFO = someInfo(); - - Path actual; - Path other; - - @BeforeEach - public void init() { - actual = mock(Path.class); - other = mock(Path.class); - } - - static void failIfStreamIsOpen(InputStream stream) { - try { - assertThat(stream.read()).as("Stream should be closed").isNegative(); - } catch (IOException e) { - assertThat(e).hasNoCause().hasMessage("Stream closed"); - } - } - - static DirectoryStream directoryStream(List directoryItems) { - DirectoryStream stream = mock(DirectoryStream.class); - given(stream.iterator()).will(inv -> directoryItems.iterator()); - given(stream.spliterator()).will(inv -> Spliterators.spliteratorUnknownSize(directoryItems.iterator(), 0)); - return stream; - } - - private DirectoryStream filterStream(Predicate filter, DirectoryStream source) throws IOException { - DirectoryStream stream = mock(DirectoryStream.class); - given(stream.iterator()).will(inv -> Iterators.filter(source.iterator(), filter::test)); - given(stream.spliterator()).will(inv -> Spliterators.spliteratorUnknownSize(Iterators.filter(source.iterator(), filter::test), - 0)); - willAnswer(inv -> { - source.close(); - return null; - }).given(stream).close(); - return stream; - } - - static Path mockPath(String... names) { - Path path = mock(Path.class); - given(path.toString()).willReturn(Strings.join(names).with(File.separator)); - if (names.length > 1) { - Path filename = mockPath(names[names.length - 1]); - given(path.getFileName()).willReturn(filename); - given(path.getParent()).will(inv -> mockPath(Arrays.copyOf(names, names.length - 1))); - } else { - given(path.getFileName()).willReturn(path); - given(path.getParent()).willReturn(null); - } - given(path.getNameCount()).willReturn(names.length); - given(path.getName(anyInt())).will(inv -> names[(int) inv.getArgument(0)]); - return path; - } - - Path mockEmptyRegularFile(String... names) { - Path path = mockPath(names); - given(nioFilesWrapper.exists(path)).willReturn(true); - given(nioFilesWrapper.isRegularFile(path)).willReturn(true); - try { - given(nioFilesWrapper.newInputStream(path)).willReturn(new ByteArrayInputStream(new byte[0])); - } catch (IOException e) { - fail("Should not happen"); - } - return path; - } - - Path mockNonEmptyRegularFile(String... names) { - Path path = mockPath(names); - given(nioFilesWrapper.exists(path)).willReturn(true); - given(nioFilesWrapper.isRegularFile(path)).willReturn(true); - try { - given(nioFilesWrapper.size(path)).willReturn(1L); - given(nioFilesWrapper.newInputStream(path)).willReturn(new ByteArrayInputStream(new byte[1])); - } catch (IOException e) { - fail("Should not happen"); - } - return path; - } - - Path mockDirectory(String name, List paths) { - DirectoryStream directoryItems = directoryStream(paths); - Path path = mockPath(name); - given(nioFilesWrapper.exists(path)).willReturn(true); - given(nioFilesWrapper.isDirectory(path)).willReturn(true); - try { - given(nioFilesWrapper.newDirectoryStream(eq(path), any())).will(inv -> filterStream(inv.getArgument(1), directoryItems)); - } catch (IOException e) { - fail("Should not happen"); - } - return path; - } - -} diff --git a/src/test/java/org/assertj/core/internal/paths/Paths_assertEndsWithRaw_Test.java b/src/test/java/org/assertj/core/internal/paths/Paths_assertEndsWithRaw_Test.java index 47a3e341b1..3c7d1625fd 100644 --- a/src/test/java/org/assertj/core/internal/paths/Paths_assertEndsWithRaw_Test.java +++ b/src/test/java/org/assertj/core/internal/paths/Paths_assertEndsWithRaw_Test.java @@ -12,46 +12,85 @@ */ package org.assertj.core.internal.paths; -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatExceptionOfType; -import static org.assertj.core.api.Assertions.assertThatNullPointerException; -import static org.assertj.core.api.ThrowableAssert.catchThrowable; +import static java.nio.file.Files.createFile; +import static java.nio.file.Files.createSymbolicLink; +import static org.assertj.core.api.Assertions.catchThrowable; +import static org.assertj.core.api.BDDAssertions.then; import static org.assertj.core.error.ShouldEndWithPath.shouldEndWith; +import static org.assertj.core.util.AssertionsUtil.expectAssertionError; import static org.assertj.core.util.FailureMessages.actualIsNull; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; +import java.io.IOException; +import java.nio.file.Path; +import java.nio.file.Paths; + +import org.assertj.core.internal.PathsBaseTest; import org.junit.jupiter.api.Test; -class Paths_assertEndsWithRaw_Test extends MockPathsBaseTest { +class Paths_assertEndsWithRaw_Test extends PathsBaseTest { @Test - void should_fail_if_actual_is_null() { - assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> paths.assertEndsWithRaw(info, null, other)) - .withMessage(actualIsNull()); + void should_fail_if_actual_is_null() throws IOException { + // GIVEN + Path other = tempDir.resolve("other"); + // WHEN + AssertionError error = expectAssertionError(() -> paths.assertEndsWithRaw(info, null, other)); + // THEN + then(error).hasMessage(actualIsNull()); } @Test - void should_fail_if_other_is_null() { - assertThatNullPointerException().isThrownBy(() -> paths.assertEndsWithRaw(info, actual, null)) - .withMessage("the expected end path should not be null"); + void should_fail_if_other_is_null() throws IOException { + // GIVEN + Path actual = createFile(tempDir.resolve("actual")); + // WHEN + Throwable thrown = catchThrowable(() -> paths.assertEndsWithRaw(info, actual, null)); + // THEN + then(thrown).isInstanceOf(NullPointerException.class) + .hasMessage("the expected end path should not be null"); } @Test - void should_fail_if_actual_does_not_end_with_other() { - // This is the default, but let's make this explicit - when(actual.endsWith(other)).thenReturn(false); - - Throwable error = catchThrowable(() -> paths.assertEndsWithRaw(info, actual, other)); + void should_fail_if_actual_does_not_end_with_other() throws IOException { + // GIVEN + Path actual = createFile(tempDir.resolve("actual")); + Path other = tempDir.resolve("other"); + // WHEN + AssertionError error = expectAssertionError(() -> paths.assertEndsWithRaw(info, actual, other)); + // THEN + then(error).hasMessage(shouldEndWith(actual, other).create()); + } - assertThat(error).isInstanceOf(AssertionError.class); - verify(failures).failure(info, shouldEndWith(actual, other)); + @Test + void should_pass_if_actual_ends_with_other() throws IOException { + // GIVEN + Path actual = createFile(tempDir.resolve("actual")); + Path other = Paths.get("actual"); + // WHEN/THEN + paths.assertEndsWithRaw(info, actual, other); } @Test - void should_succeed_if_actual_ends_with_other() { - when(actual.endsWith(other)).thenReturn(true); + void should_fail_if_actual_is_not_canonical() throws IOException { + // GIVEN + Path file = createFile(tempDir.resolve("file")); + Path actual = createSymbolicLink(tempDir.resolve("actual"), file); + Path other = Paths.get("file"); + // WHEN + AssertionError error = expectAssertionError(() -> paths.assertEndsWithRaw(info, actual, other)); + // THEN + then(error).hasMessage(shouldEndWith(actual, other).create()); + } - paths.assertEndsWithRaw(info, actual, other); + @Test + void should_fail_if_other_is_not_normalized() throws IOException { + // GIVEN + Path actual = createFile(tempDir.resolve("actual")); + Path other = Paths.get("actual", "..", "actual", "."); + // WHEN + AssertionError error = expectAssertionError(() -> paths.assertEndsWithRaw(info, actual, other)); + // THEN + then(error).hasMessage(shouldEndWith(actual, other).create()); } + } diff --git a/src/test/java/org/assertj/core/internal/paths/Paths_assertEndsWith_Test.java b/src/test/java/org/assertj/core/internal/paths/Paths_assertEndsWith_Test.java index 99e9340f4b..8b5055429e 100644 --- a/src/test/java/org/assertj/core/internal/paths/Paths_assertEndsWith_Test.java +++ b/src/test/java/org/assertj/core/internal/paths/Paths_assertEndsWith_Test.java @@ -12,74 +12,98 @@ */ package org.assertj.core.internal.paths; -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatExceptionOfType; -import static org.assertj.core.api.Assertions.assertThatNullPointerException; -import static org.assertj.core.api.ThrowableAssert.catchThrowable; +import static java.nio.file.Files.createFile; +import static java.nio.file.Files.createSymbolicLink; +import static org.assertj.core.api.Assertions.catchThrowable; +import static org.assertj.core.api.BDDAssertions.then; import static org.assertj.core.error.ShouldEndWithPath.shouldEndWith; +import static org.assertj.core.util.AssertionsUtil.expectAssertionError; import static org.assertj.core.util.FailureMessages.actualIsNull; +import static org.mockito.BDDMockito.given; import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; import java.io.IOException; +import java.io.UncheckedIOException; import java.nio.file.Path; +import java.nio.file.Paths; -import org.assertj.core.api.exception.PathsException; +import org.assertj.core.internal.PathsBaseTest; import org.junit.jupiter.api.Test; -class Paths_assertEndsWith_Test extends MockPathsBaseTest { +class Paths_assertEndsWith_Test extends PathsBaseTest { @Test - void should_fail_if_actual_is_null() { - assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> paths.assertEndsWith(info, null, other)) - .withMessage(actualIsNull()); + void should_fail_if_actual_is_null() throws IOException { + // GIVEN + Path other = tempDir.resolve("other"); + // WHEN + AssertionError error = expectAssertionError(() -> paths.assertEndsWith(info, null, other)); + // THEN + then(error).hasMessage(actualIsNull()); } @Test - void should_fail_if_other_is_null() { - assertThatNullPointerException().isThrownBy(() -> paths.assertEndsWith(info, actual, null)) - .withMessage("the expected end path should not be null"); + void should_fail_if_other_is_null() throws IOException { + // GIVEN + Path actual = createFile(tempDir.resolve("actual")); + // WHEN + Throwable thrown = catchThrowable(() -> paths.assertEndsWith(info, actual, null)); + // THEN + then(thrown).isInstanceOf(NullPointerException.class) + .hasMessage("the expected end path should not be null"); } @Test - void should_fail_with_PathsException_if_actual_cannot_be_resolved() throws IOException { - final IOException causeException = new IOException(); - when(actual.toRealPath()).thenThrow(causeException); - - assertThatExceptionOfType(PathsException.class).isThrownBy(() -> paths.assertEndsWith(info, actual, other)) - .withMessage("failed to resolve actual real path") - .withCause(causeException); + void should_rethrow_IOException_as_UncheckedIOException_if_actual_cannot_be_resolved() throws IOException { + // GIVEN + Path actual = mock(Path.class); + Path other = tempDir.resolve("other"); + IOException exception = new IOException("boom!"); + given(actual.toRealPath()).willThrow(exception); + // WHEN + Throwable thrown = catchThrowable(() -> paths.assertEndsWith(info, actual, other)); + // THEN + then(thrown).isInstanceOf(UncheckedIOException.class) + .hasCause(exception); } @Test - void should_fail_if_canonical_actual_does_not_end_with_normalized_other() throws IOException { - final Path canonicalActual = mock(Path.class); - final Path normalizedOther = mock(Path.class); - - when(actual.toRealPath()).thenReturn(canonicalActual); - when(other.normalize()).thenReturn(normalizedOther); - - // This is the default, but... - when(canonicalActual.endsWith(normalizedOther)).thenReturn(false); - - Throwable error = catchThrowable(() -> paths.assertEndsWith(info, actual, other)); - - assertThat(error).isInstanceOf(AssertionError.class); - verify(failures).failure(info, shouldEndWith(actual, other)); + void should_fail_if_actual_does_not_end_with_other() throws IOException { + // GIVEN + Path actual = createFile(tempDir.resolve("actual")); + Path other = tempDir.resolve("other"); + // WHEN + AssertionError error = expectAssertionError(() -> paths.assertEndsWith(info, actual, other)); + // THEN + then(error).hasMessage(shouldEndWith(actual, other).create()); } @Test - void should_succeed_if_canonical_actual_ends_with_normalized_other() throws IOException { - final Path canonicalActual = mock(Path.class); - final Path normalizedOther = mock(Path.class); - - when(actual.toRealPath()).thenReturn(canonicalActual); - when(other.normalize()).thenReturn(normalizedOther); + void should_pass_if_actual_ends_with_other() throws IOException { + // GIVEN + Path actual = createFile(tempDir.resolve("actual")); + Path other = Paths.get("actual"); + // WHEN/THEN + paths.assertEndsWith(info, actual, other); + } - // We want the canonical versions to be compared, not the arguments - when(canonicalActual.endsWith(normalizedOther)).thenReturn(true); + @Test + void should_pass_if_actual_is_not_canonical() throws IOException { + // GIVEN + Path file = createFile(tempDir.resolve("file")); + Path actual = createSymbolicLink(tempDir.resolve("actual"), file); + Path other = Paths.get("file"); + // WHEN/THEN + paths.assertEndsWith(info, actual, other); + } + @Test + void should_pass_if_other_is_not_normalized() throws IOException { + // GIVEN + Path actual = createFile(tempDir.resolve("actual")); + Path other = Paths.get("actual", "..", "actual", "."); + // WHEN/THEN paths.assertEndsWith(info, actual, other); } + } diff --git a/src/test/java/org/assertj/core/internal/paths/Paths_assertExistsNoFollowLinks_Test.java b/src/test/java/org/assertj/core/internal/paths/Paths_assertExistsNoFollowLinks_Test.java index 6ebced7965..38971f0ee7 100644 --- a/src/test/java/org/assertj/core/internal/paths/Paths_assertExistsNoFollowLinks_Test.java +++ b/src/test/java/org/assertj/core/internal/paths/Paths_assertExistsNoFollowLinks_Test.java @@ -12,39 +12,62 @@ */ package org.assertj.core.internal.paths; -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatExceptionOfType; -import static org.assertj.core.api.ThrowableAssert.catchThrowable; +import static java.nio.file.Files.createFile; +import static java.nio.file.Files.createSymbolicLink; +import static org.assertj.core.api.BDDAssertions.then; import static org.assertj.core.error.ShouldExist.shouldExistNoFollowLinks; +import static org.assertj.core.util.AssertionsUtil.expectAssertionError; import static org.assertj.core.util.FailureMessages.actualIsNull; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; -import java.nio.file.LinkOption; +import java.io.IOException; +import java.nio.file.Path; +import org.assertj.core.internal.PathsBaseTest; import org.junit.jupiter.api.Test; -class Paths_assertExistsNoFollowLinks_Test extends MockPathsBaseTest { +class Paths_assertExistsNoFollowLinks_Test extends PathsBaseTest { @Test void should_fail_if_actual_is_null() { - assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> paths.assertExistsNoFollowLinks(info, null)) - .withMessage(actualIsNull()); + // WHEN + AssertionError error = expectAssertionError(() -> paths.assertExistsNoFollowLinks(info, null)); + // THEN + then(error).hasMessage(actualIsNull()); } @Test void should_fail_if_actual_does_not_exist() { - when(nioFilesWrapper.exists(actual, LinkOption.NOFOLLOW_LINKS)).thenReturn(false); + // GIVEN + Path actual = tempDir.resolve("non-existent"); + // WHEN + AssertionError error = expectAssertionError(() -> paths.assertExistsNoFollowLinks(info, actual)); + // THEN + then(error).hasMessage(shouldExistNoFollowLinks(actual).create()); + } - Throwable error = catchThrowable(() -> paths.assertExistsNoFollowLinks(info, actual)); + @Test + void should_pass_if_actual_exists() throws IOException { + // GIVEN + Path actual = createFile(tempDir.resolve("actual")); + // WHEN/THEN + paths.assertExistsNoFollowLinks(info, actual); + } - assertThat(error).isInstanceOf(AssertionError.class); - verify(failures).failure(info, shouldExistNoFollowLinks(actual)); + @Test + void should_pass_if_actual_is_a_symbolic_link_and_target_exists() throws IOException { + // GIVEN + Path target = createFile(tempDir.resolve("target")); + Path actual = createSymbolicLink(tempDir.resolve("actual"), target); + // WHEN/THEN + paths.assertExistsNoFollowLinks(info, actual); } @Test - void should_pass_if_actual_exists() { - when(nioFilesWrapper.exists(actual, LinkOption.NOFOLLOW_LINKS)).thenReturn(true); + void should_pass_if_actual_is_a_symbolic_link_and_target_does_not_exist() throws IOException { + // GIVEN + Path target = tempDir.resolve("non-existent"); + Path actual = createSymbolicLink(tempDir.resolve("actual"), target); + // WHEN/THEN paths.assertExistsNoFollowLinks(info, actual); } diff --git a/src/test/java/org/assertj/core/internal/paths/Paths_assertExists_Test.java b/src/test/java/org/assertj/core/internal/paths/Paths_assertExists_Test.java index 90048fcd58..fd08291a68 100644 --- a/src/test/java/org/assertj/core/internal/paths/Paths_assertExists_Test.java +++ b/src/test/java/org/assertj/core/internal/paths/Paths_assertExists_Test.java @@ -12,38 +12,65 @@ */ package org.assertj.core.internal.paths; -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatExceptionOfType; -import static org.assertj.core.api.ThrowableAssert.catchThrowable; +import static java.nio.file.Files.createFile; +import static java.nio.file.Files.createSymbolicLink; +import static org.assertj.core.api.BDDAssertions.then; import static org.assertj.core.error.ShouldExist.shouldExist; +import static org.assertj.core.util.AssertionsUtil.expectAssertionError; import static org.assertj.core.util.FailureMessages.actualIsNull; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; +import java.io.IOException; +import java.nio.file.Path; + +import org.assertj.core.internal.PathsBaseTest; import org.junit.jupiter.api.Test; -class Paths_assertExists_Test extends MockPathsBaseTest { +class Paths_assertExists_Test extends PathsBaseTest { @Test void should_fail_if_actual_is_null() { - assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> paths.assertExists(info, null)) - .withMessage(actualIsNull()); + // WHEN + AssertionError error = expectAssertionError(() -> paths.assertExists(info, null)); + // THEN + then(error).hasMessage(actualIsNull()); } @Test void should_fail_if_actual_does_not_exist() { - when(nioFilesWrapper.exists(actual)).thenReturn(false); - - Throwable error = catchThrowable(() -> paths.assertExists(info, actual)); + // GIVEN + Path actual = tempDir.resolve("non-existent"); + // WHEN + AssertionError error = expectAssertionError(() -> paths.assertExists(info, actual)); + // THEN + then(error).hasMessage(shouldExist(actual).create()); + } - assertThat(error).isInstanceOf(AssertionError.class); - verify(failures).failure(info, shouldExist(actual)); + @Test + void should_pass_if_actual_exists() throws IOException { + // GIVEN + Path actual = createFile(tempDir.resolve("actual")); + // WHEN/THEN + paths.assertExists(info, actual); } @Test - void should_pass_if_actual_exists() { - when(nioFilesWrapper.exists(actual)).thenReturn(true); + void should_pass_if_actual_is_a_symbolic_link_and_target_exists() throws IOException { + // GIVEN + Path target = createFile(tempDir.resolve("target")); + Path actual = createSymbolicLink(tempDir.resolve("actual"), target); + // WHEN/THEN paths.assertExists(info, actual); } + @Test + void should_fail_if_actual_is_a_symbolic_link_and_target_does_not_exist() throws IOException { + // GIVEN + Path target = tempDir.resolve("non-existent"); + Path actual = createSymbolicLink(tempDir.resolve("actual"), target); + // WHEN + AssertionError error = expectAssertionError(() -> paths.assertExists(info, actual)); + // THEN + then(error).hasMessage(shouldExist(actual).create()); + } + } diff --git a/src/test/java/org/assertj/core/internal/paths/Paths_assertHasBinaryContent_Test.java b/src/test/java/org/assertj/core/internal/paths/Paths_assertHasBinaryContent_Test.java index 3279b1a0ae..cc37542e40 100644 --- a/src/test/java/org/assertj/core/internal/paths/Paths_assertHasBinaryContent_Test.java +++ b/src/test/java/org/assertj/core/internal/paths/Paths_assertHasBinaryContent_Test.java @@ -12,120 +12,109 @@ */ package org.assertj.core.internal.paths; -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatExceptionOfType; -import static org.assertj.core.api.Assertions.assertThatNullPointerException; +import static java.nio.file.Files.createFile; import static org.assertj.core.api.Assertions.catchThrowable; +import static org.assertj.core.api.BDDAssertions.then; import static org.assertj.core.error.ShouldBeReadable.shouldBeReadable; import static org.assertj.core.error.ShouldExist.shouldExist; import static org.assertj.core.error.ShouldHaveBinaryContent.shouldHaveBinaryContent; -import static org.assertj.core.internal.BinaryDiffResult.noDiff; -import static org.assertj.core.test.TestData.someInfo; +import static org.assertj.core.util.AssertionsUtil.expectAssertionError; import static org.assertj.core.util.FailureMessages.actualIsNull; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; +import static org.junit.jupiter.api.condition.OS.WINDOWS; +import static org.mockito.BDDMockito.given; -import java.io.File; import java.io.IOException; import java.io.UncheckedIOException; +import java.nio.file.Files; import java.nio.file.Path; -import org.assertj.core.api.AssertionInfo; import org.assertj.core.internal.BinaryDiffResult; -import org.assertj.core.internal.Paths; import org.assertj.core.internal.PathsBaseTest; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.condition.DisabledOnOs; -/** - * Tests for {@link Paths#assertHasBinaryContent(AssertionInfo, Path, byte[])}. - */ class Paths_assertHasBinaryContent_Test extends PathsBaseTest { - private static Path path; - private static byte[] expected; - private Path mockPath; - - @BeforeAll - static void setUpOnce() { - // Does not matter if the values binaryDiffer, the actual comparison is mocked in this test - path = new File("src/test/resources/actual_file.txt").toPath(); - expected = new byte[] { 0, 1 }; - } - - @BeforeEach - void init() { - mockPath = mock(Path.class); - } - @Test - void should_pass_if_path_has_expected_text_content() throws IOException { - when(binaryDiff.diff(path, expected)).thenReturn(noDiff()); - when(nioFilesWrapper.exists(path)).thenReturn(true); - when(nioFilesWrapper.isReadable(path)).thenReturn(true); - paths.assertHasBinaryContent(someInfo(), path, expected); + void should_fail_if_expected_is_null() throws IOException { + // GIVEN + Path actual = createFile(tempDir.resolve("actual")); + // WHEN + Throwable thrown = catchThrowable(() -> paths.assertHasBinaryContent(info, actual, null)); + // THEN + then(thrown).isInstanceOf(NullPointerException.class) + .hasMessage("The binary content to compare to should not be null"); } @Test - void should_throw_error_if_expected_is_null() { - assertThatNullPointerException().isThrownBy(() -> paths.assertHasBinaryContent(someInfo(), path, null)) - .withMessage("The binary content to compare to should not be null"); + void should_fail_if_actual_is_null() throws IOException { + // GIVEN + byte[] expected = "expected".getBytes(); + // WHEN + AssertionError error = expectAssertionError(() -> paths.assertHasBinaryContent(info, null, expected)); + // THEN + then(error).hasMessage(actualIsNull()); } @Test - void should_fail_if_actual_is_null() { - assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> paths.assertHasBinaryContent(someInfo(), null, expected)) - .withMessage(actualIsNull()); + void should_fail_if_actual_does_not_exist() throws IOException { + // GIVEN + Path actual = tempDir.resolve("non-existent"); + byte[] expected = "expected".getBytes(); + // WHEN + AssertionError error = expectAssertionError(() -> paths.assertHasBinaryContent(info, actual, expected)); + // THEN + then(error).hasMessage(shouldExist(actual).create()); } @Test - void should_fail_if_actual_path_does_not_exist() { - AssertionInfo info = someInfo(); - when(nioFilesWrapper.exists(mockPath)).thenReturn(false); - - Throwable error = catchThrowable(() -> paths.assertHasBinaryContent(info, mockPath, expected)); - - assertThat(error).isInstanceOf(AssertionError.class); - verify(failures).failure(info, shouldExist(mockPath)); + @DisabledOnOs(value = WINDOWS, disabledReason = "gh-2312") + void should_fail_if_actual_is_not_readable() throws IOException { + // GIVEN + Path actual = createFile(tempDir.resolve("actual")); + actual.toFile().setReadable(false); + byte[] expected = "expected".getBytes(); + // WHEN + AssertionError error = expectAssertionError(() -> paths.assertHasBinaryContent(info, actual, expected)); + // THEN + then(error).hasMessage(shouldBeReadable(actual).create()); } @Test - void should_fail_if_actual_is_not_a_readable_file() { - AssertionInfo info = someInfo(); - when(nioFilesWrapper.exists(mockPath)).thenReturn(true); - when(nioFilesWrapper.isReadable(mockPath)).thenReturn(false); - - Throwable error = catchThrowable(() -> paths.assertHasBinaryContent(info, mockPath, expected)); - - assertThat(error).isInstanceOf(AssertionError.class); - verify(failures).failure(info, shouldBeReadable(mockPath)); + void should_pass_if_actual_has_expected_binary_content() throws IOException { + // GIVEN + Path actual = Files.write(tempDir.resolve("actual"), "Content".getBytes()); + byte[] expected = "Content".getBytes(); + // WHEN/THEN + paths.assertHasBinaryContent(info, actual, expected); } @Test - void should_throw_error_wrapping_caught_IOException() throws IOException { - IOException cause = new IOException(); - when(binaryDiff.diff(path, expected)).thenThrow(cause); - when(nioFilesWrapper.exists(path)).thenReturn(true); - when(nioFilesWrapper.isReadable(path)).thenReturn(true); - - assertThatExceptionOfType(UncheckedIOException.class).isThrownBy(() -> paths.assertHasBinaryContent(someInfo(), - path, expected)) - .withCause(cause); + void should_fail_if_actual_does_not_have_expected_binary_content() throws IOException { + // GIVEN + Path actual = Files.write(tempDir.resolve("actual"), "Content".getBytes()); + byte[] expected = "Another content".getBytes(); + BinaryDiffResult diff = binaryDiff.diff(actual, expected); + // WHEN + AssertionError error = expectAssertionError(() -> paths.assertHasBinaryContent(info, actual, expected)); + // THEN + then(error).hasMessage(shouldHaveBinaryContent(actual, diff).create(info.description(), info.representation())); } @Test - void should_fail_if_path_does_not_have_expected_binary_content() throws IOException { - BinaryDiffResult binaryDiffs = new BinaryDiffResult(15, (byte) 0xCA, (byte) 0xFE); - when(binaryDiff.diff(path, expected)).thenReturn(binaryDiffs); - when(nioFilesWrapper.exists(path)).thenReturn(true); - when(nioFilesWrapper.isReadable(path)).thenReturn(true); - AssertionInfo info = someInfo(); - - Throwable error = catchThrowable(() -> paths.assertHasBinaryContent(info, path, expected)); - - assertThat(error).isInstanceOf(AssertionError.class); - verify(failures).failure(info, shouldHaveBinaryContent(path, binaryDiffs)); + @DisabledOnOs(value = WINDOWS, disabledReason = "gh-2312") + void should_rethrow_IOException_as_UncheckedIOException() throws IOException { + // GIVEN + Path actual = createFile(tempDir.resolve("actual")); + byte[] expected = "expected".getBytes(); + IOException exception = new IOException("boom!"); + given(binaryDiff.diff(actual, expected)).willThrow(exception); + // WHEN + Throwable thrown = catchThrowable(() -> paths.assertHasBinaryContent(info, actual, expected)); + // THEN + then(thrown).isInstanceOf(UncheckedIOException.class) + .hasMessage("Unable to verify binary contents of path:<%s>", actual) + .hasCause(exception); } + } diff --git a/src/test/java/org/assertj/core/internal/paths/Paths_assertHasContent_Test.java b/src/test/java/org/assertj/core/internal/paths/Paths_assertHasContent_Test.java deleted file mode 100644 index c88afae178..0000000000 --- a/src/test/java/org/assertj/core/internal/paths/Paths_assertHasContent_Test.java +++ /dev/null @@ -1,139 +0,0 @@ -/* - * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on - * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - * - * Copyright 2012-2021 the original author or authors. - */ -package org.assertj.core.internal.paths; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatExceptionOfType; -import static org.assertj.core.api.Assertions.assertThatNullPointerException; -import static org.assertj.core.api.Assertions.catchThrowable; -import static org.assertj.core.error.ShouldBeReadable.shouldBeReadable; -import static org.assertj.core.error.ShouldExist.shouldExist; -import static org.assertj.core.error.ShouldHaveContent.shouldHaveContent; -import static org.assertj.core.test.TestData.someInfo; -import static org.assertj.core.util.FailureMessages.actualIsNull; -import static org.assertj.core.util.Lists.newArrayList; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -import java.io.File; -import java.io.IOException; -import java.io.UncheckedIOException; -import java.nio.charset.Charset; -import java.nio.file.Path; -import java.util.ArrayList; -import java.util.List; - -import org.assertj.core.api.AssertionInfo; -import org.assertj.core.internal.Paths; -import org.assertj.core.internal.PathsBaseTest; -import org.assertj.core.util.diff.Delta; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; - -/** - * Tests for {@link Paths#assertHasContent(AssertionInfo, Path, String, Charset)}. - * - * @author Olivier Michallat - * @author Joel Costigliola - */ -class Paths_assertHasContent_Test extends PathsBaseTest { - - private static Path path; - private static String expected; - private static Charset charset; - private Path mockPath; - - @BeforeAll - static void setUpOnce() { - // Does not matter if the values differ, the actual comparison is mocked in this test - path = new File("src/test/resources/actual_file.txt").toPath(); - expected = "xyz"; - charset = Charset.defaultCharset(); - } - - @BeforeEach - void init() { - mockPath = mock(Path.class); - } - - @Test - void should_pass_if_path_has_expected_text_content() throws IOException { - when(diff.diff(path, expected, charset)).thenReturn(new ArrayList<>()); - when(nioFilesWrapper.exists(path)).thenReturn(true); - when(nioFilesWrapper.isReadable(path)).thenReturn(true); - paths.assertHasContent(someInfo(), path, expected, charset); - } - - @Test - void should_throw_error_if_expected_is_null() { - assertThatNullPointerException().isThrownBy(() -> paths.assertHasContent(someInfo(), path, null, charset)) - .withMessage("The text to compare to should not be null"); - } - - @Test - void should_fail_if_actual_is_null() { - assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> paths.assertHasContent(someInfo(), null, expected, charset)) - .withMessage(actualIsNull()); - } - - @Test - void should_fail_if_actual_path_does_not_exist() { - AssertionInfo info = someInfo(); - when(nioFilesWrapper.exists(mockPath)).thenReturn(false); - - Throwable error = catchThrowable(() -> paths.assertHasContent(info, mockPath, expected, charset)); - - assertThat(error).isInstanceOf(AssertionError.class); - verify(failures).failure(info, shouldExist(mockPath)); - } - - @Test - void should_fail_if_actual_is_not_a_readable_file() { - AssertionInfo info = someInfo(); - when(nioFilesWrapper.exists(mockPath)).thenReturn(true); - when(nioFilesWrapper.isReadable(mockPath)).thenReturn(false); - - Throwable error = catchThrowable(() -> paths.assertHasContent(info, mockPath, expected, charset)); - - assertThat(error).isInstanceOf(AssertionError.class); - verify(failures).failure(info, shouldBeReadable(mockPath)); - } - - @Test - void should_throw_error_wrapping_caught_IOException() throws IOException { - IOException cause = new IOException(); - when(diff.diff(path, expected, charset)).thenThrow(cause); - when(nioFilesWrapper.exists(path)).thenReturn(true); - when(nioFilesWrapper.isReadable(path)).thenReturn(true); - - assertThatExceptionOfType(UncheckedIOException.class).isThrownBy(() -> paths.assertHasContent(someInfo(), path, - expected, charset)) - .withCause(cause); - } - - @Test - void should_fail_if_path_does_not_have_expected_text_content() throws IOException { - List> diffs = newArrayList((Delta) mock(Delta.class)); - when(diff.diff(path, expected, charset)).thenReturn(diffs); - when(nioFilesWrapper.exists(path)).thenReturn(true); - when(nioFilesWrapper.isReadable(path)).thenReturn(true); - AssertionInfo info = someInfo(); - - Throwable error = catchThrowable(() -> paths.assertHasContent(info, path, expected, charset)); - - assertThat(error).isInstanceOf(AssertionError.class); - verify(failures).failure(info, shouldHaveContent(path, charset, diffs)); - } -} diff --git a/src/test/java/org/assertj/core/internal/paths/Paths_assertHasDigest_AlgorithmBytes_Test.java b/src/test/java/org/assertj/core/internal/paths/Paths_assertHasDigest_AlgorithmBytes_Test.java deleted file mode 100644 index cf0625faf6..0000000000 --- a/src/test/java/org/assertj/core/internal/paths/Paths_assertHasDigest_AlgorithmBytes_Test.java +++ /dev/null @@ -1,155 +0,0 @@ -/* - * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on - * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - * - * Copyright 2012-2021 the original author or authors. - */ -package org.assertj.core.internal.paths; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatExceptionOfType; -import static org.assertj.core.api.Assertions.assertThatNullPointerException; -import static org.assertj.core.api.Assertions.catchThrowable; -import static org.assertj.core.error.ShouldBeReadable.shouldBeReadable; -import static org.assertj.core.error.ShouldBeRegularFile.shouldBeRegularFile; -import static org.assertj.core.error.ShouldExist.shouldExist; -import static org.assertj.core.error.ShouldHaveDigest.shouldHaveDigest; -import static org.assertj.core.util.FailureMessages.actualIsNull; -import static org.mockito.BDDMockito.given; -import static org.mockito.Mockito.verify; - -import java.io.IOException; -import java.io.InputStream; -import java.io.UncheckedIOException; -import java.nio.file.Path; -import java.security.MessageDigest; -import java.security.NoSuchAlgorithmException; - -import org.assertj.core.api.AssertionInfo; -import org.assertj.core.internal.DigestDiff; -import org.assertj.core.internal.Digests; -import org.assertj.core.internal.Paths; -import org.junit.jupiter.api.Test; - -/** - * Tests for {@link Paths#assertHasDigest(AssertionInfo, Path, String, byte[])} - * - * @author Valeriy Vyrva - */ -class Paths_assertHasDigest_AlgorithmBytes_Test extends MockPathsBaseTest { - private final String algorithm = "MD5"; - private final byte[] expected = new byte[0]; - private final String real = "3AC1AFA2A89B7E4F1866502877BF1DC5"; - - @Test - void should_fail_if_actual_is_null() { - assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> paths.assertHasDigest(info, null, algorithm, expected)) - .withMessage(actualIsNull()); - } - - @Test - void should_fail_with_should_exist_error_if_actual_does_not_exist() { - // GIVEN - given(nioFilesWrapper.exists(actual)).willReturn(false); - // WHEN - catchThrowable(() -> paths.assertHasDigest(INFO, actual, algorithm, expected)); - // THEN - verify(failures).failure(INFO, shouldExist(actual)); - } - - @Test - void should_fail_if_actual_exists_but_is_not_file() { - // GIVEN - given(nioFilesWrapper.exists(actual)).willReturn(true); - given(nioFilesWrapper.isRegularFile(actual)).willReturn(false); - // WHEN - catchThrowable(() -> paths.assertHasDigest(INFO, actual, algorithm, expected)); - // THEN - verify(failures).failure(INFO, shouldBeRegularFile(actual)); - } - - @Test - void should_fail_if_actual_exists_but_is_not_readable() { - // GIVEN - given(nioFilesWrapper.exists(actual)).willReturn(true); - given(nioFilesWrapper.isRegularFile(actual)).willReturn(true); - given(nioFilesWrapper.isReadable(actual)).willReturn(false); - // WHEN - catchThrowable(() -> paths.assertHasDigest(INFO, actual, algorithm, expected)); - // THEN - verify(failures).failure(INFO, shouldBeReadable(actual)); - } - - @Test - void should_throw_error_if_digest_is_null() { - assertThatNullPointerException().isThrownBy(() -> paths.assertHasDigest(INFO, null, (MessageDigest) null, expected)) - .withMessage("The message digest algorithm should not be null"); - } - - @Test - void should_throw_error_if_expected_is_null() { - assertThatNullPointerException().isThrownBy(() -> paths.assertHasDigest(INFO, null, algorithm, (byte[]) null)) - .withMessage("The binary representation of digest to compare to should not be null"); - } - - @Test - void should_throw_error_wrapping_caught_IOException() throws IOException { - // GIVEN - IOException cause = new IOException(); - given(nioFilesWrapper.exists(actual)).willReturn(true); - given(nioFilesWrapper.isRegularFile(actual)).willReturn(true); - given(nioFilesWrapper.isReadable(actual)).willReturn(true); - given(nioFilesWrapper.newInputStream(actual)).willThrow(cause); - // WHEN - Throwable error = catchThrowable(() -> paths.assertHasDigest(INFO, actual, algorithm, expected)); - // THEN - assertThat(error).isInstanceOf(UncheckedIOException.class) - .hasCause(cause); - } - - @Test - void should_throw_error_wrapping_caught_NoSuchAlgorithmException() { - // GIVEN - String unknownDigestAlgorithm = "UnknownDigestAlgorithm"; - // WHEN - Throwable error = catchThrowable(() -> paths.assertHasDigest(INFO, actual, unknownDigestAlgorithm, expected)); - // THEN - assertThat(error).isInstanceOf(IllegalStateException.class) - .hasMessage("Unable to find digest implementation for: "); - } - - @Test - void should_fail_if_actual_does_not_have_expected_digest() throws IOException, NoSuchAlgorithmException { - // GIVEN - InputStream stream = getClass().getResourceAsStream("/red.png"); - given(nioFilesWrapper.exists(actual)).willReturn(true); - given(nioFilesWrapper.isRegularFile(actual)).willReturn(true); - given(nioFilesWrapper.isReadable(actual)).willReturn(true); - given(nioFilesWrapper.newInputStream(actual)).willReturn(stream); - // WHEN - catchThrowable(() -> paths.assertHasDigest(INFO, actual, algorithm, expected)); - // THEN - verify(failures).failure(INFO, shouldHaveDigest(actual, new DigestDiff(real, "", MessageDigest.getInstance(algorithm)))); - failIfStreamIsOpen(stream); - } - - @Test - void should_pass_if_actual_has_expected_digest() throws IOException { - // GIVEN - InputStream stream = getClass().getResourceAsStream("/red.png"); - given(nioFilesWrapper.exists(actual)).willReturn(true); - given(nioFilesWrapper.isRegularFile(actual)).willReturn(true); - given(nioFilesWrapper.isReadable(actual)).willReturn(true); - given(nioFilesWrapper.newInputStream(actual)).willReturn(stream); - // WHEN - paths.assertHasDigest(INFO, actual, algorithm, Digests.fromHex(real)); - // THEN - failIfStreamIsOpen(stream); - } -} diff --git a/src/test/java/org/assertj/core/internal/paths/Paths_assertHasDigest_AlgorithmString_Test.java b/src/test/java/org/assertj/core/internal/paths/Paths_assertHasDigest_AlgorithmString_Test.java deleted file mode 100644 index b12bcb1480..0000000000 --- a/src/test/java/org/assertj/core/internal/paths/Paths_assertHasDigest_AlgorithmString_Test.java +++ /dev/null @@ -1,155 +0,0 @@ -/* - * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on - * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - * - * Copyright 2012-2021 the original author or authors. - */ -package org.assertj.core.internal.paths; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatExceptionOfType; -import static org.assertj.core.api.Assertions.assertThatNullPointerException; -import static org.assertj.core.api.Assertions.catchThrowable; -import static org.assertj.core.error.ShouldBeReadable.shouldBeReadable; -import static org.assertj.core.error.ShouldBeRegularFile.shouldBeRegularFile; -import static org.assertj.core.error.ShouldExist.shouldExist; -import static org.assertj.core.error.ShouldHaveDigest.shouldHaveDigest; -import static org.assertj.core.util.FailureMessages.actualIsNull; -import static org.mockito.BDDMockito.given; -import static org.mockito.Mockito.verify; - -import java.io.IOException; -import java.io.InputStream; -import java.io.UncheckedIOException; -import java.nio.file.Path; -import java.security.MessageDigest; -import java.security.NoSuchAlgorithmException; - -import org.assertj.core.api.AssertionInfo; -import org.assertj.core.internal.DigestDiff; -import org.assertj.core.internal.Digests; -import org.assertj.core.internal.Paths; -import org.junit.jupiter.api.Test; - -/** - * Tests for {@link Paths#assertHasDigest(AssertionInfo, Path, String, String)} - * - * @author Valeriy Vyrva - */ -class Paths_assertHasDigest_AlgorithmString_Test extends MockPathsBaseTest { - private final String algorithm = "MD5"; - private final String expected = ""; - private final String real = "3AC1AFA2A89B7E4F1866502877BF1DC5"; - - @Test - void should_fail_if_actual_is_null() { - assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> paths.assertHasDigest(info, null, algorithm, expected)) - .withMessage(actualIsNull()); - } - - @Test - void should_fail_with_should_exist_error_if_actual_does_not_exist() { - // GIVEN - given(nioFilesWrapper.exists(actual)).willReturn(false); - // WHEN - catchThrowable(() -> paths.assertHasDigest(INFO, actual, algorithm, expected)); - // THEN - verify(failures).failure(INFO, shouldExist(actual)); - } - - @Test - void should_fail_if_actual_exists_but_is_not_file() { - // GIVEN - given(nioFilesWrapper.exists(actual)).willReturn(true); - given(nioFilesWrapper.isRegularFile(actual)).willReturn(false); - // WHEN - catchThrowable(() -> paths.assertHasDigest(INFO, actual, algorithm, expected)); - // THEN - verify(failures).failure(INFO, shouldBeRegularFile(actual)); - } - - @Test - void should_fail_if_actual_exists_but_is_not_readable() { - // GIVEN - given(nioFilesWrapper.exists(actual)).willReturn(true); - given(nioFilesWrapper.isRegularFile(actual)).willReturn(true); - given(nioFilesWrapper.isReadable(actual)).willReturn(false); - // WHEN - catchThrowable(() -> paths.assertHasDigest(INFO, actual, algorithm, expected)); - // THEN - verify(failures).failure(INFO, shouldBeReadable(actual)); - } - - @Test - void should_throw_error_if_digest_is_null() { - assertThatNullPointerException().isThrownBy(() -> paths.assertHasDigest(INFO, null, (MessageDigest) null, expected)) - .withMessage("The message digest algorithm should not be null"); - } - - @Test - void should_throw_error_if_expected_is_null() { - assertThatNullPointerException().isThrownBy(() -> paths.assertHasDigest(INFO, null, algorithm, (byte[]) null)) - .withMessage("The binary representation of digest to compare to should not be null"); - } - - @Test - void should_throw_error_wrapping_caught_IOException() throws IOException { - // GIVEN - IOException cause = new IOException(); - given(nioFilesWrapper.exists(actual)).willReturn(true); - given(nioFilesWrapper.isRegularFile(actual)).willReturn(true); - given(nioFilesWrapper.isReadable(actual)).willReturn(true); - given(nioFilesWrapper.newInputStream(actual)).willThrow(cause); - // WHEN - Throwable error = catchThrowable(() -> paths.assertHasDigest(INFO, actual, algorithm, expected)); - // THEN - assertThat(error).isInstanceOf(UncheckedIOException.class) - .hasCause(cause); - } - - @Test - void should_throw_error_wrapping_caught_NoSuchAlgorithmException() { - // GIVEN - String unknownDigestAlgorithm = "UnknownDigestAlgorithm"; - // WHEN - Throwable error = catchThrowable(() -> paths.assertHasDigest(INFO, actual, unknownDigestAlgorithm, expected)); - // THEN - assertThat(error).isInstanceOf(IllegalStateException.class) - .hasMessage("Unable to find digest implementation for: "); - } - - @Test - void should_fail_if_actual_does_not_have_expected_digest() throws IOException, NoSuchAlgorithmException { - // GIVEN - InputStream stream = getClass().getResourceAsStream("/red.png"); - given(nioFilesWrapper.exists(actual)).willReturn(true); - given(nioFilesWrapper.isRegularFile(actual)).willReturn(true); - given(nioFilesWrapper.isReadable(actual)).willReturn(true); - given(nioFilesWrapper.newInputStream(actual)).willReturn(stream); - // WHEN - catchThrowable(() -> paths.assertHasDigest(INFO, actual, algorithm, expected)); - // THEN - verify(failures).failure(INFO, shouldHaveDigest(actual, new DigestDiff(real, "", MessageDigest.getInstance(algorithm)))); - failIfStreamIsOpen(stream); - } - - @Test - void should_pass_if_actual_has_expected_digest() throws IOException { - // GIVEN - InputStream stream = getClass().getResourceAsStream("/red.png"); - given(nioFilesWrapper.exists(actual)).willReturn(true); - given(nioFilesWrapper.isRegularFile(actual)).willReturn(true); - given(nioFilesWrapper.isReadable(actual)).willReturn(true); - given(nioFilesWrapper.newInputStream(actual)).willReturn(stream); - // WHEN - paths.assertHasDigest(INFO, actual, algorithm, Digests.fromHex(real)); - // THEN - failIfStreamIsOpen(stream); - } -} diff --git a/src/test/java/org/assertj/core/internal/paths/Paths_assertHasDigest_DigestBytes_Test.java b/src/test/java/org/assertj/core/internal/paths/Paths_assertHasDigest_DigestBytes_Test.java deleted file mode 100644 index 91705abead..0000000000 --- a/src/test/java/org/assertj/core/internal/paths/Paths_assertHasDigest_DigestBytes_Test.java +++ /dev/null @@ -1,155 +0,0 @@ -/* - * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on - * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - * - * Copyright 2012-2021 the original author or authors. - */ -package org.assertj.core.internal.paths; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatExceptionOfType; -import static org.assertj.core.api.Assertions.assertThatNullPointerException; -import static org.assertj.core.api.Assertions.catchThrowable; -import static org.assertj.core.error.ShouldBeReadable.shouldBeReadable; -import static org.assertj.core.error.ShouldBeRegularFile.shouldBeRegularFile; -import static org.assertj.core.error.ShouldExist.shouldExist; -import static org.assertj.core.error.ShouldHaveDigest.shouldHaveDigest; -import static org.assertj.core.util.FailureMessages.actualIsNull; -import static org.mockito.BDDMockito.given; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; - -import java.io.IOException; -import java.io.InputStream; -import java.io.UncheckedIOException; -import java.nio.file.Path; -import java.security.MessageDigest; - -import org.assertj.core.api.AssertionInfo; -import org.assertj.core.internal.DigestDiff; -import org.assertj.core.internal.Paths; -import org.junit.jupiter.api.Test; - -/** - * Tests for {@link Paths#assertHasDigest(AssertionInfo, Path, MessageDigest, byte[])} - * - * @author Valeriy Vyrva - */ -class Paths_assertHasDigest_DigestBytes_Test extends MockPathsBaseTest { - private final MessageDigest digest = mock(MessageDigest.class); - private final byte[] expected = new byte[0]; - - @Test - void should_fail_if_actual_is_null() { - assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> paths.assertHasDigest(info, null, digest, expected)) - .withMessage(actualIsNull()); - } - - @Test - void should_fail_with_should_exist_error_if_actual_does_not_exist() { - // GIVEN - given(nioFilesWrapper.exists(actual)).willReturn(false); - // WHEN - catchThrowable(() -> paths.assertHasDigest(INFO, actual, digest, expected)); - // THEN - verify(failures).failure(INFO, shouldExist(actual)); - } - - @Test - void should_fail_if_actual_exists_but_is_not_file() { - // GIVEN - given(nioFilesWrapper.exists(actual)).willReturn(true); - given(nioFilesWrapper.isRegularFile(actual)).willReturn(false); - // WHEN - catchThrowable(() -> paths.assertHasDigest(INFO, actual, digest, expected)); - // THEN - verify(failures).failure(INFO, shouldBeRegularFile(actual)); - } - - @Test - void should_fail_if_actual_exists_but_is_not_readable() { - // GIVEN - given(nioFilesWrapper.exists(actual)).willReturn(true); - given(nioFilesWrapper.isRegularFile(actual)).willReturn(true); - given(nioFilesWrapper.isReadable(actual)).willReturn(false); - // WHEN - catchThrowable(() -> paths.assertHasDigest(INFO, actual, digest, expected)); - // THEN - verify(failures).failure(INFO, shouldBeReadable(actual)); - } - - @Test - void should_throw_error_if_digest_is_null() { - assertThatNullPointerException().isThrownBy(() -> paths.assertHasDigest(INFO, null, (MessageDigest) null, expected)) - .withMessage("The message digest algorithm should not be null"); - } - - @Test - void should_throw_error_if_expected_is_null() { - assertThatNullPointerException().isThrownBy(() -> paths.assertHasDigest(INFO, null, digest, (byte[]) null)) - .withMessage("The binary representation of digest to compare to should not be null"); - } - - @Test - void should_throw_error_wrapping_caught_IOException() throws IOException { - // GIVEN - IOException cause = new IOException(); - given(nioFilesWrapper.exists(actual)).willReturn(true); - given(nioFilesWrapper.isRegularFile(actual)).willReturn(true); - given(nioFilesWrapper.isReadable(actual)).willReturn(true); - given(nioFilesWrapper.newInputStream(actual)).willThrow(cause); - // WHEN - Throwable error = catchThrowable(() -> paths.assertHasDigest(INFO, actual, digest, expected)); - // THEN - assertThat(error).isInstanceOf(UncheckedIOException.class) - .hasCause(cause); - } - - @Test - void should_throw_error_wrapping_caught_NoSuchAlgorithmException() { - // GIVEN - String unknownDigestAlgorithm = "UnknownDigestAlgorithm"; - // WHEN - Throwable error = catchThrowable(() -> paths.assertHasDigest(INFO, actual, unknownDigestAlgorithm, expected)); - // THEN - assertThat(error).isInstanceOf(IllegalStateException.class) - .hasMessage("Unable to find digest implementation for: "); - } - - @Test - void should_fail_if_actual_does_not_have_expected_digest() throws IOException { - // GIVEN - InputStream stream = getClass().getResourceAsStream("/red.png"); - given(nioFilesWrapper.exists(actual)).willReturn(true); - given(nioFilesWrapper.isRegularFile(actual)).willReturn(true); - given(nioFilesWrapper.isReadable(actual)).willReturn(true); - given(nioFilesWrapper.newInputStream(actual)).willReturn(stream); - given(digest.digest()).willReturn(new byte[] { 0, 1 }); - // WHEN - catchThrowable(() -> paths.assertHasDigest(INFO, actual, digest, expected)); - // THEN - verify(failures).failure(INFO, shouldHaveDigest(actual, new DigestDiff("0001", "", digest))); - failIfStreamIsOpen(stream); - } - - @Test - void should_pass_if_actual_has_expected_digest() throws IOException { - // GIVEN - InputStream stream = getClass().getResourceAsStream("/red.png"); - given(nioFilesWrapper.exists(actual)).willReturn(true); - given(nioFilesWrapper.isRegularFile(actual)).willReturn(true); - given(nioFilesWrapper.isReadable(actual)).willReturn(true); - given(nioFilesWrapper.newInputStream(actual)).willReturn(stream); - given(digest.digest()).willReturn(expected); - // WHEN - paths.assertHasDigest(INFO, actual, digest, expected); - // THEN - failIfStreamIsOpen(stream); - } -} diff --git a/src/test/java/org/assertj/core/internal/paths/Paths_assertHasDigest_DigestString_Test.java b/src/test/java/org/assertj/core/internal/paths/Paths_assertHasDigest_DigestString_Test.java deleted file mode 100644 index 7f26ea4207..0000000000 --- a/src/test/java/org/assertj/core/internal/paths/Paths_assertHasDigest_DigestString_Test.java +++ /dev/null @@ -1,147 +0,0 @@ -/* - * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on - * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - * - * Copyright 2012-2021 the original author or authors. - */ -package org.assertj.core.internal.paths; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatNullPointerException; -import static org.assertj.core.api.Assertions.catchThrowable; -import static org.assertj.core.error.ShouldBeReadable.shouldBeReadable; -import static org.assertj.core.error.ShouldBeRegularFile.shouldBeRegularFile; -import static org.assertj.core.error.ShouldExist.shouldExist; -import static org.assertj.core.error.ShouldHaveDigest.shouldHaveDigest; -import static org.mockito.BDDMockito.given; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; - -import java.io.IOException; -import java.io.InputStream; -import java.io.UncheckedIOException; -import java.nio.file.Path; -import java.security.MessageDigest; - -import org.assertj.core.api.AssertionInfo; -import org.assertj.core.internal.DigestDiff; -import org.assertj.core.internal.Paths; -import org.junit.jupiter.api.Test; - -/** - * Tests for {@link Paths#assertHasDigest(AssertionInfo, Path, MessageDigest, String)} - * - * @author Valeriy Vyrva - */ -class Paths_assertHasDigest_DigestString_Test extends MockPathsBaseTest { - private final MessageDigest digest = mock(MessageDigest.class); - private final String expected = ""; - - @Test - void should_fail_with_should_exist_error_if_actual_does_not_exist() { - // GIVEN - given(nioFilesWrapper.exists(actual)).willReturn(false); - // WHEN - catchThrowable(() -> paths.assertHasDigest(INFO, actual, digest, expected)); - // THEN - verify(failures).failure(INFO, shouldExist(actual)); - } - - @Test - void should_fail_if_actual_exists_but_is_not_file() { - // GIVEN - given(nioFilesWrapper.exists(actual)).willReturn(true); - given(nioFilesWrapper.isRegularFile(actual)).willReturn(false); - // WHEN - catchThrowable(() -> paths.assertHasDigest(INFO, actual, digest, expected)); - // THEN - verify(failures).failure(INFO, shouldBeRegularFile(actual)); - } - - @Test - void should_fail_if_actual_exists_but_is_not_readable() { - // GIVEN - given(nioFilesWrapper.exists(actual)).willReturn(true); - given(nioFilesWrapper.isRegularFile(actual)).willReturn(true); - given(nioFilesWrapper.isReadable(actual)).willReturn(false); - // WHEN - catchThrowable(() -> paths.assertHasDigest(INFO, actual, digest, expected)); - // THEN - verify(failures).failure(INFO, shouldBeReadable(actual)); - } - - @Test - void should_throw_error_if_digest_is_null() { - assertThatNullPointerException().isThrownBy(() -> paths.assertHasDigest(INFO, null, (MessageDigest) null, expected)) - .withMessage("The message digest algorithm should not be null"); - } - - @Test - void should_throw_error_if_expected_is_null() { - assertThatNullPointerException().isThrownBy(() -> paths.assertHasDigest(INFO, null, digest, (byte[]) null)) - .withMessage("The binary representation of digest to compare to should not be null"); - } - - @Test - void should_throw_error_wrapping_caught_IOException() throws IOException { - // GIVEN - IOException cause = new IOException(); - given(nioFilesWrapper.exists(actual)).willReturn(true); - given(nioFilesWrapper.isRegularFile(actual)).willReturn(true); - given(nioFilesWrapper.isReadable(actual)).willReturn(true); - given(nioFilesWrapper.newInputStream(actual)).willThrow(cause); - // WHEN - Throwable error = catchThrowable(() -> paths.assertHasDigest(INFO, actual, digest, expected)); - // THEN - assertThat(error).isInstanceOf(UncheckedIOException.class) - .hasCause(cause); - } - - @Test - void should_throw_error_wrapping_caught_NoSuchAlgorithmException() { - // GIVEN - String unknownDigestAlgorithm = "UnknownDigestAlgorithm"; - // WHEN - Throwable error = catchThrowable(() -> paths.assertHasDigest(INFO, actual, unknownDigestAlgorithm, expected)); - // THEN - assertThat(error).isInstanceOf(IllegalStateException.class) - .hasMessage("Unable to find digest implementation for: "); - } - - @Test - void should_fail_if_actual_does_not_have_expected_digest() throws IOException { - // GIVEN - InputStream stream = getClass().getResourceAsStream("/red.png"); - given(nioFilesWrapper.exists(actual)).willReturn(true); - given(nioFilesWrapper.isRegularFile(actual)).willReturn(true); - given(nioFilesWrapper.isReadable(actual)).willReturn(true); - given(nioFilesWrapper.newInputStream(actual)).willReturn(stream); - given(digest.digest()).willReturn(new byte[] { 0, 1 }); - // WHEN - catchThrowable(() -> paths.assertHasDigest(INFO, actual, digest, expected)); - // THEN - verify(failures).failure(INFO, shouldHaveDigest(actual, new DigestDiff("0001", "", digest))); - failIfStreamIsOpen(stream); - } - - @Test - void should_pass_if_actual_has_expected_digest() throws IOException { - // GIVEN - InputStream stream = getClass().getResourceAsStream("/red.png"); - given(nioFilesWrapper.exists(actual)).willReturn(true); - given(nioFilesWrapper.isRegularFile(actual)).willReturn(true); - given(nioFilesWrapper.isReadable(actual)).willReturn(true); - given(nioFilesWrapper.newInputStream(actual)).willReturn(stream); - given(digest.digest()).willReturn(expected.getBytes()); - // WHEN - paths.assertHasDigest(INFO, actual, digest, expected); - // THEN - failIfStreamIsOpen(stream); - } -} diff --git a/src/test/java/org/assertj/core/internal/paths/Paths_assertHasDigest_with_MessageDigest_and_Byte_array_Test.java b/src/test/java/org/assertj/core/internal/paths/Paths_assertHasDigest_with_MessageDigest_and_Byte_array_Test.java new file mode 100644 index 0000000000..cb8ac78540 --- /dev/null +++ b/src/test/java/org/assertj/core/internal/paths/Paths_assertHasDigest_with_MessageDigest_and_Byte_array_Test.java @@ -0,0 +1,163 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + * + * Copyright 2012-2021 the original author or authors. + */ +package org.assertj.core.internal.paths; + +import static java.nio.file.Files.createDirectory; +import static java.nio.file.Files.createFile; +import static java.nio.file.Files.readAllBytes; +import static org.assertj.core.api.Assertions.catchThrowable; +import static org.assertj.core.api.BDDAssertions.then; +import static org.assertj.core.error.ShouldBeReadable.shouldBeReadable; +import static org.assertj.core.error.ShouldBeRegularFile.shouldBeRegularFile; +import static org.assertj.core.error.ShouldExist.shouldExist; +import static org.assertj.core.error.ShouldHaveDigest.shouldHaveDigest; +import static org.assertj.core.internal.Digests.toHex; +import static org.assertj.core.util.AssertionsUtil.expectAssertionError; +import static org.assertj.core.util.FailureMessages.actualIsNull; +import static org.junit.jupiter.api.condition.OS.WINDOWS; +import static org.mockito.BDDMockito.given; + +import java.io.IOException; +import java.io.UncheckedIOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; + +import org.assertj.core.internal.DigestDiff; +import org.assertj.core.internal.PathsBaseTest; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.condition.DisabledOnOs; + +/** + * @author Valeriy Vyrva + */ +class Paths_assertHasDigest_with_MessageDigest_and_Byte_array_Test extends PathsBaseTest { + + @Test + void should_fail_if_digest_is_null() throws IOException { + // GIVEN + Path actual = createFile(tempDir.resolve("actual")); + MessageDigest digest = null; + byte[] expected = {}; + // WHEN + Throwable thrown = catchThrowable(() -> paths.assertHasDigest(info, actual, digest, expected)); + // THEN + then(thrown).isInstanceOf(NullPointerException.class) + .hasMessage("The message digest algorithm should not be null"); + } + + @Test + void should_fail_if_expected_is_null() throws Exception { + // GIVEN + Path actual = createFile(tempDir.resolve("actual")); + MessageDigest digest = MessageDigest.getInstance("MD5"); + byte[] expected = null; + // WHEN + Throwable thrown = catchThrowable(() -> paths.assertHasDigest(info, actual, digest, expected)); + // THEN + then(thrown).isInstanceOf(NullPointerException.class) + .hasMessage("The binary representation of digest to compare to should not be null"); + } + + @Test + void should_fail_if_actual_is_null() throws NoSuchAlgorithmException { + // GIVEN + Path actual = null; + MessageDigest digest = MessageDigest.getInstance("MD5"); + byte[] expected = {}; + // WHEN + AssertionError error = expectAssertionError(() -> paths.assertHasDigest(info, actual, digest, expected)); + // THEN + then(error).hasMessage(actualIsNull()); + } + + @Test + void should_fail_if_actual_does_not_exist() throws NoSuchAlgorithmException { + // GIVEN + Path actual = tempDir.resolve("non-existent"); + MessageDigest digest = MessageDigest.getInstance("MD5"); + byte[] expected = {}; + // WHEN + AssertionError error = expectAssertionError(() -> paths.assertHasDigest(info, actual, digest, expected)); + // THEN + then(error).hasMessage(shouldExist(actual).create()); + } + + @Test + void should_fail_if_actual_is_not_a_regular_file() throws Exception { + // GIVEN + Path actual = createDirectory(tempDir.resolve("directory")); + MessageDigest digest = MessageDigest.getInstance("MD5"); + byte[] expected = {}; + // WHEN + AssertionError error = expectAssertionError(() -> paths.assertHasDigest(info, actual, digest, expected)); + // THEN + then(error).hasMessage(shouldBeRegularFile(actual).create()); + } + + @Test + @DisabledOnOs(value = WINDOWS, disabledReason = "gh-2312") + void should_fail_if_actual_is_not_readable() throws Exception { + // GIVEN + Path actual = createFile(tempDir.resolve("actual")); + actual.toFile().setReadable(false); + MessageDigest digest = MessageDigest.getInstance("MD5"); + byte[] expected = {}; + // WHEN + AssertionError error = expectAssertionError(() -> paths.assertHasDigest(info, actual, digest, expected)); + // THEN + then(error).hasMessage(shouldBeReadable(actual).create()); + } + + @Test + @DisabledOnOs(value = WINDOWS, disabledReason = "gh-2312") + void should_rethrow_IOException_as_UncheckedIOException() throws Exception { + // GIVEN + Path actual = createFile(tempDir.resolve("actual")); + MessageDigest digest = MessageDigest.getInstance("MD5"); + byte[] expected = {}; + IOException cause = new IOException("boom!"); + given(nioFilesWrapper.newInputStream(actual)).willThrow(cause); + // WHEN + Throwable thrown = catchThrowable(() -> paths.assertHasDigest(info, actual, digest, expected)); + // THEN + then(thrown).isInstanceOf(UncheckedIOException.class) + .hasCause(cause); + } + + @Test + void should_fail_if_actual_does_not_have_expected_digest() throws Exception { + // GIVEN + Path actual = Files.write(tempDir.resolve("actual"), "Content".getBytes()); + MessageDigest digest = MessageDigest.getInstance("MD5"); + byte[] expected = digest.digest("Another content".getBytes()); + // WHEN + AssertionError error = expectAssertionError(() -> paths.assertHasDigest(info, actual, digest, expected)); + // THEN + then(error).hasMessage(shouldHaveDigest(actual, new DigestDiff(toHex(digest.digest(readAllBytes(actual))), + toHex(expected), + digest)).create()); + } + + @Test + void should_pass_if_actual_has_expected_digest() throws Exception { + // GIVEN + Path actual = Files.write(tempDir.resolve("actual"), "Content".getBytes()); + MessageDigest digest = MessageDigest.getInstance("MD5"); + byte[] expected = digest.digest(readAllBytes(actual)); + // WHEN/THEN + paths.assertHasDigest(info, actual, digest, expected); + } + +} diff --git a/src/test/java/org/assertj/core/internal/paths/Paths_assertHasDigest_with_MessageDigest_and_String_Test.java b/src/test/java/org/assertj/core/internal/paths/Paths_assertHasDigest_with_MessageDigest_and_String_Test.java new file mode 100644 index 0000000000..8037ac36eb --- /dev/null +++ b/src/test/java/org/assertj/core/internal/paths/Paths_assertHasDigest_with_MessageDigest_and_String_Test.java @@ -0,0 +1,163 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + * + * Copyright 2012-2021 the original author or authors. + */ +package org.assertj.core.internal.paths; + +import static java.nio.file.Files.createDirectory; +import static java.nio.file.Files.createFile; +import static java.nio.file.Files.readAllBytes; +import static org.assertj.core.api.Assertions.catchThrowable; +import static org.assertj.core.api.BDDAssertions.then; +import static org.assertj.core.error.ShouldBeReadable.shouldBeReadable; +import static org.assertj.core.error.ShouldBeRegularFile.shouldBeRegularFile; +import static org.assertj.core.error.ShouldExist.shouldExist; +import static org.assertj.core.error.ShouldHaveDigest.shouldHaveDigest; +import static org.assertj.core.internal.Digests.toHex; +import static org.assertj.core.util.AssertionsUtil.expectAssertionError; +import static org.assertj.core.util.FailureMessages.actualIsNull; +import static org.junit.jupiter.api.condition.OS.WINDOWS; +import static org.mockito.BDDMockito.given; + +import java.io.IOException; +import java.io.UncheckedIOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; + +import org.assertj.core.internal.DigestDiff; +import org.assertj.core.internal.PathsBaseTest; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.condition.DisabledOnOs; + +/** + * @author Valeriy Vyrva + */ +class Paths_assertHasDigest_with_MessageDigest_and_String_Test extends PathsBaseTest { + + @Test + void should_fail_if_digest_is_null() throws IOException { + // GIVEN + Path actual = createFile(tempDir.resolve("actual")); + MessageDigest digest = null; + String expected = ""; + // WHEN + Throwable thrown = catchThrowable(() -> paths.assertHasDigest(info, actual, digest, expected)); + // THEN + then(thrown).isInstanceOf(NullPointerException.class) + .hasMessage("The message digest algorithm should not be null"); + } + + @Test + void should_fail_if_expected_is_null() throws Exception { + // GIVEN + Path actual = createFile(tempDir.resolve("actual")); + MessageDigest digest = MessageDigest.getInstance("MD5"); + String expected = null; + // WHEN + Throwable thrown = catchThrowable(() -> paths.assertHasDigest(info, actual, digest, expected)); + // THEN + then(thrown).isInstanceOf(NullPointerException.class) + .hasMessage("The string representation of digest to compare to should not be null"); + } + + @Test + void should_fail_if_actual_is_null() throws NoSuchAlgorithmException { + // GIVEN + Path actual = null; + MessageDigest digest = MessageDigest.getInstance("MD5"); + String expected = ""; + // WHEN + AssertionError error = expectAssertionError(() -> paths.assertHasDigest(info, actual, digest, expected)); + // THEN + then(error).hasMessage(actualIsNull()); + } + + @Test + void should_fail_if_actual_does_not_exist() throws NoSuchAlgorithmException { + // GIVEN + Path actual = tempDir.resolve("non-existent"); + MessageDigest digest = MessageDigest.getInstance("MD5"); + String expected = ""; + // WHEN + AssertionError error = expectAssertionError(() -> paths.assertHasDigest(info, actual, digest, expected)); + // THEN + then(error).hasMessage(shouldExist(actual).create()); + } + + @Test + void should_fail_if_actual_is_not_a_regular_file() throws Exception { + // GIVEN + Path actual = createDirectory(tempDir.resolve("directory")); + MessageDigest digest = MessageDigest.getInstance("MD5"); + String expected = ""; + // WHEN + AssertionError error = expectAssertionError(() -> paths.assertHasDigest(info, actual, digest, expected)); + // THEN + then(error).hasMessage(shouldBeRegularFile(actual).create()); + } + + @Test + @DisabledOnOs(value = WINDOWS, disabledReason = "gh-2312") + void should_fail_if_actual_is_not_readable() throws Exception { + // GIVEN + Path actual = createFile(tempDir.resolve("actual")); + actual.toFile().setReadable(false); + MessageDigest digest = MessageDigest.getInstance("MD5"); + String expected = ""; + // WHEN + AssertionError error = expectAssertionError(() -> paths.assertHasDigest(info, actual, digest, expected)); + // THEN + then(error).hasMessage(shouldBeReadable(actual).create()); + } + + @Test + @DisabledOnOs(value = WINDOWS, disabledReason = "gh-2312") + void should_rethrow_IOException_as_UncheckedIOException() throws Exception { + // GIVEN + Path actual = createFile(tempDir.resolve("actual")); + MessageDigest digest = MessageDigest.getInstance("MD5"); + String expected = ""; + IOException cause = new IOException("boom!"); + given(nioFilesWrapper.newInputStream(actual)).willThrow(cause); + // WHEN + Throwable thrown = catchThrowable(() -> paths.assertHasDigest(info, actual, digest, expected)); + // THEN + then(thrown).isInstanceOf(UncheckedIOException.class) + .hasCause(cause); + } + + @Test + void should_fail_if_actual_does_not_have_expected_digest() throws Exception { + // GIVEN + Path actual = Files.write(tempDir.resolve("actual"), "Content".getBytes()); + MessageDigest digest = MessageDigest.getInstance("MD5"); + String expected = toHex(digest.digest("Another content".getBytes())); + // WHEN + AssertionError error = expectAssertionError(() -> paths.assertHasDigest(info, actual, digest, expected)); + // THEN + then(error).hasMessage(shouldHaveDigest(actual, new DigestDiff(toHex(digest.digest(readAllBytes(actual))), + expected, + digest)).create()); + } + + @Test + void should_pass_if_actual_has_expected_digest() throws Exception { + // GIVEN + Path actual = Files.write(tempDir.resolve("actual"), "Content".getBytes()); + MessageDigest digest = MessageDigest.getInstance("MD5"); + String expected = toHex(digest.digest(readAllBytes(actual))); + // WHEN/THEN + paths.assertHasDigest(info, actual, digest, expected); + } + +} diff --git a/src/test/java/org/assertj/core/internal/paths/Paths_assertHasDigest_with_String_and_Byte_array_Test.java b/src/test/java/org/assertj/core/internal/paths/Paths_assertHasDigest_with_String_and_Byte_array_Test.java new file mode 100644 index 0000000000..46722a9c59 --- /dev/null +++ b/src/test/java/org/assertj/core/internal/paths/Paths_assertHasDigest_with_String_and_Byte_array_Test.java @@ -0,0 +1,178 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + * + * Copyright 2012-2021 the original author or authors. + */ +package org.assertj.core.internal.paths; + +import static java.nio.file.Files.createDirectory; +import static java.nio.file.Files.createFile; +import static java.nio.file.Files.readAllBytes; +import static org.assertj.core.api.Assertions.catchThrowable; +import static org.assertj.core.api.BDDAssertions.then; +import static org.assertj.core.error.ShouldBeReadable.shouldBeReadable; +import static org.assertj.core.error.ShouldBeRegularFile.shouldBeRegularFile; +import static org.assertj.core.error.ShouldExist.shouldExist; +import static org.assertj.core.error.ShouldHaveDigest.shouldHaveDigest; +import static org.assertj.core.internal.Digests.toHex; +import static org.assertj.core.util.AssertionsUtil.expectAssertionError; +import static org.assertj.core.util.FailureMessages.actualIsNull; +import static org.junit.jupiter.api.condition.OS.WINDOWS; +import static org.mockito.BDDMockito.given; + +import java.io.IOException; +import java.io.UncheckedIOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; + +import org.assertj.core.internal.DigestDiff; +import org.assertj.core.internal.PathsBaseTest; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.condition.DisabledOnOs; + +/** + * @author Valeriy Vyrva + */ +class Paths_assertHasDigest_with_String_and_Byte_array_Test extends PathsBaseTest { + + @Test + void should_fail_if_algorithm_is_null() throws IOException { + // GIVEN + Path actual = createFile(tempDir.resolve("actual")); + String algorithm = null; + byte[] expected = {}; + // WHEN + Throwable thrown = catchThrowable(() -> paths.assertHasDigest(info, actual, algorithm, expected)); + // THEN + then(thrown).isInstanceOf(NullPointerException.class) + .hasMessage("The message digest algorithm should not be null"); + } + + @Test + void should_fail_if_algorithm_is_invalid() throws IOException { + // GIVEN + Path actual = createFile(tempDir.resolve("actual")); + String algorithm = "invalid"; + byte[] expected = {}; + // WHEN + Throwable thrown = catchThrowable(() -> paths.assertHasDigest(info, actual, algorithm, expected)); + // THEN + then(thrown).isInstanceOf(IllegalStateException.class) + .hasMessage("Unable to find digest implementation for: ") + .hasCauseInstanceOf(NoSuchAlgorithmException.class); + } + + @Test + void should_fail_if_expected_is_null() throws Exception { + // GIVEN + Path actual = createFile(tempDir.resolve("actual")); + String algorithm = "MD5"; + byte[] expected = null; + // WHEN + Throwable thrown = catchThrowable(() -> paths.assertHasDigest(info, actual, algorithm, expected)); + // THEN + then(thrown).isInstanceOf(NullPointerException.class) + .hasMessage("The binary representation of digest to compare to should not be null"); + } + + @Test + void should_fail_if_actual_is_null() throws NoSuchAlgorithmException { + // GIVEN + Path actual = null; + String algorithm = "MD5"; + byte[] expected = {}; + // WHEN + AssertionError error = expectAssertionError(() -> paths.assertHasDigest(info, actual, algorithm, expected)); + // THEN + then(error).hasMessage(actualIsNull()); + } + + @Test + void should_fail_if_actual_does_not_exist() throws NoSuchAlgorithmException { + // GIVEN + Path actual = tempDir.resolve("non-existent"); + String algorithm = "MD5"; + byte[] expected = {}; + // WHEN + AssertionError error = expectAssertionError(() -> paths.assertHasDigest(info, actual, algorithm, expected)); + // THEN + then(error).hasMessage(shouldExist(actual).create()); + } + + @Test + void should_fail_if_actual_is_not_a_regular_file() throws Exception { + // GIVEN + Path actual = createDirectory(tempDir.resolve("directory")); + String algorithm = "MD5"; + byte[] expected = {}; + // WHEN + AssertionError error = expectAssertionError(() -> paths.assertHasDigest(info, actual, algorithm, expected)); + // THEN + then(error).hasMessage(shouldBeRegularFile(actual).create()); + } + + @Test + @DisabledOnOs(value = WINDOWS, disabledReason = "gh-2312") + void should_fail_if_actual_is_not_readable() throws IOException { + // GIVEN + Path actual = createFile(tempDir.resolve("actual")); + actual.toFile().setReadable(false); + String algorithm = "MD5"; + byte[] expected = {}; + // WHEN + AssertionError error = expectAssertionError(() -> paths.assertHasDigest(info, actual, algorithm, expected)); + // THEN + then(error).hasMessage(shouldBeReadable(actual).create()); + } + + @Test + @DisabledOnOs(value = WINDOWS, disabledReason = "gh-2312") + void should_rethrow_IOException_as_UncheckedIOException() throws Exception { + // GIVEN + Path actual = createFile(tempDir.resolve("actual")); + String algorithm = "MD5"; + byte[] expected = {}; + IOException cause = new IOException("boom!"); + given(nioFilesWrapper.newInputStream(actual)).willThrow(cause); + // WHEN + Throwable thrown = catchThrowable(() -> paths.assertHasDigest(info, actual, algorithm, expected)); + // THEN + then(thrown).isInstanceOf(UncheckedIOException.class) + .hasCause(cause); + } + + @Test + void should_fail_if_actual_does_not_have_expected_digest() throws Exception { + // GIVEN + Path actual = Files.write(tempDir.resolve("actual"), "Content".getBytes()); + String algorithm = "MD5"; + MessageDigest digest = MessageDigest.getInstance(algorithm); + byte[] expected = digest.digest("Another content".getBytes()); + // WHEN + AssertionError error = expectAssertionError(() -> paths.assertHasDigest(info, actual, algorithm, expected)); + // THEN + then(error).hasMessage(shouldHaveDigest(actual, new DigestDiff(toHex(digest.digest(readAllBytes(actual))), + toHex(expected), + digest)).create()); + } + + @Test + void should_pass_if_actual_has_expected_digest() throws Exception { + // GIVEN + Path actual = Files.write(tempDir.resolve("actual"), "Content".getBytes()); + String algorithm = "MD5"; + byte[] expected = MessageDigest.getInstance(algorithm).digest(readAllBytes(actual)); + // WHEN/THEN + paths.assertHasDigest(info, actual, algorithm, expected); + } + +} diff --git a/src/test/java/org/assertj/core/internal/paths/Paths_assertHasDigest_with_String_and_String_Test.java b/src/test/java/org/assertj/core/internal/paths/Paths_assertHasDigest_with_String_and_String_Test.java new file mode 100644 index 0000000000..c7f207c03f --- /dev/null +++ b/src/test/java/org/assertj/core/internal/paths/Paths_assertHasDigest_with_String_and_String_Test.java @@ -0,0 +1,178 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + * + * Copyright 2012-2021 the original author or authors. + */ +package org.assertj.core.internal.paths; + +import static java.nio.file.Files.createDirectory; +import static java.nio.file.Files.createFile; +import static java.nio.file.Files.readAllBytes; +import static org.assertj.core.api.Assertions.catchThrowable; +import static org.assertj.core.api.BDDAssertions.then; +import static org.assertj.core.error.ShouldBeReadable.shouldBeReadable; +import static org.assertj.core.error.ShouldBeRegularFile.shouldBeRegularFile; +import static org.assertj.core.error.ShouldExist.shouldExist; +import static org.assertj.core.error.ShouldHaveDigest.shouldHaveDigest; +import static org.assertj.core.internal.Digests.toHex; +import static org.assertj.core.util.AssertionsUtil.expectAssertionError; +import static org.assertj.core.util.FailureMessages.actualIsNull; +import static org.junit.jupiter.api.condition.OS.WINDOWS; +import static org.mockito.BDDMockito.given; + +import java.io.IOException; +import java.io.UncheckedIOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; + +import org.assertj.core.internal.DigestDiff; +import org.assertj.core.internal.PathsBaseTest; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.condition.DisabledOnOs; + +/** + * @author Valeriy Vyrva + */ +class Paths_assertHasDigest_with_String_and_String_Test extends PathsBaseTest { + + @Test + void should_fail_if_algorithm_is_null() throws IOException { + // GIVEN + Path actual = createFile(tempDir.resolve("actual")); + String algorithm = null; + String expected = ""; + // WHEN + Throwable thrown = catchThrowable(() -> paths.assertHasDigest(info, actual, algorithm, expected)); + // THEN + then(thrown).isInstanceOf(NullPointerException.class) + .hasMessage("The message digest algorithm should not be null"); + } + + @Test + void should_fail_if_algorithm_is_invalid() throws IOException { + // GIVEN + Path actual = createFile(tempDir.resolve("actual")); + String algorithm = "invalid"; + String expected = ""; + // WHEN + Throwable thrown = catchThrowable(() -> paths.assertHasDigest(info, actual, algorithm, expected)); + // THEN + then(thrown).isInstanceOf(IllegalStateException.class) + .hasMessage("Unable to find digest implementation for: ") + .hasCauseInstanceOf(NoSuchAlgorithmException.class); + } + + @Test + void should_fail_if_expected_is_null() throws Exception { + // GIVEN + Path actual = createFile(tempDir.resolve("actual")); + String algorithm = "MD5"; + String expected = null; + // WHEN + Throwable thrown = catchThrowable(() -> paths.assertHasDigest(info, actual, algorithm, expected)); + // THEN + then(thrown).isInstanceOf(NullPointerException.class) + .hasMessage("The string representation of digest to compare to should not be null"); + } + + @Test + void should_fail_if_actual_is_null() throws NoSuchAlgorithmException { + // GIVEN + Path actual = null; + String algorithm = "MD5"; + String expected = ""; + // WHEN + AssertionError error = expectAssertionError(() -> paths.assertHasDigest(info, actual, algorithm, expected)); + // THEN + then(error).hasMessage(actualIsNull()); + } + + @Test + void should_fail_if_actual_does_not_exist() throws NoSuchAlgorithmException { + // GIVEN + Path actual = tempDir.resolve("non-existent"); + String algorithm = "MD5"; + String expected = ""; + // WHEN + AssertionError error = expectAssertionError(() -> paths.assertHasDigest(info, actual, algorithm, expected)); + // THEN + then(error).hasMessage(shouldExist(actual).create()); + } + + @Test + void should_fail_if_actual_is_not_a_regular_file() throws Exception { + // GIVEN + Path actual = createDirectory(tempDir.resolve("directory")); + String algorithm = "MD5"; + String expected = ""; + // WHEN + AssertionError error = expectAssertionError(() -> paths.assertHasDigest(info, actual, algorithm, expected)); + // THEN + then(error).hasMessage(shouldBeRegularFile(actual).create()); + } + + @Test + @DisabledOnOs(value = WINDOWS, disabledReason = "gh-2312") + void should_fail_if_actual_is_not_readable() throws IOException { + // GIVEN + Path actual = createFile(tempDir.resolve("actual")); + actual.toFile().setReadable(false); + String algorithm = "MD5"; + String expected = ""; + // WHEN + AssertionError error = expectAssertionError(() -> paths.assertHasDigest(info, actual, algorithm, expected)); + // THEN + then(error).hasMessage(shouldBeReadable(actual).create()); + } + + @Test + @DisabledOnOs(value = WINDOWS, disabledReason = "gh-2312") + void should_rethrow_IOException_as_UncheckedIOException() throws Exception { + // GIVEN + Path actual = createFile(tempDir.resolve("actual")); + String algorithm = "MD5"; + String expected = ""; + IOException cause = new IOException("boom!"); + given(nioFilesWrapper.newInputStream(actual)).willThrow(cause); + // WHEN + Throwable thrown = catchThrowable(() -> paths.assertHasDigest(info, actual, algorithm, expected)); + // THEN + then(thrown).isInstanceOf(UncheckedIOException.class) + .hasCause(cause); + } + + @Test + void should_fail_if_actual_does_not_have_expected_digest() throws Exception { + // GIVEN + Path actual = Files.write(tempDir.resolve("actual"), "Content".getBytes()); + String algorithm = "MD5"; + MessageDigest digest = MessageDigest.getInstance(algorithm); + String expected = toHex(digest.digest("Another content".getBytes())); + // WHEN + AssertionError error = expectAssertionError(() -> paths.assertHasDigest(info, actual, algorithm, expected)); + // THEN + then(error).hasMessage(shouldHaveDigest(actual, new DigestDiff(toHex(digest.digest(readAllBytes(actual))), + expected, + digest)).create()); + } + + @Test + void should_pass_if_actual_has_expected_digest() throws Exception { + // GIVEN + Path actual = Files.write(tempDir.resolve("actual"), "Content".getBytes()); + String algorithm = "MD5"; + String expected = toHex(MessageDigest.getInstance(algorithm).digest(readAllBytes(actual))); + // WHEN/THEN + paths.assertHasDigest(info, actual, algorithm, expected); + } + +} diff --git a/src/test/java/org/assertj/core/internal/paths/Paths_assertHasFileName_Test.java b/src/test/java/org/assertj/core/internal/paths/Paths_assertHasFileName_Test.java index ae80e27b8b..b668ee3f6c 100644 --- a/src/test/java/org/assertj/core/internal/paths/Paths_assertHasFileName_Test.java +++ b/src/test/java/org/assertj/core/internal/paths/Paths_assertHasFileName_Test.java @@ -12,13 +12,15 @@ */ package org.assertj.core.internal.paths; +import static java.nio.file.Files.createDirectory; +import static java.nio.file.Files.createFile; +import static java.nio.file.Files.createSymbolicLink; import static org.assertj.core.api.Assertions.assertThatExceptionOfType; import static org.assertj.core.api.Assertions.assertThatNullPointerException; import static org.assertj.core.util.FailureMessages.actualIsNull; import java.io.IOException; import java.nio.file.FileSystem; -import java.nio.file.Files; import java.nio.file.Path; import org.assertj.core.internal.PathsBaseTest; @@ -44,18 +46,18 @@ static void initPaths() throws IOException { existingDirectory = fs.getPath("/dir1/dir2"); symlinkToExistingDirectory = fs.getPath("/symlinkToExistingDirectory"); - Files.createDirectory(fs.getPath("/dir1")); - Files.createDirectory(existingDirectory); - Files.createSymbolicLink(symlinkToExistingDirectory, existingDirectory); + createDirectory(fs.getPath("/dir1")); + createDirectory(existingDirectory); + createSymbolicLink(symlinkToExistingDirectory, existingDirectory); existingFile = fs.getPath("/dir1/dir2/gc.log"); symlinkToExistingFile = fs.getPath("/dir1/good-symlink"); - Files.createFile(existingFile); - Files.createSymbolicLink(symlinkToExistingFile, existingFile); + createFile(existingFile); + createSymbolicLink(symlinkToExistingFile, existingFile); nonExistingPath = fs.getPath("/dir1/fake.log"); symlinkToNonExistingPath = fs.getPath("/dir1/bad-symlink"); - Files.createSymbolicLink(symlinkToNonExistingPath, nonExistingPath); + createSymbolicLink(symlinkToNonExistingPath, nonExistingPath); } @AfterAll diff --git a/src/test/java/org/assertj/core/internal/paths/Paths_assertHasNoParentRaw_Test.java b/src/test/java/org/assertj/core/internal/paths/Paths_assertHasNoParentRaw_Test.java index 376f39640f..9d70be9e89 100644 --- a/src/test/java/org/assertj/core/internal/paths/Paths_assertHasNoParentRaw_Test.java +++ b/src/test/java/org/assertj/core/internal/paths/Paths_assertHasNoParentRaw_Test.java @@ -12,43 +12,56 @@ */ package org.assertj.core.internal.paths; -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatExceptionOfType; -import static org.assertj.core.api.ThrowableAssert.catchThrowable; +import static java.nio.file.Files.createFile; +import static java.nio.file.Files.createSymbolicLink; +import static org.assertj.core.api.BDDAssertions.then; import static org.assertj.core.error.ShouldHaveNoParent.shouldHaveNoParent; +import static org.assertj.core.util.AssertionsUtil.expectAssertionError; import static org.assertj.core.util.FailureMessages.actualIsNull; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; +import java.io.IOException; import java.nio.file.Path; +import org.assertj.core.internal.PathsBaseTest; import org.junit.jupiter.api.Test; -class Paths_assertHasNoParentRaw_Test extends MockPathsBaseTest { +class Paths_assertHasNoParentRaw_Test extends PathsBaseTest { @Test void should_fail_if_actual_is_null() { - assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> paths.assertHasNoParentRaw(info, null)) - .withMessage(actualIsNull()); + // WHEN + AssertionError error = expectAssertionError(() -> paths.assertHasNoParentRaw(info, null)); + // THEN + then(error).hasMessage(actualIsNull()); } @Test - void should_fail_if_actual_has_parent() { - final Path parent = mock(Path.class); - when(actual.getParent()).thenReturn(parent); - - Throwable error = catchThrowable(() -> paths.assertHasNoParentRaw(info, actual)); - - assertThat(error).isInstanceOf(AssertionError.class); - verify(failures).failure(info, shouldHaveNoParent(actual)); + void should_fail_if_actual_has_parent() throws IOException { + // GIVEN + Path actual = createFile(tempDir.resolve("actual")); + // WHEN + AssertionError error = expectAssertionError(() -> paths.assertHasNoParentRaw(info, actual)); + // THEN + then(error).hasMessage(shouldHaveNoParent(actual).create()); } @Test - void should_succeed_if_actual_has_no_parent() { - // This is the default, but let's make that clear - when(actual.getParent()).thenReturn(null); - + void should_pass_if_actual_has_no_parent() { + // GIVEN + Path actual = tempDir.getRoot(); + // WHEN/THEN paths.assertHasNoParentRaw(info, actual); } + + @Test + void should_fail_if_actual_is_not_canonical() throws IOException { + // GIVEN + Path root = tempDir.getRoot(); + Path actual = createSymbolicLink(tempDir.resolve("actual"), root); + // WHEN + AssertionError error = expectAssertionError(() -> paths.assertHasNoParentRaw(info, actual)); + // THEN + then(error).hasMessage(shouldHaveNoParent(actual).create()); + } + } diff --git a/src/test/java/org/assertj/core/internal/paths/Paths_assertHasNoParent_Test.java b/src/test/java/org/assertj/core/internal/paths/Paths_assertHasNoParent_Test.java index 5e3e793fac..2653dce0ad 100644 --- a/src/test/java/org/assertj/core/internal/paths/Paths_assertHasNoParent_Test.java +++ b/src/test/java/org/assertj/core/internal/paths/Paths_assertHasNoParent_Test.java @@ -12,59 +12,71 @@ */ package org.assertj.core.internal.paths; -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatExceptionOfType; -import static org.assertj.core.api.ThrowableAssert.catchThrowable; +import static java.nio.file.Files.createFile; +import static java.nio.file.Files.createSymbolicLink; +import static org.assertj.core.api.Assertions.catchThrowable; +import static org.assertj.core.api.BDDAssertions.then; import static org.assertj.core.error.ShouldHaveNoParent.shouldHaveNoParent; +import static org.assertj.core.util.AssertionsUtil.expectAssertionError; import static org.assertj.core.util.FailureMessages.actualIsNull; +import static org.mockito.BDDMockito.given; import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; import java.io.IOException; +import java.io.UncheckedIOException; import java.nio.file.Path; -import org.assertj.core.api.exception.PathsException; +import org.assertj.core.internal.PathsBaseTest; import org.junit.jupiter.api.Test; -class Paths_assertHasNoParent_Test extends MockPathsBaseTest { +class Paths_assertHasNoParent_Test extends PathsBaseTest { @Test void should_fail_if_actual_is_null() { - assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> paths.assertHasNoParent(info, null)) - .withMessage(actualIsNull()); + // WHEN + AssertionError error = expectAssertionError(() -> paths.assertHasNoParent(info, null)); + // THEN + then(error).hasMessage(actualIsNull()); } @Test - void should_throw_PathsException_if_actual_cannot_be_canonicalized() throws IOException { - final IOException exception = new IOException(); - when(actual.toRealPath()).thenThrow(exception); - - assertThatExceptionOfType(PathsException.class).isThrownBy(() -> paths.assertHasNoParent(info, actual)) - .withMessage("failed to resolve actual real path") - .withCause(exception); + void should_rethrow_IOException_as_UncheckedIOException_if_actual_cannot_be_resolved() throws IOException { + // GIVEN + Path actual = mock(Path.class); + IOException exception = new IOException("boom!"); + given(actual.toRealPath()).willThrow(exception); + // WHEN + Throwable thrown = catchThrowable(() -> paths.assertHasNoParent(info, actual)); + // THEN + then(thrown).isInstanceOf(UncheckedIOException.class) + .hasCause(exception); } @Test void should_fail_if_actual_has_parent() throws IOException { - final Path canonicalActual = mock(Path.class); - final Path parent = mock(Path.class); - when(actual.toRealPath()).thenReturn(canonicalActual); - when(canonicalActual.getParent()).thenReturn(parent); - - Throwable error = catchThrowable(() -> paths.assertHasNoParent(info, actual)); - - assertThat(error).isInstanceOf(AssertionError.class); - verify(failures).failure(info, shouldHaveNoParent(actual)); + // GIVEN + Path actual = createFile(tempDir.resolve("actual")).toRealPath(); + // WHEN + AssertionError error = expectAssertionError(() -> paths.assertHasNoParent(info, actual)); + // THEN + then(error).hasMessage(shouldHaveNoParent(actual).create()); } @Test - void should_succeed_if_actual_has_no_parent() throws IOException { - final Path canonicalActual = mock(Path.class); - when(actual.toRealPath()).thenReturn(canonicalActual); - // This is the default, but let's make that clear - when(canonicalActual.getParent()).thenReturn(null); + void should_pass_if_actual_has_no_parent() { + // GIVEN + Path actual = tempDir.getRoot(); + // WHEN/THEN + paths.assertHasNoParent(info, actual); + } + @Test + void should_pass_if_actual_is_not_canonical() throws IOException { + // GIVEN + Path root = tempDir.getRoot(); + Path actual = createSymbolicLink(tempDir.resolve("actual"), root); + // WHEN/THEN paths.assertHasNoParent(info, actual); } + } diff --git a/src/test/java/org/assertj/core/internal/paths/Paths_assertHasParentRaw_Test.java b/src/test/java/org/assertj/core/internal/paths/Paths_assertHasParentRaw_Test.java index fbf22a5f32..5c72b2742c 100644 --- a/src/test/java/org/assertj/core/internal/paths/Paths_assertHasParentRaw_Test.java +++ b/src/test/java/org/assertj/core/internal/paths/Paths_assertHasParentRaw_Test.java @@ -12,70 +12,97 @@ */ package org.assertj.core.internal.paths; -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatExceptionOfType; -import static org.assertj.core.api.Assertions.assertThatNullPointerException; -import static org.assertj.core.api.ThrowableAssert.catchThrowable; +import static java.nio.file.Files.createDirectory; +import static java.nio.file.Files.createFile; +import static java.nio.file.Files.createSymbolicLink; +import static org.assertj.core.api.Assertions.catchThrowable; +import static org.assertj.core.api.BDDAssertions.then; import static org.assertj.core.error.ShouldHaveParent.shouldHaveParent; +import static org.assertj.core.util.AssertionsUtil.expectAssertionError; import static org.assertj.core.util.FailureMessages.actualIsNull; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; +import java.io.IOException; import java.nio.file.Path; -import org.junit.jupiter.api.BeforeEach; +import org.assertj.core.internal.PathsBaseTest; import org.junit.jupiter.api.Test; -class Paths_assertHasParentRaw_Test extends MockPathsBaseTest { - - private Path expectedParent; - - @Override - @BeforeEach - public void init() { - super.init(); - expectedParent = mock(Path.class); - } +class Paths_assertHasParentRaw_Test extends PathsBaseTest { @Test void should_fail_if_actual_is_null() { - assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> paths.assertHasParentRaw(info, null, expectedParent)) - .withMessage(actualIsNull()); + // GIVEN + Path expected = tempDir.resolve("expected"); + // WHEN + AssertionError error = expectAssertionError(() -> paths.assertHasParentRaw(info, null, expected)); + // THEN + then(error).hasMessage(actualIsNull()); } @Test - void should_fail_if_provided_parent_is_null() { - assertThatNullPointerException().isThrownBy(() -> paths.assertHasParentRaw(info, actual, null)) - .withMessage("expected parent path should not be null"); + void should_fail_if_expected_is_null() { + // GIVEN + Path actual = tempDir.resolve("actual"); + // WHEN + Throwable thrown = catchThrowable(() -> paths.assertHasParentRaw(info, actual, null)); + // THEN + then(thrown).isInstanceOf(NullPointerException.class) + .hasMessage("expected parent path should not be null"); } @Test void should_fail_if_actual_has_no_parent() { - // This is the default, but... - when(actual.getParent()).thenReturn(null); - - Throwable error = catchThrowable(() -> paths.assertHasParentRaw(info, actual, expectedParent)); - - assertThat(error).isInstanceOf(AssertionError.class); - verify(failures).failure(info, shouldHaveParent(actual, expectedParent)); + // GIVEN + Path actual = tempDir.getRoot(); + Path expected = tempDir.resolve("expected"); + // WHEN + AssertionError error = expectAssertionError(() -> paths.assertHasParentRaw(info, actual, expected)); + // THEN + then(error).hasMessage(shouldHaveParent(actual, expected).create()); } @Test - void should_fail_if_actual_parent_is_not_expected_parent() { - final Path actualParent = mock(Path.class); - when(actual.getParent()).thenReturn(actualParent); - - Throwable error = catchThrowable(() -> paths.assertHasParentRaw(info, actual, expectedParent)); + void should_fail_if_actual_parent_is_not_expected_parent() throws IOException { + // GIVEN + Path actual = createFile(tempDir.resolve("actual")); + Path expected = createFile(tempDir.resolve("expected")); + // WHEN + AssertionError error = expectAssertionError(() -> paths.assertHasParentRaw(info, actual, expected)); + // THEN + then(error).hasMessage(shouldHaveParent(actual, actual.getParent(), expected).create()); + } - assertThat(error).isInstanceOf(AssertionError.class); - verify(failures).failure(info, shouldHaveParent(actual, actualParent, expectedParent)); + @Test + void should_pass_if_actual_has_expected_parent() throws IOException { + // GIVEN + Path actual = createFile(tempDir.resolve("actual")); + Path expected = tempDir; + // WHEN + paths.assertHasParentRaw(info, actual, expected); } @Test - void should_succeed_if_parent_is_expected_parent() { - when(actual.getParent()).thenReturn(expectedParent); + void should_fail_if_actual_is_not_canonical() throws IOException { + // GIVEN + Path expected = createDirectory(tempDir.resolve("expected")); + Path file = createFile(expected.resolve("file")); + Path actual = createSymbolicLink(tempDir.resolve("actual"), file); + // WHEN + AssertionError error = expectAssertionError(() -> paths.assertHasParentRaw(info, actual, expected)); + // THEN + then(error).hasMessage(shouldHaveParent(actual, actual.getParent(), expected).create()); + } - paths.assertHasParentRaw(info, actual, expectedParent); + @Test + void should_fail_if_expected_is_not_canonical() throws IOException { + // GIVEN + Path directory = createDirectory(tempDir.resolve("directory")); + Path expected = createSymbolicLink(tempDir.resolve("expected"), directory); + Path actual = createFile(directory.resolve("actual")); + // WHEN + AssertionError error = expectAssertionError(() -> paths.assertHasParentRaw(info, actual, expected)); + // THEN + then(error).hasMessage(shouldHaveParent(actual, actual.getParent(), expected).create()); } + } diff --git a/src/test/java/org/assertj/core/internal/paths/Paths_assertHasParent_Test.java b/src/test/java/org/assertj/core/internal/paths/Paths_assertHasParent_Test.java index f817914246..aa88b0143c 100644 --- a/src/test/java/org/assertj/core/internal/paths/Paths_assertHasParent_Test.java +++ b/src/test/java/org/assertj/core/internal/paths/Paths_assertHasParent_Test.java @@ -12,108 +12,124 @@ */ package org.assertj.core.internal.paths; -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatExceptionOfType; -import static org.assertj.core.api.Assertions.assertThatNullPointerException; -import static org.assertj.core.api.ThrowableAssert.catchThrowable; +import static java.nio.file.Files.createDirectory; +import static java.nio.file.Files.createFile; +import static java.nio.file.Files.createSymbolicLink; +import static org.assertj.core.api.Assertions.catchThrowable; +import static org.assertj.core.api.BDDAssertions.then; import static org.assertj.core.error.ShouldHaveParent.shouldHaveParent; +import static org.assertj.core.util.AssertionsUtil.expectAssertionError; import static org.assertj.core.util.FailureMessages.actualIsNull; +import static org.mockito.BDDMockito.given; import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; import java.io.IOException; +import java.io.UncheckedIOException; import java.nio.file.Path; -import org.assertj.core.api.exception.PathsException; -import org.junit.jupiter.api.BeforeEach; +import org.assertj.core.internal.PathsBaseTest; import org.junit.jupiter.api.Test; -class Paths_assertHasParent_Test extends MockPathsBaseTest { - - private Path canonicalActual; - private Path expected; - private Path canonicalExpected; - - @Override - @BeforeEach - public void init() { - super.init(); - canonicalActual = mock(Path.class); - expected = mock(Path.class); - canonicalExpected = mock(Path.class); - } +class Paths_assertHasParent_Test extends PathsBaseTest { @Test void should_fail_if_actual_is_null() { - assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> paths.assertHasParent(info, null, expected)) - .withMessage(actualIsNull()); + // GIVEN + Path expected = tempDir.resolve("expected"); + // WHEN + AssertionError error = expectAssertionError(() -> paths.assertHasParent(info, null, expected)); + // THEN + then(error).hasMessage(actualIsNull()); } @Test - void should_fail_if_given_parent_is_null() { - assertThatNullPointerException().isThrownBy(() -> paths.assertHasParent(info, actual, null)) - .withMessage("expected parent path should not be null"); + void should_fail_if_expected_is_null() { + // GIVEN + Path actual = tempDir.resolve("actual"); + // WHEN + Throwable thrown = catchThrowable(() -> paths.assertHasParent(info, actual, null)); + // THEN + then(thrown).isInstanceOf(NullPointerException.class) + .hasMessage("expected parent path should not be null"); } @Test - void should_fail_if_actual_cannot_be_canonicalized() throws IOException { - final IOException exception = new IOException(); - when(actual.toRealPath()).thenThrow(exception); - - assertThatExceptionOfType(PathsException.class).isThrownBy(() -> paths.assertHasParent(info, actual, expected)) - .withMessage("failed to resolve actual real path") - .withCause(exception); + void should_fail_if_actual_has_no_parent() { + // GIVEN + Path actual = tempDir.getRoot(); + Path expected = tempDir.resolve("expected"); + // WHEN + AssertionError error = expectAssertionError(() -> paths.assertHasParent(info, actual, expected)); + // THEN + then(error).hasMessage(shouldHaveParent(actual, expected).create()); } @Test - void should_fail_if_expected_parent_cannot_be_canonicalized() throws IOException { - final IOException exception = new IOException(); - - when(actual.toRealPath()).thenReturn(canonicalActual); - when(expected.toRealPath()).thenThrow(exception); - - assertThatExceptionOfType(PathsException.class).isThrownBy(() -> paths.assertHasParent(info, actual, expected)) - .withMessage("failed to resolve argument real path") - .withCause(exception); + void should_rethrow_IOException_as_UncheckedIOException_if_actual_cannot_be_resolved() throws IOException { + // GIVEN + Path actual = mock(Path.class); + Path expected = createFile(tempDir.resolve("expected")); + IOException exception = new IOException("boom!"); + given(actual.toRealPath()).willThrow(exception); + // WHEN + Throwable thrown = catchThrowable(() -> paths.assertHasParent(info, actual, expected)); + // THEN + then(thrown).isInstanceOf(UncheckedIOException.class) + .hasCause(exception); } @Test - void should_fail_if_actual_has_no_parent() throws IOException { - when(actual.toRealPath()).thenReturn(canonicalActual); - when(expected.toRealPath()).thenReturn(canonicalExpected); - - // This is the default, but... - when(canonicalActual.getParent()).thenReturn(null); - - Throwable error = catchThrowable(() -> paths.assertHasParent(info, actual, expected)); - - assertThat(error).isInstanceOf(AssertionError.class); - verify(failures).failure(info, shouldHaveParent(actual, expected)); + void should_rethrow_IOException_as_UncheckedIOException_if_other_cannot_be_resolved() throws IOException { + // GIVEN + Path actual = createFile(tempDir.resolve("actual")); + Path expected = mock(Path.class); + IOException exception = new IOException("boom!"); + given(expected.toRealPath()).willThrow(exception); + // WHEN + Throwable thrown = catchThrowable(() -> paths.assertHasParent(info, actual, expected)); + // THEN + then(thrown).isInstanceOf(UncheckedIOException.class) + .hasCause(exception); } @Test void should_fail_if_actual_parent_is_not_expected_parent() throws IOException { - final Path actualParent = mock(Path.class); - - when(actual.toRealPath()).thenReturn(canonicalActual); - when(expected.toRealPath()).thenReturn(canonicalExpected); - - when(canonicalActual.getParent()).thenReturn(actualParent); - - Throwable error = catchThrowable(() -> paths.assertHasParent(info, actual, expected)); - - assertThat(error).isInstanceOf(AssertionError.class); - verify(failures).failure(info, shouldHaveParent(actual, actualParent, expected)); + // GIVEN + Path actual = createFile(tempDir.resolve("actual")).toRealPath(); + Path expected = createFile(tempDir.resolve("expected")).toRealPath(); + // WHEN + AssertionError error = expectAssertionError(() -> paths.assertHasParent(info, actual, expected)); + // THEN + then(error).hasMessage(shouldHaveParent(actual, actual.getParent(), expected).create()); } @Test - void should_succeed_if_canonical_actual_has_expected_parent() throws IOException { - when(actual.toRealPath()).thenReturn(canonicalActual); - when(expected.toRealPath()).thenReturn(canonicalExpected); + void should_pass_if_actual_has_expected_parent() throws IOException { + // GIVEN + Path actual = createFile(tempDir.resolve("actual")).toRealPath(); + Path expected = tempDir.toRealPath(); + // WHEN/THEN + paths.assertHasParent(info, actual, expected); + } - when(canonicalActual.getParent()).thenReturn(canonicalExpected); + @Test + void should_pass_if_actual_is_not_canonical() throws IOException { + // GIVEN + Path expected = createDirectory(tempDir.resolve("expected")); + Path file = createFile(expected.resolve("file")); + Path actual = createSymbolicLink(tempDir.resolve("actual"), file); + // WHEN/THEN + paths.assertHasParent(info, actual, expected); + } + @Test + void should_pass_if_expected_is_not_canonical() throws IOException { + // GIVEN + Path directory = createDirectory(tempDir.resolve("directory")); + Path expected = createSymbolicLink(tempDir.resolve("expected"), directory); + Path actual = createFile(directory.resolve("actual")); + // WHEN/THEN paths.assertHasParent(info, actual, expected); } + } diff --git a/src/test/java/org/assertj/core/internal/paths/Paths_assertHasSameBinaryContentAs_Test.java b/src/test/java/org/assertj/core/internal/paths/Paths_assertHasSameBinaryContentAs_Test.java index 2805539b6d..c3a5eeb2b4 100644 --- a/src/test/java/org/assertj/core/internal/paths/Paths_assertHasSameBinaryContentAs_Test.java +++ b/src/test/java/org/assertj/core/internal/paths/Paths_assertHasSameBinaryContentAs_Test.java @@ -12,140 +12,151 @@ */ package org.assertj.core.internal.paths; -import static java.nio.charset.Charset.defaultCharset; -import static java.nio.file.Files.readAllBytes; -import static org.assertj.core.api.Assertions.catchThrowableOfType; +import static java.nio.file.Files.createFile; +import static org.assertj.core.api.Assertions.catchThrowable; import static org.assertj.core.api.BDDAssertions.then; import static org.assertj.core.error.ShouldBeReadable.shouldBeReadable; import static org.assertj.core.error.ShouldExist.shouldExist; import static org.assertj.core.error.ShouldHaveBinaryContent.shouldHaveBinaryContent; -import static org.assertj.core.internal.BinaryDiffResult.noDiff; -import static org.assertj.core.test.TestData.someInfo; import static org.assertj.core.util.AssertionsUtil.expectAssertionError; import static org.assertj.core.util.FailureMessages.actualIsNull; -import static org.assertj.core.util.TempFileUtil.createTempPathWithContent; +import static org.junit.jupiter.api.condition.OS.WINDOWS; import static org.mockito.BDDMockito.given; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; import java.io.IOException; import java.io.UncheckedIOException; +import java.nio.charset.Charset; +import java.nio.file.Files; import java.nio.file.Path; import org.assertj.core.internal.BinaryDiffResult; import org.assertj.core.internal.PathsBaseTest; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.condition.DisabledOnOs; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; class Paths_assertHasSameBinaryContentAs_Test extends PathsBaseTest { - private Path actual; - private Path expected; - private byte[] expectedBytes; - - @BeforeEach - void setUpOnce() throws IOException { - // Does not matter if the values differ, the actual comparison is mocked in this test - actual = createTempPathWithContent("foo", defaultCharset()); - expected = createTempPathWithContent("bar", defaultCharset()); - expectedBytes = readAllBytes(expected); - when(nioFilesWrapper.exists(actual)).thenReturn(true); - when(nioFilesWrapper.isReadable(actual)).thenReturn(true); - when(nioFilesWrapper.exists(expected)).thenReturn(true); - when(nioFilesWrapper.isReadable(expected)).thenReturn(true); - } - @Test - void should_pass_if_path_has_same_binary_content_as_expected() throws IOException { + void should_fail_if_expected_is_null() throws IOException { // GIVEN - given(binaryDiff.diff(actual, expectedBytes)).willReturn(noDiff()); - // WHEN/THEN - paths.assertHasSameBinaryContentAs(someInfo(), actual, expected); + Path actual = createFile(tempDir.resolve("actual")); + // WHEN + Throwable thrown = catchThrowable(() -> paths.assertHasSameBinaryContentAs(info, actual, null)); + // THEN + then(thrown).isInstanceOf(NullPointerException.class) + .hasMessage("The given Path to compare actual content to should not be null"); } @Test - void should_throw_error_if_expected_is_null() { + void should_fail_if_expected_does_not_exist() throws IOException { // GIVEN - Path nullExpected = null; + Path actual = createFile(tempDir.resolve("actual")); + Path expected = tempDir.resolve("non-existent"); // WHEN - NullPointerException npe = catchThrowableOfType(() -> paths.assertHasSameBinaryContentAs(someInfo(), actual, nullExpected), - NullPointerException.class); + Throwable thrown = catchThrowable(() -> paths.assertHasSameBinaryContentAs(info, actual, expected)); // THEN - then(npe).hasMessage("The given Path to compare actual content to should not be null"); + then(thrown).isInstanceOf(IllegalArgumentException.class) + .hasMessage("The given Path <%s> to compare actual content to should exist", expected); } @Test - void should_fail_if_actual_is_null() { + @DisabledOnOs(value = WINDOWS, disabledReason = "gh-2312") + void should_fail_if_expected_is_not_readable() throws IOException { // GIVEN - Path path = null; + Path actual = createFile(tempDir.resolve("actual")); + Path expected = createFile(tempDir.resolve("expected")); + expected.toFile().setReadable(false); // WHEN - AssertionError error = expectAssertionError(() -> paths.assertHasSameBinaryContentAs(someInfo(), path, expected)); + Throwable thrown = catchThrowable(() -> paths.assertHasSameBinaryContentAs(info, actual, expected)); // THEN - then(error).hasMessage(actualIsNull()); + then(thrown).isInstanceOf(IllegalArgumentException.class) + .hasMessage("The given Path <%s> to compare actual content to should be readable", expected); } @Test - void should_fail_if_actual_path_does_not_exist() { + void should_fail_if_actual_is_null() throws IOException { // GIVEN - given(nioFilesWrapper.exists(actual)).willReturn(false); + Path expected = createFile(tempDir.resolve("expected")); // WHEN - expectAssertionError(() -> paths.assertHasSameBinaryContentAs(someInfo(), actual, expected)); + AssertionError error = expectAssertionError(() -> paths.assertHasSameBinaryContentAs(info, null, expected)); // THEN - verify(failures).failure(someInfo(), shouldExist(actual)); + then(error).hasMessage(actualIsNull()); } @Test - void should_fail_if_actual_is_not_readable() { + void should_fail_if_actual_does_not_exist() throws IOException { // GIVEN - when(nioFilesWrapper.isReadable(actual)).thenReturn(false); + Path actual = tempDir.resolve("non-existent"); + Path expected = createFile(tempDir.resolve("expected")); // WHEN - expectAssertionError(() -> paths.assertHasSameBinaryContentAs(someInfo(), actual, expected)); + AssertionError error = expectAssertionError(() -> paths.assertHasSameBinaryContentAs(info, actual, expected)); // THEN - verify(failures).failure(someInfo(), shouldBeReadable(actual)); + then(error).hasMessage(shouldExist(actual).create()); } @Test - void should_fail_if_expected_path_is_does_not_exist() { + @DisabledOnOs(value = WINDOWS, disabledReason = "gh-2312") + void should_fail_if_actual_is_not_readable() throws IOException { // GIVEN - when(nioFilesWrapper.exists(expected)).thenReturn(false); + Path actual = createFile(tempDir.resolve("actual")); + actual.toFile().setReadable(false); + Path expected = createFile(tempDir.resolve("expected")); // WHEN - IllegalArgumentException iae = catchThrowableOfType(() -> paths.assertHasSameBinaryContentAs(someInfo(), actual, expected), - IllegalArgumentException.class); + AssertionError error = expectAssertionError(() -> paths.assertHasSameBinaryContentAs(info, actual, expected)); // THEN - then(iae).hasMessage("The given Path <%s> to compare actual content to should exist", expected); + then(error).hasMessage(shouldBeReadable(actual).create()); } - @Test - void should_fail_if_expected_path_is_not_readable() { + @ParameterizedTest + @CsvSource({ + "Content, US-ASCII, US-ASCII", + "Content, US-ASCII, ISO_8859_1", + "Content, US-ASCII, UTF-8", + }) + void should_pass_if_actual_has_the_same_content_as_expected(String content, + Charset actualCharset, + Charset expectedCharset) throws IOException { // GIVEN - when(nioFilesWrapper.isReadable(expected)).thenReturn(false); - // WHEN - IllegalArgumentException iae = catchThrowableOfType(() -> paths.assertHasSameBinaryContentAs(someInfo(), actual, expected), - IllegalArgumentException.class); - // THEN - then(iae).hasMessage("The given Path <%s> to compare actual content to should be readable", expected); + Path actual = Files.write(tempDir.resolve("actual"), content.getBytes(actualCharset)); + Path expected = Files.write(tempDir.resolve("expected"), content.getBytes(expectedCharset)); + // WHEN/THEN + paths.assertHasSameBinaryContentAs(info, actual, expected); } - @Test - void should_throw_error_wrapping_caught_IOException() throws IOException { + @ParameterizedTest + @CsvSource({ + "Content, US-ASCII, Content, UTF-16", + "Content, US-ASCII, Another content, US-ASCII", + }) + void should_fail_if_actual_does_not_have_the_same_binary_content_as_expected(String actualContent, + Charset actualCharset, + String expectedContent, + Charset expectedCharset) throws IOException { // GIVEN - IOException cause = new IOException(); - given(binaryDiff.diff(actual, expectedBytes)).willThrow(cause); + Path actual = Files.write(tempDir.resolve("actual"), actualContent.getBytes(actualCharset)); + Path expected = Files.write(tempDir.resolve("expected"), expectedContent.getBytes(expectedCharset)); + BinaryDiffResult diff = binaryDiff.diff(actual, expectedContent.getBytes(expectedCharset)); // WHEN - UncheckedIOException uioe = catchThrowableOfType(() -> paths.assertHasSameBinaryContentAs(someInfo(), actual, expected), - UncheckedIOException.class); + AssertionError error = expectAssertionError(() -> paths.assertHasSameBinaryContentAs(info, actual, expected)); // THEN - then(uioe).hasCause(cause); + then(error).hasMessage(shouldHaveBinaryContent(actual, diff).create(info.description(), info.representation())); } @Test - void should_fail_if_path_does_not_have_expected_binary_content() throws IOException { + @DisabledOnOs(value = WINDOWS, disabledReason = "gh-2312") + void should_rethrow_IOException_as_UncheckedIOException() throws IOException { // GIVEN - BinaryDiffResult diff = new BinaryDiffResult(15, (byte) 0xCA, (byte) 0xFE); - when(binaryDiff.diff(actual, expectedBytes)).thenReturn(diff); + Path actual = Files.write(tempDir.resolve("actual"), "Content".getBytes()); + Path expected = Files.write(tempDir.resolve("expected"), "Content".getBytes()); + IOException exception = new IOException("boom!"); + given(binaryDiff.diff(actual, "Content".getBytes())).willThrow(exception); // WHEN - expectAssertionError(() -> paths.assertHasSameBinaryContentAs(someInfo(), actual, expected)); + Throwable thrown = catchThrowable(() -> paths.assertHasSameBinaryContentAs(info, actual, expected)); // THEN - verify(failures).failure(someInfo(), shouldHaveBinaryContent(actual, diff)); + then(thrown).isInstanceOf(UncheckedIOException.class) + .hasCause(exception); } + } diff --git a/src/test/java/org/assertj/core/internal/paths/Paths_assertHasSameContentAs_Test.java b/src/test/java/org/assertj/core/internal/paths/Paths_assertHasSameContentAs_Test.java deleted file mode 100644 index adfc6b4865..0000000000 --- a/src/test/java/org/assertj/core/internal/paths/Paths_assertHasSameContentAs_Test.java +++ /dev/null @@ -1,163 +0,0 @@ -/* - * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on - * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - * - * Copyright 2012-2021 the original author or authors. - */ -package org.assertj.core.internal.paths; - -import static java.nio.charset.Charset.defaultCharset; -import static java.util.Collections.emptyList; -import static org.assertj.core.api.Assertions.catchThrowableOfType; -import static org.assertj.core.api.BDDAssertions.then; -import static org.assertj.core.error.ShouldBeReadable.shouldBeReadable; -import static org.assertj.core.error.ShouldExist.shouldExist; -import static org.assertj.core.error.ShouldHaveSameContent.shouldHaveSameContent; -import static org.assertj.core.test.TestData.someInfo; -import static org.assertj.core.util.AssertionsUtil.expectAssertionError; -import static org.assertj.core.util.FailureMessages.actualIsNull; -import static org.assertj.core.util.Lists.list; -import static org.assertj.core.util.TempFileUtil.createTempPathWithContent; -import static org.mockito.BDDMockito.given; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; - -import java.io.IOException; -import java.io.UncheckedIOException; -import java.nio.charset.Charset; -import java.nio.file.Path; -import java.util.List; - -import org.assertj.core.api.AssertionInfo; -import org.assertj.core.internal.Paths; -import org.assertj.core.util.diff.Delta; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; - -/** - * Tests for {@link Paths#assertHasSameContentAs(AssertionInfo, Path, Charset, Path, Charset)}. - */ -class Paths_assertHasSameContentAs_Test extends MockPathsBaseTest { - - private static final Charset CHARSET = defaultCharset(); - private Path actual; - private Path expected; - - @BeforeEach - void setUpOnce() throws IOException { - // Does not matter if the values differ, the actual comparison is mocked in this test - actual = createTempPathWithContent("foo", CHARSET); - expected = createTempPathWithContent("bar", CHARSET); - given(nioFilesWrapper.exists(actual)).willReturn(true); - given(nioFilesWrapper.isReadable(actual)).willReturn(true); - given(nioFilesWrapper.exists(expected)).willReturn(true); - given(nioFilesWrapper.isReadable(expected)).willReturn(true); - } - - @Test - void should_pass_if_path_has_same_textual_content_as_expected() throws IOException { - // GIVEN - given(diff.diff(actual, CHARSET, expected, CHARSET)).willReturn(emptyList()); - // WHEN/THEN - paths.assertHasSameContentAs(someInfo(), actual, CHARSET, expected, CHARSET); - } - - @Test - void should_throw_error_if_expected_is_null() { - // GIVEN - Path nullExpected = null; - // WHEN - NullPointerException npe = catchThrowableOfType(() -> paths.assertHasSameContentAs(someInfo(), actual, CHARSET, nullExpected, - CHARSET), - NullPointerException.class); - // THEN - then(npe).hasMessage("The given Path to compare actual content to should not be null"); - } - - @Test - void should_fail_if_actual_is_null() { - // GIVEN - Path path = null; - // WHEN - AssertionError error = expectAssertionError(() -> paths.assertHasSameContentAs(someInfo(), path, CHARSET, expected, - CHARSET)); - // THEN - then(error).hasMessage(actualIsNull()); - } - - @Test - void should_fail_if_actual_path_does_not_exist() { - // GIVEN - AssertionInfo info = someInfo(); - given(nioFilesWrapper.exists(actual)).willReturn(false); - // WHEN - expectAssertionError(() -> paths.assertHasSameContentAs(info, actual, CHARSET, expected, CHARSET)); - // THEN - verify(failures).failure(info, shouldExist(actual)); - } - - @Test - void should_fail_if_actual_is_not_a_readable_file() { - // GIVEN - given(nioFilesWrapper.isReadable(actual)).willReturn(false); - // WHEN - expectAssertionError(() -> paths.assertHasSameContentAs(someInfo(), actual, CHARSET, expected, CHARSET)); - // THEN - verify(failures).failure(someInfo(), shouldBeReadable(actual)); - } - - @Test - void should_fail_if_expected_path_is_does_not_exist() { - // GIVEN - given(nioFilesWrapper.exists(expected)).willReturn(false); - // WHEN - IllegalArgumentException iae = catchThrowableOfType(() -> paths.assertHasSameContentAs(someInfo(), actual, CHARSET, - expected, CHARSET), - IllegalArgumentException.class); - // THEN - then(iae).hasMessage("The given Path <%s> to compare actual content to should exist", expected); - } - - @Test - void should_fail_if_expected_path_is_not_readable() { - // GIVEN - given(nioFilesWrapper.isReadable(expected)).willReturn(false); - // WHEN - IllegalArgumentException iae = catchThrowableOfType(() -> paths.assertHasSameContentAs(someInfo(), actual, CHARSET, - expected, CHARSET), - IllegalArgumentException.class); - // THEN - then(iae).hasMessage("The given Path <%s> to compare actual content to should be readable", expected); - } - - @Test - void should_throw_error_wrapping_caught_IOException() throws IOException { - // GIVEN - IOException cause = new IOException(); - given(diff.diff(actual, CHARSET, expected, CHARSET)).willThrow(cause); - // WHEN - UncheckedIOException uioe = catchThrowableOfType(() -> paths.assertHasSameContentAs(someInfo(), actual, CHARSET, - expected, CHARSET), - UncheckedIOException.class); - // THEN - then(uioe).hasCause(cause); - } - - @Test - void should_fail_if_actual_and_given_path_does_not_have_the_same_content() throws IOException { - // GIVEN - List> diffs = list((Delta) mock(Delta.class)); - given(diff.diff(actual, CHARSET, expected, CHARSET)).willReturn(diffs); - AssertionInfo info = someInfo(); - // WHEN - expectAssertionError(() -> paths.assertHasSameContentAs(someInfo(), actual, CHARSET, expected, CHARSET)); - // THEN - verify(failures).failure(info, shouldHaveSameContent(actual, expected, diffs)); - } -} diff --git a/src/test/java/org/assertj/core/internal/paths/Paths_assertHasSameTextualContentAs_Test.java b/src/test/java/org/assertj/core/internal/paths/Paths_assertHasSameTextualContentAs_Test.java new file mode 100644 index 0000000000..9f1dafe7cb --- /dev/null +++ b/src/test/java/org/assertj/core/internal/paths/Paths_assertHasSameTextualContentAs_Test.java @@ -0,0 +1,170 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + * + * Copyright 2012-2021 the original author or authors. + */ +package org.assertj.core.internal.paths; + +import static java.nio.charset.Charset.defaultCharset; +import static java.nio.file.Files.createFile; +import static org.assertj.core.api.Assertions.catchThrowable; +import static org.assertj.core.api.BDDAssertions.then; +import static org.assertj.core.error.ShouldBeReadable.shouldBeReadable; +import static org.assertj.core.error.ShouldExist.shouldExist; +import static org.assertj.core.error.ShouldHaveSameContent.shouldHaveSameContent; +import static org.assertj.core.util.AssertionsUtil.expectAssertionError; +import static org.assertj.core.util.FailureMessages.actualIsNull; +import static org.junit.jupiter.api.condition.OS.WINDOWS; +import static org.mockito.BDDMockito.given; + +import java.io.IOException; +import java.io.UncheckedIOException; +import java.nio.charset.Charset; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.List; + +import org.assertj.core.internal.PathsBaseTest; +import org.assertj.core.util.diff.Delta; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.condition.DisabledOnOs; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; + +class Paths_assertHasSameTextualContentAs_Test extends PathsBaseTest { + + private static final Charset CHARSET = defaultCharset(); + + @Test + void should_fail_if_expected_is_null() throws IOException { + // GIVEN + Path actual = createFile(tempDir.resolve("actual")); + // WHEN + Throwable thrown = catchThrowable(() -> paths.assertHasSameTextualContentAs(info, actual, CHARSET, null, CHARSET)); + // THEN + then(thrown).isInstanceOf(NullPointerException.class) + .hasMessage("The given Path to compare actual content to should not be null"); + } + + @Test + void should_fail_if_expected_does_not_exist() throws IOException { + // GIVEN + Path actual = createFile(tempDir.resolve("actual")); + Path expected = tempDir.resolve("non-existent"); + // WHEN + Throwable thrown = catchThrowable(() -> paths.assertHasSameTextualContentAs(info, actual, CHARSET, expected, CHARSET)); + // THEN + then(thrown).isInstanceOf(IllegalArgumentException.class) + .hasMessage("The given Path <%s> to compare actual content to should exist", expected); + } + + @Test + @DisabledOnOs(value = WINDOWS, disabledReason = "gh-2312") + void should_fail_if_expected_is_not_readable() throws IOException { + // GIVEN + Path actual = createFile(tempDir.resolve("actual")); + Path expected = createFile(tempDir.resolve("expected")); + expected.toFile().setReadable(false); + // WHEN + Throwable thrown = catchThrowable(() -> paths.assertHasSameTextualContentAs(info, actual, CHARSET, expected, CHARSET)); + // THEN + then(thrown).isInstanceOf(IllegalArgumentException.class) + .hasMessage("The given Path <%s> to compare actual content to should be readable", expected); + } + + @Test + void should_fail_if_actual_is_null() throws IOException { + // GIVEN + Path expected = createFile(tempDir.resolve("expected")); + // WHEN + AssertionError error = expectAssertionError(() -> paths.assertHasSameTextualContentAs(info, null, CHARSET, expected, + CHARSET)); + // THEN + then(error).hasMessage(actualIsNull()); + } + + @Test + void should_fail_if_actual_does_not_exist() throws IOException { + // GIVEN + Path actual = tempDir.resolve("non-existent"); + Path expected = createFile(tempDir.resolve("expected")); + // WHEN + AssertionError error = expectAssertionError(() -> paths.assertHasSameTextualContentAs(info, actual, CHARSET, expected, + CHARSET)); + // THEN + then(error).hasMessage(shouldExist(actual).create()); + } + + @Test + @DisabledOnOs(value = WINDOWS, disabledReason = "gh-2312") + void should_fail_if_actual_is_not_readable() throws IOException { + // GIVEN + Path actual = createFile(tempDir.resolve("actual")); + actual.toFile().setReadable(false); + Path expected = createFile(tempDir.resolve("expected")); + // WHEN + AssertionError error = expectAssertionError(() -> paths.assertHasSameTextualContentAs(info, actual, CHARSET, expected, + CHARSET)); + // THEN + then(error).hasMessage(shouldBeReadable(actual).create()); + } + + @ParameterizedTest + @CsvSource({ + "Content, US-ASCII, US-ASCII", + "Content, US-ASCII, ISO_8859_1", + "Content, US-ASCII, UTF-8", + "Content, US-ASCII, UTF-16", + }) + void should_pass_if_actual_has_the_same_content_as_expected(String content, + Charset actualCharset, + Charset expectedCharset) throws IOException { + // GIVEN + Path actual = Files.write(tempDir.resolve("actual"), content.getBytes(actualCharset)); + Path expected = Files.write(tempDir.resolve("expected"), content.getBytes(expectedCharset)); + // WHEN/THEN + paths.assertHasSameTextualContentAs(info, actual, actualCharset, expected, expectedCharset); + } + + @ParameterizedTest + @CsvSource({ + "Content, US-ASCII, Another content, US-ASCII", + }) + void should_fail_if_actual_does_not_have_the_same_content_as_expected(String actualContent, + Charset actualCharset, + String expectedContent, + Charset expectedCharset) throws IOException { + // GIVEN + Path actual = Files.write(tempDir.resolve("actual"), actualContent.getBytes(actualCharset)); + Path expected = Files.write(tempDir.resolve("expected"), expectedContent.getBytes(expectedCharset)); + List> diffs = diff.diff(actual, actualCharset, expected, expectedCharset); + // WHEN + AssertionError error = expectAssertionError(() -> paths.assertHasSameTextualContentAs(info, actual, actualCharset, + expected, expectedCharset)); + // THEN + then(error).hasMessage(shouldHaveSameContent(actual, expected, diffs).create(info.description(), info.representation())); + } + + @Test + @DisabledOnOs(value = WINDOWS, disabledReason = "gh-2312") + void should_rethrow_IOException_as_UncheckedIOException() throws IOException { + // GIVEN + Path actual = createFile(tempDir.resolve("actual")); + Path expected = createFile(tempDir.resolve("expected")); + IOException exception = new IOException("boom!"); + given(diff.diff(actual, CHARSET, expected, CHARSET)).willThrow(exception); + // WHEN + Throwable thrown = catchThrowable(() -> paths.assertHasSameTextualContentAs(info, actual, CHARSET, expected, CHARSET)); + // THEN + then(thrown).isInstanceOf(UncheckedIOException.class) + .hasCause(exception); + } + +} diff --git a/src/test/java/org/assertj/core/internal/paths/Paths_assertHasSize_Test.java b/src/test/java/org/assertj/core/internal/paths/Paths_assertHasSize_Test.java index ebe28763cf..3402f177e2 100644 --- a/src/test/java/org/assertj/core/internal/paths/Paths_assertHasSize_Test.java +++ b/src/test/java/org/assertj/core/internal/paths/Paths_assertHasSize_Test.java @@ -12,6 +12,8 @@ */ package org.assertj.core.internal.paths; +import static java.nio.file.Files.createDirectory; +import static java.nio.file.Files.createFile; import static org.assertj.core.api.Assertions.catchThrowable; import static org.assertj.core.api.BDDAssertions.then; import static org.assertj.core.error.ShouldBeRegularFile.shouldBeRegularFile; @@ -19,7 +21,6 @@ import static org.assertj.core.error.ShouldHaveSize.shouldHaveSize; import static org.assertj.core.util.AssertionsUtil.expectAssertionError; import static org.assertj.core.util.FailureMessages.actualIsNull; -import static org.mockito.ArgumentMatchers.any; import static org.mockito.BDDMockito.given; import java.io.IOException; @@ -28,22 +29,10 @@ import java.nio.file.Path; import org.assertj.core.internal.PathsBaseTest; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.io.TempDir; class Paths_assertHasSize_Test extends PathsBaseTest { - @TempDir - Path tempDir; - - @BeforeEach - void setUpNioFilesWrapper() throws IOException { - given(nioFilesWrapper.exists(any())).willCallRealMethod(); - given(nioFilesWrapper.isRegularFile(any())).willCallRealMethod(); - given(nioFilesWrapper.size(any())).willCallRealMethod(); - } - @Test void should_fail_if_actual_is_null() { // WHEN @@ -65,7 +54,7 @@ void should_fail_if_actual_does_not_exist() { @Test void should_fail_if_actual_is_not_regular_file() throws IOException { // GIVEN - Path actual = Files.createDirectory(tempDir.resolve("directory")); + Path actual = createDirectory(tempDir.resolve("directory")); // WHEN AssertionError error = expectAssertionError(() -> paths.assertHasSize(info, actual, 0L)); // THEN @@ -75,14 +64,13 @@ void should_fail_if_actual_is_not_regular_file() throws IOException { @Test void should_rethrow_IOException_as_UncheckedIOException() throws IOException { // GIVEN - Path actual = Files.createFile(tempDir.resolve("actual")); + Path actual = createFile(tempDir.resolve("actual")); IOException exception = new IOException("boom!"); given(nioFilesWrapper.size(actual)).willThrow(exception); // WHEN Throwable thrown = catchThrowable(() -> paths.assertHasSize(info, actual, 0L)); // THEN then(thrown).isInstanceOf(UncheckedIOException.class) - .hasMessage("unable to verify the size of the path: <%s>", actual) .hasCause(exception); } diff --git a/src/test/java/org/assertj/core/internal/paths/Paths_assertHasTextualContent_Test.java b/src/test/java/org/assertj/core/internal/paths/Paths_assertHasTextualContent_Test.java new file mode 100644 index 0000000000..32827fedc8 --- /dev/null +++ b/src/test/java/org/assertj/core/internal/paths/Paths_assertHasTextualContent_Test.java @@ -0,0 +1,129 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + * + * Copyright 2012-2021 the original author or authors. + */ +package org.assertj.core.internal.paths; + +import static java.nio.charset.Charset.defaultCharset; +import static java.nio.file.Files.createFile; +import static org.assertj.core.api.Assertions.catchThrowable; +import static org.assertj.core.api.BDDAssertions.then; +import static org.assertj.core.error.ShouldBeReadable.shouldBeReadable; +import static org.assertj.core.error.ShouldExist.shouldExist; +import static org.assertj.core.error.ShouldHaveContent.shouldHaveContent; +import static org.assertj.core.util.AssertionsUtil.expectAssertionError; +import static org.assertj.core.util.FailureMessages.actualIsNull; +import static org.junit.jupiter.api.condition.OS.WINDOWS; +import static org.mockito.BDDMockito.given; + +import java.io.IOException; +import java.io.UncheckedIOException; +import java.nio.charset.Charset; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.List; + +import org.assertj.core.internal.PathsBaseTest; +import org.assertj.core.util.diff.Delta; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.condition.DisabledOnOs; + +/** + * @author Olivier Michallat + * @author Joel Costigliola + */ +class Paths_assertHasTextualContent_Test extends PathsBaseTest { + + private static final Charset CHARSET = defaultCharset(); + + @Test + void should_fail_if_expected_is_null() throws IOException { + // GIVEN + Path actual = createFile(tempDir.resolve("actual")); + // WHEN + Throwable thrown = catchThrowable(() -> paths.assertHasTextualContent(info, actual, null, CHARSET)); + // THEN + then(thrown).isInstanceOf(NullPointerException.class) + .hasMessage("The text to compare to should not be null"); + } + + @Test + void should_fail_if_actual_is_null() throws IOException { + // GIVEN + String expected = "expected"; + // WHEN + AssertionError error = expectAssertionError(() -> paths.assertHasTextualContent(info, null, expected, CHARSET)); + // THEN + then(error).hasMessage(actualIsNull()); + } + + @Test + void should_fail_if_actual_does_not_exist() throws IOException { + // GIVEN + Path actual = tempDir.resolve("non-existent"); + String expected = "expected"; + // WHEN + AssertionError error = expectAssertionError(() -> paths.assertHasTextualContent(info, actual, expected, CHARSET)); + // THEN + then(error).hasMessage(shouldExist(actual).create()); + } + + @Test + @DisabledOnOs(value = WINDOWS, disabledReason = "gh-2312") + void should_fail_if_actual_is_not_readable() throws IOException { + // GIVEN + Path actual = createFile(tempDir.resolve("actual")); + actual.toFile().setReadable(false); + String expected = "expected"; + // WHEN + AssertionError error = expectAssertionError(() -> paths.assertHasTextualContent(info, actual, expected, CHARSET)); + // THEN + then(error).hasMessage(shouldBeReadable(actual).create()); + } + + @Test + void should_pass_if_actual_has_expected_textual_content() throws IOException { + // GIVEN + Path actual = Files.write(tempDir.resolve("actual"), "Content".getBytes(CHARSET)); + String expected = "Content"; + // WHEN/THEN + paths.assertHasTextualContent(info, actual, expected, CHARSET); + } + + @Test + void should_fail_if_actual_does_not_have_expected_textual_content() throws IOException { + // GIVEN + Path actual = Files.write(tempDir.resolve("actual"), "Content".getBytes(CHARSET)); + String expected = "Another content"; + List> diffs = diff.diff(actual, expected, CHARSET); + // WHEN + AssertionError error = expectAssertionError(() -> paths.assertHasTextualContent(info, actual, expected, CHARSET)); + // THEN + then(error).hasMessage(shouldHaveContent(actual, CHARSET, diffs).create(info.description(), info.representation())); + } + + @Test + @DisabledOnOs(value = WINDOWS, disabledReason = "gh-2312") + void should_rethrow_IOException_as_UncheckedIOException() throws IOException { + // GIVEN + Path actual = createFile(tempDir.resolve("actual")); + String expected = "expected"; + IOException exception = new IOException("boom!"); + given(diff.diff(actual, expected, CHARSET)).willThrow(exception); + // WHEN + Throwable thrown = catchThrowable(() -> paths.assertHasTextualContent(info, actual, expected, CHARSET)); + // THEN + then(thrown).isInstanceOf(UncheckedIOException.class) + .hasMessage("Unable to verify text contents of path:<%s>", actual) + .hasCause(exception); + } + +} diff --git a/src/test/java/org/assertj/core/internal/paths/Paths_assertIsAbsolute_Test.java b/src/test/java/org/assertj/core/internal/paths/Paths_assertIsAbsolute_Test.java index 83ec627cc6..15d34fb288 100644 --- a/src/test/java/org/assertj/core/internal/paths/Paths_assertIsAbsolute_Test.java +++ b/src/test/java/org/assertj/core/internal/paths/Paths_assertIsAbsolute_Test.java @@ -12,38 +12,52 @@ */ package org.assertj.core.internal.paths; -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatExceptionOfType; -import static org.assertj.core.api.ThrowableAssert.catchThrowable; +import static org.assertj.core.api.BDDAssertions.then; import static org.assertj.core.error.ShouldBeAbsolutePath.shouldBeAbsolutePath; +import static org.assertj.core.util.AssertionsUtil.expectAssertionError; import static org.assertj.core.util.FailureMessages.actualIsNull; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.stream.Stream; + +import org.assertj.core.internal.PathsBaseTest; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; -class Paths_assertIsAbsolute_Test extends MockPathsBaseTest { +class Paths_assertIsAbsolute_Test extends PathsBaseTest { @Test void should_fail_if_actual_is_null() { - assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> paths.assertIsAbsolute(info, null)) - .withMessage(actualIsNull()); + // WHEN + AssertionError error = expectAssertionError(() -> paths.assertIsAbsolute(info, null)); + // THEN + then(error).hasMessage(actualIsNull()); } - @Test + @ParameterizedTest + @MethodSource("nonAbsolutePaths") void should_fail_if_actual_is_not_absolute() { - // This is the default, but make it explicit - when(actual.isAbsolute()).thenReturn(false); - - Throwable error = catchThrowable(() -> paths.assertIsAbsolute(info, actual)); + // GIVEN + Path actual = Paths.get("relative"); + // WHEN + AssertionError error = expectAssertionError(() -> paths.assertIsAbsolute(info, actual)); + // THEN + then(error).hasMessage(shouldBeAbsolutePath(actual).create()); + } - assertThat(error).isInstanceOf(AssertionError.class); - verify(failures).failure(info, shouldBeAbsolutePath(actual)); + private static Stream nonAbsolutePaths() { + return Stream.of(Paths.get("foo"), + Paths.get("foo", "bar")); } @Test void should_pass_if_actual_is_absolute() { - when(actual.isAbsolute()).thenReturn(true); + // GIVEN + Path actual = tempDir.getRoot().resolve("foo").resolve("bar"); + // WHEN/THEN paths.assertIsAbsolute(info, actual); } + } diff --git a/src/test/java/org/assertj/core/internal/paths/Paths_assertIsCanonical_Test.java b/src/test/java/org/assertj/core/internal/paths/Paths_assertIsCanonical_Test.java index b63bf97de5..0d3c6183dc 100644 --- a/src/test/java/org/assertj/core/internal/paths/Paths_assertIsCanonical_Test.java +++ b/src/test/java/org/assertj/core/internal/paths/Paths_assertIsCanonical_Test.java @@ -12,53 +12,63 @@ */ package org.assertj.core.internal.paths; -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatExceptionOfType; -import static org.assertj.core.api.ThrowableAssert.catchThrowable; +import static java.nio.file.Files.createFile; +import static java.nio.file.Files.createSymbolicLink; +import static org.assertj.core.api.Assertions.catchThrowable; +import static org.assertj.core.api.BDDAssertions.then; import static org.assertj.core.error.ShouldBeCanonicalPath.shouldBeCanonicalPath; +import static org.assertj.core.util.AssertionsUtil.expectAssertionError; import static org.assertj.core.util.FailureMessages.actualIsNull; +import static org.mockito.BDDMockito.given; import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; import java.io.IOException; +import java.io.UncheckedIOException; import java.nio.file.Path; -import org.assertj.core.api.exception.PathsException; +import org.assertj.core.internal.PathsBaseTest; import org.junit.jupiter.api.Test; -class Paths_assertIsCanonical_Test extends MockPathsBaseTest { +class Paths_assertIsCanonical_Test extends PathsBaseTest { @Test void should_fail_if_actual_is_null() { - assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> paths.assertIsCanonical(info, null)) - .withMessage(actualIsNull()); + // WHEN + AssertionError error = expectAssertionError(() -> paths.assertIsCanonical(info, null)); + // THEN + then(error).hasMessage(actualIsNull()); } @Test - void should_throw_PathsException_on_io_error() throws IOException { - final IOException exception = new IOException(); - when(actual.toRealPath()).thenThrow(exception); - - assertThatExceptionOfType(PathsException.class).isThrownBy(() -> paths.assertIsCanonical(info, actual)) - .withMessage("failed to resolve actual real path") - .withCause(exception); + void should_rethrow_IOException_as_UncheckedIOException() throws IOException { + // GIVEN + Path actual = mock(Path.class); + IOException exception = new IOException("boom!"); + given(actual.toRealPath()).willThrow(exception); + // WHEN + Throwable thrown = catchThrowable(() -> paths.assertIsCanonical(info, actual)); + // THEN + then(thrown).isInstanceOf(UncheckedIOException.class) + .hasCause(exception); } @Test - void should_fail_if_actual_real_path_differs_from_actual() throws IOException { - final Path other = mock(Path.class); - when(actual.toRealPath()).thenReturn(other); - - Throwable error = catchThrowable(() -> paths.assertIsCanonical(info, actual)); - - assertThat(error).isInstanceOf(AssertionError.class); - verify(failures).failure(info, shouldBeCanonicalPath(actual)); + void should_fail_if_actual_is_not_canonical() throws IOException { + // GIVEN + Path file = createFile(tempDir.resolve("file")); + Path actual = createSymbolicLink(tempDir.resolve("actual"), file); + // WHEN + AssertionError error = expectAssertionError(() -> paths.assertIsCanonical(info, actual)); + // THEN + then(error).hasMessage(shouldBeCanonicalPath(actual).create()); } @Test - void should_succeed_if_actual_real_path_is_same_as_actual() throws IOException { - when(actual.toRealPath()).thenReturn(actual); + void should_pass_if_actual_is_canonical() throws IOException { + // GIVEN + Path actual = createFile(tempDir.resolve("actual")).toRealPath(); + // WHEN/THEN paths.assertIsCanonical(info, actual); } + } diff --git a/src/test/java/org/assertj/core/internal/paths/Paths_assertIsDirectoryContaining_Predicate_Test.java b/src/test/java/org/assertj/core/internal/paths/Paths_assertIsDirectoryContaining_Predicate_Test.java deleted file mode 100644 index 4e9fc68e1c..0000000000 --- a/src/test/java/org/assertj/core/internal/paths/Paths_assertIsDirectoryContaining_Predicate_Test.java +++ /dev/null @@ -1,166 +0,0 @@ -/* - * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on - * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - * - * Copyright 2012-2021 the original author or authors. - */ -package org.assertj.core.internal.paths; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatNullPointerException; -import static org.assertj.core.api.Assertions.catchThrowable; -import static org.assertj.core.error.ShouldBeDirectory.shouldBeDirectory; -import static org.assertj.core.error.ShouldContain.directoryShouldContain; -import static org.assertj.core.error.ShouldExist.shouldExist; -import static org.assertj.core.internal.Paths.toPathNames; -import static org.assertj.core.util.AssertionsUtil.expectAssertionError; -import static org.assertj.core.util.FailureMessages.actualIsNull; -import static org.assertj.core.util.Lists.emptyList; -import static org.assertj.core.util.Lists.list; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.eq; -import static org.mockito.BDDMockito.given; -import static org.mockito.Mockito.verify; - -import java.io.IOException; -import java.io.UncheckedIOException; -import java.nio.file.Path; -import java.util.List; -import java.util.Optional; -import java.util.function.Predicate; - -import org.assertj.core.api.AssertionInfo; -import org.assertj.core.internal.Paths; -import org.junit.jupiter.api.Test; - -/** - * Tests for {@link Paths#assertIsDirectoryContaining(AssertionInfo, Path, Predicate)} - * - * @author Valeriy Vyrva - */ -class Paths_assertIsDirectoryContaining_Predicate_Test extends MockPathsBaseTest { - - /** - * We will check count call to {@link Path#getFileName()} - */ - private static final Predicate JAVA_SOURCE = path -> Optional.ofNullable(path.getFileName()) - .map(Path::toString) - .filter(pathName -> pathName.endsWith(".java")) - .isPresent(); - - @Test - void should_pass_if_actual_contains_a_file_matching_the_given_predicate() { - // GIVEN - Path file = mockEmptyRegularFile("Test.java"); - Path actual = mockDirectory("root", list(file)); - // THEN - paths.assertIsDirectoryContaining(INFO, actual, JAVA_SOURCE); - } - - @Test - void should_pass_if_all_actual_files_match_the_given_predicate() { - // GIVEN - Path file1 = mockEmptyRegularFile("Test.java"); - Path file2 = mockEmptyRegularFile("Utils.java"); - Path actual = mockDirectory("root", list(file1, file2)); - // THEN - paths.assertIsDirectoryContaining(INFO, actual, JAVA_SOURCE); - } - - @Test - void should_pass_if_actual_contains_at_least_one_file_matching_the_given_predicate() { - // GIVEN - Path file1 = mockEmptyRegularFile("Test.class"); - Path file2 = mockEmptyRegularFile("Test.java"); - Path file3 = mockEmptyRegularFile("Utils.class"); - Path file4 = mockEmptyRegularFile("Utils.java"); - Path file5 = mockEmptyRegularFile("application.yml"); - Path actual = mockDirectory("root", list(file1, file2, file3, file4, file5)); - // THEN - paths.assertIsDirectoryContaining(INFO, actual, JAVA_SOURCE); - } - - @Test - void should_throw_error_if_filter_is_null() { - // GIVEN - Predicate filter = null; - // THEN - assertThatNullPointerException().isThrownBy(() -> paths.assertIsDirectoryContaining(INFO, null, filter)) - .withMessage("The paths filter should not be null"); - } - - @Test - void should_fail_if_actual_is_null() { - // GIVEN - Path actual = null; - // WHEN - AssertionError error = expectAssertionError(() -> paths.assertIsDirectoryContaining(INFO, actual, JAVA_SOURCE)); - // THEN - assertThat(error).hasMessage(actualIsNull()); - } - - @Test - void should_fail_if_actual_does_not_exist() { - // GIVEN - given(nioFilesWrapper.exists(actual)).willReturn(false); - // WHEN - expectAssertionError(() -> paths.assertIsDirectoryContaining(INFO, actual, JAVA_SOURCE)); - // THEN - verify(failures).failure(INFO, shouldExist(actual)); - } - - @Test - void should_fail_if_actual_exists_but_is_not_directory() { - // GIVEN - given(nioFilesWrapper.exists(actual)).willReturn(true); - given(nioFilesWrapper.isDirectory(actual)).willReturn(false); - // WHEN - expectAssertionError(() -> paths.assertIsDirectoryContaining(INFO, actual, JAVA_SOURCE)); - // THEN - verify(failures).failure(INFO, shouldBeDirectory(actual)); - } - - @Test - void should_throw_runtime_error_wrapping_caught_IOException() throws IOException { - // GIVEN - IOException cause = new IOException(); - given(nioFilesWrapper.exists(actual)).willReturn(true); - given(nioFilesWrapper.isDirectory(actual)).willReturn(true); - given(nioFilesWrapper.newDirectoryStream(eq(actual), any())).willThrow(cause); - // WHEN - Throwable error = catchThrowable(() -> paths.assertIsDirectoryContaining(INFO, actual, JAVA_SOURCE)); - // THEN - assertThat(error).isInstanceOf(UncheckedIOException.class) - .hasCause(cause); - } - - @Test - void should_fail_if_actual_is_empty() { - // GIVEN - List emptyList = emptyList(); - Path actual = mockDirectory("root", emptyList); - // WHEN - expectAssertionError(() -> paths.assertIsDirectoryContaining(INFO, actual, JAVA_SOURCE)); - // THEN - verify(failures).failure(INFO, directoryShouldContain(actual, emptyList(), "the given filter")); - } - - @Test - void should_fail_if_actual_does_not_contain_any_files_matching_the_given_predicate() { - // GIVEN - Path file = mockEmptyRegularFile("root", "Test.class"); - List files = list(file); - Path actual = mockDirectory("root", files); - // WHEN - expectAssertionError(() -> paths.assertIsDirectoryContaining(INFO, actual, JAVA_SOURCE)); - // THEN - verify(failures).failure(INFO, directoryShouldContain(actual, toPathNames(files), "the given filter")); - } - -} diff --git a/src/test/java/org/assertj/core/internal/paths/Paths_assertIsDirectoryContaining_SyntaxAndPattern_Test.java b/src/test/java/org/assertj/core/internal/paths/Paths_assertIsDirectoryContaining_SyntaxAndPattern_Test.java deleted file mode 100644 index 249854d439..0000000000 --- a/src/test/java/org/assertj/core/internal/paths/Paths_assertIsDirectoryContaining_SyntaxAndPattern_Test.java +++ /dev/null @@ -1,187 +0,0 @@ -/* - * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on - * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - * - * Copyright 2012-2021 the original author or authors. - */ -package org.assertj.core.internal.paths; - -import static java.lang.String.format; -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatNullPointerException; -import static org.assertj.core.api.Assertions.catchThrowable; -import static org.assertj.core.error.ShouldBeDirectory.shouldBeDirectory; -import static org.assertj.core.error.ShouldContain.directoryShouldContain; -import static org.assertj.core.error.ShouldExist.shouldExist; -import static org.assertj.core.internal.Paths.toPathNames; -import static org.assertj.core.util.AssertionsUtil.expectAssertionError; -import static org.assertj.core.util.FailureMessages.actualIsNull; -import static org.assertj.core.util.Lists.emptyList; -import static org.assertj.core.util.Lists.list; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.anyString; -import static org.mockito.ArgumentMatchers.eq; -import static org.mockito.BDDMockito.given; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; - -import java.io.IOException; -import java.io.UncheckedIOException; -import java.nio.file.FileSystem; -import java.nio.file.Path; -import java.nio.file.PathMatcher; -import java.util.List; -import java.util.Optional; -import java.util.regex.Pattern; - -import org.assertj.core.api.AssertionInfo; -import org.assertj.core.internal.Paths; -import org.junit.jupiter.api.Test; - -/** - * Tests for {@link Paths#assertIsDirectoryContaining(AssertionInfo, Path, String)} - * - * @author Valeriy Vyrva - */ -class Paths_assertIsDirectoryContaining_SyntaxAndPattern_Test extends MockPathsBaseTest { - - private static final String JAVA_SOURCE_PATTERN = "regex:.+\\.java"; - private static final String JAVA_SOURCE_PATTERN_DESCRIPTION = format("the '%s' pattern", JAVA_SOURCE_PATTERN); - - @Test - void should_pass_if_actual_contains_a_file_matching_the_given_pattern() { - // GIVEN - Path file = mockEmptyRegularFile("Test.java"); - Path actual = mockDirectory("root", list(file)); - mockPathMatcher(actual); - // THEN - paths.assertIsDirectoryContaining(INFO, actual, JAVA_SOURCE_PATTERN); - } - - @Test - void should_pass_if_all_actual_files_match_the_given_pattern() { - // GIVEN - Path file1 = mockEmptyRegularFile("Test.java"); - Path file2 = mockEmptyRegularFile("Utils.java"); - Path actual = mockDirectory("root", list(file1, file2)); - mockPathMatcher(actual); - // THEN - paths.assertIsDirectoryContaining(INFO, actual, JAVA_SOURCE_PATTERN); - } - - @Test - void should_pass_if_actual_contains_at_least_one_file_matching_the_given_pattern() { - // GIVEN - Path file1 = mockEmptyRegularFile("Test.class"); - Path file2 = mockEmptyRegularFile("Test.java"); - Path file3 = mockEmptyRegularFile("Utils.class"); - Path file4 = mockEmptyRegularFile("Utils.java"); - Path file5 = mockEmptyRegularFile("application.yml"); - Path actual = mockDirectory("root", list(file1, file2, file3, file4, file5)); - mockPathMatcher(actual); - // THEN - paths.assertIsDirectoryContaining(INFO, actual, JAVA_SOURCE_PATTERN); - } - - @Test - void should_throw_error_if_filter_is_null() { - // GIVEN - String filter = null; - // THEN - assertThatNullPointerException().isThrownBy(() -> paths.assertIsDirectoryContaining(INFO, null, filter)) - .withMessage("The syntax and pattern should not be null"); - } - - @Test - void should_fail_if_actual_is_null() { - // GIVEN - Path actual = null; - // WHEN - AssertionError error = expectAssertionError(() -> paths.assertIsDirectoryContaining(INFO, actual, JAVA_SOURCE_PATTERN)); - // THEN - assertThat(error).hasMessage(actualIsNull()); - } - - @Test - void should_fail_if_actual_does_not_exist() { - // GIVEN - given(nioFilesWrapper.exists(actual)).willReturn(false); - mockPathMatcher(actual); - // WHEN - expectAssertionError(() -> paths.assertIsDirectoryContaining(INFO, actual, JAVA_SOURCE_PATTERN)); - // THEN - verify(failures).failure(INFO, shouldExist(actual)); - } - - @Test - void should_fail_if_actual_exists_but_is_not_directory() { - // GIVEN - given(nioFilesWrapper.exists(actual)).willReturn(true); - given(nioFilesWrapper.isDirectory(actual)).willReturn(false); - mockPathMatcher(actual); - // WHEN - expectAssertionError(() -> paths.assertIsDirectoryContaining(INFO, actual, JAVA_SOURCE_PATTERN)); - // THEN - verify(failures).failure(INFO, shouldBeDirectory(actual)); - } - - @Test - void should_throw_runtime_error_wrapping_caught_IOException() throws IOException { - // GIVEN - IOException cause = new IOException(); - given(nioFilesWrapper.exists(actual)).willReturn(true); - given(nioFilesWrapper.isDirectory(actual)).willReturn(true); - given(nioFilesWrapper.newDirectoryStream(eq(actual), any())).willThrow(cause); - mockPathMatcher(actual); - // WHEN - Throwable error = catchThrowable(() -> paths.assertIsDirectoryContaining(INFO, actual, JAVA_SOURCE_PATTERN)); - // THEN - assertThat(error).isInstanceOf(UncheckedIOException.class) - .hasCause(cause); - } - - @Test - void should_fail_if_actual_is_empty() { - // GIVEN - List emptyList = emptyList(); - Path actual = mockDirectory("root", emptyList); - mockPathMatcher(actual); - // WHEN - expectAssertionError(() -> paths.assertIsDirectoryContaining(INFO, actual, JAVA_SOURCE_PATTERN)); - // THEN - verify(failures).failure(INFO, directoryShouldContain(actual, emptyList(), JAVA_SOURCE_PATTERN_DESCRIPTION)); - } - - @Test - void should_fail_if_actual_does_not_contain_any_files_matching_the_given_predicate() { - // GIVEN - Path file = mockEmptyRegularFile("root", "Test.class"); - List files = list(file); - Path actual = mockDirectory("root", files); - mockPathMatcher(actual); - // WHEN - expectAssertionError(() -> paths.assertIsDirectoryContaining(INFO, actual, JAVA_SOURCE_PATTERN)); - // THEN - verify(failures).failure(INFO, directoryShouldContain(actual, toPathNames(files), JAVA_SOURCE_PATTERN_DESCRIPTION)); - } - - static void mockPathMatcher(Path actual) { - FileSystem fileSystem = mock(FileSystem.class); - given(fileSystem.getPathMatcher(anyString())).will(inv -> { - String regex = inv.getArgument(0).toString().split(":")[1]; - Pattern pattern = Pattern.compile("^" + regex + "$", Pattern.CASE_INSENSITIVE); - return (PathMatcher) path -> Optional.ofNullable(path.getFileName()) - .map(Path::toString) - .filter(pattern.asPredicate()) - .isPresent(); - }); - given(actual.getFileSystem()).willReturn(fileSystem); - } - -} diff --git a/src/test/java/org/assertj/core/internal/paths/Paths_assertIsDirectoryContaining_with_Predicate_Test.java b/src/test/java/org/assertj/core/internal/paths/Paths_assertIsDirectoryContaining_with_Predicate_Test.java new file mode 100644 index 0000000000..603a336601 --- /dev/null +++ b/src/test/java/org/assertj/core/internal/paths/Paths_assertIsDirectoryContaining_with_Predicate_Test.java @@ -0,0 +1,135 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + * + * Copyright 2012-2021 the original author or authors. + */ +package org.assertj.core.internal.paths; + +import static java.nio.file.Files.createDirectory; +import static java.nio.file.Files.createFile; +import static java.util.Collections.singletonList; +import static org.assertj.core.api.Assertions.catchThrowable; +import static org.assertj.core.api.BDDAssertions.then; +import static org.assertj.core.error.ShouldBeDirectory.shouldBeDirectory; +import static org.assertj.core.error.ShouldContain.directoryShouldContain; +import static org.assertj.core.error.ShouldExist.shouldExist; +import static org.assertj.core.util.AssertionsUtil.expectAssertionError; +import static org.assertj.core.util.FailureMessages.actualIsNull; +import static org.assertj.core.util.Lists.emptyList; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.BDDMockito.willThrow; + +import java.io.IOException; +import java.io.UncheckedIOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.function.Predicate; + +import org.assertj.core.internal.PathsBaseTest; +import org.junit.jupiter.api.Test; + +/** + * @author Valeriy Vyrva + */ +class Paths_assertIsDirectoryContaining_with_Predicate_Test extends PathsBaseTest { + + @Test + void should_fail_if_filter_is_null() throws IOException { + // GIVEN + Path actual = createDirectory(tempDir.resolve("actual")); + Predicate filter = null; + // WHEN + Throwable thrown = catchThrowable(() -> paths.assertIsDirectoryContaining(info, actual, filter)); + // THEN + then(thrown).isInstanceOf(NullPointerException.class) + .hasMessage("The paths filter should not be null"); + } + + @Test + void should_fail_if_actual_is_null() { + // GIVEN + Path actual = null; + Predicate filter = path -> true; + // WHEN + AssertionError error = expectAssertionError(() -> paths.assertIsDirectoryContaining(info, actual, filter)); + // THEN + then(error).hasMessage(actualIsNull()); + } + + @Test + void should_fail_if_actual_does_not_exist() { + // GIVEN + Path actual = tempDir.resolve("non-existent"); + Predicate filter = path -> true; + // WHEN + AssertionError error = expectAssertionError(() -> paths.assertIsDirectoryContaining(info, actual, filter)); + // THEN + then(error).hasMessage(shouldExist(actual).create()); + } + + @Test + void should_fail_if_actual_is_not_a_directory() throws IOException { + // GIVEN + Path actual = createFile(tempDir.resolve("file")); + Predicate filter = path -> true; + // WHEN + AssertionError error = expectAssertionError(() -> paths.assertIsDirectoryContaining(info, actual, filter)); + // THEN + then(error).hasMessage(shouldBeDirectory(actual).create()); + } + + @Test + void should_rethrow_IOException_as_UncheckedIOException() throws Exception { + // GIVEN + Path actual = createDirectory(tempDir.resolve("actual")); + Predicate filter = path -> true; + IOException cause = new IOException("boom!"); + willThrow(cause).given(nioFilesWrapper).newDirectoryStream(any(), any()); + // WHEN + Throwable thrown = catchThrowable(() -> paths.assertIsDirectoryContaining(info, actual, filter)); + // THEN + then(thrown).isInstanceOf(UncheckedIOException.class) + .hasCause(cause); + } + + @Test + void should_fail_if_actual_is_empty() throws IOException { + // GIVEN + Path actual = createDirectory(tempDir.resolve("actual")); + Predicate filter = path -> true; + // WHEN + AssertionError error = expectAssertionError(() -> paths.assertIsDirectoryContaining(info, actual, filter)); + // THEN + then(error).hasMessage(directoryShouldContain(actual, emptyList(), "the given filter").create()); + } + + @Test + void should_pass_if_actual_contains_at_least_one_path_matching_the_given_predicate() throws IOException { + // GIVEN + Path actual = createDirectory(tempDir.resolve("actual")); + createFile(actual.resolve("file")); + Predicate filter = Files::isRegularFile; + // WHEN/THEN + paths.assertIsDirectoryContaining(info, actual, filter); + } + + @Test + void should_fail_if_actual_does_not_contain_any_paths_matching_the_given_predicate() throws IOException { + // GIVEN + Path actual = createDirectory(tempDir.resolve("actual")); + Path directory = createDirectory(actual.resolve("directory")); + Predicate filter = Files::isRegularFile; + // WHEN + AssertionError error = expectAssertionError(() -> paths.assertIsDirectoryContaining(info, actual, filter)); + // THEN + then(error).hasMessage(directoryShouldContain(actual, singletonList(directory.toString()), "the given filter").create()); + } + +} diff --git a/src/test/java/org/assertj/core/internal/paths/Paths_assertIsDirectoryContaining_with_String_Test.java b/src/test/java/org/assertj/core/internal/paths/Paths_assertIsDirectoryContaining_with_String_Test.java new file mode 100644 index 0000000000..8ec4f544a6 --- /dev/null +++ b/src/test/java/org/assertj/core/internal/paths/Paths_assertIsDirectoryContaining_with_String_Test.java @@ -0,0 +1,135 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + * + * Copyright 2012-2021 the original author or authors. + */ +package org.assertj.core.internal.paths; + +import static java.nio.file.Files.createDirectory; +import static java.nio.file.Files.createFile; +import static java.util.Collections.singletonList; +import static org.assertj.core.api.Assertions.catchThrowable; +import static org.assertj.core.api.BDDAssertions.then; +import static org.assertj.core.error.ShouldBeDirectory.shouldBeDirectory; +import static org.assertj.core.error.ShouldContain.directoryShouldContain; +import static org.assertj.core.error.ShouldExist.shouldExist; +import static org.assertj.core.util.AssertionsUtil.expectAssertionError; +import static org.assertj.core.util.FailureMessages.actualIsNull; +import static org.assertj.core.util.Lists.emptyList; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.BDDMockito.willThrow; + +import java.io.IOException; +import java.io.UncheckedIOException; +import java.nio.file.Path; + +import org.assertj.core.internal.PathsBaseTest; +import org.junit.jupiter.api.Test; + +/** + * @author Valeriy Vyrva + */ +class Paths_assertIsDirectoryContaining_with_String_Test extends PathsBaseTest { + + @Test + void should_fail_if_syntaxAndPattern_is_null() throws IOException { + // GIVEN + Path actual = createDirectory(tempDir.resolve("actual")); + String syntaxAndPattern = null; + // WHEN + Throwable thrown = catchThrowable(() -> paths.assertIsDirectoryContaining(info, actual, syntaxAndPattern)); + // THEN + then(thrown).isInstanceOf(NullPointerException.class) + .hasMessage("The syntax and pattern should not be null"); + } + + @Test + void should_fail_if_actual_is_null() { + // GIVEN + Path actual = null; + String syntaxAndPattern = "glob:**"; + // WHEN + AssertionError error = expectAssertionError(() -> paths.assertIsDirectoryContaining(info, actual, syntaxAndPattern)); + // THEN + then(error).hasMessage(actualIsNull()); + } + + @Test + void should_fail_if_actual_does_not_exist() { + // GIVEN + Path actual = tempDir.resolve("non-existent"); + String syntaxAndPattern = "glob:**"; + // WHEN + AssertionError error = expectAssertionError(() -> paths.assertIsDirectoryContaining(info, actual, syntaxAndPattern)); + // THEN + then(error).hasMessage(shouldExist(actual).create()); + } + + @Test + void should_fail_if_actual_is_not_a_directory() throws IOException { + // GIVEN + Path actual = createFile(tempDir.resolve("file")); + String syntaxAndPattern = "glob:**"; + // WHEN + AssertionError error = expectAssertionError(() -> paths.assertIsDirectoryContaining(info, actual, syntaxAndPattern)); + // THEN + then(error).hasMessage(shouldBeDirectory(actual).create()); + } + + @Test + void should_rethrow_IOException_as_UncheckedIOException() throws Exception { + // GIVEN + Path actual = createDirectory(tempDir.resolve("actual")); + String syntaxAndPattern = "glob:*"; + IOException cause = new IOException("boom!"); + willThrow(cause).given(nioFilesWrapper).newDirectoryStream(any(), any()); + // WHEN + Throwable thrown = catchThrowable(() -> paths.assertIsDirectoryContaining(info, actual, syntaxAndPattern)); + // THEN + then(thrown).isInstanceOf(UncheckedIOException.class) + .hasCause(cause); + } + + @Test + void should_fail_if_actual_is_empty() throws IOException { + // GIVEN + Path actual = createDirectory(tempDir.resolve("actual")); + String syntaxAndPattern = "glob:**"; + // WHEN + AssertionError error = expectAssertionError(() -> paths.assertIsDirectoryContaining(info, actual, syntaxAndPattern)); + // THEN + then(error).hasMessage(directoryShouldContain(actual, emptyList(), "the 'glob:**' pattern").create()); + } + + @Test + void should_pass_if_actual_contains_at_least_one_path_matching_the_given_pattern() throws IOException { + // GIVEN + Path actual = createDirectory(tempDir.resolve("actual")); + createFile(actual.resolve("file")); + createDirectory(actual.resolve("directory")); + String syntaxAndPattern = "glob:**file"; + // WHEN/THEN + paths.assertIsDirectoryContaining(info, actual, syntaxAndPattern); + } + + @Test + void should_fail_if_actual_does_not_contain_any_paths_matching_the_given_pattern() throws IOException { + // GIVEN + Path actual = createDirectory(tempDir.resolve("actual")); + Path directory = createDirectory(actual.resolve("directory")); + String syntaxAndPattern = "glob:**file"; + // WHEN + AssertionError error = expectAssertionError(() -> paths.assertIsDirectoryContaining(info, actual, syntaxAndPattern)); + // THEN + then(error).hasMessage(directoryShouldContain(actual, singletonList(directory.toString()), + "the 'glob:**file' pattern").create()); + } + +} diff --git a/src/test/java/org/assertj/core/internal/paths/Paths_assertIsDirectoryNotContaining_Predicate_Test.java b/src/test/java/org/assertj/core/internal/paths/Paths_assertIsDirectoryNotContaining_Predicate_Test.java deleted file mode 100644 index 78a60b4538..0000000000 --- a/src/test/java/org/assertj/core/internal/paths/Paths_assertIsDirectoryNotContaining_Predicate_Test.java +++ /dev/null @@ -1,171 +0,0 @@ -/* - * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on - * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - * - * Copyright 2012-2021 the original author or authors. - */ -package org.assertj.core.internal.paths; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatNullPointerException; -import static org.assertj.core.api.Assertions.catchThrowable; -import static org.assertj.core.error.ShouldBeDirectory.shouldBeDirectory; -import static org.assertj.core.error.ShouldExist.shouldExist; -import static org.assertj.core.error.ShouldNotContain.directoryShouldNotContain; -import static org.assertj.core.internal.Paths.toPathNames; -import static org.assertj.core.util.AssertionsUtil.expectAssertionError; -import static org.assertj.core.util.FailureMessages.actualIsNull; -import static org.assertj.core.util.Lists.emptyList; -import static org.assertj.core.util.Lists.list; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.eq; -import static org.mockito.BDDMockito.given; -import static org.mockito.Mockito.verify; - -import java.io.IOException; -import java.io.UncheckedIOException; -import java.nio.file.Path; -import java.util.List; -import java.util.Optional; -import java.util.function.Predicate; - -import org.assertj.core.api.AssertionInfo; -import org.assertj.core.internal.Paths; -import org.junit.jupiter.api.Test; - -/** - * Tests for {@link Paths#assertIsDirectoryNotContaining(AssertionInfo, Path, Predicate)} - * - * @author Valeriy Vyrva - */ -class Paths_assertIsDirectoryNotContaining_Predicate_Test extends MockPathsBaseTest { - - /** - * We will check count call to {@link Path#getFileName()} - */ - private static final Predicate JAVA_SOURCE = path -> Optional.ofNullable(path.getFileName()) - .map(Path::toString) - .filter(fileName -> fileName.endsWith(".java")) - .isPresent(); - - @Test - void should_pass_if_actual_does_not_contain_files_matching_the_given_filter() { - // GIVEN - Path file = mockEmptyRegularFile("root", "Test.class"); - List items = list(file); - Path actual = mockDirectory("root", items); - // THEN - paths.assertIsDirectoryNotContaining(INFO, actual, JAVA_SOURCE); - } - - @Test - void should_pass_if_actual_is_empty() { - // GIVEN - List items = emptyList(); - Path actual = mockDirectory("root", items); - // THEN - paths.assertIsDirectoryNotContaining(INFO, actual, JAVA_SOURCE); - } - - @Test - void should_throw_error_if_filter_is_null() { - // GIVEN - Predicate filter = null; - // THEN - assertThatNullPointerException().isThrownBy(() -> paths.assertIsDirectoryNotContaining(INFO, null, filter)) - .withMessage("The paths filter should not be null"); - } - - @Test - void should_fail_if_actual_is_null() { - // GIVEN - Path actual = null; - // WHEN - AssertionError error = expectAssertionError(() -> paths.assertIsDirectoryNotContaining(INFO, actual, JAVA_SOURCE)); - // THEN - assertThat(error).hasMessage(actualIsNull()); - } - - @Test - void should_fail_if_actual_does_not_exist() { - // GIVEN - given(nioFilesWrapper.exists(actual)).willReturn(false); - // WHEN - expectAssertionError(() -> paths.assertIsDirectoryNotContaining(INFO, actual, JAVA_SOURCE)); - // THEN - verify(failures).failure(INFO, shouldExist(actual)); - } - - @Test - void should_fail_if_actual_exists_but_is_not_directory() { - // GIVEN - given(nioFilesWrapper.exists(actual)).willReturn(true); - given(nioFilesWrapper.isDirectory(actual)).willReturn(false); - // WHEN - expectAssertionError(() -> paths.assertIsDirectoryNotContaining(INFO, actual, JAVA_SOURCE)); - // THEN - verify(failures).failure(INFO, shouldBeDirectory(actual)); - } - - @Test - void should_throw_runtime_error_wrapping_caught_IOException() throws IOException { - // GIVEN - IOException cause = new IOException(); - given(nioFilesWrapper.exists(actual)).willReturn(true); - given(nioFilesWrapper.isDirectory(actual)).willReturn(true); - given(nioFilesWrapper.newDirectoryStream(eq(actual), any())).willThrow(cause); - // WHEN - Throwable error = catchThrowable(() -> paths.assertIsDirectoryNotContaining(INFO, actual, JAVA_SOURCE)); - // THEN - assertThat(error).isInstanceOf(UncheckedIOException.class) - .hasCause(cause); - } - - @Test - void should_fail_if_one_actual_file_matches_the_filter() { - // GIVEN - Path file = mockEmptyRegularFile("Test.java"); - List items = list(file); - Path actual = mockDirectory("root", items); - // WHEN - expectAssertionError(() -> paths.assertIsDirectoryNotContaining(INFO, actual, JAVA_SOURCE)); - // THEN - verify(failures).failure(INFO, directoryShouldNotContain(actual, toPathNames(items), "the given filter")); - } - - @Test - void should_fail_if_all_actual_files_match_the_filter() { - // GIVEN - Path file1 = mockEmptyRegularFile("Test.java"); - Path file2 = mockEmptyRegularFile("Utils.java"); - List items = list(file1, file2); - Path actual = mockDirectory("root", items); - // WHEN - expectAssertionError(() -> paths.assertIsDirectoryNotContaining(INFO, actual, JAVA_SOURCE)); - // THEN - verify(failures).failure(INFO, directoryShouldNotContain(actual, toPathNames(items), "the given filter")); - } - - @Test - void should_fail_if_some_actual_files_match_the_filter() { - // GIVEN - Path file1 = mockEmptyRegularFile("Test.class"); - Path file2 = mockEmptyRegularFile("Test.java"); - Path file3 = mockEmptyRegularFile("Utils.class"); - Path file4 = mockEmptyRegularFile("Utils.java"); - Path file5 = mockEmptyRegularFile("application.yml"); - List items = list(file1, file2, file3, file4, file5); - Path actual = mockDirectory("root", items); - // WHEN - expectAssertionError(() -> paths.assertIsDirectoryNotContaining(INFO, actual, JAVA_SOURCE)); - // THEN - verify(failures).failure(INFO, directoryShouldNotContain(actual, toPathNames(list(file2, file4)), "the given filter")); - } - -} diff --git a/src/test/java/org/assertj/core/internal/paths/Paths_assertIsDirectoryNotContaining_SyntaxAndPattern_Test.java b/src/test/java/org/assertj/core/internal/paths/Paths_assertIsDirectoryNotContaining_SyntaxAndPattern_Test.java deleted file mode 100644 index a3dea60de0..0000000000 --- a/src/test/java/org/assertj/core/internal/paths/Paths_assertIsDirectoryNotContaining_SyntaxAndPattern_Test.java +++ /dev/null @@ -1,177 +0,0 @@ -/* - * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on - * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - * - * Copyright 2012-2021 the original author or authors. - */ -package org.assertj.core.internal.paths; - -import static java.lang.String.format; -import static java.util.Collections.singletonList; -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatNullPointerException; -import static org.assertj.core.api.Assertions.catchThrowable; -import static org.assertj.core.error.ShouldBeDirectory.shouldBeDirectory; -import static org.assertj.core.error.ShouldExist.shouldExist; -import static org.assertj.core.error.ShouldNotContain.directoryShouldNotContain; -import static org.assertj.core.internal.Paths.toPathNames; -import static org.assertj.core.internal.paths.Paths_assertIsDirectoryContaining_SyntaxAndPattern_Test.mockPathMatcher; -import static org.assertj.core.util.AssertionsUtil.expectAssertionError; -import static org.assertj.core.util.FailureMessages.actualIsNull; -import static org.assertj.core.util.Lists.emptyList; -import static org.assertj.core.util.Lists.list; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.eq; -import static org.mockito.BDDMockito.given; -import static org.mockito.Mockito.verify; - -import java.io.IOException; -import java.io.UncheckedIOException; -import java.nio.file.Path; -import java.util.List; - -import org.assertj.core.api.AssertionInfo; -import org.assertj.core.internal.Paths; -import org.junit.jupiter.api.Test; - -/** - * Tests for {@link Paths#assertIsDirectoryNotContaining(AssertionInfo, Path, String)} - * - * @author Valeriy Vyrva - */ -class Paths_assertIsDirectoryNotContaining_SyntaxAndPattern_Test extends MockPathsBaseTest { - - private static final String JAVA_SOURCE_PATTERN = "regex:.+\\.java"; - private static final String JAVA_SOURCE_PATTERN_DESCRIPTION = format("the '%s' pattern", JAVA_SOURCE_PATTERN); - - @Test - void should_pass_if_actual_does_not_contain_files_matching_the_given_pattern() { - // GIVEN - Path file = mockEmptyRegularFile("root", "Test.class"); - List items = singletonList(file); - Path actual = mockDirectory("root", items); - mockPathMatcher(actual); - // THEN - paths.assertIsDirectoryNotContaining(INFO, actual, JAVA_SOURCE_PATTERN); - } - - @Test - void should_pass_if_actual_is_empty() { - // GIVEN - List items = emptyList(); - Path actual = mockDirectory("root", items); - mockPathMatcher(actual); - // THEN - paths.assertIsDirectoryNotContaining(INFO, actual, JAVA_SOURCE_PATTERN); - } - - @Test - void should_throw_error_if_pattern_is_null() { - // GIVEN - String pattern = null; - // THEN - assertThatNullPointerException().isThrownBy(() -> paths.assertIsDirectoryNotContaining(INFO, null, pattern)) - .withMessage("The syntax and pattern should not be null"); - } - - @Test - void should_fail_if_actual_is_null() { - // GIVEN - Path actual = null; - // WHEN - AssertionError error = expectAssertionError(() -> paths.assertIsDirectoryNotContaining(INFO, actual, JAVA_SOURCE_PATTERN)); - // THEN - assertThat(error).hasMessage(actualIsNull()); - } - - @Test - void should_fail_if_actual_does_not_exist() { - // GIVEN - given(nioFilesWrapper.exists(actual)).willReturn(false); - given(nioFilesWrapper.isDirectory(actual)).willReturn(false); - mockPathMatcher(actual); - // WHEN - expectAssertionError(() -> paths.assertIsDirectoryNotContaining(INFO, actual, JAVA_SOURCE_PATTERN)); - // THEN - verify(failures).failure(INFO, shouldExist(actual)); - } - - @Test - void should_fail_if_actual_exists_but_is_not_directory() { - // GIVEN - given(nioFilesWrapper.exists(actual)).willReturn(true); - given(nioFilesWrapper.isDirectory(actual)).willReturn(false); - mockPathMatcher(actual); - // WHEN - expectAssertionError(() -> paths.assertIsDirectoryNotContaining(INFO, actual, JAVA_SOURCE_PATTERN)); - // THEN - verify(failures).failure(INFO, shouldBeDirectory(actual)); - } - - @Test - void should_throw_runtime_error_wrapping_caught_IOException() throws IOException { - // GIVEN - IOException cause = new IOException(); - given(nioFilesWrapper.exists(actual)).willReturn(true); - given(nioFilesWrapper.isDirectory(actual)).willReturn(true); - given(nioFilesWrapper.newDirectoryStream(eq(actual), any())).willThrow(cause); - mockPathMatcher(actual); - // WHEN - Throwable error = catchThrowable(() -> paths.assertIsDirectoryNotContaining(INFO, actual, JAVA_SOURCE_PATTERN)); - // THEN - assertThat(error).isInstanceOf(UncheckedIOException.class) - .hasCause(cause); - } - - @Test - void should_fail_if_one_actual_file_matches_the_given_pattern() { - // GIVEN - Path file = mockEmptyRegularFile("Test.java"); - List items = list(file); - Path actual = mockDirectory("root", items); - mockPathMatcher(actual); - // WHEN - expectAssertionError(() -> paths.assertIsDirectoryNotContaining(INFO, actual, JAVA_SOURCE_PATTERN)); - // THEN - verify(failures).failure(INFO, directoryShouldNotContain(actual, toPathNames(items), JAVA_SOURCE_PATTERN_DESCRIPTION)); - } - - @Test - void should_fail_if_actual_contains_only_not_expected() { - // GIVEN - Path file1 = mockEmptyRegularFile("Test.java"); - Path file2 = mockEmptyRegularFile("Utils.java"); - List items = list(file1, file2); - Path actual = mockDirectory("root", items); - mockPathMatcher(actual); - // WHEN - expectAssertionError(() -> paths.assertIsDirectoryNotContaining(INFO, actual, JAVA_SOURCE_PATTERN)); - // THEN - verify(failures).failure(INFO, directoryShouldNotContain(actual, toPathNames(items), JAVA_SOURCE_PATTERN_DESCRIPTION)); - } - - @Test - void should_fail_if_some_actual_files_match_the_filter() { - // GIVEN - Path file1 = mockEmptyRegularFile("Test.class"); - Path file2 = mockEmptyRegularFile("Test.java"); - Path file3 = mockEmptyRegularFile("Utils.class"); - Path file4 = mockEmptyRegularFile("Utils.java"); - Path file5 = mockEmptyRegularFile("application.yml"); - List items = list(file1, file2, file3, file4, file5); - Path actual = mockDirectory("root", items); - mockPathMatcher(actual); - // WHEN - expectAssertionError(() -> paths.assertIsDirectoryNotContaining(INFO, actual, JAVA_SOURCE_PATTERN)); - // THEN - verify(failures).failure(INFO, - directoryShouldNotContain(actual, toPathNames(list(file2, file4)), JAVA_SOURCE_PATTERN_DESCRIPTION)); - } - -} diff --git a/src/test/java/org/assertj/core/internal/paths/Paths_assertIsDirectoryNotContaining_with_Predicate_Test.java b/src/test/java/org/assertj/core/internal/paths/Paths_assertIsDirectoryNotContaining_with_Predicate_Test.java new file mode 100644 index 0000000000..91cd31ed35 --- /dev/null +++ b/src/test/java/org/assertj/core/internal/paths/Paths_assertIsDirectoryNotContaining_with_Predicate_Test.java @@ -0,0 +1,132 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + * + * Copyright 2012-2021 the original author or authors. + */ +package org.assertj.core.internal.paths; + +import static java.nio.file.Files.createDirectory; +import static java.nio.file.Files.createFile; +import static java.util.Collections.singletonList; +import static org.assertj.core.api.Assertions.catchThrowable; +import static org.assertj.core.api.BDDAssertions.then; +import static org.assertj.core.error.ShouldBeDirectory.shouldBeDirectory; +import static org.assertj.core.error.ShouldExist.shouldExist; +import static org.assertj.core.error.ShouldNotContain.directoryShouldNotContain; +import static org.assertj.core.util.AssertionsUtil.expectAssertionError; +import static org.assertj.core.util.FailureMessages.actualIsNull; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.BDDMockito.willThrow; + +import java.io.IOException; +import java.io.UncheckedIOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.function.Predicate; + +import org.assertj.core.internal.PathsBaseTest; +import org.junit.jupiter.api.Test; + +/** + * @author Valeriy Vyrva + */ +class Paths_assertIsDirectoryNotContaining_with_Predicate_Test extends PathsBaseTest { + + @Test + void should_fail_if_filter_is_null() throws IOException { + // GIVEN + Path actual = createDirectory(tempDir.resolve("actual")); + Predicate filter = null; + // WHEN + Throwable thrown = catchThrowable(() -> paths.assertIsDirectoryNotContaining(info, actual, filter)); + // THEN + then(thrown).isInstanceOf(NullPointerException.class) + .hasMessage("The paths filter should not be null"); + } + + @Test + void should_fail_if_actual_is_null() { + // GIVEN + Path actual = null; + Predicate filter = path -> true; + // WHEN + AssertionError error = expectAssertionError(() -> paths.assertIsDirectoryNotContaining(info, actual, filter)); + // THEN + then(error).hasMessage(actualIsNull()); + } + + @Test + void should_fail_if_actual_does_not_exist() { + // GIVEN + Path actual = tempDir.resolve("non-existent"); + Predicate filter = path -> true; + // WHEN + AssertionError error = expectAssertionError(() -> paths.assertIsDirectoryNotContaining(info, actual, filter)); + // THEN + then(error).hasMessage(shouldExist(actual).create()); + } + + @Test + void should_fail_if_actual_is_not_a_directory() throws IOException { + // GIVEN + Path actual = createFile(tempDir.resolve("file")); + Predicate filter = path -> true; + // WHEN + AssertionError error = expectAssertionError(() -> paths.assertIsDirectoryNotContaining(info, actual, filter)); + // THEN + then(error).hasMessage(shouldBeDirectory(actual).create()); + } + + @Test + void should_rethrow_IOException_as_UncheckedIOException() throws Exception { + // GIVEN + Path actual = createDirectory(tempDir.resolve("actual")); + Predicate filter = path -> true; + IOException cause = new IOException("boom!"); + willThrow(cause).given(nioFilesWrapper).newDirectoryStream(any(), any()); + // WHEN + Throwable thrown = catchThrowable(() -> paths.assertIsDirectoryNotContaining(info, actual, filter)); + // THEN + then(thrown).isInstanceOf(UncheckedIOException.class) + .hasCause(cause); + } + + @Test + void should_pass_if_actual_is_empty() throws IOException { + // GIVEN + Path actual = createDirectory(tempDir.resolve("actual")); + Predicate filter = path -> true; + // WHEN/THEN + paths.assertIsDirectoryNotContaining(info, actual, filter); + } + + @Test + void should_fail_if_actual_contains_at_least_one_path_matching_the_given_predicate() throws IOException { + // GIVEN + Path actual = createDirectory(tempDir.resolve("actual")); + Path file = createFile(actual.resolve("file")); + Predicate filter = Files::isRegularFile; + // WHEN + AssertionError error = expectAssertionError(() -> paths.assertIsDirectoryNotContaining(info, actual, filter)); + // THEN + then(error).hasMessage(directoryShouldNotContain(actual, singletonList(file.toString()), "the given filter").create()); + } + + @Test + void should_pass_if_actual_does_not_contain_any_paths_matching_the_given_predicate() throws IOException { + // GIVEN + Path actual = createDirectory(tempDir.resolve("actual")); + createDirectory(actual.resolve("directory")); + Predicate filter = Files::isRegularFile; + // WHEN/THEN + paths.assertIsDirectoryNotContaining(info, actual, filter); + } + +} diff --git a/src/test/java/org/assertj/core/internal/paths/Paths_assertIsDirectoryNotContaining_with_String_Test.java b/src/test/java/org/assertj/core/internal/paths/Paths_assertIsDirectoryNotContaining_with_String_Test.java new file mode 100644 index 0000000000..e4abaf148e --- /dev/null +++ b/src/test/java/org/assertj/core/internal/paths/Paths_assertIsDirectoryNotContaining_with_String_Test.java @@ -0,0 +1,131 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + * + * Copyright 2012-2021 the original author or authors. + */ +package org.assertj.core.internal.paths; + +import static java.nio.file.Files.createDirectory; +import static java.nio.file.Files.createFile; +import static java.util.Collections.singletonList; +import static org.assertj.core.api.Assertions.catchThrowable; +import static org.assertj.core.api.BDDAssertions.then; +import static org.assertj.core.error.ShouldBeDirectory.shouldBeDirectory; +import static org.assertj.core.error.ShouldExist.shouldExist; +import static org.assertj.core.error.ShouldNotContain.directoryShouldNotContain; +import static org.assertj.core.util.AssertionsUtil.expectAssertionError; +import static org.assertj.core.util.FailureMessages.actualIsNull; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.BDDMockito.willThrow; + +import java.io.IOException; +import java.io.UncheckedIOException; +import java.nio.file.Path; + +import org.assertj.core.internal.PathsBaseTest; +import org.junit.jupiter.api.Test; + +/** + * @author Valeriy Vyrva + */ +class Paths_assertIsDirectoryNotContaining_with_String_Test extends PathsBaseTest { + + @Test + void should_fail_if_syntaxAndPattern_is_null() throws IOException { + // GIVEN + Path actual = createDirectory(tempDir.resolve("actual")); + String syntaxAndPattern = null; + // WHEN + Throwable thrown = catchThrowable(() -> paths.assertIsDirectoryNotContaining(info, actual, syntaxAndPattern)); + // THEN + then(thrown).isInstanceOf(NullPointerException.class) + .hasMessage("The syntax and pattern should not be null"); + } + + @Test + void should_fail_if_actual_is_null() { + // GIVEN + Path actual = null; + String syntaxAndPattern = "glob:**"; + // WHEN + AssertionError error = expectAssertionError(() -> paths.assertIsDirectoryNotContaining(info, actual, syntaxAndPattern)); + // THEN + then(error).hasMessage(actualIsNull()); + } + + @Test + void should_fail_if_actual_does_not_exist() { + // GIVEN + Path actual = tempDir.resolve("non-existent"); + String syntaxAndPattern = "glob:**"; + // WHEN + AssertionError error = expectAssertionError(() -> paths.assertIsDirectoryNotContaining(info, actual, syntaxAndPattern)); + // THEN + then(error).hasMessage(shouldExist(actual).create()); + } + + @Test + void should_fail_if_actual_is_not_a_directory() throws IOException { + // GIVEN + Path actual = createFile(tempDir.resolve("file")); + String syntaxAndPattern = "glob:**"; + // WHEN + AssertionError error = expectAssertionError(() -> paths.assertIsDirectoryNotContaining(info, actual, syntaxAndPattern)); + // THEN + then(error).hasMessage(shouldBeDirectory(actual).create()); + } + + @Test + void should_rethrow_IOException_as_UncheckedIOException() throws Exception { + // GIVEN + Path actual = createDirectory(tempDir.resolve("actual")); + String syntaxAndPattern = "glob:*"; + IOException cause = new IOException("boom!"); + willThrow(cause).given(nioFilesWrapper).newDirectoryStream(any(), any()); + // WHEN + Throwable thrown = catchThrowable(() -> paths.assertIsDirectoryNotContaining(info, actual, syntaxAndPattern)); + // THEN + then(thrown).isInstanceOf(UncheckedIOException.class) + .hasCause(cause); + } + + @Test + void should_pass_if_actual_is_empty() throws IOException { + // GIVEN + Path actual = createDirectory(tempDir.resolve("actual")); + String syntaxAndPattern = "glob:**"; + // WHEN/THEN + paths.assertIsDirectoryNotContaining(info, actual, syntaxAndPattern); + } + + @Test + void should_fail_if_actual_contains_at_least_one_path_matching_the_given_pattern() throws IOException { + // GIVEN + Path actual = createDirectory(tempDir.resolve("actual")); + Path file = createFile(actual.resolve("file")); + String syntaxAndPattern = "glob:**file"; + // WHEN + AssertionError error = expectAssertionError(() -> paths.assertIsDirectoryNotContaining(info, actual, syntaxAndPattern)); + // THEN + then(error).hasMessage(directoryShouldNotContain(actual, singletonList(file.toString()), + "the 'glob:**file' pattern").create()); + } + + @Test + void should_pass_if_actual_does_not_contain_any_paths_matching_the_given_pattern() throws IOException { + // GIVEN + Path actual = createDirectory(tempDir.resolve("actual")); + createDirectory(actual.resolve("directory")); + String syntaxAndPattern = "glob:**file"; + // WHEN/THEN + paths.assertIsDirectoryNotContaining(info, actual, syntaxAndPattern); + } + +} diff --git a/src/test/java/org/assertj/core/internal/paths/Paths_assertIsDirectory_Test.java b/src/test/java/org/assertj/core/internal/paths/Paths_assertIsDirectory_Test.java index fc6d353e67..dc557fbe76 100644 --- a/src/test/java/org/assertj/core/internal/paths/Paths_assertIsDirectory_Test.java +++ b/src/test/java/org/assertj/core/internal/paths/Paths_assertIsDirectory_Test.java @@ -12,50 +12,55 @@ */ package org.assertj.core.internal.paths; -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatExceptionOfType; -import static org.assertj.core.api.ThrowableAssert.catchThrowable; +import static java.nio.file.Files.createDirectory; +import static java.nio.file.Files.createFile; +import static org.assertj.core.api.BDDAssertions.then; import static org.assertj.core.error.ShouldBeDirectory.shouldBeDirectory; import static org.assertj.core.error.ShouldExist.shouldExist; +import static org.assertj.core.util.AssertionsUtil.expectAssertionError; import static org.assertj.core.util.FailureMessages.actualIsNull; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; +import java.io.IOException; +import java.nio.file.Path; + +import org.assertj.core.internal.PathsBaseTest; import org.junit.jupiter.api.Test; -class Paths_assertIsDirectory_Test extends MockPathsBaseTest { +class Paths_assertIsDirectory_Test extends PathsBaseTest { @Test void should_fail_if_actual_is_null() { - assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> paths.assertIsDirectory(info, null)) - .withMessage(actualIsNull()); + // WHEN + AssertionError error = expectAssertionError(() -> paths.assertIsDirectory(info, null)); + // THEN + then(error).hasMessage(actualIsNull()); } @Test - void should_fail_with_should_exist_error_if_actual_does_not_exist() { - when(nioFilesWrapper.exists(actual)).thenReturn(false); - - Throwable error = catchThrowable(() -> paths.assertIsDirectory(info, actual)); - - assertThat(error).isInstanceOf(AssertionError.class); - verify(failures).failure(info, shouldExist(actual)); + void should_fail_if_actual_does_not_exist() { + // GIVEN + Path actual = tempDir.resolve("non-existent"); + // WHEN + AssertionError error = expectAssertionError(() -> paths.assertIsDirectory(info, actual)); + // THEN + then(error).hasMessage(shouldExist(actual).create()); } @Test - void should_fail_if_target_exists_but_is_not_a_directory() { - when(nioFilesWrapper.exists(actual)).thenReturn(true); - when(nioFilesWrapper.isDirectory(actual)).thenReturn(false); - - Throwable error = catchThrowable(() -> paths.assertIsDirectory(info, actual)); - - assertThat(error).isInstanceOf(AssertionError.class); - verify(failures).failure(info, shouldBeDirectory(actual)); + void should_fail_if_actual_is_not_a_directory() throws IOException { + // GIVEN + Path actual = createFile(tempDir.resolve("file")); + // WHEN + AssertionError error = expectAssertionError(() -> paths.assertIsDirectory(info, actual)); + // THEN + then(error).hasMessage(shouldBeDirectory(actual).create()); } @Test - void should_succeed_if_actual_is_a_directory() { - when(nioFilesWrapper.exists(actual)).thenReturn(true); - when(nioFilesWrapper.isDirectory(actual)).thenReturn(true); + void should_pass_if_actual_is_a_directory() throws IOException { + // GIVEN + Path actual = createDirectory(tempDir.resolve("actual")); + // WHEN/THEN paths.assertIsDirectory(info, actual); } diff --git a/src/test/java/org/assertj/core/internal/paths/Paths_assertIsEmptyDirectory_Test.java b/src/test/java/org/assertj/core/internal/paths/Paths_assertIsEmptyDirectory_Test.java index 01f83ce53b..50e829d721 100644 --- a/src/test/java/org/assertj/core/internal/paths/Paths_assertIsEmptyDirectory_Test.java +++ b/src/test/java/org/assertj/core/internal/paths/Paths_assertIsEmptyDirectory_Test.java @@ -12,99 +12,90 @@ */ package org.assertj.core.internal.paths; -import static org.assertj.core.api.Assertions.assertThat; +import static java.nio.file.Files.createDirectory; +import static java.nio.file.Files.createFile; +import static java.util.Collections.singletonList; import static org.assertj.core.api.Assertions.catchThrowable; +import static org.assertj.core.api.BDDAssertions.then; import static org.assertj.core.error.ShouldBeDirectory.shouldBeDirectory; import static org.assertj.core.error.ShouldBeEmptyDirectory.shouldBeEmptyDirectory; import static org.assertj.core.error.ShouldExist.shouldExist; import static org.assertj.core.util.AssertionsUtil.expectAssertionError; import static org.assertj.core.util.FailureMessages.actualIsNull; -import static org.assertj.core.util.Lists.emptyList; -import static org.assertj.core.util.Lists.list; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.eq; -import static org.mockito.BDDMockito.given; -import static org.mockito.Mockito.verify; +import static org.mockito.BDDMockito.willThrow; import java.io.IOException; import java.io.UncheckedIOException; import java.nio.file.Path; -import java.util.List; -import org.assertj.core.api.AssertionInfo; -import org.assertj.core.internal.Paths; +import org.assertj.core.internal.PathsBaseTest; import org.junit.jupiter.api.Test; /** - * Tests for {@link Paths#assertIsEmptyDirectory(AssertionInfo, Path)} - * * @author Valeriy Vyrva */ -class Paths_assertIsEmptyDirectory_Test extends MockPathsBaseTest { +class Paths_assertIsEmptyDirectory_Test extends PathsBaseTest { @Test - void should_pass_if_actual_is_empty() { - // GIVEN - List pathList = emptyList(); - Path actual = mockDirectory("root", pathList); + void should_fail_if_actual_is_null() { + // WHEN + AssertionError error = expectAssertionError(() -> paths.assertIsEmptyDirectory(info, null)); // THEN - paths.assertIsEmptyDirectory(INFO, actual); + then(error).hasMessage(actualIsNull()); } @Test - void should_fail_if_actual_is_not_empty() { + void should_fail_if_actual_does_not_exist() { // GIVEN - Path file = mockEmptyRegularFile("root", "Test.class"); - List files = list(file); - Path actual = mockDirectory("root", files); + Path actual = tempDir.resolve("non-existent"); // WHEN - expectAssertionError(() -> paths.assertIsEmptyDirectory(INFO, actual)); + AssertionError error = expectAssertionError(() -> paths.assertIsEmptyDirectory(info, actual)); // THEN - verify(failures).failure(INFO, shouldBeEmptyDirectory(actual, files)); + then(error).hasMessage(shouldExist(actual).create()); } @Test - void should_fail_if_actual_is_null() { + void should_fail_if_actual_is_not_a_directory() throws IOException { // GIVEN - Path actual = null; + Path actual = createFile(tempDir.resolve("file")); // WHEN - AssertionError error = expectAssertionError(() -> paths.assertIsEmptyDirectory(INFO, actual)); + AssertionError error = expectAssertionError(() -> paths.assertIsEmptyDirectory(info, actual)); // THEN - assertThat(error).hasMessage(actualIsNull()); + then(error).hasMessage(shouldBeDirectory(actual).create()); } @Test - void should_fail_if_actual_does_not_exist() { + void should_pass_if_actual_is_empty() throws IOException { // GIVEN - given(nioFilesWrapper.exists(actual)).willReturn(false); - // WHEN - expectAssertionError(() -> paths.assertIsEmptyDirectory(INFO, actual)); - // THEN - verify(failures).failure(INFO, shouldExist(actual)); + Path actual = createDirectory(tempDir.resolve("actual")); + // WHEN/THEN + paths.assertIsEmptyDirectory(info, actual); } @Test - void should_fail_if_actual_exists_but_is_not_directory() { + void should_fail_if_actual_is_not_empty() throws IOException { // GIVEN - given(nioFilesWrapper.exists(actual)).willReturn(true); - given(nioFilesWrapper.isDirectory(actual)).willReturn(false); + Path actual = createDirectory(tempDir.resolve("actual")); + Path file = createFile(actual.resolve("file")); // WHEN - expectAssertionError(() -> paths.assertIsEmptyDirectory(INFO, actual)); + AssertionError error = expectAssertionError(() -> paths.assertIsEmptyDirectory(info, actual)); // THEN - verify(failures).failure(INFO, shouldBeDirectory(actual)); + then(error).hasMessage(shouldBeEmptyDirectory(actual, singletonList(file)).create()); } @Test - void should_throw_runtime_error_wrapping_caught_IOException() throws IOException { + void should_rethrow_IOException_as_UncheckedIOException() throws IOException { // GIVEN - IOException cause = new IOException(); - given(nioFilesWrapper.exists(actual)).willReturn(true); - given(nioFilesWrapper.isDirectory(actual)).willReturn(true); - given(nioFilesWrapper.newDirectoryStream(eq(actual), any())).willThrow(cause); + Path actual = createDirectory(tempDir.resolve("actual")); + IOException exception = new IOException("boom!"); + willThrow(exception).given(nioFilesWrapper).newDirectoryStream(eq(actual), any()); // WHEN - Throwable error = catchThrowable(() -> paths.assertIsEmptyDirectory(INFO, actual)); + Throwable thrown = catchThrowable(() -> paths.assertIsEmptyDirectory(info, actual)); // THEN - assertThat(error).isInstanceOf(UncheckedIOException.class) - .hasCause(cause); + then(thrown).isInstanceOf(UncheckedIOException.class) + .hasCause(exception); } + } diff --git a/src/test/java/org/assertj/core/internal/paths/Paths_assertIsEmptyFile_Test.java b/src/test/java/org/assertj/core/internal/paths/Paths_assertIsEmptyFile_Test.java index 9d329273ff..ed232ed58d 100644 --- a/src/test/java/org/assertj/core/internal/paths/Paths_assertIsEmptyFile_Test.java +++ b/src/test/java/org/assertj/core/internal/paths/Paths_assertIsEmptyFile_Test.java @@ -12,6 +12,8 @@ */ package org.assertj.core.internal.paths; +import static java.nio.file.Files.createDirectory; +import static java.nio.file.Files.createFile; import static org.assertj.core.api.Assertions.catchThrowable; import static org.assertj.core.api.BDDAssertions.then; import static org.assertj.core.error.ShouldBeEmpty.shouldBeEmpty; @@ -22,83 +24,72 @@ import static org.mockito.BDDMockito.given; import java.io.IOException; +import java.io.UncheckedIOException; +import java.nio.file.Files; import java.nio.file.Path; -import org.assertj.core.api.AssertionInfo; -import org.assertj.core.api.exception.PathsException; -import org.assertj.core.internal.Paths; -import org.junit.jupiter.api.DisplayName; +import org.assertj.core.internal.PathsBaseTest; import org.junit.jupiter.api.Test; -/** - * Tests for {@link Paths#assertIsEmptyFile(AssertionInfo, Path)} - * - * @author Stefano Cordio - */ -@DisplayName("Paths assertIsEmptyFile") -class Paths_assertIsEmptyFile_Test extends MockPathsBaseTest { +class Paths_assertIsEmptyFile_Test extends PathsBaseTest { @Test - void should_pass_if_actual_is_empty() { - // GIVEN - Path actual = mockEmptyRegularFile("file"); - // WHEN/THEN - paths.assertIsEmptyFile(INFO, actual); + void should_fail_if_actual_is_null() { + // WHEN + AssertionError error = expectAssertionError(() -> paths.assertIsEmptyFile(info, null)); + // THEN + then(error).hasMessage(actualIsNull()); } @Test - void should_fail_if_actual_is_not_empty() { + void should_fail_if_actual_does_not_exist() { // GIVEN - Path actual = mockNonEmptyRegularFile("file"); + Path actual = tempDir.resolve("non-existent"); // WHEN - AssertionError error = expectAssertionError(() -> paths.assertIsEmptyFile(INFO, actual)); + AssertionError error = expectAssertionError(() -> paths.assertIsEmptyFile(info, actual)); // THEN - then(error).hasMessage(shouldBeEmpty(actual).create()); + then(error).hasMessage(shouldExist(actual).create()); } @Test - void should_fail_if_actual_is_null() { + void should_fail_if_actual_is_not_regular_file() throws IOException { // GIVEN - Path actual = null; + Path actual = createDirectory(tempDir.resolve("directory")); // WHEN - AssertionError error = expectAssertionError(() -> paths.assertIsEmptyFile(INFO, actual)); + AssertionError error = expectAssertionError(() -> paths.assertIsEmptyFile(info, actual)); // THEN - then(error).hasMessage(actualIsNull()); + then(error).hasMessage(shouldBeRegularFile(actual).create()); } @Test - void should_fail_if_actual_does_not_exist() { + void should_pass_if_actual_is_empty() throws IOException { // GIVEN - given(nioFilesWrapper.exists(actual)).willReturn(false); - // WHEN - AssertionError error = expectAssertionError(() -> paths.assertIsEmptyFile(INFO, actual)); - // THEN - then(error).hasMessage(shouldExist(actual).create()); + Path actual = createFile(tempDir.resolve("actual")); + // WHEN/THEN + paths.assertIsEmptyFile(info, actual); } @Test - void should_fail_if_actual_exists_but_is_not_regular_file() { + void should_fail_if_actual_is_not_empty() throws IOException { // GIVEN - given(nioFilesWrapper.exists(actual)).willReturn(true); - given(nioFilesWrapper.isRegularFile(actual)).willReturn(false); + Path actual = Files.write(tempDir.resolve("actual"), "content".getBytes()); // WHEN - AssertionError error = expectAssertionError(() -> paths.assertIsEmptyFile(INFO, actual)); + AssertionError error = expectAssertionError(() -> paths.assertIsEmptyFile(info, actual)); // THEN - then(error).hasMessage(shouldBeRegularFile(actual).create()); + then(error).hasMessage(shouldBeEmpty(actual).create()); } @Test - void should_throw_runtime_error_wrapping_caught_IOException() throws IOException { + void should_rethrow_IOException_as_UncheckedIOException() throws IOException { // GIVEN - IOException cause = new IOException(); - given(nioFilesWrapper.exists(actual)).willReturn(true); - given(nioFilesWrapper.isRegularFile(actual)).willReturn(true); - given(nioFilesWrapper.size(actual)).willThrow(cause); + Path actual = createFile(tempDir.resolve("actual")); + IOException exception = new IOException("boom!"); + given(nioFilesWrapper.size(actual)).willThrow(exception); // WHEN - Throwable error = catchThrowable(() -> paths.assertIsEmptyFile(INFO, actual)); + Throwable thrown = catchThrowable(() -> paths.assertIsEmptyFile(info, actual)); // THEN - then(error).isInstanceOf(PathsException.class) - .hasCause(cause); + then(thrown).isInstanceOf(UncheckedIOException.class) + .hasCause(exception); } } diff --git a/src/test/java/org/assertj/core/internal/paths/Paths_assertIsExecutable_Test.java b/src/test/java/org/assertj/core/internal/paths/Paths_assertIsExecutable_Test.java index e07170e1c3..f02b2b2b96 100644 --- a/src/test/java/org/assertj/core/internal/paths/Paths_assertIsExecutable_Test.java +++ b/src/test/java/org/assertj/core/internal/paths/Paths_assertIsExecutable_Test.java @@ -12,50 +12,58 @@ */ package org.assertj.core.internal.paths; -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatExceptionOfType; -import static org.assertj.core.api.ThrowableAssert.catchThrowable; +import static java.nio.file.Files.createFile; +import static org.assertj.core.api.BDDAssertions.then; import static org.assertj.core.error.ShouldBeExecutable.shouldBeExecutable; import static org.assertj.core.error.ShouldExist.shouldExist; +import static org.assertj.core.util.AssertionsUtil.expectAssertionError; import static org.assertj.core.util.FailureMessages.actualIsNull; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; +import static org.junit.jupiter.api.condition.OS.WINDOWS; +import java.io.IOException; +import java.nio.file.Path; + +import org.assertj.core.internal.PathsBaseTest; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.condition.DisabledOnOs; -class Paths_assertIsExecutable_Test extends MockPathsBaseTest { +class Paths_assertIsExecutable_Test extends PathsBaseTest { @Test void should_fail_if_actual_is_null() { - assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> paths.assertIsExecutable(info, null)) - .withMessage(actualIsNull()); + // WHEN + AssertionError error = expectAssertionError(() -> paths.assertIsExecutable(info, null)); + // THEN + then(error).hasMessage(actualIsNull()); } @Test - void should_fail_with_should_exist_error_if_actual_does_not_exist() { - when(nioFilesWrapper.exists(actual)).thenReturn(false); - - Throwable error = catchThrowable(() -> paths.assertIsExecutable(info, actual)); - - assertThat(error).isInstanceOf(AssertionError.class); - verify(failures).failure(info, shouldExist(actual)); + void should_fail_if_actual_does_not_exist() { + // GIVEN + Path actual = tempDir.resolve("non-existent"); + // WHEN + AssertionError error = expectAssertionError(() -> paths.assertIsExecutable(info, actual)); + // THEN + then(error).hasMessage(shouldExist(actual).create()); } @Test - void should_fail_if_actual_exists_but_is_not_executable() { - when(nioFilesWrapper.exists(actual)).thenReturn(true); - when(nioFilesWrapper.isExecutable(actual)).thenReturn(false); - - Throwable error = catchThrowable(() -> paths.assertIsExecutable(info, actual)); - - assertThat(error).isInstanceOf(AssertionError.class); - verify(failures).failure(info, shouldBeExecutable(actual)); + @DisabledOnOs(value = WINDOWS, disabledReason = "gh-2312") + void should_fail_if_actual_is_not_executable() throws IOException { + // GIVEN + Path actual = createFile(tempDir.resolve("actual")); + // WHEN + AssertionError error = expectAssertionError(() -> paths.assertIsExecutable(info, actual)); + // THEN + then(error).hasMessage(shouldBeExecutable(actual).create()); } @Test - void should_succeed_if_actual_exist_and_is_executable() { - when(nioFilesWrapper.exists(actual)).thenReturn(true); - when(nioFilesWrapper.isExecutable(actual)).thenReturn(true); + void should_pass_if_actual_is_executable() throws IOException { + // GIVEN + Path actual = createFile(tempDir.resolve("actual")); + actual.toFile().setExecutable(true); + // WHEN/THEN paths.assertIsExecutable(info, actual); } diff --git a/src/test/java/org/assertj/core/internal/paths/Paths_assertIsNormalized_Test.java b/src/test/java/org/assertj/core/internal/paths/Paths_assertIsNormalized_Test.java index fab20e5c46..6843b51deb 100644 --- a/src/test/java/org/assertj/core/internal/paths/Paths_assertIsNormalized_Test.java +++ b/src/test/java/org/assertj/core/internal/paths/Paths_assertIsNormalized_Test.java @@ -12,41 +12,81 @@ */ package org.assertj.core.internal.paths; -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatExceptionOfType; -import static org.assertj.core.api.ThrowableAssert.catchThrowable; +import static org.assertj.core.api.BDDAssertions.then; import static org.assertj.core.error.ShouldBeNormalized.shouldBeNormalized; +import static org.assertj.core.util.AssertionsUtil.expectAssertionError; import static org.assertj.core.util.FailureMessages.actualIsNull; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; +import static org.junit.jupiter.api.condition.OS.WINDOWS; import java.nio.file.Path; +import org.assertj.core.internal.PathsBaseTest; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.condition.DisabledOnOs; +import org.junit.jupiter.api.condition.EnabledOnOs; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; -class Paths_assertIsNormalized_Test extends MockPathsBaseTest { +class Paths_assertIsNormalized_Test extends PathsBaseTest { @Test void should_fail_if_actual_is_null() { - assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> paths.assertIsNormalized(info, null)) - .withMessage(actualIsNull()); + // WHEN + AssertionError error = expectAssertionError(() -> paths.assertIsNormalized(info, null)); + // THEN + then(error).hasMessage(actualIsNull()); } - @Test - void should_fail_if_actual_is_not_normalized() { - when(actual.normalize()).thenReturn(mock(Path.class)); - - Throwable error = catchThrowable(() -> paths.assertIsNormalized(info, actual)); + @ParameterizedTest + @DisabledOnOs(WINDOWS) + @CsvSource({ + "/a/./b", + "c/d/..", + "/../../e", + }) + void should_fail_on_unix_if_actual_is_not_normalized(Path actual) { + // WHEN + AssertionError error = expectAssertionError(() -> paths.assertIsNormalized(info, actual)); + // THEN + then(error).hasMessage(shouldBeNormalized(actual).create()); + } - assertThat(error).isInstanceOf(AssertionError.class); - verify(failures).failure(info, shouldBeNormalized(actual)); + @ParameterizedTest + @EnabledOnOs(WINDOWS) + @CsvSource({ + "C:\\a\\.\\b", + "c\\d\\..", + "C:\\..\\..\\e", + }) + void should_fail_on_windows_if_actual_is_not_normalized(Path actual) { + // WHEN + AssertionError error = expectAssertionError(() -> paths.assertIsNormalized(info, actual)); + // THEN + then(error).hasMessage(shouldBeNormalized(actual).create()); } - @Test - void should_pass_if_actual_is_normalized() { - when(actual.normalize()).thenReturn(actual); + @ParameterizedTest + @DisabledOnOs(WINDOWS) + @CsvSource({ + "/usr/lib", + "a/b/c", + "../d", + }) + void should_pass_on_unix_if_actual_is_normalized(Path actual) { + // WHEN/THEN + paths.assertIsNormalized(info, actual); + } + @ParameterizedTest + @EnabledOnOs(WINDOWS) + @CsvSource({ + "C:\\usr\\lib", + "a\\b\\c", + "..\\d", + }) + void should_pass_on_windows_if_actual_is_normalized(Path actual) { + // WHEN/THEN paths.assertIsNormalized(info, actual); } + } diff --git a/src/test/java/org/assertj/core/internal/paths/Paths_assertIsNotEmptyDirectory_Test.java b/src/test/java/org/assertj/core/internal/paths/Paths_assertIsNotEmptyDirectory_Test.java index 2742f96104..77f4efdc73 100644 --- a/src/test/java/org/assertj/core/internal/paths/Paths_assertIsNotEmptyDirectory_Test.java +++ b/src/test/java/org/assertj/core/internal/paths/Paths_assertIsNotEmptyDirectory_Test.java @@ -12,98 +12,89 @@ */ package org.assertj.core.internal.paths; -import static org.assertj.core.api.Assertions.assertThat; +import static java.nio.file.Files.createDirectory; +import static java.nio.file.Files.createFile; import static org.assertj.core.api.Assertions.catchThrowable; +import static org.assertj.core.api.BDDAssertions.then; import static org.assertj.core.error.ShouldBeDirectory.shouldBeDirectory; import static org.assertj.core.error.ShouldExist.shouldExist; import static org.assertj.core.error.ShouldNotBeEmpty.shouldNotBeEmpty; import static org.assertj.core.util.AssertionsUtil.expectAssertionError; import static org.assertj.core.util.FailureMessages.actualIsNull; -import static org.assertj.core.util.Lists.emptyList; -import static org.assertj.core.util.Lists.list; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.eq; -import static org.mockito.BDDMockito.given; -import static org.mockito.Mockito.verify; +import static org.mockito.BDDMockito.willThrow; import java.io.IOException; import java.io.UncheckedIOException; import java.nio.file.Path; -import java.util.List; -import org.assertj.core.api.AssertionInfo; -import org.assertj.core.internal.Paths; +import org.assertj.core.internal.PathsBaseTest; import org.junit.jupiter.api.Test; /** - * Tests for {@link Paths#assertIsNotEmptyDirectory(AssertionInfo, Path)} - * * @author Valeriy Vyrva */ -class Paths_assertIsNotEmptyDirectory_Test extends MockPathsBaseTest { +class Paths_assertIsNotEmptyDirectory_Test extends PathsBaseTest { @Test - void should_pass_if_actual_is_not_empty() { - // GIVEN - List files = list(mockEmptyRegularFile("root", "Test.class")); - Path actual = mockDirectory("root", files); + void should_fail_if_actual_is_null() { + // WHEN + AssertionError error = expectAssertionError(() -> paths.assertIsNotEmptyDirectory(info, null)); // THEN - paths.assertIsNotEmptyDirectory(INFO, actual); + then(error).hasMessage(actualIsNull()); } @Test - void should_fail_if_actual_is_empty() { + void should_fail_if_actual_does_not_exist() { // GIVEN - Path actual = mockDirectory("root", emptyList()); + Path actual = tempDir.resolve("non-existent"); // WHEN - expectAssertionError(() -> paths.assertIsNotEmptyDirectory(INFO, actual)); + AssertionError error = expectAssertionError(() -> paths.assertIsNotEmptyDirectory(info, actual)); // THEN - verify(failures).failure(INFO, shouldNotBeEmpty()); + then(error).hasMessage(shouldExist(actual).create()); } @Test - void should_fail_if_actual_is_null() { + void should_fail_if_actual_is_not_a_directory() throws IOException { // GIVEN - Path actual = null; + Path actual = createFile(tempDir.resolve("file")); // WHEN - AssertionError error = expectAssertionError(() -> paths.assertIsNotEmptyDirectory(INFO, actual)); + AssertionError error = expectAssertionError(() -> paths.assertIsNotEmptyDirectory(info, actual)); // THEN - assertThat(error).hasMessage(actualIsNull()); + then(error).hasMessage(shouldBeDirectory(actual).create()); } @Test - void should_fail_if_actual_does_not_exist() { + void should_pass_if_actual_is_not_empty() throws IOException { // GIVEN - given(nioFilesWrapper.exists(actual)).willReturn(false); - // WHEN - expectAssertionError(() -> paths.assertIsNotEmptyDirectory(INFO, actual)); - // THEN - verify(failures).failure(INFO, shouldExist(actual)); + Path actual = createDirectory(tempDir.resolve("actual")); + createFile(actual.resolve("file")); + // WHEN/THEN + paths.assertIsNotEmptyDirectory(info, actual); } @Test - void should_fail_if_actual_exists_but_is_not_directory() { + void should_fail_if_actual_is_empty() throws IOException { // GIVEN - given(nioFilesWrapper.exists(actual)).willReturn(true); - given(nioFilesWrapper.isDirectory(actual)).willReturn(false); + Path actual = createDirectory(tempDir.resolve("actual")); // WHEN - expectAssertionError(() -> paths.assertIsNotEmptyDirectory(INFO, actual)); + AssertionError error = expectAssertionError(() -> paths.assertIsNotEmptyDirectory(info, actual)); // THEN - verify(failures).failure(INFO, shouldBeDirectory(actual)); + then(error).hasMessage(shouldNotBeEmpty(actual).create()); } @Test - void should_throw_runtime_error_wrapping_caught_IOException() throws IOException { + void should_rethrow_IOException_as_UncheckedIOException() throws IOException { // GIVEN - IOException cause = new IOException(); - given(nioFilesWrapper.exists(actual)).willReturn(true); - given(nioFilesWrapper.isDirectory(actual)).willReturn(true); - given(nioFilesWrapper.newDirectoryStream(eq(actual), any())).willThrow(cause); + Path actual = createDirectory(tempDir.resolve("actual")); + IOException exception = new IOException("boom!"); + willThrow(exception).given(nioFilesWrapper).newDirectoryStream(eq(actual), any()); // WHEN - Throwable error = catchThrowable(() -> paths.assertIsNotEmptyDirectory(INFO, actual)); + Throwable thrown = catchThrowable(() -> paths.assertIsNotEmptyDirectory(info, actual)); // THEN - assertThat(error).isInstanceOf(UncheckedIOException.class) - .hasCause(cause); + then(thrown).isInstanceOf(UncheckedIOException.class) + .hasCause(exception); } } diff --git a/src/test/java/org/assertj/core/internal/paths/Paths_assertIsNotEmptyFile_Test.java b/src/test/java/org/assertj/core/internal/paths/Paths_assertIsNotEmptyFile_Test.java index 1463d325b1..21d61da005 100644 --- a/src/test/java/org/assertj/core/internal/paths/Paths_assertIsNotEmptyFile_Test.java +++ b/src/test/java/org/assertj/core/internal/paths/Paths_assertIsNotEmptyFile_Test.java @@ -12,6 +12,8 @@ */ package org.assertj.core.internal.paths; +import static java.nio.file.Files.createDirectory; +import static java.nio.file.Files.createFile; import static org.assertj.core.api.Assertions.catchThrowable; import static org.assertj.core.api.BDDAssertions.then; import static org.assertj.core.error.ShouldBeRegularFile.shouldBeRegularFile; @@ -22,83 +24,72 @@ import static org.mockito.BDDMockito.given; import java.io.IOException; +import java.io.UncheckedIOException; +import java.nio.file.Files; import java.nio.file.Path; -import org.assertj.core.api.AssertionInfo; -import org.assertj.core.api.exception.PathsException; -import org.assertj.core.internal.Paths; -import org.junit.jupiter.api.DisplayName; +import org.assertj.core.internal.PathsBaseTest; import org.junit.jupiter.api.Test; -/** - * Tests for {@link Paths#assertIsNotEmptyFile(AssertionInfo, Path)} - * - * @author Stefano Cordio - */ -@DisplayName("Paths assertIsNotEmptyFile") -class Paths_assertIsNotEmptyFile_Test extends MockPathsBaseTest { +class Paths_assertIsNotEmptyFile_Test extends PathsBaseTest { @Test - void should_pass_if_actual_is_empty() { - // GIVEN - Path actual = mockNonEmptyRegularFile("file"); - // WHEN/THEN - paths.assertIsNotEmptyFile(INFO, actual); + void should_fail_if_actual_is_null() { + // WHEN + AssertionError error = expectAssertionError(() -> paths.assertIsNotEmptyFile(info, null)); + // THEN + then(error).hasMessage(actualIsNull()); } @Test - void should_fail_if_actual_is_not_empty() { + void should_fail_if_actual_does_not_exist() { // GIVEN - Path actual = mockEmptyRegularFile("file"); + Path actual = tempDir.resolve("non-existent"); // WHEN - AssertionError error = expectAssertionError(() -> paths.assertIsNotEmptyFile(INFO, actual)); + AssertionError error = expectAssertionError(() -> paths.assertIsNotEmptyFile(info, actual)); // THEN - then(error).hasMessage(shouldNotBeEmpty(actual).create()); + then(error).hasMessage(shouldExist(actual).create()); } @Test - void should_fail_if_actual_is_null() { + void should_fail_if_actual_is_not_regular_file() throws IOException { // GIVEN - Path actual = null; + Path actual = createDirectory(tempDir.resolve("directory")); // WHEN - AssertionError error = expectAssertionError(() -> paths.assertIsNotEmptyFile(INFO, actual)); + AssertionError error = expectAssertionError(() -> paths.assertIsNotEmptyFile(info, actual)); // THEN - then(error).hasMessage(actualIsNull()); + then(error).hasMessage(shouldBeRegularFile(actual).create()); } @Test - void should_fail_if_actual_does_not_exist() { + void should_pass_if_actual_is_not_empty() throws IOException { // GIVEN - given(nioFilesWrapper.exists(actual)).willReturn(false); - // WHEN - AssertionError error = expectAssertionError(() -> paths.assertIsNotEmptyFile(INFO, actual)); - // THEN - then(error).hasMessage(shouldExist(actual).create()); + Path actual = Files.write(tempDir.resolve("actual"), "content".getBytes()); + // WHEN/THEN + paths.assertIsNotEmptyFile(info, actual); } @Test - void should_fail_if_actual_exists_but_is_not_regular_file() { + void should_fail_if_actual_is_empty() throws IOException { // GIVEN - given(nioFilesWrapper.exists(actual)).willReturn(true); - given(nioFilesWrapper.isRegularFile(actual)).willReturn(false); + Path actual = createFile(tempDir.resolve("actual")); // WHEN - AssertionError error = expectAssertionError(() -> paths.assertIsNotEmptyFile(INFO, actual)); + AssertionError error = expectAssertionError(() -> paths.assertIsNotEmptyFile(info, actual)); // THEN - then(error).hasMessage(shouldBeRegularFile(actual).create()); + then(error).hasMessage(shouldNotBeEmpty(actual).create()); } @Test - void should_throw_runtime_error_wrapping_caught_IOException() throws IOException { + void should_rethrow_IOException_as_UncheckedIOException() throws IOException { // GIVEN - IOException cause = new IOException(); - given(nioFilesWrapper.exists(actual)).willReturn(true); - given(nioFilesWrapper.isRegularFile(actual)).willReturn(true); - given(nioFilesWrapper.size(actual)).willThrow(cause); + Path actual = createFile(tempDir.resolve("actual")); + IOException exception = new IOException("boom!"); + given(nioFilesWrapper.size(actual)).willThrow(exception); // WHEN - Throwable error = catchThrowable(() -> paths.assertIsNotEmptyFile(INFO, actual)); + Throwable thrown = catchThrowable(() -> paths.assertIsNotEmptyFile(info, actual)); // THEN - then(error).isInstanceOf(PathsException.class) - .hasCause(cause); + then(thrown).isInstanceOf(UncheckedIOException.class) + .hasCause(exception); } } diff --git a/src/test/java/org/assertj/core/internal/paths/Paths_assertIsReadable_Test.java b/src/test/java/org/assertj/core/internal/paths/Paths_assertIsReadable_Test.java index 3faafb2845..e96b6e47c7 100644 --- a/src/test/java/org/assertj/core/internal/paths/Paths_assertIsReadable_Test.java +++ b/src/test/java/org/assertj/core/internal/paths/Paths_assertIsReadable_Test.java @@ -12,50 +12,58 @@ */ package org.assertj.core.internal.paths; -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatExceptionOfType; -import static org.assertj.core.api.ThrowableAssert.catchThrowable; +import static java.nio.file.Files.createFile; +import static org.assertj.core.api.BDDAssertions.then; import static org.assertj.core.error.ShouldBeReadable.shouldBeReadable; import static org.assertj.core.error.ShouldExist.shouldExist; +import static org.assertj.core.util.AssertionsUtil.expectAssertionError; import static org.assertj.core.util.FailureMessages.actualIsNull; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; +import static org.junit.jupiter.api.condition.OS.WINDOWS; +import java.io.IOException; +import java.nio.file.Path; + +import org.assertj.core.internal.PathsBaseTest; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.condition.DisabledOnOs; -class Paths_assertIsReadable_Test extends MockPathsBaseTest { +class Paths_assertIsReadable_Test extends PathsBaseTest { @Test void should_fail_if_actual_is_null() { - assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> paths.assertIsReadable(info, null)) - .withMessage(actualIsNull()); + // WHEN + AssertionError error = expectAssertionError(() -> paths.assertIsReadable(info, null)); + // THEN + then(error).hasMessage(actualIsNull()); } @Test - void should_fail_with_should_exist_error_if_actual_does_not_exist() { - when(nioFilesWrapper.exists(actual)).thenReturn(false); - - Throwable error = catchThrowable(() -> paths.assertIsReadable(info, actual)); - - assertThat(error).isInstanceOf(AssertionError.class); - verify(failures).failure(info, shouldExist(actual)); + void should_fail_if_actual_does_not_exist() { + // GIVEN + Path actual = tempDir.resolve("non-existent"); + // WHEN + AssertionError error = expectAssertionError(() -> paths.assertIsReadable(info, actual)); + // THEN + then(error).hasMessage(shouldExist(actual).create()); } @Test - void should_fail_if_actual_exists_but_is_not_readable() { - when(nioFilesWrapper.exists(actual)).thenReturn(true); - when(nioFilesWrapper.isReadable(actual)).thenReturn(false); - - Throwable error = catchThrowable(() -> paths.assertIsReadable(info, actual)); - - assertThat(error).isInstanceOf(AssertionError.class); - verify(failures).failure(info, shouldBeReadable(actual)); + @DisabledOnOs(value = WINDOWS, disabledReason = "gh-2312") + void should_fail_if_actual_is_not_readable() throws IOException { + // GIVEN + Path actual = createFile(tempDir.resolve("actual")); + actual.toFile().setReadable(false); + // WHEN + AssertionError error = expectAssertionError(() -> paths.assertIsReadable(info, actual)); + // THEN + then(error).hasMessage(shouldBeReadable(actual).create()); } @Test - void should_succeed_if_actual_exist_and_is_readable() { - when(nioFilesWrapper.exists(actual)).thenReturn(true); - when(nioFilesWrapper.isReadable(actual)).thenReturn(true); + void should_pass_if_actual_is_readable() throws IOException { + // GIVEN + Path actual = createFile(tempDir.resolve("actual")); + // WHEN/THEN paths.assertIsReadable(info, actual); } diff --git a/src/test/java/org/assertj/core/internal/paths/Paths_assertIsRegularFile_Test.java b/src/test/java/org/assertj/core/internal/paths/Paths_assertIsRegularFile_Test.java index 080fc5c26d..98caffab65 100644 --- a/src/test/java/org/assertj/core/internal/paths/Paths_assertIsRegularFile_Test.java +++ b/src/test/java/org/assertj/core/internal/paths/Paths_assertIsRegularFile_Test.java @@ -12,50 +12,56 @@ */ package org.assertj.core.internal.paths; -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatExceptionOfType; -import static org.assertj.core.api.ThrowableAssert.catchThrowable; +import static java.nio.file.Files.createDirectory; +import static org.assertj.core.api.BDDAssertions.then; import static org.assertj.core.error.ShouldBeRegularFile.shouldBeRegularFile; import static org.assertj.core.error.ShouldExist.shouldExist; +import static org.assertj.core.util.AssertionsUtil.expectAssertionError; import static org.assertj.core.util.FailureMessages.actualIsNull; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; + +import org.assertj.core.internal.PathsBaseTest; import org.junit.jupiter.api.Test; -class Paths_assertIsRegularFile_Test extends MockPathsBaseTest { +class Paths_assertIsRegularFile_Test extends PathsBaseTest { @Test void should_fail_if_actual_is_null() { - assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> paths.assertIsRegularFile(info, null)) - .withMessage(actualIsNull()); + // WHEN + AssertionError error = expectAssertionError(() -> paths.assertIsRegularFile(info, null)); + // THEN + then(error).hasMessage(actualIsNull()); } @Test - void should_fail_with_should_exist_error_if_actual_does_not_exist() { - when(nioFilesWrapper.exists(actual)).thenReturn(false); - - Throwable error = catchThrowable(() -> paths.assertIsRegularFile(info, actual)); - - assertThat(error).isInstanceOf(AssertionError.class); - verify(failures).failure(info, shouldExist(actual)); + void should_fail_if_actual_does_not_exist() { + // GIVEN + Path actual = tempDir.resolve("non-existent"); + // WHEN + AssertionError error = expectAssertionError(() -> paths.assertIsRegularFile(info, actual)); + // THEN + then(error).hasMessage(shouldExist(actual).create()); } @Test - void should_fail_if_target_exists_but_is_not_a_regular_file() { - when(nioFilesWrapper.exists(actual)).thenReturn(true); - when(nioFilesWrapper.isRegularFile(actual)).thenReturn(false); - - Throwable error = catchThrowable(() -> paths.assertIsRegularFile(info, actual)); - - assertThat(error).isInstanceOf(AssertionError.class); - verify(failures).failure(info, shouldBeRegularFile(actual)); + void should_fail_if_actual_is_not_a_regular_file() throws IOException { + // GIVEN + Path actual = createDirectory(tempDir.resolve("directory")); + // WHEN + AssertionError error = expectAssertionError(() -> paths.assertIsRegularFile(info, actual)); + // THEN + then(error).hasMessage(shouldBeRegularFile(actual).create()); } @Test - void should_succeed_if_actual_is_an_existing_regular_file() { - when(nioFilesWrapper.exists(actual)).thenReturn(true); - when(nioFilesWrapper.isRegularFile(actual)).thenReturn(true); + void should_pass_if_actual_is_a_regular_file() throws IOException { + // GIVEN + Path actual = Files.write(tempDir.resolve("actual"), "content".getBytes()); + // WHEN/THEN paths.assertIsRegularFile(info, actual); } + } diff --git a/src/test/java/org/assertj/core/internal/paths/Paths_assertIsRelative_Test.java b/src/test/java/org/assertj/core/internal/paths/Paths_assertIsRelative_Test.java index eb91d221db..8632bac42d 100644 --- a/src/test/java/org/assertj/core/internal/paths/Paths_assertIsRelative_Test.java +++ b/src/test/java/org/assertj/core/internal/paths/Paths_assertIsRelative_Test.java @@ -12,38 +12,43 @@ */ package org.assertj.core.internal.paths; -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatExceptionOfType; -import static org.assertj.core.api.ThrowableAssert.catchThrowable; +import static org.assertj.core.api.BDDAssertions.then; import static org.assertj.core.error.ShouldBeRelativePath.shouldBeRelativePath; +import static org.assertj.core.util.AssertionsUtil.expectAssertionError; import static org.assertj.core.util.FailureMessages.actualIsNull; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; +import java.nio.file.Path; +import java.nio.file.Paths; + +import org.assertj.core.internal.PathsBaseTest; import org.junit.jupiter.api.Test; -class Paths_assertIsRelative_Test extends MockPathsBaseTest { +class Paths_assertIsRelative_Test extends PathsBaseTest { @Test void should_fail_if_actual_is_null() { - assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> paths.assertIsRelative(info, null)) - .withMessage(actualIsNull()); + // WHEN + AssertionError error = expectAssertionError(() -> paths.assertIsRelative(info, null)); + // THEN + then(error).hasMessage(actualIsNull()); } @Test void should_fail_if_actual_is_not_relative() { - // This is the default, but make it explicit - when(actual.isAbsolute()).thenReturn(true); - - Throwable error = catchThrowable(() -> paths.assertIsRelative(info, actual)); - - assertThat(error).isInstanceOf(AssertionError.class); - verify(failures).failure(info, shouldBeRelativePath(actual)); + // GIVEN + Path actual = tempDir.getRoot().resolve("absolute"); + // WHEN + AssertionError error = expectAssertionError(() -> paths.assertIsRelative(info, actual)); + // THEN + then(error).hasMessage(shouldBeRelativePath(actual).create()); } @Test void should_pass_if_actual_is_relative() { - when(actual.isAbsolute()).thenReturn(false); + // GIVEN + Path actual = Paths.get("relative"); + // WHEN/THEN paths.assertIsRelative(info, actual); } + } diff --git a/src/test/java/org/assertj/core/internal/paths/Paths_assertIsSymbolicLink_Test.java b/src/test/java/org/assertj/core/internal/paths/Paths_assertIsSymbolicLink_Test.java index 1e5b635170..e6b810e336 100644 --- a/src/test/java/org/assertj/core/internal/paths/Paths_assertIsSymbolicLink_Test.java +++ b/src/test/java/org/assertj/core/internal/paths/Paths_assertIsSymbolicLink_Test.java @@ -12,52 +12,56 @@ */ package org.assertj.core.internal.paths; -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatExceptionOfType; -import static org.assertj.core.api.ThrowableAssert.catchThrowable; +import static java.nio.file.Files.createFile; +import static java.nio.file.Files.createSymbolicLink; +import static org.assertj.core.api.BDDAssertions.then; import static org.assertj.core.error.ShouldBeSymbolicLink.shouldBeSymbolicLink; import static org.assertj.core.error.ShouldExist.shouldExistNoFollowLinks; +import static org.assertj.core.util.AssertionsUtil.expectAssertionError; import static org.assertj.core.util.FailureMessages.actualIsNull; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; -import java.nio.file.LinkOption; +import java.io.IOException; +import java.nio.file.Path; +import org.assertj.core.internal.PathsBaseTest; import org.junit.jupiter.api.Test; -class Paths_assertIsSymbolicLink_Test extends MockPathsBaseTest { +class Paths_assertIsSymbolicLink_Test extends PathsBaseTest { @Test void should_fail_if_actual_is_null() { - assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> paths.assertIsSymbolicLink(info, null)) - .withMessage(actualIsNull()); + // WHEN + AssertionError error = expectAssertionError(() -> paths.assertIsSymbolicLink(info, null)); + // THEN + then(error).hasMessage(actualIsNull()); } @Test - void should_fail_with_should_exist_error_if_actual_does_not_exist() { - when(nioFilesWrapper.exists(actual, LinkOption.NOFOLLOW_LINKS)).thenReturn(false); - - Throwable error = catchThrowable(() -> paths.assertIsSymbolicLink(info, actual)); - - assertThat(error).isInstanceOf(AssertionError.class); - verify(failures).failure(info, shouldExistNoFollowLinks(actual)); + void should_fail_if_actual_does_not_exist() { + // GIVEN + Path actual = tempDir.resolve("non-existent"); + // WHEN + AssertionError error = expectAssertionError(() -> paths.assertIsSymbolicLink(info, actual)); + // THEN + then(error).hasMessage(shouldExistNoFollowLinks(actual).create()); } @Test - void should_fail_if_actual_exists_but_is_not_a_symbolic_link() { - when(nioFilesWrapper.exists(actual, LinkOption.NOFOLLOW_LINKS)).thenReturn(true); - when(nioFilesWrapper.isSymbolicLink(actual)).thenReturn(false); - - Throwable error = catchThrowable(() -> paths.assertIsSymbolicLink(info, actual)); - - assertThat(error).isInstanceOf(AssertionError.class); - verify(failures).failure(info, shouldBeSymbolicLink(actual)); + void should_fail_if_actual_is_not_a_symbolic_link() throws IOException { + // GIVEN + Path actual = createFile(tempDir.resolve("actual")); + // WHEN + AssertionError error = expectAssertionError(() -> paths.assertIsSymbolicLink(info, actual)); + // THEN + then(error).hasMessage(shouldBeSymbolicLink(actual).create()); } @Test - void should_succeed_if_actual_is_a_symbolic_link() { - when(nioFilesWrapper.exists(actual, LinkOption.NOFOLLOW_LINKS)).thenReturn(true); - when(nioFilesWrapper.isSymbolicLink(actual)).thenReturn(true); + void should_succeed_if_actual_is_a_symbolic_link() throws IOException { + // GIVEN + Path actual = createSymbolicLink(tempDir.resolve("actual"), tempDir.resolve("target")); + // WHEN/THEN paths.assertIsSymbolicLink(info, actual); } + } diff --git a/src/test/java/org/assertj/core/internal/paths/Paths_assertIsWritable_Test.java b/src/test/java/org/assertj/core/internal/paths/Paths_assertIsWritable_Test.java index c7bb1221d4..5fc64c4ce0 100644 --- a/src/test/java/org/assertj/core/internal/paths/Paths_assertIsWritable_Test.java +++ b/src/test/java/org/assertj/core/internal/paths/Paths_assertIsWritable_Test.java @@ -12,50 +12,55 @@ */ package org.assertj.core.internal.paths; -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatExceptionOfType; -import static org.assertj.core.api.ThrowableAssert.catchThrowable; +import static java.nio.file.Files.createFile; +import static org.assertj.core.api.BDDAssertions.then; import static org.assertj.core.error.ShouldBeWritable.shouldBeWritable; import static org.assertj.core.error.ShouldExist.shouldExist; +import static org.assertj.core.util.AssertionsUtil.expectAssertionError; import static org.assertj.core.util.FailureMessages.actualIsNull; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; +import java.io.IOException; +import java.nio.file.Path; + +import org.assertj.core.internal.PathsBaseTest; import org.junit.jupiter.api.Test; -class Paths_assertIsWritable_Test extends MockPathsBaseTest { +class Paths_assertIsWritable_Test extends PathsBaseTest { @Test void should_fail_if_actual_is_null() { - assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> paths.assertIsWritable(info, null)) - .withMessage(actualIsNull()); + // WHEN + AssertionError error = expectAssertionError(() -> paths.assertIsWritable(info, null)); + // THEN + then(error).hasMessage(actualIsNull()); } @Test - void should_fail_with_should_exist_error_if_actual_does_not_exist() { - when(nioFilesWrapper.exists(actual)).thenReturn(false); - - Throwable error = catchThrowable(() -> paths.assertIsWritable(info, actual)); - - assertThat(error).isInstanceOf(AssertionError.class); - verify(failures).failure(info, shouldExist(actual)); + void should_fail_if_actual_does_not_exist() { + // GIVEN + Path actual = tempDir.resolve("non-existent"); + // WHEN + AssertionError error = expectAssertionError(() -> paths.assertIsWritable(info, actual)); + // THEN + then(error).hasMessage(shouldExist(actual).create()); } @Test - void should_fail_if_actual_exists_but_is_not_writable() { - when(nioFilesWrapper.exists(actual)).thenReturn(true); - when(nioFilesWrapper.isWritable(actual)).thenReturn(false); - - Throwable error = catchThrowable(() -> paths.assertIsWritable(info, actual)); - - assertThat(error).isInstanceOf(AssertionError.class); - verify(failures).failure(info, shouldBeWritable(actual)); + void should_fail_if_actual_is_not_writable() throws IOException { + // GIVEN + Path actual = createFile(tempDir.resolve("actual")); + actual.toFile().setWritable(false); + // WHEN + AssertionError error = expectAssertionError(() -> paths.assertIsWritable(info, actual)); + // THEN + then(error).hasMessage(shouldBeWritable(actual).create()); } @Test - void should_succeed_if_actual_exist_and_is_writable() { - when(nioFilesWrapper.exists(actual)).thenReturn(true); - when(nioFilesWrapper.isWritable(actual)).thenReturn(true); + void should_pass_if_actual_is_writable() throws IOException { + // GIVEN + Path actual = createFile(tempDir.resolve("actual")); + // WHEN/THEN paths.assertIsWritable(info, actual); } diff --git a/src/test/java/org/assertj/core/internal/paths/Paths_assertNotExists_Test.java b/src/test/java/org/assertj/core/internal/paths/Paths_assertNotExists_Test.java index 2255b74b61..2d523563b4 100644 --- a/src/test/java/org/assertj/core/internal/paths/Paths_assertNotExists_Test.java +++ b/src/test/java/org/assertj/core/internal/paths/Paths_assertNotExists_Test.java @@ -12,40 +12,56 @@ */ package org.assertj.core.internal.paths; -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatExceptionOfType; -import static org.assertj.core.api.ThrowableAssert.catchThrowable; +import static java.nio.file.Files.createFile; +import static java.nio.file.Files.createSymbolicLink; +import static org.assertj.core.api.BDDAssertions.then; import static org.assertj.core.error.ShouldNotExist.shouldNotExist; +import static org.assertj.core.util.AssertionsUtil.expectAssertionError; import static org.assertj.core.util.FailureMessages.actualIsNull; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; -import java.nio.file.LinkOption; +import java.io.IOException; +import java.nio.file.Path; +import org.assertj.core.internal.PathsBaseTest; import org.junit.jupiter.api.Test; -class Paths_assertNotExists_Test extends MockPathsBaseTest { +class Paths_assertNotExists_Test extends PathsBaseTest { @Test void should_fail_if_actual_is_null() { - assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> paths.assertDoesNotExist(info, null)) - .withMessage(actualIsNull()); + // WHEN + AssertionError error = expectAssertionError(() -> paths.assertDoesNotExist(info, null)); + // THEN + then(error).hasMessage(actualIsNull()); } @Test - void should_fail_if_actual_exists() { - when(nioFilesWrapper.notExists(actual)).thenReturn(false); - - Throwable error = catchThrowable(() -> paths.assertDoesNotExist(info, actual)); - - assertThat(error).isInstanceOf(AssertionError.class); - verify(failures).failure(info, shouldNotExist(actual)); + void should_fail_if_actual_exists() throws IOException { + // GIVEN + Path actual = createFile(tempDir.resolve("actual")); + // WHEN + AssertionError error = expectAssertionError(() -> paths.assertDoesNotExist(info, actual)); + // THEN + then(error).hasMessage(shouldNotExist(actual).create()); } @Test - void should_pass_if_actual_does_not_exists() { - when(nioFilesWrapper.notExists(actual, LinkOption.NOFOLLOW_LINKS)).thenReturn(true); + void should_pass_if_actual_does_not_exist() throws IOException { + // GIVEN + Path actual = tempDir.resolve("non-existent"); + // WHEN/THEN paths.assertDoesNotExist(info, actual); } + @Test + void should_fail_if_actual_is_a_symbolic_link_and_target_does_not_exist() throws IOException { + // GIVEN + Path target = tempDir.resolve("non-existent"); + Path actual = createSymbolicLink(tempDir.resolve("actual"), target); + // WHEN + AssertionError error = expectAssertionError(() -> paths.assertDoesNotExist(info, actual)); + // THEN + then(error).hasMessage(shouldNotExist(actual).create()); + } + } diff --git a/src/test/java/org/assertj/core/internal/paths/Paths_assertStartsWithRaw_Test.java b/src/test/java/org/assertj/core/internal/paths/Paths_assertStartsWithRaw_Test.java index b0ad798c08..15bc272bb7 100644 --- a/src/test/java/org/assertj/core/internal/paths/Paths_assertStartsWithRaw_Test.java +++ b/src/test/java/org/assertj/core/internal/paths/Paths_assertStartsWithRaw_Test.java @@ -12,46 +12,86 @@ */ package org.assertj.core.internal.paths; -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatExceptionOfType; -import static org.assertj.core.api.Assertions.assertThatNullPointerException; -import static org.assertj.core.api.ThrowableAssert.catchThrowable; +import static java.nio.file.Files.createDirectory; +import static java.nio.file.Files.createFile; +import static java.nio.file.Files.createSymbolicLink; +import static org.assertj.core.api.Assertions.catchThrowable; +import static org.assertj.core.api.BDDAssertions.then; import static org.assertj.core.error.ShouldStartWithPath.shouldStartWith; +import static org.assertj.core.util.AssertionsUtil.expectAssertionError; import static org.assertj.core.util.FailureMessages.actualIsNull; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; +import java.io.IOException; +import java.nio.file.Path; + +import org.assertj.core.internal.PathsBaseTest; import org.junit.jupiter.api.Test; -class Paths_assertStartsWithRaw_Test extends MockPathsBaseTest { +class Paths_assertStartsWithRaw_Test extends PathsBaseTest { @Test - void should_fail_if_actual_is_null() { - assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> paths.assertStartsWithRaw(info, null, other)) - .withMessage(actualIsNull()); + void should_fail_if_actual_is_null() throws IOException { + // GIVEN + Path other = createFile(tempDir.resolve("other")); + // WHEN + AssertionError error = expectAssertionError(() -> paths.assertStartsWithRaw(info, null, other)); + // THEN + then(error).hasMessage(actualIsNull()); } @Test - void should_fail_if_other_is_null() { - assertThatNullPointerException().isThrownBy(() -> paths.assertStartsWithRaw(info, actual, null)) - .withMessage("the expected start path should not be null"); + void should_fail_if_other_is_null() throws IOException { + // GIVEN + Path actual = createFile(tempDir.resolve("actual")); + // WHEN + Throwable thrown = catchThrowable(() -> paths.assertStartsWithRaw(info, actual, null)); + // THEN + then(thrown).isInstanceOf(NullPointerException.class) + .hasMessage("the expected start path should not be null"); } @Test - void should_fail_if_actual_does_not_start_with_other() { - // This is the default, but let's make this explicit - when(actual.startsWith(other)).thenReturn(false); - - Throwable error = catchThrowable(() -> paths.assertStartsWithRaw(info, actual, other)); + void should_fail_if_actual_does_not_start_with_other() throws IOException { + // GIVEN + Path actual = createFile(tempDir.resolve("actual")); + Path other = createFile(tempDir.resolve("other")); + // WHEN + AssertionError error = expectAssertionError(() -> paths.assertStartsWithRaw(info, actual, other)); + // THEN + then(error).hasMessage(shouldStartWith(actual, other).create()); + } - assertThat(error).isInstanceOf(AssertionError.class); - verify(failures).failure(info, shouldStartWith(actual, other)); + @Test + void should_pass_if_actual_starts_with_other() throws IOException { + // GIVEN + Path other = createDirectory(tempDir.resolve("other")).toRealPath(); + Path actual = createFile(other.resolve("actual")).toRealPath(); + // WHEN/THEN + paths.assertStartsWithRaw(info, actual, other); } @Test - void should_succeed_if_actual_starts_with_other() { - when(actual.startsWith(other)).thenReturn(true); + void should_fail_if_actual_is_not_canonical() throws IOException { + // GIVEN + Path other = createDirectory(tempDir.resolve("other")); + Path file = createFile(other.resolve("file")); + Path actual = createSymbolicLink(tempDir.resolve("actual"), file); + // WHEN + AssertionError error = expectAssertionError(() -> paths.assertStartsWithRaw(info, actual, other)); + // THEN + then(error).hasMessage(shouldStartWith(actual, other).create()); + } - paths.assertStartsWithRaw(info, actual, other); + @Test + void should_fail_if_other_is_not_canonical() throws IOException { + // GIVEN + Path directory = createDirectory(tempDir.resolve("directory")); + Path other = createSymbolicLink(tempDir.resolve("other"), directory); + Path actual = createFile(directory.resolve("actual")); + // WHEN + AssertionError error = expectAssertionError(() -> paths.assertStartsWithRaw(info, actual, other)); + // THEN + then(error).hasMessage(shouldStartWith(actual, other).create()); } + } diff --git a/src/test/java/org/assertj/core/internal/paths/Paths_assertStartsWith_Test.java b/src/test/java/org/assertj/core/internal/paths/Paths_assertStartsWith_Test.java index 09fc59ad0f..a4ea502fec 100644 --- a/src/test/java/org/assertj/core/internal/paths/Paths_assertStartsWith_Test.java +++ b/src/test/java/org/assertj/core/internal/paths/Paths_assertStartsWith_Test.java @@ -12,89 +12,112 @@ */ package org.assertj.core.internal.paths; -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatExceptionOfType; -import static org.assertj.core.api.Assertions.assertThatNullPointerException; -import static org.assertj.core.api.ThrowableAssert.catchThrowable; +import static java.nio.file.Files.createDirectory; +import static java.nio.file.Files.createFile; +import static java.nio.file.Files.createSymbolicLink; +import static org.assertj.core.api.Assertions.catchThrowable; +import static org.assertj.core.api.BDDAssertions.then; import static org.assertj.core.error.ShouldStartWithPath.shouldStartWith; +import static org.assertj.core.util.AssertionsUtil.expectAssertionError; import static org.assertj.core.util.FailureMessages.actualIsNull; +import static org.mockito.BDDMockito.given; import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; import java.io.IOException; +import java.io.UncheckedIOException; import java.nio.file.Path; -import org.assertj.core.api.exception.PathsException; -import org.junit.jupiter.api.BeforeEach; +import org.assertj.core.internal.PathsBaseTest; import org.junit.jupiter.api.Test; -class Paths_assertStartsWith_Test extends MockPathsBaseTest { - - private Path canonicalActual; - private Path canonicalOther; - - @Override - @BeforeEach - public void init() { - super.init(); - canonicalActual = mock(Path.class); - canonicalOther = mock(Path.class); - } +class Paths_assertStartsWith_Test extends PathsBaseTest { @Test - void should_fail_if_actual_is_null() { - assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> paths.assertStartsWith(info, null, other)) - .withMessage(actualIsNull()); + void should_fail_if_actual_is_null() throws IOException { + // GIVEN + Path other = createFile(tempDir.resolve("other")); + // WHEN + AssertionError error = expectAssertionError(() -> paths.assertStartsWith(info, null, other)); + // THEN + then(error).hasMessage(actualIsNull()); } @Test - void should_fail_if_other_is_null() { - assertThatNullPointerException().isThrownBy(() -> paths.assertStartsWith(info, actual, null)) - .withMessage("the expected start path should not be null"); + void should_fail_if_other_is_null() throws IOException { + // GIVEN + Path actual = createFile(tempDir.resolve("actual")); + // WHEN + Throwable thrown = catchThrowable(() -> paths.assertStartsWith(info, actual, null)); + // THEN + then(thrown).isInstanceOf(NullPointerException.class) + .hasMessage("the expected start path should not be null"); } @Test - void should_throw_PathsException_if_actual_cannot_be_resolved() throws IOException { - final IOException exception = new IOException(); - when(actual.toRealPath()).thenThrow(exception); - - assertThatExceptionOfType(PathsException.class).isThrownBy(() -> paths.assertStartsWith(info, actual, other)) - .withMessage("failed to resolve actual real path") - .withCause(exception); + void should_rethrow_IOException_as_UncheckedIOException_if_actual_cannot_be_resolved() throws IOException { + // GIVEN + Path actual = mock(Path.class); + Path other = createFile(tempDir.resolve("other")); + IOException exception = new IOException("boom!"); + given(actual.toRealPath()).willThrow(exception); + // WHEN + Throwable thrown = catchThrowable(() -> paths.assertStartsWith(info, actual, other)); + // THEN + then(thrown).isInstanceOf(UncheckedIOException.class) + .hasCause(exception); } @Test - void should_throw_PathsException_if_other_cannot_be_resolved() throws IOException { - final IOException exception = new IOException(); - when(actual.toRealPath()).thenReturn(canonicalActual); - when(other.toRealPath()).thenThrow(exception); - - assertThatExceptionOfType(PathsException.class).isThrownBy(() -> paths.assertStartsWith(info, actual, other)) - .withMessage("failed to resolve argument real path") - .withCause(exception); + void should_rethrow_IOException_as_UncheckedIOException_if_other_cannot_be_resolved() throws IOException { + // GIVEN + Path actual = createFile(tempDir.resolve("actual")); + Path other = mock(Path.class); + IOException exception = new IOException("boom!"); + given(other.toRealPath()).willThrow(exception); + // WHEN + Throwable thrown = catchThrowable(() -> paths.assertStartsWith(info, actual, other)); + // THEN + then(thrown).isInstanceOf(UncheckedIOException.class) + .hasCause(exception); } @Test void should_fail_if_actual_does_not_start_with_other() throws IOException { - when(actual.toRealPath()).thenReturn(canonicalActual); - when(other.toRealPath()).thenReturn(canonicalOther); - // This is the default, but let's make this explicit - when(canonicalActual.startsWith(canonicalOther)).thenReturn(false); - - Throwable error = catchThrowable(() -> paths.assertStartsWith(info, actual, other)); - - assertThat(error).isInstanceOf(AssertionError.class); - verify(failures).failure(info, shouldStartWith(actual, other)); + // GIVEN + Path actual = createFile(tempDir.resolve("actual")); + Path other = createFile(tempDir.resolve("other")); + // WHEN + AssertionError error = expectAssertionError(() -> paths.assertStartsWith(info, actual, other)); + // THEN + then(error).hasMessage(shouldStartWith(actual, other).create()); } @Test - void should_succeed_if_actual_starts_with_other() throws IOException { - when(actual.toRealPath()).thenReturn(canonicalActual); - when(other.toRealPath()).thenReturn(canonicalOther); + void should_pass_if_actual_starts_with_other() throws IOException { + // GIVEN + Path other = createDirectory(tempDir.resolve("other")).toRealPath(); + Path actual = createFile(other.resolve("actual")).toRealPath(); + // WHEN/THEN + paths.assertStartsWith(info, actual, other); + } - when(canonicalActual.startsWith(canonicalOther)).thenReturn(true); + @Test + void should_pass_if_actual_is_not_canonical() throws IOException { + // GIVEN + Path other = createDirectory(tempDir.resolve("other")); + Path file = createFile(other.resolve("file")); + Path actual = createSymbolicLink(tempDir.resolve("actual"), file); + // WHEN/THEN + paths.assertStartsWith(info, actual, other); + } + @Test + void should_pass_if_other_is_not_canonical() throws IOException { + // GIVEN + Path directory = createDirectory(tempDir.resolve("directory")); + Path other = createSymbolicLink(tempDir.resolve("other"), directory); + Path actual = createFile(directory.resolve("actual")); + // WHEN/THEN paths.assertStartsWith(info, actual, other); }