edited TestUtils#readResource to read the file "as-is" (i.e. character by character). If it is important for you to define the line endings used, provide a LineEnding as a second parameter else use TestUtils#readResourceUsingSystemEol
diff --git a/javaparser-core-testing/src/test/java/com/github/javaparser/utils/TestUtils.java b/javaparser-core-testing/src/test/java/com/github/javaparser/utils/TestUtils.java
index 4b20464..2e0613b 100644
--- a/javaparser-core-testing/src/test/java/com/github/javaparser/utils/TestUtils.java
+++ b/javaparser-core-testing/src/test/java/com/github/javaparser/utils/TestUtils.java
@@ -53,15 +53,58 @@
import static com.github.javaparser.ParserConfiguration.LanguageLevel.JAVA_9;
import static com.github.javaparser.Providers.provider;
import static com.github.javaparser.utils.CodeGenerationUtils.f;
-import static com.github.javaparser.utils.Utils.EOL;
import static com.github.javaparser.utils.Utils.normalizeEolInTextBlock;
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.Arrays.asList;
import static org.junit.jupiter.api.Assertions.*;
public class TestUtils {
+
/**
+ * Read the resource's contents line-by-line, and use the <strong>system's line separator</strong> to separate lines.
* Takes care of setting all the end of line character to platform specific ones.
+ * <br>
+ * <br>If you wish to read the file as-is, use {@link #readResource(String)} which reads the file stream character-by-character.
+ */
+ public static String readResourceUsingSystemEol(String resourceName) {
+ return readResource(resourceName, LineEnding.SYSTEM);
+ }
+
+ /**
+ * Read the resource's contents line-by-line, and use the <strong>given line separator</strong> to separate lines.
+ * <br>
+ * <br>If you wish to read the file as-is, use {@link #readResource(String)} which reads the file stream character-by-character.
+ */
+ public static String readResource(String resourceName, LineEnding lineEnding) {
+ if (resourceName.startsWith("/")) {
+ resourceName = resourceName.substring(1);
+ }
+ try (final InputStream resourceAsStream = TestUtils.class.getClassLoader().getResourceAsStream(resourceName)) {
+ if (resourceAsStream == null) {
+ fail("resource not found by name: " + resourceName);
+ }
+ try (final InputStreamReader reader = new InputStreamReader(resourceAsStream, UTF_8);
+ final BufferedReader br = new BufferedReader(reader)) {
+ final StringBuilder builder = new StringBuilder(4096);
+ String line;
+ while ((line = br.readLine()) != null) {
+ builder.append(line).append(lineEnding.toString());
+ }
+ return builder.toString();
+ }
+ } catch (IOException e) {
+ fail(e);
+ return null;
+ }
+ }
+
+
+ /**
+ * Read the resource's contents as-is.
+ * <br>
+ * <br>If you wish to specify the line endings,
+ * use {@link #readResourceUsingSystemEol(String)}
+ * or {@link #readResource(String, LineEnding)}
*/
public static String readResource(String resourceName) {
if (resourceName.startsWith("/")) {
@@ -72,11 +115,13 @@
fail("not found: " + resourceName);
}
try (final InputStreamReader reader = new InputStreamReader(resourceAsStream, UTF_8);
- final BufferedReader br = new BufferedReader(reader)) {
- final StringBuilder builder = new StringBuilder();
- String line;
- while ((line = br.readLine()) != null) {
- builder.append(line).append(EOL);
+ final BufferedReader br = new BufferedReader(reader)
+ ) {
+ // Switched to reading char-by-char as opposed to line-by-line.
+ // This helps to retain the resource's own line endings.
+ final StringBuilder builder = new StringBuilder(4096);
+ for (int c = br.read(); c != -1; c = br.read()) {
+ builder.append((char) c);
}
return builder.toString();
}
@@ -90,7 +135,7 @@
* Use this assertion if line endings are important, otherwise use {@link #assertEqualToTextResourceNoEol(String, String)}
*/
public static void assertEqualToTextResource(String resourceName, String actual) {
- String expected = readResource(resourceName);
+ String expected = readResourceUsingSystemEol(resourceName);
// First test equality ignoring EOL chars
assertEqualsNoEol(expected, actual);
@@ -111,7 +156,7 @@
* If line endings are important, use {@link #assertEqualToTextResource(String, String)}
*/
public static void assertEqualToTextResourceNoEol(String resourceName, String actual) {
- String expected = readResource(resourceName);
+ String expected = readResourceUsingSystemEol(resourceName);
assertEquals(expected, actual, "failed due to line separator differences");
}
@@ -189,11 +234,11 @@
if (actual.contains(e)) {
actual.remove(e);
} else {
- out.append("Missing: ").append(e).append(EOL);
+ out.append("Missing: ").append(e).append(LineEnding.SYSTEM);
}
}
for (Object a : actual) {
- out.append("Unexpected: ").append(a).append(EOL);
+ out.append("Unexpected: ").append(a).append(LineEnding.SYSTEM);
}
String s = out.toString();
@@ -227,13 +272,13 @@
* Assert that "actual" equals "expected", and that any EOL characters in "actual" are correct for the platform.
*/
public static void assertEqualsNoEol(String expected, String actual) {
- assertEquals(normalizeEolInTextBlock(expected, EOL), normalizeEolInTextBlock(actual, EOL));
+ assertEquals(normalizeEolInTextBlock(expected, LineEnding.ARBITRARY), normalizeEolInTextBlock(actual, LineEnding.ARBITRARY));
}
/**
* Assert that "actual" equals "expected", and that any EOL characters in "actual" are correct for the platform.
*/
public static void assertEqualsNoEol(String expected, String actual, String message) {
- assertEquals(normalizeEolInTextBlock(expected, EOL), normalizeEolInTextBlock(actual, EOL), message);
+ assertEquals(normalizeEolInTextBlock(expected, LineEnding.ARBITRARY), normalizeEolInTextBlock(actual, LineEnding.ARBITRARY), message);
}
}