You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@nemo.apache.org by GitBox <gi...@apache.org> on 2018/06/15 12:32:27 UTC

[GitHub] jeongyooneo closed pull request #42: [NEMO-41] SonarCloud Bugs and Vulnerabilities for NemoCommon

jeongyooneo closed pull request #42: [NEMO-41] SonarCloud Bugs and Vulnerabilities for NemoCommon
URL: https://github.com/apache/incubator-nemo/pull/42
 
 
   

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/common/src/main/java/edu/snu/nemo/common/dag/DAG.java b/common/src/main/java/edu/snu/nemo/common/dag/DAG.java
index f82d3a40..1366ecc9 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 void storeJSON(final String directory, final String name, final String de
 
     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 7af6d333..84866f42 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.List;
 import java.util.Set;
 import java.util.stream.Collectors;
+import java.util.stream.Stream;
 
 /**
  * Test Utils for Examples.
@@ -42,27 +43,34 @@ private 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 static void ensureOutputValidity(final String resourcePath,
   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 static void ensureALSOutputValidity(final String resourcePath,
    */
   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);
+      }
     }
   }
 }


 

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services