Skip to content

Commit

Permalink
No issue: Fix more resource leaks in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
reckart committed Aug 3, 2024
1 parent 3a37f0a commit ba4f2d1
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 48 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/maven.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ jobs:
${{ runner.os }}-maven-
- name: Cache DKPro Core dataset repository
id: maven-cache
id: dataset-cache
uses: actions/cache@v4
with:
path: |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.ArrayList;
Expand Down Expand Up @@ -78,17 +79,13 @@
*/
@ResourceMetaData(name = "IMS CWB Writer")
@DocumentationResource("${docbase}/format-reference.html#format-${command}")
@Parameters(
exclude = {
ImsCwbWriter.PARAM_TARGET_LOCATION })
@MimeTypeCapability({MimeTypes.TEXT_X_IMSCWB})
@TypeCapability(
inputs = {
"de.tudarmstadt.ukp.dkpro.core.api.metadata.type.DocumentMetaData",
"de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Sentence",
"de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Token",
"de.tudarmstadt.ukp.dkpro.core.api.lexmorph.type.pos.POS",
"de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Lemma" })
@Parameters(exclude = { ImsCwbWriter.PARAM_TARGET_LOCATION })
@MimeTypeCapability({ MimeTypes.TEXT_X_IMSCWB })
@TypeCapability(inputs = { "de.tudarmstadt.ukp.dkpro.core.api.metadata.type.DocumentMetaData",
"de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Sentence",
"de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Token",
"de.tudarmstadt.ukp.dkpro.core.api.lexmorph.type.pos.POS",
"de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Lemma" })
public class ImsCwbWriter
extends JCasFileWriter_ImplBase
{
Expand All @@ -107,17 +104,15 @@ public class ImsCwbWriter
* Specify the suffix of output files. Default value <code>.vrt</code>. If the suffix is not
* needed, provide an empty string as value.
*/
public static final String PARAM_FILENAME_EXTENSION =
ComponentParameters.PARAM_FILENAME_EXTENSION;
public static final String PARAM_FILENAME_EXTENSION = ComponentParameters.PARAM_FILENAME_EXTENSION;
@ConfigurationParameter(name = PARAM_FILENAME_EXTENSION, mandatory = true, defaultValue = ".vrt")
private String filenameSuffix;

/**
* Character encoding of the output data.
*/
public static final String PARAM_TARGET_ENCODING = ComponentParameters.PARAM_TARGET_ENCODING;
@ConfigurationParameter(name = PARAM_TARGET_ENCODING, mandatory = true,
defaultValue = DEFAULT_ENCODING)
@ConfigurationParameter(name = PARAM_TARGET_ENCODING, mandatory = true, defaultValue = DEFAULT_ENCODING)
private String encoding;

/**
Expand Down Expand Up @@ -230,19 +225,19 @@ public class ImsCwbWriter
private Process childProcess;
private File dataDirectory;
private File registryDirectory;

private OutputStream targetStream;

@Override
public void initialize(UimaContext context)
throws ResourceInitializationException
public void initialize(UimaContext context) throws ResourceInitializationException
{
super.initialize(context);

currentId = 0;
}

@Override
public void process(JCas jcas)
throws AnalysisEngineProcessException
public void process(JCas jcas) throws AnalysisEngineProcessException
{
String documentId = DocumentMetaData.get(jcas).getDocumentId();
String documentUri = DocumentMetaData.get(jcas).getDocumentUri();
Expand All @@ -264,7 +259,7 @@ public void process(JCas jcas)
}
}

try (BufferedWriter out = new BufferedWriter(
try (var out = new BufferedWriter(
new OutputStreamWriter(getOutputStream(jcas, filenameSuffix), encoding))) {
if (writeTextTag) {
startElement(out, E_TEXT, ATTR_ID, documentId);
Expand Down Expand Up @@ -352,8 +347,7 @@ private void startElement(Writer aOut, String aElement, String... aAttributes)
aOut.write(LS);
}

private void endElement(Writer aOut, String aElement)
throws IOException
private void endElement(Writer aOut, String aElement) throws IOException
{
aOut.write("</");
aOut.write(aElement);
Expand All @@ -362,8 +356,7 @@ private void endElement(Writer aOut, String aElement)

}

private void field(Writer aOut, String aValue)
throws IOException
private void field(Writer aOut, String aValue) throws IOException
{
aOut.write(TAB);
aOut.write(escapeXml(aValue));
Expand All @@ -378,13 +371,14 @@ protected NamedOutputStream getOutputStream(JCas aJCas, String aExtension) throw
if (childProcess == null) {
startCqpProcess();
}

return new NamedOutputStream(null,
new CloseShieldOutputStream(childProcess.getOutputStream()));
}

return super.getOutputStream(aJCas, aExtension);
}

private void startCqpProcess() throws IOException
{
dataDirectory = new File(getTargetLocation(), "data");
Expand Down Expand Up @@ -447,8 +441,7 @@ private void startCqpProcess() throws IOException
}
String typeName = segments[0];
String featureName = segments.length > 1 ? segments[1] : "";
String name = (substringAfterLast(typeName, ".") + "_" + featureName)
.toLowerCase();
String name = (substringAfterLast(typeName, ".") + "_" + featureName).toLowerCase();
cmd.add("-P");
cmd.add(name);
}
Expand Down Expand Up @@ -500,12 +493,11 @@ private void attendChildProceess()
}

@Override
public void collectionProcessComplete()
throws AnalysisEngineProcessException
public void collectionProcessComplete() throws AnalysisEngineProcessException
{
if (childProcess != null) {
IOUtils.closeQuietly(childProcess.getOutputStream());

try {
childProcess.waitFor();
attendChildProceess();
Expand Down Expand Up @@ -539,6 +531,8 @@ public void collectionProcessComplete()
}
}
}

super.collectionProcessComplete();
}

private void runCwbCommand(String aCommand, String... aArguments)
Expand Down Expand Up @@ -622,20 +616,18 @@ public String getCoveredAnnotationFeatureValue(String aFeaturePath,
switch (covered.size()) {
case 0:
if (getLogger().isWarnEnabled()) {
getLogger().warn(
"There is no annotation of type [" + typeName
+ "] available which is covered by [" + aCoveringAnnotation
+ "], returning empty string.");
getLogger().warn("There is no annotation of type [" + typeName
+ "] available which is covered by [" + aCoveringAnnotation
+ "], returning empty string.");
}
return "";
case 1:
return covered.get(0).getFeatureValueAsString(feature);
default:
if (getLogger().isWarnEnabled()) {
getLogger().warn(
"There are multiple annotations of type [" + typeName
+ "] available which are covered by [" + aCoveringAnnotation
+ "], returning the first.");
getLogger().warn("There are multiple annotations of type [" + typeName
+ "] available which are covered by [" + aCoveringAnnotation
+ "], returning the first.");
}
return covered.get(0).getFeatureValueAsString(feature);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/
package org.dkpro.core.testing;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.apache.uima.fit.factory.AnalysisEngineFactory.createEngine;
import static org.apache.uima.fit.factory.AnalysisEngineFactory.createEngineDescription;
import static org.apache.uima.fit.factory.ConfigurationParameterFactory.canParameterBeSet;
Expand All @@ -25,12 +26,9 @@
import static org.dkpro.core.api.parameter.ComponentParameters.PARAM_TARGET_LOCATION;

import java.io.File;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.apache.uima.analysis_component.AnalysisComponent;
import org.apache.uima.analysis_engine.AnalysisEngine;
import org.apache.uima.analysis_engine.AnalysisEngineDescription;
Expand Down Expand Up @@ -314,9 +312,9 @@ public StringAssert outputAsString(String aPathSuffix)
}

if (aPathSuffix != null) {
files = files.stream()
.filter(file -> file.getPath().endsWith(aPathSuffix))
.collect(Collectors.toList());
files = files.stream() //
.filter(file -> file.getPath().endsWith(aPathSuffix)) //
.toList();
}

if (files.isEmpty()) {
Expand Down Expand Up @@ -344,7 +342,7 @@ public StringAssert outputAsString(String aPathSuffix)
}
}

return new StringAssert(Files.contentOf(files.get(0), StandardCharsets.UTF_8));
return new StringAssert(Files.contentOf(files.get(0), UTF_8));
}

/**
Expand Down

0 comments on commit ba4f2d1

Please sign in to comment.