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 HttpOpener resources. #517

Merged
merged 3 commits into from
Jan 15, 2024
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
10 changes: 7 additions & 3 deletions metafacture-io/src/main/java/org/metafacture/io/HttpOpener.java
Original file line number Diff line number Diff line change
Expand Up @@ -295,12 +295,16 @@ public void process(final String input) {
final HttpURLConnection connection = requestBody != null ?
doOutput(urlToOpen, requestBody) : doRedirects(urlToOpen);

final InputStream inputStream = getInputStream(connection);
final String charset = getContentCharset(connection);

getReceiver().process(new InputStreamReader(
try (
InputStream inputStream = getInputStream(connection);
Reader reader = new InputStreamReader(
"gzip".equalsIgnoreCase(connection.getContentEncoding()) ?
new GZIPInputStream(inputStream) : inputStream, charset));
new GZIPInputStream(inputStream) : inputStream, charset)
) {
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 @@ -93,7 +93,7 @@ public void testOpenCompressedFiles() throws IOException {
Files.copy(in, file.toPath(), StandardCopyOption.REPLACE_EXISTING);
}

FileOpenerTest.assertData(receiver, DATA, file, o -> o.setCompression(compression));
TestHelpers.assertFile(receiver, DATA, file, o -> o.setCompression(compression));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
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;

Expand All @@ -40,7 +39,6 @@
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.function.Consumer;

/**
* Tests for class {@link FileOpener}.
Expand All @@ -66,7 +64,7 @@ public void testUtf8IsDefaultEncoding() throws IOException {
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()));

assertData(receiver, DATA, createTestFile(), null);
TestHelpers.assertFile(receiver, DATA, createTestFile(), null);
}

@Test
Expand All @@ -93,27 +91,10 @@ private void testDecompressConcatenated(final boolean decompressConcatenated) th
final String data = sb.toString();
Assert.assertTrue(data.length() + " > " + maxBytes, data.length() > maxBytes);

assertData(receiver, decompressConcatenated ? data : data.substring(0, maxBytes),
TestHelpers.assertFile(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();
if (consumer != null) {
consumer.accept(opener);
}

opener.setReceiver(receiver);
opener.process(file.getAbsolutePath());
opener.closeStream();

Mockito.verify(receiver).process(Mockito.any(Reader.class));
Assert.assertEquals(expected, sb.toString());
}

private File createTestFile() throws IOException {
final File file = tempFolder.newFile();
try (OutputStream stream = new FileOutputStream(file)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,12 @@

package org.metafacture.io;

import org.metafacture.commons.ResourceUtil;
import org.metafacture.framework.ObjectReceiver;

import com.github.tomakehurst.wiremock.client.MappingBuilder;
import com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder;
import com.github.tomakehurst.wiremock.client.WireMock;
import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
import com.github.tomakehurst.wiremock.http.HttpHeader;
import com.github.tomakehurst.wiremock.http.HttpHeaders;
import com.github.tomakehurst.wiremock.http.RequestMethod;
import com.github.tomakehurst.wiremock.junit.WireMockRule;
import com.github.tomakehurst.wiremock.matching.RequestPatternBuilder;
Expand All @@ -34,10 +31,7 @@
import org.junit.ComparisonFailure;
import org.junit.Rule;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;

Expand All @@ -49,8 +43,6 @@
import java.util.function.Consumer;
import java.util.zip.GZIPOutputStream;

import static org.mockito.Mockito.times;

/**
* Tests for class {@link HttpOpener}.
*
Expand Down Expand Up @@ -92,9 +84,6 @@ public final class HttpOpenerTest {
@Mock
private ObjectReceiver<Reader> receiver;

@Captor
private ArgumentCaptor<Reader> processedObject;

@Test
public void shouldPerformGetRequestWithInputAsUrlByDefault() throws IOException {
shouldPerformRequest(TEST_URL, HttpOpener.Method.GET, (o, u) -> {});
Expand Down Expand Up @@ -341,17 +330,16 @@ private void shouldPerformRequest(final String input, final HttpOpener.Method me

WireMock.stubFor(stub);

opener.process(String.format(input, baseUrl));
TestHelpers.assertReader(receiver, () -> {
opener.process(String.format(input, baseUrl));

// use the opener a second time in a workflow:
opener.process(String.format(input, baseUrl));
// use the opener a second time in a workflow:
opener.process(String.format(input, baseUrl));

opener.closeStream();
opener.closeStream();
}, responseBody, responseBody);

WireMock.verify(request);

Mockito.verify(receiver, times(2)).process(processedObject.capture());
Assert.assertEquals(responseBody, ResourceUtil.readAll(processedObject.getValue()));
}

}
58 changes: 58 additions & 0 deletions metafacture-io/src/test/java/org/metafacture/io/TestHelpers.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright 2024 hbz NRW
*
* Licensed under the Apache License, Version 2.0 the "License";
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.metafacture.io;

import org.metafacture.commons.ResourceUtil;
import org.metafacture.framework.ObjectReceiver;

import org.junit.Assert;
import org.mockito.Mockito;

import java.io.File;
import java.io.Reader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;

public final class TestHelpers {

public static void assertFile(final ObjectReceiver<Reader> receiver, final String expected, final File file, final Consumer<FileOpener> consumer) {
assertReader(receiver, () -> {
final FileOpener opener = new FileOpener();
if (consumer != null) {
consumer.accept(opener);
}

opener.setReceiver(receiver);
opener.process(file.getAbsolutePath());
opener.closeStream();
}, expected);
}

public static void assertReader(final ObjectReceiver<Reader> receiver, final Runnable runnable, final String... expected) {
final List<String> actual = new ArrayList<>();
Mockito.doAnswer(i -> actual.add(ResourceUtil.readAll(i.getArgument(0)))).when(receiver).process(Mockito.any(Reader.class));

runnable.run();

Mockito.verify(receiver, Mockito.times(expected.length)).process(Mockito.any(Reader.class));
Arrays.stream(expected).forEach(i -> Assert.assertEquals(i, actual.remove(0)));
Assert.assertEquals(0, actual.size());
}

}
Loading