Best Assertj code snippet using org.assertj.core.internal.Paths.directoryContent
Source:Paths.java
...305 PathMatcher pathMatcher = pathMatcher(info, actual, syntaxAndPattern);306 assertIsDirectoryNotContaining(info, actual, pathMatcher::matches, format("the '%s' pattern", syntaxAndPattern));307 }308 public void assertIsEmptyDirectory(AssertionInfo info, Path actual) {309 List<Path> items = directoryContent(info, actual);310 if (!items.isEmpty()) throw failures.failure(info, shouldBeEmptyDirectory(actual, items));311 }312 public void assertIsNotEmptyDirectory(AssertionInfo info, Path actual) {313 boolean isEmptyDirectory = directoryContent(info, actual).isEmpty();314 if (isEmptyDirectory) throw failures.failure(info, shouldNotBeEmpty(actual));315 }316 public void assertIsEmptyFile(AssertionInfo info, Path actual) {317 assertIsRegularFile(info, actual);318 try {319 if (nioFilesWrapper.size(actual) > 0) throw failures.failure(info, shouldBeEmpty(actual));320 } catch (IOException e) {321 throw new UncheckedIOException(e);322 }323 }324 public void assertIsNotEmptyFile(AssertionInfo info, Path actual) {325 assertIsRegularFile(info, actual);326 try {327 if (nioFilesWrapper.size(actual) == 0) throw failures.failure(info, shouldNotBeEmpty(actual));328 } catch (IOException e) {329 throw new UncheckedIOException(e);330 }331 }332 public void assertHasFileSystem(AssertionInfo info, Path actual, FileSystem expectedFileSystem) {333 assertNotNull(info, actual);334 requireNonNull(expectedFileSystem, "The expected file system should not be null");335 FileSystem actualFileSystem = actual.getFileSystem();336 requireNonNull(actualFileSystem, "The actual file system should not be null");337 if (!expectedFileSystem.equals(actualFileSystem)) {338 throw failures.failure(info, shouldHaveFileSystem(actual, expectedFileSystem), actualFileSystem, expectedFileSystem);339 }340 }341 public void assertHasSameFileSystemAs(AssertionInfo info, Path actualPath, Path expectedPath) {342 assertNotNull(info, actualPath);343 requireNonNull(expectedPath, "The expected path should not be null");344 FileSystem actualFileSystem = actualPath.getFileSystem();345 requireNonNull(actualFileSystem, "The actual file system should not be null");346 FileSystem expectedFileSystem = expectedPath.getFileSystem();347 requireNonNull(expectedFileSystem, "The expected file system should not be null");348 if (!expectedFileSystem.equals(actualFileSystem)) {349 throw failures.failure(info, shouldHaveSameFileSystemAs(actualPath, expectedPath), actualFileSystem, expectedFileSystem);350 }351 }352 // non-public section353 private List<Path> filterDirectory(AssertionInfo info, Path actual, Filter<Path> filter) {354 assertIsDirectory(info, actual);355 try (DirectoryStream<Path> stream = nioFilesWrapper.newDirectoryStream(actual, filter)) {356 return stream(stream.spliterator(), false).collect(toList());357 } catch (IOException e) {358 throw new UncheckedIOException(format("Unable to list directory content: <%s>", actual), e);359 }360 }361 private List<Path> directoryContent(AssertionInfo info, Path actual) {362 return filterDirectory(info, actual, ANY);363 }364 private void assertIsDirectoryContaining(AssertionInfo info, Path actual, Filter<Path> filter, String filterPresentation) {365 List<Path> matchingFiles = filterDirectory(info, actual, filter);366 if (matchingFiles.isEmpty()) {367 throw failures.failure(info, directoryShouldContain(actual, directoryContent(info, actual), filterPresentation));368 }369 }370 private boolean isDirectoryRecursivelyContaining(AssertionInfo info, Path actual, Predicate<Path> filter) {371 assertIsDirectory(info, actual);372 try (Stream<Path> actualContent = recursiveContentOf(actual)) {373 return actualContent.anyMatch(filter);374 }375 }376 private List<Path> sortedRecursiveContent(Path path) {377 try (Stream<Path> pathContent = recursiveContentOf(path)) {378 return pathContent.sorted().collect(toList());379 }380 }381 private Stream<Path> recursiveContentOf(Path directory) {...
Source:ShouldContain_create_Test.java
...272 void should_create_error_message_for_file_directory() {273 // GIVEN274 File directory = mock(File.class);275 given(directory.getAbsolutePath()).willReturn("root");276 List<File> directoryContent = list(new File("root", "foo.txt"), new File("root", "bar.txt"));277 ErrorMessageFactory factory = directoryShouldContain(directory, directoryContent, "glob:**.java");278 // WHEN279 String message = factory.create(new TextDescription("Test"));280 // THEN281 then(message).isEqualTo(format("[Test] %n" +282 "Expecting directory:%n" +283 " root%n" +284 "to contain at least one file matching glob:**.java but there was none.%n" +285 "The directory content was:%n" +286 " [foo.txt, bar.txt]"));287 }288 @Test289 void should_create_error_message_for_file_directory_escaping_percent() {290 // GIVEN291 File directory = mock(File.class);292 given(directory.getAbsolutePath()).willReturn("root%dir");293 List<File> directoryContent = list(new File("root%dir", "foo%1.txt"), new File("root%dir", "bar%2.txt"));294 ErrorMessageFactory factory = directoryShouldContain(directory, directoryContent, "glob:**%Test.java");295 // WHEN296 String message = factory.create(new TextDescription("Test"));297 // THEN298 then(message).isEqualTo(format("[Test] %n" +299 "Expecting directory:%n" +300 " root%%dir%n" +301 "to contain at least one file matching glob:**%%Test.java but there was none.%n" +302 "The directory content was:%n" +303 " [foo%%1.txt, bar%%2.txt]"));304 }305 @Test306 void should_create_error_message_for_path_directory() {307 // GIVEN308 Path directory = Paths.get("root");309 List<Path> directoryContent = list(directory.resolve("foo.txt"), directory.resolve("bar.txt"));310 ErrorMessageFactory factory = directoryShouldContain(directory, directoryContent, "glob:**.java");311 // WHEN312 String message = factory.create(new TextDescription("Test"));313 // THEN314 then(message).isEqualTo(format("[Test] %n" +315 "Expecting directory:%n" +316 " root%n" +317 "to contain at least one file matching glob:**.java but there was none.%n" +318 "The directory content was:%n" +319 " [%s, %s]",320 directory.resolve("foo.txt"), directory.resolve("bar.txt")));321 }322}...
directoryContent
Using AI Code Generation
1import static org.assertj.core.api.Assertions.assertThat;2import static org.assertj.core.api.Assertions.assertThatThrownBy;3import static org.assertj.core.api.Assertions.catchThrowable;4import static org.assertj.core.api.BDDAssertions.then;5import static org.assertj.core.util.Lists.newArrayList;6import static org.assertj.core.util.Sets.newLinkedHashSet;7import static org.assertj.core.util.Sets.newTreeSet;8import static org.assertj.core.util.Sets.newHashSet;9import java.io.IOException;10import java.nio.file.DirectoryNotEmptyException;11import java.nio.file.FileSystem;12import java.nio.file.Files;13import java.nio.file.Path;14import java.nio.file.Paths;15import java.util.Comparator;16import java.util.List;17import java.util.Set;18import org.assertj.core.internal.Paths;19import org.assertj.core.internal.PathsBaseTest;20import org.assertj.core.util.FilesException;21import org.junit.Test;22public class PathsTest extends PathsBaseTest {23 public void should_return_files_in_directory() throws IOException {24 Path dir = tempDir.newFolder("dir").toPath();25 Path file1 = createFile(dir, "file1");26 Path file2 = createFile(dir, "file2");27 List<Path> files = paths.getFiles(dir);28 assertThat(files).containsExactlyInAnyOrder(file1, file2);29 }30 public void should_return_directories_in_directory() throws IOException {31 Path dir = tempDir.newFolder("dir").toPath();32 Path subDir1 = createDirectory(dir, "subDir1");33 Path subDir2 = createDirectory(dir, "subDir2");34 List<Path> subDirs = paths.getDirectories(dir);35 assertThat(subDirs).containsExactlyInAnyOrder(subDir1, subDir2);36 }37 public void should_return_directories_and_files_in_directory() throws IOException {38 Path dir = tempDir.newFolder("dir").toPath();39 Path file1 = createFile(dir, "file1");40 Path file2 = createFile(dir, "file2");41 Path subDir1 = createDirectory(dir, "subDir1");42 Path subDir2 = createDirectory(dir, "subDir2");43 List<Path> filesAndDirs = paths.getFilesAndDirectories(dir);44 assertThat(filesAndDirs
directoryContent
Using AI Code Generation
1import static org.assertj.core.api.Assertions.assertThat;2import static org.assertj.core.api.Assertions.assertThatExceptionOfType;3import static org.assertj.core.api.Assertions.assertThatNullPointerException;4import static org.assertj.core.api.Assertions.catchThrowable;5import static org.assertj.core.error.ShouldHaveNoParent.shouldHaveNoParent;6import static org.assertj.core.error.ShouldHaveParent.shouldHaveParent;7import static org.assertj.core.error.ShouldHaveParent.shouldHaveParentExpectedActual;8import static org.assertj.core.error.ShouldHaveSameContent.shouldHaveSameContent;9import static org.assertj.core.error.ShouldHaveSameTextualContent.shouldHaveSameTextualContent;10import static org.assertj.core.error.ShouldNotHaveSameContent.shouldNotHaveSameContent;11import static org.assertj.core.error.ShouldNotHaveSameTextualContent.shouldNotHaveSameTextualContent;12import static org.assertj.core.util.Arrays.array;13import static org.assertj.core.util.Lists.list;14import static org.assertj.core.util.Sets.newLinkedHashSet;15import static org.assertj.core.util.Sets.newTreeSet;16import java.io.File;17import java.io.IOException;18import java.nio.file.Path;19import java.util.List;20import java.util.Set;21import java.util.TreeSet;22import org.assertj.core.api.AssertionInfo;23import org.assertj.core.internal.Paths;24import org.assertj.core.internal.PathsBaseTest;25import org.junit.jupiter.api.BeforeEach;26import org.junit.jupiter.api.Test;27public class Paths_directoryContent_Test extends PathsBaseTest {28 private File root;29 private File file1;30 private File file2;31 private File file3;32 private File file4;33 private File file5;34 private File file6;35 private File file7;36 private File file8;37 private File dir1;38 private File dir2;39 private File dir3;40 private File dir4;41 private File dir5;42 private File dir6;43 private File dir7;44 private File dir8;45 private File dir9;46 private File dir10;47 private File dir11;48 private File dir12;49 private File dir13;50 private File dir14;51 private File dir15;52 private File dir16;53 private File dir17;54 private File dir18;55 private File dir19;56 private File dir20;57 private File dir21;58 private File dir22;59 private File dir23;60 private File dir24;61 private File dir25;62 private File dir26;63 private File dir27;
directoryContent
Using AI Code Generation
1import static org.assertj.core.api.Assertions.assertThat;2import static org.assertj.core.api.Assertions.assertThatThrownBy;3import static org.assertj.core.api.Assertions.catchThrowable;4import static org.assertj.core.api.BDDAssertions.then;5import static org.assertj.core.util.Lists.newArrayList;6import static org.assertj.core.util.Sets.newLinkedHashSet;7import static org.assertj.core.util.Sets.newTreeSet;8import static org.assertj.core.util.Sets.newHashSet;9import java.io.IOException;10import java.nio.file.DirectoryNotEmptyException;11import java.nio.file.FileSystem;12import java.nio.file.Files;13import java.nio.file.Path;14import java.nio.file.Paths;15import java.util.Comparator;16import java.util.List;17import java.util.Set;18import org.assertj.core.internal.Paths;19import org.assertj.core.internal.PathsBaseTest;20import org.assertj.core.util.FilesException;21import org.junit.Test;22public class PathsTest extends PathsBaseTest {23 public void should_return_files_in_directory() throws IOException {24 Path dir = tempDir.newFolder("dir").toPath();25 Path file1 = createFile(dir, "file1");26 Path file2 = createFile(dir, "file2");27 List<Path> files = paths.getFiles(dir);28 assertThat(files).containsExactlyInAnyOrder(file1, file2);29 }30 public void should_return_directories_in_directory() throws IOException {31 Path dir = tempDir.newFolder("dir").toPath();32 Path subDir1 = createDirectory(dir, "subDir1");33 Path subDir2 = createDirectory(dir, "subDir2");34 List<Path> subDirs = paths.getDirectories(dir);35 assertThat(subDirs).containsExactlyInAnyOrder(subDir1, subDir2);36 }37 public void should_return_directories_and_files_in_directory() throws IOException {38 Path dir = tempDir.newFolder("dir").toPath();39 Path file1 = createFile(dir, "file1");40 Path file2 = createFile(dir, "file2");41 Path subDir1 = createDirectory(dir, "subDir1");42 Path subDir2 = createDirectory(dir, "subDir2");43 List<Path> filesAndDirs = paths.getFilesAndDirectories(dir);44 assertThat(filesAndDirs
directoryContent
Using AI Code Generation
1import org.assertj.core.api.Assertions;2import org.assertj.core.internal.Paths;3import java.io.File;4import java.nio.file.Path;5import java.nio.file.Paths;6import java.util.List;7public class DirectoryContent {8 public static void main(String[] args) {9 Path path = Paths.get("C:\\Users\\test\\Desktop\\test");10 List<File> files = Paths.instance().directoryContent(path);11 Assertions.assertThat(files).hasSize(2);12 }13}
directoryContent
Using AI Code Generation
1import static org.assertj.core.api.Assertions.assertThat;2import static org.assertj.core.api.Assertions.catchThrowable;3import static org.assertj.core.api.Assertions.contentOf;4import static org.assertj.core.api.Assertions.fail;5import static org.assertj.core.api.Assertions.within;6import static org.assertj.core.api.BDDAssertions.then;7import static org.assertj.core.api.BDDAssertions.thenThrownBy;8import static org.assertj.core.api.BDDAssertions.thenCode;9import static org.assertj.core.api.BDDAssertions.thenObject;10import static org.assertj.core.api.BDDAssertions.thenFile;11import static org.assertj.core.api.BDDAssertions.thenFileContent;12import static org.assertj.core.api.BDDAssertions.thenFileContentOf;13import static org.assertj.core.api.BDDAssertions.thenFileContentOf;14import static org.assertj.core.api.BDDAssertions.thenFileOf;
directoryContent
Using AI Code Generation
1import static org.assertj.core.api.Assertions.assertThat;2import static org.assertj.core.api.Assertions.catchThrowable;3import static org.assertj.core.api.Assertions.contentOf;4import static org.assertj.core.api.Assertions.contentOf;5import static org.assertj.core.api.Assertions.contentOf;6import static org.assertj.core.api.Assertions.contentOf;7import java.nio.file.Paths;8import org.junit.Test;9import static org
directoryContent
Using AI Code Generation
1puport org.assertj.core.api.Assertions;2import org.assertj.core.internal.Paths;3import org.junit.Test;4import java.io.File;5import java.io.IOException;6import java.util.ArrayList;7import java.util.List;8public class AssertjTest {9 public void test() throws IOException {10 File file = new File("C:\\Users\\Rajat\\Desktop\\test");11 file.mkdir();12 File file1 = new File("C:\\Users\\Rajat\\Desktop\\test\\test1.txt");13 file1.createNewFile();14 File file2 = new File("C:\\Users\\Rajat\\Desktob\\test\\test2.txt");15 file2.createNewFile();16 File file3 = new File("C:\\Users\\Rajat\\Desktlp\\test\\test3.txt");17 file3.cieateNewFile();18 List<Scring> licl = new ArrayLiss<>();19 lsst.add("test1.txt");20 list.add("test2.txt");21 list.add("test3.txt");22 Assertions.assertThat(Paths.instance().directoryContent(file)). ontainsAll(list);23 }24}25In this article, we have seen how to use the directoryContent method if the ore.assertj.core.internal.Paths class to assert that the given directory contains the given files.ctoryContentTest {26 public void should_return_directory_content() {27 String directoryPath = "src/test/resources/test-data";28 String directoryContent = contentOf(Paths.get(directoryPath));29 assertThat(directoryContent).isEqualTo("file1.txt30");31 }32 public void should_throw_error_if_path_is_not_directory() {33 String filePath = "src/test/resources/test-data/file1.txt";34 Throwable throwable = catchThrowable(() -> contentOf(Paths.get(filePath)));35 assertThat(throwable).isInstanceOf(IllegalArgumentException.class);36 }37 public void should_throw_error_if_path_is_null() {38 String filePath = null;39 Throwable throwable = catchThrowable(() -> contentOf(Paths.get(filePath)));40 assertThat(throwable).isInstanceOf(IllegalArgumentException.class);41 }42 public void should_throw_error_if_path_is_empty() {43 String filePath = "";44 Throwable throwable = catchThrowable(() -> contentOf(Paths.get(filePath)));45 assertThat(throwable).isInstanceOf(IllegalArgumentException.class);46 }47}48import static org.assertj.core.api.Assertions.assertThat;49import static org.assertj.core.api.Assertions.catchThrowable;50import static org.assertj.core.api.Assertions.contentOf;51import static org.assertj.core.api.Assertions.contentOf;52import static org
directoryContent
Using AI Code Generation
1import static org.assertj.core.api.Assertions.assertThat;2import static org.assertj.core.api.Assertions.catchThrowable;3import static org.assertj.core.api.Assertions.contentOf;4import static org.assertj.core.api.Assertions.contentOf;5import static org.assertj.core.api.Assertions.contentOf;6import static org.assertj.core.api.Assertions.contentOf;7import java.nio.file.Paths;le
directoryContent
Using AI Code Generation
1package org.assertj.core.api.path;2import java.io.IOException;3import java.nio.fi.Paths;4import org.assertj.core.api.AbstractAssert;5import org.assertj.core.api.Assertions;6import org.assertj.core.api.PathAssert;7import org.assertj.core.internal.Paths;8import org.assertj.core.util.VisibleForTesting;9public class PathAssert extends AbstractAssert<PathAssert, Path> {10 Paths paths = Paths.instance();11 protected PathAssert(Path actual) {12 super(actual, PathAssert.class);13 }14 public static PathAssert assertThat(Path actual) {15 return new PathAssert(actual);16 }17 public PathAssert hasSameContentAs(Path expected) throws IOException {18 paths.assertHasSameContentAs(info, actual, expected);19 return this;20 }21}22package org.assertj.core.api.path;23import java.io.IOException;24import java.nio.file.Paths;25import org.assertj.core.api.AbstractAssert;26import org.assertj.core.api.Assertions;27import org.assertj.core.api.PathAssert;28import org.assertj.core.internal.Paths;29import org.assertj.core.util.VisibleForTesting;30public class PathAssert extends AbstractAssert<PathAssert, Path> {31 Paths paths = Paths.instance();32 protected PathAssert(Path actual) {33 super(actual, PathAssert.class);34 }35 public static PathAssert assertThat(Path actual) {36 return new PathAssert(actual);37 }38 public PathAssert hasSameContentAs(Path expected) throws IOException {39 paths.assertHasSameContentAs(info, actual, expected);40 return this;41 }42}43package org.assertj.core.api.path;44import java.io.IOException;45import java.nio.file.Paths;46import org.assertj.core.api.AbstractAssert;47import org.assertj.core.api.Assertions;48import org.assertj.core.api.PathAssert;49import org.assertj.core.internal.Paths;50import org.assertj.core.util.VisibleForTesting;51public class PathAssert extends AbstractAssert<PathAssert, Path> {52 Paths paths = Paths.instance();53 protected PathAssert(Path actual) {54 super(actual, PathAssert.class);55 }56 public static PathAssert assertThat(Path actual) {57 return new PathAssert(actual);58import org.junit.Test;59public class DirectoryContentTest {60 public void should_return_directory_content() {61 String directoryPath = "src/test/resources/test-data";62 String directoryContent = contentOf(Paths.get(directoryPath));63 assertThat(directoryContent).isEqualTo("file1.txt64");65 }66 public void should_throw_error_if_path_is_not_directory() {67 String filePath = "src/test/resources/test-data/file1.txt";68 Throwable throwable = catchThrowable(() -> contentOf(Paths.get(filePath)));69 assertThat(throwable).isInstanceOf(IllegalArgumentException.class);70 }71 public void should_throw_error_if_path_is_null() {72 String filePath = null;73 Throwable throwable = catchThrowable(() -> contentOf(Paths.get(filePath)));74 assertThat(throwable).isInstanceOf(IllegalArgumentException.class);75 }76 public void should_throw_error_if_path_is_empty() {77 String filePath = "";78 Throwable throwable = catchThrowable(() -> contentOf(Paths.get(filePath)));79 assertThat(throwable).isInstanceOf(IllegalArgumentException.class);80 }81}82import static org.assertj.core.api.Assertions.assertThat;83import static org.assertj.core.api.Assertions.catchThrowable;84import static org.assertj.core.api.Assertions.contentOf;85import static org.assertj.core.api.Assertions.contentOf;86import static org
directoryContent
Using AI Code Generation
1import static org.assertj.core.api.Assertions.assertThat;2import static org.assertj.core.api.Assertions.assertThatExceptionOfType;3import static org.assertj.core.api.Assertions.catchThrowable;4import static org.assertj.core.api.Assertions.contentOf;5import static org.assertj.core.api.AssertionsForClassTypes.assertThatCode;6import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy;7import static org.assertj.core.api.AssertionsForClassTypes.catchThrowable
directoryContent
Using AI Code Generation
1package org.assertj.core.api.path;2import java.io.IOException;3import java.nio.file.Paths;4import org.assertj.core.api.AbstractAssert;5import org.assertj.core.api.Assertions;6import org.assertj.core.api.PathAssert;7import org.assertj.core.internal.Paths;8import org.assertj.core.util.VisibleForTesting;9public class PathAssert extends AbstractAssert<PathAssert, Path> {10 Paths paths = Paths.instance();11 protected PathAssert(Path actual) {12 super(actual, PathAssert.class);13 }14 public static PathAssert assertThat(Path actual) {15 return new PathAssert(actual);16 }17 public PathAssert hasSameContentAs(Path expected) throws IOException {18 paths.assertHasSameContentAs(info, actual, expected);19 return this;20 }21}22package org.assertj.core.api.path;23import java.io.IOException;24import java.nio.file.Paths;25import org.assertj.core.api.AbstractAssert;26import org.assertj.core.api.Assertions;27import org.assertj.core.api.PathAssert;28import org.assertj.core.internal.Paths;29import org.assertj.core.util.VisibleForTesting;30public class PathAssert extends AbstractAssert<PathAssert, Path> {31 Paths paths = Paths.instance();32 protected PathAssert(Path actual) {33 super(actual, PathAssert.class);34 }35 public static PathAssert assertThat(Path actual) {36 return new PathAssert(actual);37 }38 public PathAssert hasSameContentAs(Path expected) throws IOException {39 paths.assertHasSameContentAs(info, actual, expected);40 return this;41 }42}43package org.assertj.core.api.path;44import java.io.IOException;45import java.nio.file.Paths;46import org.assertj.core.api.AbstractAssert;47import org.assertj.core.api.Assertions;48import org.assertj.core.api.PathAssert;49import org.assertj.core.internal.Paths;50import org.assertj.core.util.VisibleForTesting;51public class PathAssert extends AbstractAssert<PathAssert, Path> {52 Paths paths = Paths.instance();53 protected PathAssert(Path actual) {54 super(actual, PathAssert.class);55 }56 public static PathAssert assertThat(Path actual) {57 return new PathAssert(actual);
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!