Use java.nio.file for file operations 43/95243/3
authorRobert Varga <robert.varga@pantheon.tech>
Sun, 21 Feb 2021 15:55:05 +0000 (16:55 +0100)
committerRobert Varga <nite@hq.sk>
Wed, 24 Feb 2021 10:09:18 +0000 (10:09 +0000)
java.nio.file is more expressive than java.io.File, use it to
perform reads and atomic renames.

Also clean up constant use and assertions a bit.

JIRA: NETCONF-756
Change-Id: Ia42c61f8cca4253a7db297e188053fcf2b63ec35
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
netconf/tools/netconf-testtool/src/main/java/org/opendaylight/netconf/test/tool/TesttoolParameters.java

index def348e33b33137ab3599ff350728f88b84cbe1c..22aa014bb2d426b4d9e853007695bdf7a6e863aa 100644 (file)
@@ -11,7 +11,6 @@ import static com.google.common.base.Preconditions.checkArgument;
 import static com.google.common.base.Preconditions.checkState;
 
 import com.google.common.io.CharStreams;
-import com.google.common.io.Files;
 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
 import java.io.BufferedReader;
 import java.io.File;
@@ -21,6 +20,9 @@ import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.lang.reflect.Field;
 import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.nio.file.StandardCopyOption;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collections;
@@ -34,6 +36,7 @@ import net.sourceforge.argparse4j.ArgumentParsers;
 import net.sourceforge.argparse4j.annotation.Arg;
 import net.sourceforge.argparse4j.inf.ArgumentParser;
 import net.sourceforge.argparse4j.inf.ArgumentParserException;
+import org.opendaylight.yangtools.yang.common.YangConstants;
 
 @SuppressFBWarnings({"DM_EXIT", "DM_DEFAULT_ENCODING"})
 public class TesttoolParameters {
@@ -313,7 +316,7 @@ public class TesttoolParameters {
                     try {
                         final String correctName = correctedName(file);
                         if (correctName != null) {
-                            Files.move(file, new File(correctName));
+                            Files.move(file.toPath(), Paths.get(correctName), StandardCopyOption.ATOMIC_MOVE);
                         }
                     } catch (final IOException e) {
                         // print error to console (test tool is running from console)
@@ -337,13 +340,13 @@ public class TesttoolParameters {
             }
             if (line != null) {
                 final Matcher m = REVISION_DATE_PATTERN.matcher(line);
-                checkState(m.find());
+                checkState(m.find(), "Revision pattern %s did not match line %s", REVISION_DATE_PATTERN, line);
                 String moduleName = file.getAbsolutePath();
-                if (file.getName().endsWith(".yang")) {
+                if (file.getName().endsWith(YangConstants.RFC6020_YANG_FILE_EXTENSION)) {
                     moduleName = moduleName.substring(0, moduleName.length() - 5);
                 }
-                final String revision = m.group(1);
-                return moduleName + "@" + revision + ".yang";
+
+                return moduleName + "@" + m.group(1) + YangConstants.RFC6020_YANG_FILE_EXTENSION;
             }
         }
         return null;
@@ -354,7 +357,7 @@ public class TesttoolParameters {
         final String editContentString;
         try {
             if (stream == null) {
-                editContentString = Files.asCharSource(editContent, StandardCharsets.UTF_8).read();
+                editContentString = Files.readString(editContent.toPath());
             } else {
                 editContentString = CharStreams.toString(new InputStreamReader(stream, StandardCharsets.UTF_8));
             }