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

Close resources. #514

Merged
merged 7 commits into from
Dec 21, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -201,7 +202,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);
}
}

/**
Expand Down Expand Up @@ -239,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();
}

Expand All @@ -258,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<String> loadTextFile(final String location,
final List<String> list) throws IOException {
final BufferedReader reader = new BufferedReader(getReader(location));
public static List<String> loadTextFile(final String location, final List<String> 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<String> 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;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}.
*
Expand All @@ -62,13 +57,10 @@ public final class FileOpenerCompressionTest {
@Mock
private ObjectReceiver<Reader> 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;
}
Expand All @@ -93,35 +85,15 @@ public static Iterable<Object[]> 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<Reader> 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));
}

}
68 changes: 31 additions & 37 deletions metafacture-io/src/test/java/org/metafacture/io/FileOpenerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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}.
Expand All @@ -63,24 +61,12 @@ public final class FileOpenerTest {
@Mock
private ObjectReceiver<Reader> receiver;

@Captor
private ArgumentCaptor<Reader> 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
Expand All @@ -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<Reader> receiver, final String expected, final File file, final Consumer<FileOpener> 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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
Loading