[
https://issues.apache.org/jira/browse/OPENNLP-1171?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16297764#comment-16297764
]
ASF GitHub Bot commented on OPENNLP-1171:
-----------------------------------------
kojisekig closed pull request #298: OPENNLP-1171: some tests create temp files
and directories but never …
URL: https://github.com/apache/opennlp/pull/298
This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:
As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):
diff --git
a/opennlp-morfologik-addon/src/test/java/opennlp/morfologik/builder/POSDictionayBuilderTest.java
b/opennlp-morfologik-addon/src/test/java/opennlp/morfologik/builder/POSDictionayBuilderTest.java
index f53ba247f..93bb3fa66 100644
---
a/opennlp-morfologik-addon/src/test/java/opennlp/morfologik/builder/POSDictionayBuilderTest.java
+++
b/opennlp-morfologik-addon/src/test/java/opennlp/morfologik/builder/POSDictionayBuilderTest.java
@@ -36,7 +36,9 @@
public static Path createMorfologikDictionary() throws Exception {
Path tabFilePath = File.createTempFile(
POSDictionayBuilderTest.class.getName(), ".txt").toPath();
+ tabFilePath.toFile().deleteOnExit();
Path infoFilePath =
DictionaryMetadata.getExpectedMetadataLocation(tabFilePath);
+ infoFilePath.toFile().deleteOnExit();
Files.copy(POSDictionayBuilderTest.class.getResourceAsStream(
"/dictionaryWithLemma.txt"), tabFilePath,
StandardCopyOption.REPLACE_EXISTING);
@@ -74,6 +76,7 @@ public void testBuildDictionary() throws Exception {
Path output = createMorfologikDictionary();
MorfologikLemmatizer ml = new MorfologikLemmatizer(output);
Assert.assertNotNull(ml);
+ output.toFile().deleteOnExit();
}
}
diff --git
a/opennlp-morfologik-addon/src/test/java/opennlp/morfologik/lemmatizer/MorfologikLemmatizerTest.java
b/opennlp-morfologik-addon/src/test/java/opennlp/morfologik/lemmatizer/MorfologikLemmatizerTest.java
index 426e39494..f522c5660 100644
---
a/opennlp-morfologik-addon/src/test/java/opennlp/morfologik/lemmatizer/MorfologikLemmatizerTest.java
+++
b/opennlp-morfologik-addon/src/test/java/opennlp/morfologik/lemmatizer/MorfologikLemmatizerTest.java
@@ -62,6 +62,7 @@ public void testLemmatizeMultiLemma() throws Exception {
private MorfologikLemmatizer createDictionary(boolean caseSensitive)
throws Exception {
Path output = POSDictionayBuilderTest.createMorfologikDictionary();
+ output.toFile().deleteOnExit();
return new MorfologikLemmatizer(output);
}
diff --git
a/opennlp-morfologik-addon/src/test/java/opennlp/morfologik/tagdict/MorfologikTagDictionaryTest.java
b/opennlp-morfologik-addon/src/test/java/opennlp/morfologik/tagdict/MorfologikTagDictionaryTest.java
index b1c071e89..c367d3f72 100644
---
a/opennlp-morfologik-addon/src/test/java/opennlp/morfologik/tagdict/MorfologikTagDictionaryTest.java
+++
b/opennlp-morfologik-addon/src/test/java/opennlp/morfologik/tagdict/MorfologikTagDictionaryTest.java
@@ -17,6 +17,7 @@
package opennlp.morfologik.tagdict;
+import java.nio.file.Path;
import java.util.Arrays;
import java.util.List;
@@ -82,7 +83,9 @@ private MorfologikTagDictionary createDictionary(boolean
caseSensitive)
private MorfologikTagDictionary createDictionary(boolean caseSensitive,
List<String> constant) throws Exception {
- Dictionary dic =
Dictionary.read(POSDictionayBuilderTest.createMorfologikDictionary());
+ Path output = POSDictionayBuilderTest.createMorfologikDictionary();
+ output.toFile().deleteOnExit();
+ Dictionary dic = Dictionary.read(output);
return new MorfologikTagDictionary(dic, caseSensitive);
}
diff --git
a/opennlp-morfologik-addon/src/test/java/opennlp/morfologik/tagdict/POSTaggerFactoryTest.java
b/opennlp-morfologik-addon/src/test/java/opennlp/morfologik/tagdict/POSTaggerFactoryTest.java
index 9ce22d28e..a5b74ce79 100644
---
a/opennlp-morfologik-addon/src/test/java/opennlp/morfologik/tagdict/POSTaggerFactoryTest.java
+++
b/opennlp-morfologik-addon/src/test/java/opennlp/morfologik/tagdict/POSTaggerFactoryTest.java
@@ -70,6 +70,7 @@ private static POSModel trainPOSModel(ModelType type,
POSTaggerFactory factory)
public void testPOSTaggerWithCustomFactory() throws Exception {
Path dictionary = POSDictionayBuilderTest.createMorfologikDictionary();
+ dictionary.toFile().deleteOnExit();
POSTaggerFactory inFactory = new MorfologikPOSTaggerFactory();
TagDictionary inDict = inFactory.createTagDictionary(dictionary.toFile());
inFactory.setTagDictionary(inDict);
diff --git
a/opennlp-tools/src/test/java/opennlp/tools/cmdline/TokenNameFinderToolTest.java
b/opennlp-tools/src/test/java/opennlp/tools/cmdline/TokenNameFinderToolTest.java
index e4a7fc68b..e76814ae8 100644
---
a/opennlp-tools/src/test/java/opennlp/tools/cmdline/TokenNameFinderToolTest.java
+++
b/opennlp-tools/src/test/java/opennlp/tools/cmdline/TokenNameFinderToolTest.java
@@ -64,7 +64,8 @@ public void run() throws IOException {
final String content = new String(baos.toByteArray(),
StandardCharsets.UTF_8);
Assert.assertTrue(content.contains("It is <START:person> Stefanie Schmidt.
<END>"));
-
+
+ model1.delete();
}
@Test(expected = TerminateToolException.class)
diff --git
a/opennlp-tools/src/test/java/opennlp/tools/ml/naivebayes/NaiveBayesModelReadWriteTest.java
b/opennlp-tools/src/test/java/opennlp/tools/ml/naivebayes/NaiveBayesModelReadWriteTest.java
index 7a0fb22c3..2efd08b57 100644
---
a/opennlp-tools/src/test/java/opennlp/tools/ml/naivebayes/NaiveBayesModelReadWriteTest.java
+++
b/opennlp-tools/src/test/java/opennlp/tools/ml/naivebayes/NaiveBayesModelReadWriteTest.java
@@ -55,12 +55,17 @@ public void testBinaryModelPersistence() throws Exception {
NaiveBayesModel model = (NaiveBayesModel) new
NaiveBayesTrainer().trainModel(testDataIndexer);
Path tempFile = Files.createTempFile("bnb-", ".bin");
File file = tempFile.toFile();
- NaiveBayesModelWriter modelWriter = new BinaryNaiveBayesModelWriter(model,
file);
- modelWriter.persist();
- NaiveBayesModelReader reader = new BinaryNaiveBayesModelReader(file);
- reader.checkModelType();
- AbstractModel abstractModel = reader.constructModel();
- Assert.assertNotNull(abstractModel);
+ try {
+ NaiveBayesModelWriter modelWriter = new
BinaryNaiveBayesModelWriter(model, file);
+ modelWriter.persist();
+ NaiveBayesModelReader reader = new BinaryNaiveBayesModelReader(file);
+ reader.checkModelType();
+ AbstractModel abstractModel = reader.constructModel();
+ Assert.assertNotNull(abstractModel);
+ }
+ finally {
+ file.delete();
+ }
}
@Test
@@ -69,11 +74,16 @@ public void testTextModelPersistence() throws Exception {
NaiveBayesModel model = (NaiveBayesModel) new
NaiveBayesTrainer().trainModel(testDataIndexer);
Path tempFile = Files.createTempFile("ptnb-", ".txt");
File file = tempFile.toFile();
- NaiveBayesModelWriter modelWriter = new
PlainTextNaiveBayesModelWriter(model, file);
- modelWriter.persist();
- NaiveBayesModelReader reader = new PlainTextNaiveBayesModelReader(file);
- reader.checkModelType();
- AbstractModel abstractModel = reader.constructModel();
- Assert.assertNotNull(abstractModel);
+ try {
+ NaiveBayesModelWriter modelWriter = new
PlainTextNaiveBayesModelWriter(model, file);
+ modelWriter.persist();
+ NaiveBayesModelReader reader = new PlainTextNaiveBayesModelReader(file);
+ reader.checkModelType();
+ AbstractModel abstractModel = reader.constructModel();
+ Assert.assertNotNull(abstractModel);
+ }
+ finally {
+ file.delete();
+ }
}
}
diff --git
a/opennlp-tools/src/test/java/opennlp/tools/ml/naivebayes/NaiveBayesSerializedCorrectnessTest.java
b/opennlp-tools/src/test/java/opennlp/tools/ml/naivebayes/NaiveBayesSerializedCorrectnessTest.java
index f68497486..61ad99da3 100644
---
a/opennlp-tools/src/test/java/opennlp/tools/ml/naivebayes/NaiveBayesSerializedCorrectnessTest.java
+++
b/opennlp-tools/src/test/java/opennlp/tools/ml/naivebayes/NaiveBayesSerializedCorrectnessTest.java
@@ -154,11 +154,16 @@ public void testPlainTextModel() throws IOException {
protected static NaiveBayesModel persistedModel(NaiveBayesModel model)
throws IOException {
Path tempFilePath = Files.createTempFile("ptnb-", ".bin");
File file = tempFilePath.toFile();
- NaiveBayesModelWriter modelWriter = new BinaryNaiveBayesModelWriter(model,
tempFilePath.toFile());
- modelWriter.persist();
- NaiveBayesModelReader reader = new BinaryNaiveBayesModelReader(file);
- reader.checkModelType();
- return (NaiveBayesModel)reader.constructModel();
+ try {
+ NaiveBayesModelWriter modelWriter = new
BinaryNaiveBayesModelWriter(model, file);
+ modelWriter.persist();
+ NaiveBayesModelReader reader = new BinaryNaiveBayesModelReader(file);
+ reader.checkModelType();
+ return (NaiveBayesModel)reader.constructModel();
+ }
+ finally {
+ file.delete();
+ }
}
protected static void testModelOutcome(NaiveBayesModel model1,
NaiveBayesModel model2, Event event) {
diff --git
a/opennlp-tools/src/test/java/opennlp/tools/namefind/TokenNameFinderModelTest.java
b/opennlp-tools/src/test/java/opennlp/tools/namefind/TokenNameFinderModelTest.java
index 9d58993fa..fd05d0960 100644
---
a/opennlp-tools/src/test/java/opennlp/tools/namefind/TokenNameFinderModelTest.java
+++
b/opennlp-tools/src/test/java/opennlp/tools/namefind/TokenNameFinderModelTest.java
@@ -35,6 +35,7 @@
import opennlp.tools.cmdline.namefind.TokenNameFinderTrainerTool;
import opennlp.tools.postag.POSModel;
import opennlp.tools.postag.POSTaggerMETest;
+import opennlp.tools.util.FileUtil;
import opennlp.tools.util.MockInputStreamFactory;
import opennlp.tools.util.ObjectStream;
import opennlp.tools.util.PlainTextByLineStream;
@@ -52,7 +53,6 @@ public void testNERWithPOSModel() throws IOException {
// save a POS model there
POSModel posModel = POSTaggerMETest.trainPOSModel(ModelType.MAXENT);
File posModelFile = new File(resourcesFolder.toFile(),"pos-model.bin");
- FileOutputStream fos = new FileOutputStream(posModelFile);
posModel.serialize(posModelFile);
@@ -77,6 +77,9 @@ public void testNERWithPOSModel() throws IOException {
catch (IOException e) {
throw new TerminateToolException(-1, e.getMessage(), e);
}
+ finally {
+ Files.delete(featureGenerator);
+ }
// train a name finder
@@ -94,11 +97,17 @@ public void testNERWithPOSModel() throws IOException {
File model = File.createTempFile("nermodel", ".bin");
- FileOutputStream modelOut = new FileOutputStream(model);
- nameFinderModel.serialize(modelOut);
+ try {
+ FileOutputStream modelOut = new FileOutputStream(model);
+ nameFinderModel.serialize(modelOut);
- modelOut.close();
+ modelOut.close();
- Assert.assertTrue(model.exists());
+ Assert.assertTrue(model.exists());
+ }
+ finally {
+ model.delete();
+ FileUtil.deleteDirectory(resourcesFolder.toFile());
+ }
}
}
diff --git a/opennlp-tools/src/test/java/opennlp/tools/util/FileUtil.java
b/opennlp-tools/src/test/java/opennlp/tools/util/FileUtil.java
new file mode 100644
index 000000000..bcde8c04e
--- /dev/null
+++ b/opennlp-tools/src/test/java/opennlp/tools/util/FileUtil.java
@@ -0,0 +1,42 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 opennlp.tools.util;
+
+import java.io.File;
+
+public class FileUtil {
+
+ /**
+ * delete the specified directory and its child directories and files
+ * @param file specify the file or directory to be deleted
+ */
+ public static void deleteDirectory(File file) {
+ if (file.exists()) {
+ if (file.isDirectory()) {
+ File[] files = file.listFiles();
+ for (File f: files) {
+ deleteDirectory(f);
+ }
+ file.delete();
+ }
+ else if (file.isFile()) {
+ file.delete();
+ }
+ }
+ }
+}
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
> some tests create temp files and directories but never delete them
> ------------------------------------------------------------------
>
> Key: OPENNLP-1171
> URL: https://issues.apache.org/jira/browse/OPENNLP-1171
> Project: OpenNLP
> Issue Type: Bug
> Components: Build, Packaging and Test
> Affects Versions: 1.8.3
> Reporter: Koji Sekiguchi
> Assignee: Koji Sekiguchi
> Priority: Minor
> Fix For: 1.8.4
>
>
> Some temporary files and directories that are created in some tests are never
> deleted and the number of temporary files/directories is increasing after
> running mvn clean test.
--
This message was sent by Atlassian JIRA
(v6.4.14#64029)