Skip to content

Commit 59e27a0

Browse files
Add linesOf variants for a Path content (#2618)
1 parent 24c4fcb commit 59e27a0

File tree

9 files changed

+424
-10
lines changed

9 files changed

+424
-10
lines changed

src/main/java/org/assertj/core/api/Assertions.java

+50-1
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@
104104
import org.assertj.core.util.CanIgnoreReturnValue;
105105
import org.assertj.core.util.CheckReturnValue;
106106
import org.assertj.core.util.Files;
107+
import org.assertj.core.util.Paths;
107108
import org.assertj.core.util.URLs;
108109
import org.assertj.core.util.introspection.FieldSupport;
109110
import org.assertj.core.util.introspection.Introspection;
@@ -169,7 +170,7 @@ public static <T> PredicateAssert<T> assertThat(Predicate<T> actual) {
169170
return AssertionsForInterfaceTypes.assertThat(actual);
170171
}
171172

172-
173+
173174
/**
174175
* Create assertion for {@link Predicate}.
175176
* <p>
@@ -2842,6 +2843,54 @@ public static List<String> linesOf(File file, String charsetName) {
28422843
return Files.linesOf(file, charsetName);
28432844
}
28442845

2846+
/**
2847+
* 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
2848+
* line.
2849+
* The line endings are either \n, \r or \r\n.
2850+
*
2851+
* @param path the path.
2852+
* @return the content of the file at the given path.
2853+
* @throws NullPointerException if the given charset is {@code null}.
2854+
* @throws UncheckedIOException if an I/O exception occurs.
2855+
*
2856+
* @since 3.23.0
2857+
*/
2858+
public static List<String> linesOf(Path path) {
2859+
return Paths.linesOf(path, Charset.defaultCharset());
2860+
}
2861+
2862+
/**
2863+
* Loads the text content of a file at a given path into a list of strings, each string corresponding to a line.
2864+
* The line endings are either \n, \r or \r\n.
2865+
*
2866+
* @param path the path.
2867+
* @param charset the character set to use.
2868+
* @return the content of the file at the given path.
2869+
* @throws NullPointerException if the given charset is {@code null}.
2870+
* @throws UncheckedIOException if an I/O exception occurs.
2871+
*
2872+
* @since 3.23.0
2873+
*/
2874+
public static List<String> linesOf(Path path, Charset charset) {
2875+
return Paths.linesOf(path, charset);
2876+
}
2877+
2878+
/**
2879+
* 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
2880+
* either \n, \r or \r\n.
2881+
*
2882+
* @param path the path.
2883+
* @param charsetName the name of the character set to use.
2884+
* @return the content of the file at the given path.
2885+
* @throws NullPointerException if the given charset is {@code null}.
2886+
* @throws UncheckedIOException if an I/O exception occurs.
2887+
*
2888+
* @since 3.23.0
2889+
*/
2890+
public static List<String> linesOf(Path path, String charsetName) {
2891+
return Paths.linesOf(path, charsetName);
2892+
}
2893+
28452894
// --------------------------------------------------------------------------------------------------
28462895
// URL/Resource methods : not assertions but here to have a single entry point to all AssertJ features.
28472896
// --------------------------------------------------------------------------------------------------

src/main/java/org/assertj/core/api/AssertionsForClassTypes.java

+51-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.net.URI;
2222
import java.net.URL;
2323
import java.nio.charset.Charset;
24+
import java.nio.file.Path;
2425
import java.text.DateFormat;
2526
import java.time.Duration;
2627
import java.time.Instant;
@@ -38,8 +39,8 @@
3839
import java.util.OptionalInt;
3940
import java.util.OptionalLong;
4041
import java.util.concurrent.CompletableFuture;
41-
4242
import java.util.regex.Matcher;
43+
4344
import org.assertj.core.api.ThrowableAssert.ThrowingCallable;
4445
import org.assertj.core.api.filter.FilterOperator;
4546
import org.assertj.core.api.filter.Filters;
@@ -60,6 +61,7 @@
6061
import org.assertj.core.util.CanIgnoreReturnValue;
6162
import org.assertj.core.util.CheckReturnValue;
6263
import org.assertj.core.util.Files;
64+
import org.assertj.core.util.Paths;
6365
import org.assertj.core.util.URLs;
6466
import org.assertj.core.util.introspection.FieldSupport;
6567

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

1657+
/**
1658+
* Loads the text content of a path into a list of strings with the default charset, each string corresponding to a
1659+
* line.
1660+
* The line endings are either \n, \r or \r\n.
1661+
*
1662+
* @param path the path.
1663+
* @return the content of the file.
1664+
* @throws NullPointerException if the given charset is {@code null}.
1665+
* @throws UncheckedIOException if an I/O exception occurs.
1666+
*
1667+
* @since 3.23.0
1668+
*/
1669+
public static List<String> linesOf(Path path) {
1670+
return Paths.linesOf(path, Charset.defaultCharset());
1671+
}
1672+
1673+
/**
1674+
* Loads the text content of a path into a list of strings, each string corresponding to a line.
1675+
* The line endings are either \n, \r or \r\n.
1676+
*
1677+
* @param path the path.
1678+
* @param charset the character set to use.
1679+
* @return the content of the file.
1680+
* @throws NullPointerException if the given charset is {@code null}.
1681+
* @throws UncheckedIOException if an I/O exception occurs.
1682+
*
1683+
* @since 3.23.0
1684+
*/
1685+
public static List<String> linesOf(Path path, Charset charset) {
1686+
return Paths.linesOf(path, charset);
1687+
}
1688+
1689+
/**
1690+
* Loads the text content of a path into a list of strings, each string corresponding to a line. The line endings are
1691+
* either \n, \r or \r\n.
1692+
*
1693+
* @param path the path.
1694+
* @param charsetName the name of the character set to use.
1695+
* @return the content of the file.
1696+
* @throws NullPointerException if the given charset is {@code null}.
1697+
* @throws UncheckedIOException if an I/O exception occurs.
1698+
*
1699+
* @since 3.23.0
1700+
*/
1701+
public static List<String> linesOf(Path path, String charsetName) {
1702+
return Paths.linesOf(path, charsetName);
1703+
}
1704+
16551705
// --------------------------------------------------------------------------------------------------
16561706
// URL/Resource methods : not assertions but here to have a single entry point to all AssertJ features.
16571707
// --------------------------------------------------------------------------------------------------

src/main/java/org/assertj/core/api/BDDAssertions.java

+48
Original file line numberDiff line numberDiff line change
@@ -3338,6 +3338,54 @@ public static List<String> linesOf(File file, String charsetName) {
33383338
return Assertions.linesOf(file, charsetName);
33393339
}
33403340

3341+
/**
3342+
* 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
3343+
* line.
3344+
* The line endings are either \n, \r or \r\n.
3345+
*
3346+
* @param path the path.
3347+
* @return the content of the file at the given path.
3348+
* @throws NullPointerException if the given charset is {@code null}.
3349+
* @throws UncheckedIOException if an I/O exception occurs.
3350+
*
3351+
* @since 3.23.0
3352+
*/
3353+
public static List<String> linesOf(Path path) {
3354+
return Assertions.linesOf(path, Charset.defaultCharset());
3355+
}
3356+
3357+
/**
3358+
* Loads the text content of a file at a given path into a list of strings, each string corresponding to a line.
3359+
* The line endings are either \n, \r or \r\n.
3360+
*
3361+
* @param path the path.
3362+
* @param charset the character set to use.
3363+
* @return the content of the file at the given path.
3364+
* @throws NullPointerException if the given charset is {@code null}.
3365+
* @throws UncheckedIOException if an I/O exception occurs.
3366+
*
3367+
* @since 3.23.0
3368+
*/
3369+
public static List<String> linesOf(Path path, Charset charset) {
3370+
return Assertions.linesOf(path, charset);
3371+
}
3372+
3373+
/**
3374+
* 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
3375+
* either \n, \r or \r\n.
3376+
*
3377+
* @param path the path.
3378+
* @param charsetName the name of the character set to use.
3379+
* @return the content of the file at the given path.
3380+
* @throws NullPointerException if the given charset is {@code null}.
3381+
* @throws UncheckedIOException if an I/O exception occurs.
3382+
*
3383+
* @since 3.23.0
3384+
*/
3385+
public static List<String> linesOf(Path path, String charsetName) {
3386+
return Assertions.linesOf(path, charsetName);
3387+
}
3388+
33413389
// --------------------------------------------------------------------------------------------------
33423390
// URL/Resource methods : not assertions but here to have a single entry point to all AssertJ features.
33433391
// --------------------------------------------------------------------------------------------------

src/main/java/org/assertj/core/api/WithAssertions.java

+48
Original file line numberDiff line numberDiff line change
@@ -2067,6 +2067,54 @@ default List<String> linesOf(final File file, final Charset charset) {
20672067
return Assertions.linesOf(file, charset);
20682068
}
20692069

2070+
/**
2071+
* 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
2072+
* line.
2073+
* The line endings are either \n, \r or \r\n.
2074+
*
2075+
* @param path the path.
2076+
* @return the content of the file at the given path.
2077+
* @throws NullPointerException if the given charset is {@code null}.
2078+
* @throws UncheckedIOException if an I/O exception occurs.
2079+
*
2080+
* @since 3.23.0
2081+
*/
2082+
default List<String> linesOf(final Path path) {
2083+
return Assertions.linesOf(path);
2084+
}
2085+
2086+
/**
2087+
* 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
2088+
* either \n, \r or \r\n.
2089+
*
2090+
* @param path the file.
2091+
* @param charsetName the name of the character set to use.
2092+
* @return the content of the file at the given path.
2093+
* @throws NullPointerException if the given charset is {@code null}.
2094+
* @throws UncheckedIOException if an I/O exception occurs.
2095+
*
2096+
* @since 3.23.0
2097+
*/
2098+
default List<String> linesOf(final Path path, final String charsetName) {
2099+
return Assertions.linesOf(path, charsetName);
2100+
}
2101+
2102+
/**
2103+
* Loads the text content of a file at a given path into a list of strings, each string corresponding to a line.
2104+
* The line endings are either \n, \r or \r\n.
2105+
*
2106+
* @param path the path.
2107+
* @param charset the character set to use.
2108+
* @return the content of the file at the given path.
2109+
* @throws NullPointerException if the given charset is {@code null}.
2110+
* @throws UncheckedIOException if an I/O exception occurs.
2111+
*
2112+
* @since 3.23.0
2113+
*/
2114+
default List<String> linesOf(final Path path, final Charset charset) {
2115+
return Assertions.linesOf(path, charset);
2116+
}
2117+
20702118
/**
20712119
* Sets whether we remove elements related to AssertJ from assertion error stack trace.
20722120
*

src/main/java/org/assertj/core/util/Files.java

+2-8
Original file line numberDiff line numberDiff line change
@@ -288,12 +288,7 @@ public static String contentOf(File file, Charset charset) {
288288
* @throws UncheckedIOException if an I/O exception occurs.
289289
*/
290290
public static List<String> linesOf(File file, Charset charset) {
291-
requireNonNull(charset, "The charset should not be null");
292-
try {
293-
return java.nio.file.Files.readAllLines(file.toPath(), charset);
294-
} catch (IOException e) {
295-
throw new UncheckedIOException("Unable to read " + file.getAbsolutePath(), e);
296-
}
291+
return Paths.linesOf(file.toPath(), charset);
297292
}
298293

299294
/**
@@ -307,8 +302,7 @@ public static List<String> linesOf(File file, Charset charset) {
307302
* @throws UncheckedIOException if an I/O exception occurs.
308303
*/
309304
public static List<String> linesOf(File file, String charsetName) {
310-
checkArgumentCharsetIsSupported(charsetName);
311-
return linesOf(file, Charset.forName(charsetName));
305+
return Paths.linesOf(file.toPath(), charsetName);
312306
}
313307

314308
private static void checkArgumentCharsetIsSupported(String charsetName) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
3+
* the License. You may obtain a copy of the License at
4+
*
5+
* http://www.apache.org/licenses/LICENSE-2.0
6+
*
7+
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
8+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
9+
* specific language governing permissions and limitations under the License.
10+
*
11+
* Copyright 2012-2022 the original author or authors.
12+
*/
13+
package org.assertj.core.util;
14+
15+
import static java.util.Objects.requireNonNull;
16+
import static org.assertj.core.util.Preconditions.checkArgument;
17+
18+
import java.io.IOException;
19+
import java.io.UncheckedIOException;
20+
import java.nio.charset.Charset;
21+
import java.nio.file.Files;
22+
import java.nio.file.Path;
23+
import java.util.List;
24+
25+
/**
26+
* Utility methods related to {@link Path}s.
27+
*
28+
* @author Stefan Bratanov
29+
*
30+
* @since 3.23.0
31+
*/
32+
public class Paths {
33+
34+
private Paths() {}
35+
36+
/**
37+
* 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
38+
* either \n, \r or \r\n.
39+
*
40+
* @param path the path.
41+
* @param charset the character set to use.
42+
* @return the content of the file at the given path.
43+
* @throws NullPointerException if the given charset is {@code null}.
44+
* @throws UncheckedIOException if an I/O exception occurs.
45+
*/
46+
public static List<String> linesOf(Path path, Charset charset) {
47+
requireNonNull(charset, "The charset should not be null");
48+
try {
49+
return Files.readAllLines(path, charset);
50+
} catch (IOException e) {
51+
throw new UncheckedIOException("Unable to read " + path, e);
52+
}
53+
}
54+
55+
/**
56+
* 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
57+
* either \n, \r or \r\n.
58+
*
59+
* @param path the path.
60+
* @param charsetName the name of the character set to use.
61+
* @return the content of the file at the given path.
62+
* @throws NullPointerException if the given charset is {@code null}.
63+
* @throws UncheckedIOException if an I/O exception occurs.
64+
*/
65+
public static List<String> linesOf(Path path, String charsetName) {
66+
checkArgumentCharsetIsSupported(charsetName);
67+
return linesOf(path, Charset.forName(charsetName));
68+
}
69+
70+
private static void checkArgumentCharsetIsSupported(String charsetName) {
71+
checkArgument(Charset.isSupported(charsetName), "Charset:<'%s'> is not supported on this system", charsetName);
72+
}
73+
}

src/test/java/org/assertj/core/api/Assertions_linesOf_Test.java

+9
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
import java.io.File;
2020
import java.nio.charset.StandardCharsets;
21+
import java.nio.file.Path;
22+
import java.nio.file.Paths;
2123
import java.util.List;
2224

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

38+
@Test
39+
void should_read_lines_of_path_with_UTF8_charset() {
40+
Path path = Paths.get("src", "test", "resources", "utf8.txt");
41+
assertThat(linesOf(path, "UTF-8")).isEqualTo(EXPECTED_CONTENT);
42+
assertThat(linesOf(path, StandardCharsets.UTF_8)).isEqualTo(EXPECTED_CONTENT);
43+
}
44+
3645
}

0 commit comments

Comments
 (0)