From ba4f2d1e3cfbb16cc0a1ebc922189241997850b0 Mon Sep 17 00:00:00 2001 From: Richard Eckart de Castilho Date: Sat, 3 Aug 2024 19:42:34 -0300 Subject: [PATCH] No issue: Fix more resource leaks in tests --- .github/workflows/maven.yml | 2 +- .../dkpro/core/io/imscwb/ImsCwbWriter.java | 72 +++++++++---------- .../org/dkpro/core/testing/WriterAssert.java | 12 ++-- 3 files changed, 38 insertions(+), 48 deletions(-) diff --git a/.github/workflows/maven.yml b/.github/workflows/maven.yml index 5789dca5e1..3e35ccee23 100644 --- a/.github/workflows/maven.yml +++ b/.github/workflows/maven.yml @@ -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: | diff --git a/dkpro-core-io-imscwb-asl/src/main/java/org/dkpro/core/io/imscwb/ImsCwbWriter.java b/dkpro-core-io-imscwb-asl/src/main/java/org/dkpro/core/io/imscwb/ImsCwbWriter.java index 953a21cf72..c09cf7c7ee 100644 --- a/dkpro-core-io-imscwb-asl/src/main/java/org/dkpro/core/io/imscwb/ImsCwbWriter.java +++ b/dkpro-core-io-imscwb-asl/src/main/java/org/dkpro/core/io/imscwb/ImsCwbWriter.java @@ -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; @@ -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 { @@ -107,17 +104,15 @@ public class ImsCwbWriter * Specify the suffix of output files. Default value .vrt. 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; /** @@ -230,10 +225,11 @@ 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); @@ -241,8 +237,7 @@ public void initialize(UimaContext context) } @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(); @@ -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); @@ -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(" 1 ? segments[1] : ""; - String name = (substringAfterLast(typeName, ".") + "_" + featureName) - .toLowerCase(); + String name = (substringAfterLast(typeName, ".") + "_" + featureName).toLowerCase(); cmd.add("-P"); cmd.add(name); } @@ -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(); @@ -539,6 +531,8 @@ public void collectionProcessComplete() } } } + + super.collectionProcessComplete(); } private void runCwbCommand(String aCommand, String... aArguments) @@ -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); } diff --git a/dkpro-core-testing-asl/src/main/java/org/dkpro/core/testing/WriterAssert.java b/dkpro-core-testing-asl/src/main/java/org/dkpro/core/testing/WriterAssert.java index 89f80c8dbb..0b23e3e448 100644 --- a/dkpro-core-testing-asl/src/main/java/org/dkpro/core/testing/WriterAssert.java +++ b/dkpro-core-testing-asl/src/main/java/org/dkpro/core/testing/WriterAssert.java @@ -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; @@ -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; @@ -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()) { @@ -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)); } /**