You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nemo.apache.org by je...@apache.org on 2018/06/15 12:32:29 UTC

[incubator-nemo] branch master updated: [NEMO-41] SonarCloud Bugs and Vulnerabilities for NemoCommon (#42)

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

jeongyoon pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-nemo.git


The following commit(s) were added to refs/heads/master by this push:
     new 2c93e7b  [NEMO-41] SonarCloud Bugs and Vulnerabilities for NemoCommon (#42)
2c93e7b is described below

commit 2c93e7b3d6ecb94fc4bf6be0dc3168ff322fd369
Author: Gyewon Lee <st...@gmail.com>
AuthorDate: Fri Jun 15 21:32:25 2018 +0900

    [NEMO-41] SonarCloud Bugs and Vulnerabilities for NemoCommon (#42)
    
    JIRA: NEMO-41: SonarCloud Bugs and Vulnerabilities for NemoCommon
    
    Major changes:
    * Resolve reported bugs and vulnerabilities from SonarCloud.
    
    Minor changes to note:
    n/a
    
    Tests for the changes:
    n/a
    
    Other comments:
    n/a
    
    resolves NEMO-41
---
 .../src/main/java/edu/snu/nemo/common/dag/DAG.java |   3 +-
 .../edu/snu/nemo/common/test/ExampleTestUtil.java  | 103 ++++++++++++---------
 2 files changed, 61 insertions(+), 45 deletions(-)

diff --git a/common/src/main/java/edu/snu/nemo/common/dag/DAG.java b/common/src/main/java/edu/snu/nemo/common/dag/DAG.java
index f82d3a4..1366ecc 100644
--- a/common/src/main/java/edu/snu/nemo/common/dag/DAG.java
+++ b/common/src/main/java/edu/snu/nemo/common/dag/DAG.java
@@ -408,8 +408,7 @@ public final class DAG<V extends Vertex, E extends Edge<V>> implements Serializa
 
     final File file = new File(directory, name + ".json");
     file.getParentFile().mkdirs();
-    try {
-      final PrintWriter printWriter = new PrintWriter(file);
+    try (final PrintWriter printWriter = new PrintWriter(file)) {
       printWriter.println(toString());
       printWriter.close();
       LOG.info(String.format("DAG JSON for %s is saved at %s"
diff --git a/common/src/main/java/edu/snu/nemo/common/test/ExampleTestUtil.java b/common/src/main/java/edu/snu/nemo/common/test/ExampleTestUtil.java
index 7af6d33..84866f4 100644
--- a/common/src/main/java/edu/snu/nemo/common/test/ExampleTestUtil.java
+++ b/common/src/main/java/edu/snu/nemo/common/test/ExampleTestUtil.java
@@ -23,6 +23,7 @@ import java.util.Arrays;
 import java.util.List;
 import java.util.Set;
 import java.util.stream.Collectors;
+import java.util.stream.Stream;
 
 /**
  * Test Utils for Examples.
@@ -42,27 +43,34 @@ public final class ExampleTestUtil {
    * @param outputFileName output file name.
    * @param testResourceFileName the test result file name.
    * @throws RuntimeException if the output is invalid.
-   * @throws IOException IOException while testing.
    */
   public static void ensureOutputValidity(final String resourcePath,
                                           final String outputFileName,
                                           final String testResourceFileName) throws IOException {
-    final String testOutput = Files.list(Paths.get(resourcePath))
-        .filter(Files::isRegularFile)
-        .filter(path -> path.getFileName().toString().startsWith(outputFileName))
-        .flatMap(path -> {
-          try {
-            return Files.lines(path);
-          } catch (final IOException e) {
-            throw new RuntimeException(e);
-          }
-        })
-        .sorted()
-        .reduce("", (p, q) -> (p + "\n" + q));
 
-    final String resourceOutput = Files.lines(Paths.get(resourcePath + testResourceFileName))
-        .sorted()
-        .reduce("", (p, q) -> (p + "\n" + q));
+    final String testOutput;
+    try (final Stream<Path> fileStream = Files.list(Paths.get(resourcePath))) {
+      testOutput = fileStream
+          .filter(Files::isRegularFile)
+          .filter(path -> path.getFileName().toString().startsWith(outputFileName))
+          .flatMap(path -> {
+            try {
+              return Files.lines(path);
+            } catch (final IOException e) {
+              throw new RuntimeException(e);
+            }
+          })
+          .sorted()
+          .reduce("", (p, q) -> (p + "\n" + q));
+    }
+
+    final String resourceOutput;
+
+    try (final Stream<String> lineStream = Files.lines(Paths.get(resourcePath + testResourceFileName))) {
+      resourceOutput = lineStream
+          .sorted()
+          .reduce("", (p, q) -> (p + "\n" + q));
+    }
 
     if (!testOutput.equals(resourceOutput)) {
       final String outputMsg =
@@ -91,28 +99,35 @@ public final class ExampleTestUtil {
   public static void ensureALSOutputValidity(final String resourcePath,
                                              final String outputFileName,
                                              final String testResourceFileName) throws IOException {
-    final List<List<Double>> testOutput = Files.list(Paths.get(resourcePath))
-        .filter(Files::isRegularFile)
-        .filter(path -> path.getFileName().toString().startsWith(outputFileName))
-        .flatMap(path -> {
-          try {
-            return Files.lines(path);
-          } catch (final IOException e) {
-            throw new RuntimeException(e);
-          }
-        })
-        .sorted()
-        .filter(line -> !line.trim().equals(""))
-        .map(line -> Arrays.asList(line.split("\\s*,\\s*"))
-            .stream().map(s -> Double.valueOf(s)).collect(Collectors.toList()))
-        .collect(Collectors.toList());
 
-    final List<List<Double>> resourceOutput = Files.lines(Paths.get(resourcePath + testResourceFileName))
-        .sorted()
-        .filter(line -> !line.trim().equals(""))
-        .map(line -> Arrays.asList(line.split("\\s*,\\s*"))
-            .stream().map(s -> Double.valueOf(s)).collect(Collectors.toList()))
-        .collect(Collectors.toList());
+    final List<List<Double>> testOutput;
+    try (final Stream<Path> fileStream = Files.list(Paths.get(resourcePath))) {
+      testOutput = fileStream
+          .filter(Files::isRegularFile)
+          .filter(path -> path.getFileName().toString().startsWith(outputFileName))
+          .flatMap(path -> {
+            try {
+              return Files.lines(path);
+            } catch (final IOException e) {
+              throw new RuntimeException(e);
+            }
+          })
+          .sorted()
+          .filter(line -> !line.trim().equals(""))
+          .map(line -> Arrays.asList(line.split("\\s*,\\s*"))
+              .stream().map(s -> Double.valueOf(s)).collect(Collectors.toList()))
+          .collect(Collectors.toList());
+    }
+
+    final List<List<Double>> resourceOutput;
+    try (final Stream<String> lineStream = Files.lines(Paths.get(resourcePath + testResourceFileName))) {
+      resourceOutput = lineStream
+          .sorted()
+          .filter(line -> !line.trim().equals(""))
+          .map(line -> Arrays.asList(line.split("\\s*,\\s*"))
+              .stream().map(s -> Double.valueOf(s)).collect(Collectors.toList()))
+          .collect(Collectors.toList());
+    }
 
     if (testOutput.size() != resourceOutput.size()) {
       throw new RuntimeException("output mismatch");
@@ -138,12 +153,14 @@ public final class ExampleTestUtil {
    */
   public static void deleteOutputFile(final String directory,
                                       final String outputFileName) throws IOException {
-    final Set<Path> outputFilePaths = Files.list(Paths.get(directory))
-        .filter(Files::isRegularFile)
-        .filter(path -> path.getFileName().toString().startsWith(outputFileName))
-        .collect(Collectors.toSet());
-    for (final Path outputFilePath : outputFilePaths) {
-      Files.delete(outputFilePath);
+    try (final Stream<Path> fileStream = Files.list(Paths.get(directory))) {
+      final Set<Path> outputFilePaths = fileStream
+          .filter(Files::isRegularFile)
+          .filter(path -> path.getFileName().toString().startsWith(outputFileName))
+          .collect(Collectors.toSet());
+      for (final Path outputFilePath : outputFilePaths) {
+        Files.delete(outputFilePath);
+      }
     }
   }
 }

-- 
To stop receiving notification emails like this one, please contact
jeongyoon@apache.org.