From bd231ece921338c7c3625c74182cae3caac3a7ae Mon Sep 17 00:00:00 2001 From: Jens Wille Date: Mon, 18 Dec 2023 17:20:56 +0100 Subject: [PATCH 1/7] Flux: Close Flux file resources. --- .../src/main/java/org/metafacture/runner/Flux.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/metafacture-runner/src/main/java/org/metafacture/runner/Flux.java b/metafacture-runner/src/main/java/org/metafacture/runner/Flux.java index 9d889b450..5acdab6f4 100644 --- a/metafacture-runner/src/main/java/org/metafacture/runner/Flux.java +++ b/metafacture-runner/src/main/java/org/metafacture/runner/Flux.java @@ -25,6 +25,7 @@ import java.io.File; import java.io.IOException; +import java.io.InputStream; import java.util.HashMap; import java.util.Map; import java.util.regex.Matcher; @@ -82,7 +83,9 @@ public static void main(final String[] args) throws IOException, RecognitionExce } // run parser and builder - FluxCompiler.compile(ResourceUtil.getStream(fluxFile), vars).start(); + try (InputStream inputStream = ResourceUtil.getStream(fluxFile)) { + FluxCompiler.compile(inputStream, vars).start(); + } } } From 6f411b7955ddf58c3d811f5f82bcf4460ebe6dce Mon Sep 17 00:00:00 2001 From: Jens Wille Date: Mon, 18 Dec 2023 17:24:51 +0100 Subject: [PATCH 2/7] ResourceUtil: Close property file resources. --- .../src/main/java/org/metafacture/commons/ResourceUtil.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/metafacture-commons/src/main/java/org/metafacture/commons/ResourceUtil.java b/metafacture-commons/src/main/java/org/metafacture/commons/ResourceUtil.java index d803f0470..c4304bc4c 100644 --- a/metafacture-commons/src/main/java/org/metafacture/commons/ResourceUtil.java +++ b/metafacture-commons/src/main/java/org/metafacture/commons/ResourceUtil.java @@ -201,7 +201,9 @@ public static URL getUrl(final File file) throws MalformedURLException { */ public static Properties loadProperties(final String location) throws IOException { - return loadProperties(getStream(location)); + try (InputStream stream = getStream(location)) { + return loadProperties(stream); + } } /** From 05eff6806f926b15d927cef6b650695419e2e4ba Mon Sep 17 00:00:00 2001 From: Jens Wille Date: Mon, 18 Dec 2023 17:41:58 +0100 Subject: [PATCH 3/7] ResourceUtil: Close text file resources. --- .../org/metafacture/commons/ResourceUtil.java | 30 ++++++++----------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/metafacture-commons/src/main/java/org/metafacture/commons/ResourceUtil.java b/metafacture-commons/src/main/java/org/metafacture/commons/ResourceUtil.java index c4304bc4c..acdc88b03 100644 --- a/metafacture-commons/src/main/java/org/metafacture/commons/ResourceUtil.java +++ b/metafacture-commons/src/main/java/org/metafacture/commons/ResourceUtil.java @@ -30,6 +30,7 @@ import java.nio.charset.Charset; import java.util.List; import java.util.Properties; +import java.util.function.Consumer; /** * Various utility methods for working with files, resources and streams. @@ -241,14 +242,7 @@ public static Properties loadProperties(final URL url) throws IOException { */ public static String loadTextFile(final String location) throws IOException { final StringBuilder builder = new StringBuilder(); - final BufferedReader reader = new BufferedReader(getReader(location)); - - String line = reader.readLine(); - while (line != null) { - builder.append(line); - line = reader.readLine(); - } - + loadTextFile(location, builder::append); return builder.toString(); } @@ -260,17 +254,19 @@ public static String loadTextFile(final String location) throws IOException { * @return the List of Strings with the lines of the file appended * @throws IOException if an I/O error occurs */ - public static List loadTextFile(final String location, - final List list) throws IOException { - final BufferedReader reader = new BufferedReader(getReader(location)); + public static List loadTextFile(final String location, final List list) throws IOException { + loadTextFile(location, list::add); + return list; + } - String line = reader.readLine(); - while (line != null) { - list.add(line); - line = reader.readLine(); + private static void loadTextFile(final String location, final Consumer consumer) throws IOException { + try (BufferedReader reader = new BufferedReader(getReader(location))) { + String line = reader.readLine(); + while (line != null) { + consumer.accept(line); + line = reader.readLine(); + } } - - return list; } /** From f353d93e91f8e5368af60865d34d8e26b41bb8b6 Mon Sep 17 00:00:00 2001 From: Jens Wille Date: Mon, 18 Dec 2023 17:26:30 +0100 Subject: [PATCH 4/7] JScriptObjectPipe: Close script file resources. --- .../org/metafacture/scripting/JScriptObjectPipe.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/metafacture-scripting/src/main/java/org/metafacture/scripting/JScriptObjectPipe.java b/metafacture-scripting/src/main/java/org/metafacture/scripting/JScriptObjectPipe.java index 223e9cd11..063743f8c 100644 --- a/metafacture-scripting/src/main/java/org/metafacture/scripting/JScriptObjectPipe.java +++ b/metafacture-scripting/src/main/java/org/metafacture/scripting/JScriptObjectPipe.java @@ -25,7 +25,8 @@ import org.metafacture.framework.annotations.Out; import org.metafacture.framework.helpers.DefaultObjectPipe; -import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.Reader; import javax.script.Invocable; import javax.script.ScriptEngine; import javax.script.ScriptEngineManager; @@ -70,14 +71,14 @@ private void setScript(final String file) { final ScriptEngineManager manager = new ScriptEngineManager(); final ScriptEngine engine = manager.getEngineByName("JavaScript"); - try { + try (Reader reader = ResourceUtil.getReader(file)) { // LOG.info("loading code from '" + file + "'"); - engine.eval(ResourceUtil.getReader(file)); + engine.eval(reader); } catch (final ScriptException e) { throw new MetafactureException("Error in script", e); } - catch (final FileNotFoundException e) { + catch (final IOException e) { throw new MetafactureException("Error loading script '" + file + "'", e); } invocable = (Invocable) engine; From 2035caba6f51e54a287a05f6694319a8ece04c58 Mon Sep 17 00:00:00 2001 From: Jens Wille Date: Mon, 18 Dec 2023 17:32:47 +0100 Subject: [PATCH 5/7] Script: Close script file resources. --- .../org/metafacture/metamorph/functions/Script.java | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/metamorph/src/main/java/org/metafacture/metamorph/functions/Script.java b/metamorph/src/main/java/org/metafacture/metamorph/functions/Script.java index bded95b5d..98bd0315a 100644 --- a/metamorph/src/main/java/org/metafacture/metamorph/functions/Script.java +++ b/metamorph/src/main/java/org/metafacture/metamorph/functions/Script.java @@ -21,7 +21,8 @@ import org.metafacture.metamorph.api.MorphExecutionException; import org.metafacture.metamorph.api.helpers.AbstractSimpleStatelessFunction; -import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.Reader; import javax.script.Invocable; import javax.script.ScriptEngine; import javax.script.ScriptEngineManager; @@ -60,14 +61,14 @@ public void setInvoke(final String invoke) { public void setFile(final String file) { final ScriptEngineManager manager = new ScriptEngineManager(); final ScriptEngine engine = manager.getEngineByName("JavaScript"); - try { - // TODO: The script file should be loaded relatively to the base URI - engine.eval(ResourceUtil.getReader(file)); + // TODO: The script file should be loaded relatively to the base URI + try (Reader reader = ResourceUtil.getReader(file)) { + engine.eval(reader); } catch (final ScriptException e) { throw new MorphBuildException("Error in script", e); } - catch (final FileNotFoundException e) { + catch (final IOException e) { throw new MorphBuildException("Error loading script '" + file + "'", e); } invocable = (Invocable) engine; From e9c348ddcc94eed2723c59ccceb04c192dfdbe0d Mon Sep 17 00:00:00 2001 From: Jens Wille Date: Mon, 18 Dec 2023 17:39:47 +0100 Subject: [PATCH 6/7] ResourceOpener: Close input file resources. --- .../src/main/java/org/metafacture/io/ResourceOpener.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/metafacture-io/src/main/java/org/metafacture/io/ResourceOpener.java b/metafacture-io/src/main/java/org/metafacture/io/ResourceOpener.java index 4a2084eff..6d54e0b18 100644 --- a/metafacture-io/src/main/java/org/metafacture/io/ResourceOpener.java +++ b/metafacture-io/src/main/java/org/metafacture/io/ResourceOpener.java @@ -68,8 +68,8 @@ public void setEncoding(final String encoding) { @Override public void process(final String file) { - try { - getReceiver().process(ResourceUtil.getReader(file, encoding)); + try (Reader reader = ResourceUtil.getReader(file, encoding)) { + getReceiver().process(reader); } catch (final IOException e) { throw new MetafactureException(e); From 478cc737109eaf304a9fc742b232bfc748495281 Mon Sep 17 00:00:00 2001 From: Jens Wille Date: Mon, 18 Dec 2023 17:41:52 +0100 Subject: [PATCH 7/7] FileOpener: Close input file resources. --- .../java/org/metafacture/io/FileOpener.java | 4 +- .../io/FileOpenerCompressionTest.java | 54 ++++----------- .../org/metafacture/io/FileOpenerTest.java | 68 +++++++++---------- 3 files changed, 46 insertions(+), 80 deletions(-) diff --git a/metafacture-io/src/main/java/org/metafacture/io/FileOpener.java b/metafacture-io/src/main/java/org/metafacture/io/FileOpener.java index b88b9aabd..a2a8eeac7 100644 --- a/metafacture-io/src/main/java/org/metafacture/io/FileOpener.java +++ b/metafacture-io/src/main/java/org/metafacture/io/FileOpener.java @@ -154,8 +154,8 @@ public Reader open(final InputStream stream) throws IOException { @Override public void process(final String file) { - try { - getReceiver().process(open(file)); + try (Reader reader = open(file)) { + getReceiver().process(reader); } catch (final IOException e) { throw new MetafactureException(e); diff --git a/metafacture-io/src/test/java/org/metafacture/io/FileOpenerCompressionTest.java b/metafacture-io/src/test/java/org/metafacture/io/FileOpenerCompressionTest.java index ac665fe13..83489531a 100644 --- a/metafacture-io/src/test/java/org/metafacture/io/FileOpenerCompressionTest.java +++ b/metafacture-io/src/test/java/org/metafacture/io/FileOpenerCompressionTest.java @@ -16,31 +16,26 @@ package org.metafacture.io; -import static org.junit.Assert.assertEquals; -import static org.mockito.Mockito.verify; - -import java.io.File; -import java.io.IOException; -import java.io.InputStream; -import java.io.Reader; -import java.nio.file.Files; -import java.nio.file.StandardCopyOption; -import java.util.Arrays; +import org.metafacture.framework.ObjectReceiver; -import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import org.junit.runners.Parameterized.Parameters; -import org.metafacture.commons.ResourceUtil; -import org.metafacture.framework.ObjectReceiver; -import org.mockito.ArgumentCaptor; import org.mockito.Mock; import org.mockito.junit.MockitoJUnit; import org.mockito.junit.MockitoRule; +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.io.Reader; +import java.nio.file.Files; +import java.nio.file.StandardCopyOption; +import java.util.Arrays; + /** * Tests for file compression in class {@link FileOpener}. * @@ -62,13 +57,10 @@ public final class FileOpenerCompressionTest { @Mock private ObjectReceiver receiver; - private FileOpener fileOpener; - private final String resourcePath; private final FileCompression compression; - public FileOpenerCompressionTest(final String resourcePath, - final FileCompression compression) { + public FileOpenerCompressionTest(final String resourcePath, final FileCompression compression) { this.resourcePath = resourcePath; this.compression = compression; } @@ -93,35 +85,15 @@ public static Iterable data() { }); } - @Before - public void setup() { - fileOpener = new FileOpener(); - fileOpener.setReceiver(receiver); - } - @Test public void testOpenCompressedFiles() throws IOException { - final File file = copyResourceToTempFile(); - - fileOpener.setCompression(compression); - fileOpener.process(file.getAbsolutePath()); - - final ArgumentCaptor readerCaptor = - ArgumentCaptor.forClass(Reader.class); - verify(receiver).process(readerCaptor.capture()); - final String charsFromFile; - try (Reader reader = readerCaptor.getValue()) { - charsFromFile = ResourceUtil.readAll(reader); - } - assertEquals(DATA, charsFromFile); - } - - private File copyResourceToTempFile() throws IOException { final File file = tempFolder.newFile(); + try (InputStream in = getClass().getResourceAsStream(resourcePath)) { Files.copy(in, file.toPath(), StandardCopyOption.REPLACE_EXISTING); } - return file; + + FileOpenerTest.assertData(receiver, DATA, file, o -> o.setCompression(compression)); } } diff --git a/metafacture-io/src/test/java/org/metafacture/io/FileOpenerTest.java b/metafacture-io/src/test/java/org/metafacture/io/FileOpenerTest.java index ad834b544..a59ba5db0 100644 --- a/metafacture-io/src/test/java/org/metafacture/io/FileOpenerTest.java +++ b/metafacture-io/src/test/java/org/metafacture/io/FileOpenerTest.java @@ -16,10 +16,18 @@ package org.metafacture.io; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; -import static org.junit.Assume.assumeFalse; -import static org.mockito.Mockito.verify; +import org.metafacture.commons.ResourceUtil; +import org.metafacture.framework.ObjectReceiver; + +import org.junit.Assert; +import org.junit.Assume; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.junit.MockitoJUnit; +import org.mockito.junit.MockitoRule; import java.io.File; import java.io.FileOutputStream; @@ -32,17 +40,7 @@ import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.StandardCopyOption; - -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.TemporaryFolder; -import org.metafacture.commons.ResourceUtil; -import org.metafacture.framework.ObjectReceiver; -import org.mockito.ArgumentCaptor; -import org.mockito.Captor; -import org.mockito.Mock; -import org.mockito.junit.MockitoJUnit; -import org.mockito.junit.MockitoRule; +import java.util.function.Consumer; /** * Tests for class {@link FileOpener}. @@ -63,24 +61,12 @@ public final class FileOpenerTest { @Mock private ObjectReceiver receiver; - @Captor - private ArgumentCaptor processedObject; - @Test public void testUtf8IsDefaultEncoding() throws IOException { - assumeFalse("Default encoding is UTF-8: It is not possible to test whether " + - "FileOpener sets the encoding to UTF-8 correctly.", - StandardCharsets.UTF_8.equals(Charset.defaultCharset())); - - final File testFile = createTestFile(); - - final FileOpener opener = new FileOpener(); - opener.setReceiver(receiver); - opener.process(testFile.getAbsolutePath()); - opener.closeStream(); + Assume.assumeFalse("Default encoding is UTF-8: It is not possible to test whether FileOpener sets " + + "the encoding to UTF-8 correctly.", StandardCharsets.UTF_8.equals(Charset.defaultCharset())); - verify(receiver).process(processedObject.capture()); - assertEquals(DATA, ResourceUtil.readAll(processedObject.getValue())); + assertData(receiver, DATA, createTestFile(), null); } @Test @@ -105,19 +91,27 @@ private void testDecompressConcatenated(final boolean decompressConcatenated) th } final String data = sb.toString(); - assertTrue(data.length() + " > " + maxBytes, data.length() > maxBytes); + Assert.assertTrue(data.length() + " > " + maxBytes, data.length() > maxBytes); - final File testFile = copyResourceToTempFile("compressed-large.txt.bgzf"); + assertData(receiver, decompressConcatenated ? data : data.substring(0, maxBytes), + copyResourceToTempFile("compressed-large.txt.bgzf"), o -> o.setDecompressConcatenated(decompressConcatenated)); + } + + /*package-private*/ static void assertData(final ObjectReceiver receiver, final String expected, final File file, final Consumer consumer) { + final StringBuilder sb = new StringBuilder(); + Mockito.doAnswer(i -> sb.append(ResourceUtil.readAll(i.getArgument(0)))).when(receiver).process(Mockito.any(Reader.class)); final FileOpener opener = new FileOpener(); - opener.setDecompressConcatenated(decompressConcatenated); + if (consumer != null) { + consumer.accept(opener); + } + opener.setReceiver(receiver); - opener.process(testFile.getAbsolutePath()); + opener.process(file.getAbsolutePath()); opener.closeStream(); - verify(receiver).process(processedObject.capture()); - assertEquals(decompressConcatenated ? data : data.substring(0, maxBytes), - ResourceUtil.readAll(processedObject.getValue())); + Mockito.verify(receiver).process(Mockito.any(Reader.class)); + Assert.assertEquals(expected, sb.toString()); } private File createTestFile() throws IOException {