Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add linesOf variants for a Path content #2618

Merged
merged 5 commits into from May 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
51 changes: 50 additions & 1 deletion src/main/java/org/assertj/core/api/Assertions.java
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
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
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
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
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
@@ -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);
}
}
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);
}

}