diff --git a/.gitattributes b/.gitattributes index bcaca0269..db61f6488 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1 +1,16 @@ -gradlew text eol=lf +# Normalize all text files to LF in the repository (checkout follows the platform). +* text=auto + +# Scripts that must keep a specific line ending regardless of platform. +gradlew text eol=lf +*.sh text eol=lf +*.bat text eol=crlf +*.cmd text eol=crlf + +# Binary assets — never touch line endings. +*.rar binary +*.jar binary +*.png binary +*.jpg binary +*.gif binary +*.ico binary diff --git a/.gitignore b/.gitignore index 7f8c9de71..b542a5c89 100644 --- a/.gitignore +++ b/.gitignore @@ -7,9 +7,10 @@ examples/**/Z*.java **/examples/z/** **/examples/testapp_mc/** **/Debug*.java +**/TestDebugger* .claude/** +.editorconfig env.bat -**/TestDebugger* # Compiled source # ################### diff --git a/src/test/java/io/nats/client/utils/ResourceUtils.java b/src/test/java/io/nats/client/utils/ResourceUtils.java index 440225009..1f8c2f573 100644 --- a/src/test/java/io/nats/client/utils/ResourceUtils.java +++ b/src/test/java/io/nats/client/utils/ResourceUtils.java @@ -4,9 +4,9 @@ import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; +import java.util.ArrayList; import java.util.List; -@SuppressWarnings("DataFlowIssue") public abstract class ResourceUtils { public static List dataAsLines(String fileName) { return resourceAsLines("data/" + fileName); @@ -21,37 +21,49 @@ public static InputStream dataAsInputStream(String fileName) { } public static List resourceAsLines(String fileName) { - try { - ClassLoader classLoader = ResourceUtils.class.getClassLoader(); - File file = new File(classLoader.getResource(fileName).getFile()); - return Files.readAllLines(file.toPath()); + try (BufferedReader r = new BufferedReader( + new InputStreamReader(open(fileName), StandardCharsets.UTF_8))) { + List lines = new ArrayList<>(); + for (String l; (l = r.readLine()) != null; ) lines.add(l); + return lines; } - catch (Exception e) { + catch (IOException e) { throw new RuntimeException(e); } - } public static String resourceAsString(String fileName) { - try { - ClassLoader classLoader = ResourceUtils.class.getClassLoader(); - File file = new File(classLoader.getResource(fileName).getFile()); - return new String(Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8); + try (InputStream in = open(fileName)) { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + byte[] buffer = new byte[8192]; + int len; + while ((len = in.read(buffer)) != -1) { + out.write(buffer, 0, len); + } + return new String(out.toByteArray(), StandardCharsets.UTF_8); } - catch (Exception e) { + catch (IOException e) { throw new RuntimeException(e); } } public static InputStream resourceAsInputStream(String fileName) { try { - return ResourceUtils.class.getClassLoader().getResourceAsStream(fileName); + return open(fileName); } - catch (Exception e) { + catch (IOException e) { throw new RuntimeException(e); } } + private static InputStream open(String fileName) throws FileNotFoundException { + InputStream in = ResourceUtils.class.getClassLoader().getResourceAsStream(fileName); + if (in == null) { + throw new FileNotFoundException(fileName); + } + return in; + } + public static String createTempFile(String prefix, String suffix, String[] lines) throws IOException { File f = File.createTempFile(prefix, suffix); BufferedWriter writer = new BufferedWriter(new FileWriter(f)); diff --git a/src/test/java/io/nats/client/utils/ResourceUtilsTests.java b/src/test/java/io/nats/client/utils/ResourceUtilsTests.java new file mode 100644 index 000000000..0e7b2dddf --- /dev/null +++ b/src/test/java/io/nats/client/utils/ResourceUtilsTests.java @@ -0,0 +1,33 @@ +package io.nats.client.utils; + +import org.junit.jupiter.api.Test; + +import java.io.FileNotFoundException; + +import static io.nats.client.utils.ResourceUtils.*; +import static org.junit.jupiter.api.Assertions.*; + +public class ResourceUtilsTests { + + private static final String MISSING = "ThisResourceDoesNotExist.json"; + + @Test + public void testMissingResourceIdentifiesTheFile() { + assertMissing(assertThrows(RuntimeException.class, () -> dataAsString(MISSING))); + assertMissing(assertThrows(RuntimeException.class, () -> dataAsLines(MISSING))); + assertMissing(assertThrows(RuntimeException.class, () -> dataAsInputStream(MISSING))); + } + + private void assertMissing(RuntimeException e) { + Throwable cause = e.getCause(); + assertInstanceOf(FileNotFoundException.class, cause); + assertTrue(cause.getMessage().contains(MISSING)); + } + + @Test + public void testResourceStillLoads() { + assertTrue(dataAsString("StreamConfiguration.json").contains("retention")); + assertFalse(dataAsLines("StreamConfiguration.json").isEmpty()); + assertNotNull(dataAsInputStream("StreamConfiguration.json")); + } +}