Skip to content

Commit

Permalink
Add linesOf variants for a Path content (#2618)
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanBratanov authored May 22, 2022
1 parent 24c4fcb commit 59e27a0
Show file tree
Hide file tree
Showing 9 changed files with 424 additions and 10 deletions.
51 changes: 50 additions & 1 deletion src/main/java/org/assertj/core/api/Assertions.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@
import org.assertj.core.util.CanIgnoreReturnValue;
import org.assertj.core.util.CheckReturnValue;
import org.assertj.core.util.Files;
import org.assertj.core.util.Paths;
import org.assertj.core.util.URLs;
import org.assertj.core.util.introspection.FieldSupport;
import org.assertj.core.util.introspection.Introspection;
Expand Down Expand Up @@ -169,7 +170,7 @@ public static <T> PredicateAssert<T> assertThat(Predicate<T> actual) {
return AssertionsForInterfaceTypes.assertThat(actual);
}


/**
* Create assertion for {@link Predicate}.
* <p>
Expand Down Expand Up @@ -2842,6 +2843,54 @@ public static List<String> linesOf(File file, String charsetName) {
return Files.linesOf(file, charsetName);
}

/**
* Loads the text content of a file at a given path into a list of strings with the default charset, each string corresponding to a
* line.
* The line endings are either \n, \r or \r\n.
*
* @param path the path.
* @return the content of the file at the given path.
* @throws NullPointerException if the given charset is {@code null}.
* @throws UncheckedIOException if an I/O exception occurs.
*
* @since 3.23.0
*/
public static List<String> linesOf(Path path) {
return Paths.linesOf(path, Charset.defaultCharset());
}

/**
* Loads the text content of a file at a given path into a list of strings, each string corresponding to a line.
* The line endings are either \n, \r or \r\n.
*
* @param path the path.
* @param charset the character set to use.
* @return the content of the file at the given path.
* @throws NullPointerException if the given charset is {@code null}.
* @throws UncheckedIOException if an I/O exception occurs.
*
* @since 3.23.0
*/
public static List<String> linesOf(Path path, Charset charset) {
return Paths.linesOf(path, charset);
}

/**
* Loads the text content of a file at a given path into a list of strings, each string corresponding to a line. The line endings are
* either \n, \r or \r\n.
*
* @param path the path.
* @param charsetName the name of the character set to use.
* @return the content of the file at the given path.
* @throws NullPointerException if the given charset is {@code null}.
* @throws UncheckedIOException if an I/O exception occurs.
*
* @since 3.23.0
*/
public static List<String> linesOf(Path path, String charsetName) {
return Paths.linesOf(path, charsetName);
}

// --------------------------------------------------------------------------------------------------
// URL/Resource methods : not assertions but here to have a single entry point to all AssertJ features.
// --------------------------------------------------------------------------------------------------
Expand Down
52 changes: 51 additions & 1 deletion src/main/java/org/assertj/core/api/AssertionsForClassTypes.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.net.URI;
import java.net.URL;
import java.nio.charset.Charset;
import java.nio.file.Path;
import java.text.DateFormat;
import java.time.Duration;
import java.time.Instant;
Expand All @@ -38,8 +39,8 @@
import java.util.OptionalInt;
import java.util.OptionalLong;
import java.util.concurrent.CompletableFuture;

import java.util.regex.Matcher;

import org.assertj.core.api.ThrowableAssert.ThrowingCallable;
import org.assertj.core.api.filter.FilterOperator;
import org.assertj.core.api.filter.Filters;
Expand All @@ -60,6 +61,7 @@
import org.assertj.core.util.CanIgnoreReturnValue;
import org.assertj.core.util.CheckReturnValue;
import org.assertj.core.util.Files;
import org.assertj.core.util.Paths;
import org.assertj.core.util.URLs;
import org.assertj.core.util.introspection.FieldSupport;

Expand Down Expand Up @@ -1652,6 +1654,54 @@ public static List<String> linesOf(File file, String charsetName) {
return Files.linesOf(file, charsetName);
}

/**
* Loads the text content of a path into a list of strings with the default charset, each string corresponding to a
* line.
* The line endings are either \n, \r or \r\n.
*
* @param path the path.
* @return the content of the file.
* @throws NullPointerException if the given charset is {@code null}.
* @throws UncheckedIOException if an I/O exception occurs.
*
* @since 3.23.0
*/
public static List<String> linesOf(Path path) {
return Paths.linesOf(path, Charset.defaultCharset());
}

/**
* Loads the text content of a path into a list of strings, each string corresponding to a line.
* The line endings are either \n, \r or \r\n.
*
* @param path the path.
* @param charset the character set to use.
* @return the content of the file.
* @throws NullPointerException if the given charset is {@code null}.
* @throws UncheckedIOException if an I/O exception occurs.
*
* @since 3.23.0
*/
public static List<String> linesOf(Path path, Charset charset) {
return Paths.linesOf(path, charset);
}

/**
* Loads the text content of a path into a list of strings, each string corresponding to a line. The line endings are
* either \n, \r or \r\n.
*
* @param path the path.
* @param charsetName the name of the character set to use.
* @return the content of the file.
* @throws NullPointerException if the given charset is {@code null}.
* @throws UncheckedIOException if an I/O exception occurs.
*
* @since 3.23.0
*/
public static List<String> linesOf(Path path, String charsetName) {
return Paths.linesOf(path, charsetName);
}

// --------------------------------------------------------------------------------------------------
// URL/Resource methods : not assertions but here to have a single entry point to all AssertJ features.
// --------------------------------------------------------------------------------------------------
Expand Down
48 changes: 48 additions & 0 deletions src/main/java/org/assertj/core/api/BDDAssertions.java
Original file line number Diff line number Diff line change
Expand Up @@ -3338,6 +3338,54 @@ public static List<String> linesOf(File file, String charsetName) {
return Assertions.linesOf(file, charsetName);
}

/**
* Loads the text content of a file at a given path into a list of strings with the default charset, each string corresponding to a
* line.
* The line endings are either \n, \r or \r\n.
*
* @param path the path.
* @return the content of the file at the given path.
* @throws NullPointerException if the given charset is {@code null}.
* @throws UncheckedIOException if an I/O exception occurs.
*
* @since 3.23.0
*/
public static List<String> linesOf(Path path) {
return Assertions.linesOf(path, Charset.defaultCharset());
}

/**
* Loads the text content of a file at a given path into a list of strings, each string corresponding to a line.
* The line endings are either \n, \r or \r\n.
*
* @param path the path.
* @param charset the character set to use.
* @return the content of the file at the given path.
* @throws NullPointerException if the given charset is {@code null}.
* @throws UncheckedIOException if an I/O exception occurs.
*
* @since 3.23.0
*/
public static List<String> linesOf(Path path, Charset charset) {
return Assertions.linesOf(path, charset);
}

/**
* Loads the text content of a file at a given path into a list of strings, each string corresponding to a line. The line endings are
* either \n, \r or \r\n.
*
* @param path the path.
* @param charsetName the name of the character set to use.
* @return the content of the file at the given path.
* @throws NullPointerException if the given charset is {@code null}.
* @throws UncheckedIOException if an I/O exception occurs.
*
* @since 3.23.0
*/
public static List<String> linesOf(Path path, String charsetName) {
return Assertions.linesOf(path, charsetName);
}

// --------------------------------------------------------------------------------------------------
// URL/Resource methods : not assertions but here to have a single entry point to all AssertJ features.
// --------------------------------------------------------------------------------------------------
Expand Down
48 changes: 48 additions & 0 deletions src/main/java/org/assertj/core/api/WithAssertions.java
Original file line number Diff line number Diff line change
Expand Up @@ -2067,6 +2067,54 @@ default List<String> linesOf(final File file, final Charset charset) {
return Assertions.linesOf(file, charset);
}

/**
* Loads the text content of a file at a given path into a list of strings with the default charset, each string corresponding to a
* line.
* The line endings are either \n, \r or \r\n.
*
* @param path the path.
* @return the content of the file at the given path.
* @throws NullPointerException if the given charset is {@code null}.
* @throws UncheckedIOException if an I/O exception occurs.
*
* @since 3.23.0
*/
default List<String> linesOf(final Path path) {
return Assertions.linesOf(path);
}

/**
* Loads the text content of a file at a given path into a list of strings, each string corresponding to a line. The line endings are
* either \n, \r or \r\n.
*
* @param path the file.
* @param charsetName the name of the character set to use.
* @return the content of the file at the given path.
* @throws NullPointerException if the given charset is {@code null}.
* @throws UncheckedIOException if an I/O exception occurs.
*
* @since 3.23.0
*/
default List<String> linesOf(final Path path, final String charsetName) {
return Assertions.linesOf(path, charsetName);
}

/**
* Loads the text content of a file at a given path into a list of strings, each string corresponding to a line.
* The line endings are either \n, \r or \r\n.
*
* @param path the path.
* @param charset the character set to use.
* @return the content of the file at the given path.
* @throws NullPointerException if the given charset is {@code null}.
* @throws UncheckedIOException if an I/O exception occurs.
*
* @since 3.23.0
*/
default List<String> linesOf(final Path path, final Charset charset) {
return Assertions.linesOf(path, charset);
}

/**
* Sets whether we remove elements related to AssertJ from assertion error stack trace.
*
Expand Down
10 changes: 2 additions & 8 deletions src/main/java/org/assertj/core/util/Files.java
Original file line number Diff line number Diff line change
Expand Up @@ -288,12 +288,7 @@ public static String contentOf(File file, Charset charset) {
* @throws UncheckedIOException if an I/O exception occurs.
*/
public static List<String> linesOf(File file, Charset charset) {
requireNonNull(charset, "The charset should not be null");
try {
return java.nio.file.Files.readAllLines(file.toPath(), charset);
} catch (IOException e) {
throw new UncheckedIOException("Unable to read " + file.getAbsolutePath(), e);
}
return Paths.linesOf(file.toPath(), charset);
}

/**
Expand All @@ -307,8 +302,7 @@ public static List<String> linesOf(File file, Charset charset) {
* @throws UncheckedIOException if an I/O exception occurs.
*/
public static List<String> linesOf(File file, String charsetName) {
checkArgumentCharsetIsSupported(charsetName);
return linesOf(file, Charset.forName(charsetName));
return Paths.linesOf(file.toPath(), charsetName);
}

private static void checkArgumentCharsetIsSupported(String charsetName) {
Expand Down
73 changes: 73 additions & 0 deletions src/main/java/org/assertj/core/util/Paths.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* 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-2022 the original author or authors.
*/
package org.assertj.core.util;

import static java.util.Objects.requireNonNull;
import static org.assertj.core.util.Preconditions.checkArgument;

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;

/**
* Utility methods related to {@link Path}s.
*
* @author Stefan Bratanov
*
* @since 3.23.0
*/
public class Paths {

private Paths() {}

/**
* Loads the text content of a file at a given path into a list of strings, each string corresponding to a line. The line endings are
* either \n, \r or \r\n.
*
* @param path the path.
* @param charset the character set to use.
* @return the content of the file at the given path.
* @throws NullPointerException if the given charset is {@code null}.
* @throws UncheckedIOException if an I/O exception occurs.
*/
public static List<String> linesOf(Path path, Charset charset) {
requireNonNull(charset, "The charset should not be null");
try {
return Files.readAllLines(path, charset);
} catch (IOException e) {
throw new UncheckedIOException("Unable to read " + path, e);
}
}

/**
* Loads the text content of a file at a given path into a list of strings, each string corresponding to a line. The line endings are
* either \n, \r or \r\n.
*
* @param path the path.
* @param charsetName the name of the character set to use.
* @return the content of the file at the given path.
* @throws NullPointerException if the given charset is {@code null}.
* @throws UncheckedIOException if an I/O exception occurs.
*/
public static List<String> linesOf(Path path, String charsetName) {
checkArgumentCharsetIsSupported(charsetName);
return linesOf(path, Charset.forName(charsetName));
}

private static void checkArgumentCharsetIsSupported(String charsetName) {
checkArgument(Charset.isSupported(charsetName), "Charset:<'%s'> is not supported on this system", charsetName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

import java.io.File;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

import org.junit.jupiter.api.Test;
Expand All @@ -33,4 +35,11 @@ void should_read_lines_of_file_with_UTF8_charset() {
assertThat(linesOf(file, StandardCharsets.UTF_8)).isEqualTo(EXPECTED_CONTENT);
}

@Test
void should_read_lines_of_path_with_UTF8_charset() {
Path path = Paths.get("src", "test", "resources", "utf8.txt");
assertThat(linesOf(path, "UTF-8")).isEqualTo(EXPECTED_CONTENT);
assertThat(linesOf(path, StandardCharsets.UTF_8)).isEqualTo(EXPECTED_CONTENT);
}

}
Loading

0 comments on commit 59e27a0

Please sign in to comment.