You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@opennlp.apache.org by ko...@apache.org on 2017/12/20 01:41:24 UTC

[opennlp] branch master updated: OPENNLP-1171: some tests create temp files and directories but never … (#298)

This is an automated email from the ASF dual-hosted git repository.

koji pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/opennlp.git


The following commit(s) were added to refs/heads/master by this push:
     new 7511b06  OPENNLP-1171: some tests create temp files and directories but never … (#298)
7511b06 is described below

commit 7511b06e23d2f330a496f4e133f3b4d5bab35913
Author: Koji Sekiguchi <ko...@rondhuit.com>
AuthorDate: Wed Dec 20 10:41:22 2017 +0900

    OPENNLP-1171: some tests create temp files and directories but never … (#298)
    
    * OPENNLP-1171: some tests create temp files and directories but never delete them
---
 .../builder/POSDictionayBuilderTest.java           |  3 ++
 .../lemmatizer/MorfologikLemmatizerTest.java       |  1 +
 .../tagdict/MorfologikTagDictionaryTest.java       |  5 ++-
 .../morfologik/tagdict/POSTaggerFactoryTest.java   |  1 +
 .../tools/cmdline/TokenNameFinderToolTest.java     |  3 +-
 .../naivebayes/NaiveBayesModelReadWriteTest.java   | 34 +++++++++++-------
 .../NaiveBayesSerializedCorrectnessTest.java       | 15 +++++---
 .../tools/namefind/TokenNameFinderModelTest.java   | 19 +++++++---
 .../src/test/java/opennlp/tools/util/FileUtil.java | 42 ++++++++++++++++++++++
 9 files changed, 99 insertions(+), 24 deletions(-)

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 f53ba24..93bb3fa 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 class POSDictionayBuilderTest {
   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 class POSDictionayBuilderTest {
     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 426e394..f522c56 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 class MorfologikLemmatizerTest {
   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 b1c071e..c367d3f 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 @@ public class MorfologikTagDictionaryTest {
 
   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 9ce22d2..a5b74ce 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 @@ public class POSTaggerFactoryTest {
   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 e4a7fc6..e76814a 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 class TokenNameFinderToolTest {
     
     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 7a0fb22..2efd08b 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 class NaiveBayesModelReadWriteTest {
     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 class NaiveBayesModelReadWriteTest {
     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 f684974..61ad99d 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 class NaiveBayesSerializedCorrectnessTest {
   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 9d58993..fd05d09 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.TerminateToolException;
 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 class TokenNameFinderModelTest {
     // 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 class TokenNameFinderModelTest {
     catch (IOException e) {
       throw new TerminateToolException(-1, e.getMessage(), e);
     }
+    finally {
+      Files.delete(featureGenerator);
+    }
 
 
     // train a name finder
@@ -94,11 +97,17 @@ public class TokenNameFinderModelTest {
 
 
     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 0000000..bcde8c0
--- /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();
+      }
+    }
+  }
+}

-- 
To stop receiving notification emails like this one, please contact
['"commits@opennlp.apache.org" <co...@opennlp.apache.org>'].