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

elharo pushed a commit to branch close
in repository https://gitbox.apache.org/repos/asf/maven-scm.git

commit 0c0c03f0e8cbddea0f619b218395c42c1da309b8
Author: Elliotte Rusty Harold <elh...@ibiblio.org>
AuthorDate: Mon Dec 9 10:04:02 2024 -0500

    Prefer try with resources
---
 .../org/apache/maven/scm/util/ConsumerUtils.java   | 11 ++----
 .../local/metadata/LocalScmMetadataUtils.java      | 22 +++++-------
 .../checkout/LocalCheckOutCommandTckTest.java      | 20 +++++------
 .../command/update/LocalUpdateCommandTckTest.java  | 19 ++++------
 .../git/jgit/command/branch/JGitBranchCommand.java |  6 +---
 .../JGitCheckInCommandCommitterAuthorTckTest.java  |  9 +----
 .../svn/svnexe/command/SvnCommandLineUtils.java    |  7 ++--
 .../tck/command/branch/BranchCommandTckTest.java   | 14 ++++----
 .../tck/command/checkin/CheckInCommandTckTest.java | 41 ++++++----------------
 .../scm/tck/command/tag/TagCommandTckTest.java     | 14 ++++----
 10 files changed, 54 insertions(+), 109 deletions(-)

diff --git 
a/maven-scm-api/src/main/java/org/apache/maven/scm/util/ConsumerUtils.java 
b/maven-scm-api/src/main/java/org/apache/maven/scm/util/ConsumerUtils.java
index ed9117f87..a1c07641d 100644
--- a/maven-scm-api/src/main/java/org/apache/maven/scm/util/ConsumerUtils.java
+++ b/maven-scm-api/src/main/java/org/apache/maven/scm/util/ConsumerUtils.java
@@ -20,8 +20,8 @@ package org.apache.maven.scm.util;
 
 import java.io.BufferedReader;
 import java.io.File;
-import java.io.FileReader;
 import java.io.IOException;
+import java.nio.file.Files;
 
 import org.codehaus.plexus.util.cli.StreamConsumer;
 
@@ -40,16 +40,11 @@ public class ConsumerUtils {
      * @throws IOException if any
      */
     public static void consumeFile(File f, StreamConsumer consumer) throws 
IOException {
-        BufferedReader r = new BufferedReader(new FileReader(f));
-
-        try {
+        try (BufferedReader reader = Files.newBufferedReader(f.toPath())) {
             String line;
-            while ((line = r.readLine()) != null) {
+            while ((line = reader.readLine()) != null) {
                 consumer.consumeLine(line);
             }
-
-        } finally {
-            r.close();
         }
     }
 }
diff --git 
a/maven-scm-providers/maven-scm-provider-local/src/main/java/org/apache/maven/scm/provider/local/metadata/LocalScmMetadataUtils.java
 
b/maven-scm-providers/maven-scm-provider-local/src/main/java/org/apache/maven/scm/provider/local/metadata/LocalScmMetadataUtils.java
index 742d2c4ac..1ec78aaf9 100644
--- 
a/maven-scm-providers/maven-scm-provider-local/src/main/java/org/apache/maven/scm/provider/local/metadata/LocalScmMetadataUtils.java
+++ 
b/maven-scm-providers/maven-scm-provider-local/src/main/java/org/apache/maven/scm/provider/local/metadata/LocalScmMetadataUtils.java
@@ -27,7 +27,6 @@ import java.util.List;
 import 
org.apache.maven.scm.provider.local.metadata.io.xpp3.LocalScmMetadataXpp3Reader;
 import 
org.apache.maven.scm.provider.local.metadata.io.xpp3.LocalScmMetadataXpp3Writer;
 import org.codehaus.plexus.util.FileUtils;
-import org.codehaus.plexus.util.IOUtil;
 import org.codehaus.plexus.util.ReaderFactory;
 import org.codehaus.plexus.util.WriterFactory;
 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
@@ -73,11 +72,9 @@ public class LocalScmMetadataUtils {
     public void writeMetadata(File destinationDir, LocalScmMetadata metadata) 
throws IOException {
         File metadataFile = new File(destinationDir, FILENAME);
         metadataFile.createNewFile();
-        Writer writer = WriterFactory.newXmlWriter(metadataFile);
-        try {
+
+        try (Writer writer = WriterFactory.newXmlWriter(metadataFile)) {
             new LocalScmMetadataXpp3Writer().write(writer, metadata);
-        } finally {
-            IOUtil.close(writer);
         }
     }
 
@@ -92,11 +89,10 @@ public class LocalScmMetadataUtils {
         if (!metadataFile.exists()) {
             return null;
         }
-        LocalScmMetadata result = null;
-        Reader reader = null;
-        try {
-            reader = ReaderFactory.newXmlReader(metadataFile);
-            result = new LocalScmMetadataXpp3Reader().read(reader);
+
+        try (Reader reader = ReaderFactory.newXmlReader(metadataFile)) {
+            LocalScmMetadata result = new 
LocalScmMetadataXpp3Reader().read(reader);
+            return result;
         } catch (XmlPullParserException e) {
             if (LOGGER.isWarnEnabled()) {
                 LOGGER.warn("Could not interpret .maven-scm-local - ignoring", 
e);
@@ -104,11 +100,9 @@ public class LocalScmMetadataUtils {
             return null;
         } catch (IOException e) {
             if (LOGGER.isWarnEnabled()) {
-                LOGGER.warn("Could not Read .maven-scm-local - ignoring", e);
+                LOGGER.warn("Could not read .maven-scm-local - ignoring", e);
             }
-        } finally {
-            IOUtil.close(reader);
+            return null;
         }
-        return result;
     }
 }
diff --git 
a/maven-scm-providers/maven-scm-provider-local/src/test/java/org/apache/maven/scm/provider/local/command/checkout/LocalCheckOutCommandTckTest.java
 
b/maven-scm-providers/maven-scm-provider-local/src/test/java/org/apache/maven/scm/provider/local/command/checkout/LocalCheckOutCommandTckTest.java
index 64a0e1a8b..d3cbbabfc 100644
--- 
a/maven-scm-providers/maven-scm-provider-local/src/test/java/org/apache/maven/scm/provider/local/command/checkout/LocalCheckOutCommandTckTest.java
+++ 
b/maven-scm-providers/maven-scm-provider-local/src/test/java/org/apache/maven/scm/provider/local/command/checkout/LocalCheckOutCommandTckTest.java
@@ -19,8 +19,8 @@
 package org.apache.maven.scm.provider.local.command.checkout;
 
 import java.io.File;
-import java.io.FileReader;
 import java.io.Reader;
+import java.nio.file.Files;
 import java.util.List;
 
 import org.apache.maven.scm.ScmFile;
@@ -29,7 +29,6 @@ import 
org.apache.maven.scm.provider.local.metadata.LocalScmMetadata;
 import 
org.apache.maven.scm.provider.local.metadata.io.xpp3.LocalScmMetadataXpp3Reader;
 import org.apache.maven.scm.tck.command.checkout.CheckOutCommandTckTest;
 import org.codehaus.plexus.util.FileUtils;
-import org.codehaus.plexus.util.IOUtil;
 import org.junit.Test;
 
 import static org.junit.Assert.assertEquals;
@@ -80,16 +79,13 @@ public class LocalCheckOutCommandTckTest extends 
CheckOutCommandTckTest {
         // 
----------------------------------------------------------------------
         File metadataFile = new File(getWorkingCopy(), ".maven-scm-local");
         assertTrue("Expected metadata file .maven-scm-local does not exist", 
metadataFile.exists());
-        Reader reader = new FileReader(metadataFile);
-        LocalScmMetadata metadata;
-        try {
-            metadata = new LocalScmMetadataXpp3Reader().read(reader);
-        } finally {
-            IOUtil.close(reader);
+
+        try (Reader reader = Files.newBufferedReader(metadataFile.toPath()); ) 
{
+            LocalScmMetadata metadata = new 
LocalScmMetadataXpp3Reader().read(reader);
+            File root = new File(getRepositoryRoot() + "/" + module);
+            @SuppressWarnings("unchecked")
+            List<String> fileNames = FileUtils.getFileNames(root, "**", null, 
false);
+            assertEquals(fileNames, metadata.getRepositoryFileNames());
         }
-        File root = new File(getRepositoryRoot() + "/" + module);
-        @SuppressWarnings("unchecked")
-        List<String> fileNames = FileUtils.getFileNames(root, "**", null, 
false);
-        assertEquals(fileNames, metadata.getRepositoryFileNames());
     }
 }
diff --git 
a/maven-scm-providers/maven-scm-provider-local/src/test/java/org/apache/maven/scm/provider/local/command/update/LocalUpdateCommandTckTest.java
 
b/maven-scm-providers/maven-scm-provider-local/src/test/java/org/apache/maven/scm/provider/local/command/update/LocalUpdateCommandTckTest.java
index e896f6d36..80e240d94 100644
--- 
a/maven-scm-providers/maven-scm-provider-local/src/test/java/org/apache/maven/scm/provider/local/command/update/LocalUpdateCommandTckTest.java
+++ 
b/maven-scm-providers/maven-scm-provider-local/src/test/java/org/apache/maven/scm/provider/local/command/update/LocalUpdateCommandTckTest.java
@@ -19,8 +19,8 @@
 package org.apache.maven.scm.provider.local.command.update;
 
 import java.io.File;
-import java.io.FileReader;
 import java.io.Reader;
+import java.nio.file.Files;
 import java.util.Date;
 import java.util.Iterator;
 import java.util.List;
@@ -36,7 +36,6 @@ import 
org.apache.maven.scm.provider.local.metadata.io.xpp3.LocalScmMetadataXpp3
 import org.apache.maven.scm.repository.ScmRepository;
 import org.apache.maven.scm.tck.command.update.UpdateCommandTckTest;
 import org.codehaus.plexus.util.FileUtils;
-import org.codehaus.plexus.util.IOUtil;
 import org.junit.Test;
 
 import static org.junit.Assert.assertEquals;
@@ -130,17 +129,13 @@ public class LocalUpdateCommandTckTest extends 
UpdateCommandTckTest {
         // 
----------------------------------------------------------------------
         File metadataFile = new File(getUpdatingCopy(), ".maven-scm-local");
         assertTrue("Expected metadata file .maven-scm-local does not exist", 
metadataFile.exists());
-        Reader reader = new FileReader(metadataFile);
-        LocalScmMetadata metadata;
-        try {
-            metadata = new LocalScmMetadataXpp3Reader().read(reader);
-        } finally {
-            IOUtil.close(reader);
+        try (Reader reader = Files.newBufferedReader(metadataFile.toPath())) {
+            LocalScmMetadata metadata = new 
LocalScmMetadataXpp3Reader().read(reader);
+            File root = new File(getRepositoryRoot() + "/" + moduleName);
+            @SuppressWarnings("unchecked")
+            List<String> fileNames = FileUtils.getFileNames(root, "**", null, 
false);
+            assertEquals(fileNames, metadata.getRepositoryFileNames());
         }
-        File root = new File(getRepositoryRoot() + "/" + moduleName);
-        @SuppressWarnings("unchecked")
-        List<String> fileNames = FileUtils.getFileNames(root, "**", null, 
false);
-        assertEquals(fileNames, metadata.getRepositoryFileNames());
     }
 
     private void makeRepo(File workingDirectory) throws Exception {
diff --git 
a/maven-scm-providers/maven-scm-providers-git/maven-scm-provider-jgit/src/main/java/org/apache/maven/scm/provider/git/jgit/command/branch/JGitBranchCommand.java
 
b/maven-scm-providers/maven-scm-providers-git/maven-scm-provider-jgit/src/main/java/org/apache/maven/scm/provider/git/jgit/command/branch/JGitBranchCommand.java
index c3cf1e450..8f1c65448 100644
--- 
a/maven-scm-providers/maven-scm-providers-git/maven-scm-provider-jgit/src/main/java/org/apache/maven/scm/provider/git/jgit/command/branch/JGitBranchCommand.java
+++ 
b/maven-scm-providers/maven-scm-providers-git/maven-scm-provider-jgit/src/main/java/org/apache/maven/scm/provider/git/jgit/command/branch/JGitBranchCommand.java
@@ -65,9 +65,7 @@ public class JGitBranchCommand extends AbstractBranchCommand 
implements GitComma
             throw new ScmException("This provider doesn't support branching 
subsets of a directory");
         }
 
-        Git git = null;
-        try {
-            git = JGitUtils.openRepo(fileSet.getBasedir());
+        try (Git git = JGitUtils.openRepo(fileSet.getBasedir())) {
             Ref branchResult = git.branchCreate().setName(branch).call();
             logger.info("created [" + branchResult.getName() + "]");
 
@@ -102,8 +100,6 @@ public class JGitBranchCommand extends 
AbstractBranchCommand implements GitComma
 
         } catch (Exception e) {
             throw new ScmException("JGit branch failed!", e);
-        } finally {
-            JGitUtils.closeRepo(git);
         }
     }
 
diff --git 
a/maven-scm-providers/maven-scm-providers-git/maven-scm-provider-jgit/src/test/java/org/apache/maven/scm/provider/git/jgit/command/checkin/JGitCheckInCommandCommitterAuthorTckTest.java
 
b/maven-scm-providers/maven-scm-providers-git/maven-scm-provider-jgit/src/test/java/org/apache/maven/scm/provider/git/jgit/command/checkin/JGitCheckInCommandCommitterAuthorTckTest.java
index fb7c1a3e5..145996cde 100644
--- 
a/maven-scm-providers/maven-scm-providers-git/maven-scm-provider-jgit/src/test/java/org/apache/maven/scm/provider/git/jgit/command/checkin/JGitCheckInCommandCommitterAuthorTckTest.java
+++ 
b/maven-scm-providers/maven-scm-providers-git/maven-scm-provider-jgit/src/test/java/org/apache/maven/scm/provider/git/jgit/command/checkin/JGitCheckInCommandCommitterAuthorTckTest.java
@@ -31,7 +31,6 @@ import org.apache.maven.scm.provider.git.GitScmTestUtils;
 import 
org.apache.maven.scm.provider.git.command.checkin.GitCheckInCommandTckTest;
 import org.apache.maven.scm.provider.git.jgit.command.JGitUtils;
 import org.apache.maven.scm.repository.ScmRepository;
-import org.codehaus.plexus.util.IOUtil;
 import org.eclipse.jgit.api.Git;
 import org.eclipse.jgit.lib.AnyObjectId;
 import org.eclipse.jgit.lib.Config;
@@ -270,10 +269,7 @@ public class JGitCheckInCommandCommitterAuthorTckTest 
extends GitCheckInCommandT
     }
 
     private void createFooJava(File fooJava) throws Exception {
-        FileWriter output = new FileWriter(fooJava);
-
-        PrintWriter printer = new PrintWriter(output);
-        try {
+        try (PrintWriter printer = new PrintWriter(new FileWriter(fooJava))) {
             printer.println("public class Foo");
             printer.println("{");
 
@@ -284,9 +280,6 @@ public class JGitCheckInCommandCommitterAuthorTckTest 
extends GitCheckInCommandT
             printer.println("    }");
 
             printer.println("}");
-        } finally {
-            IOUtil.close(output);
-            IOUtil.close(printer);
         }
     }
 
diff --git 
a/maven-scm-providers/maven-scm-providers-svn/maven-scm-provider-svnexe/src/main/java/org/apache/maven/scm/provider/svn/svnexe/command/SvnCommandLineUtils.java
 
b/maven-scm-providers/maven-scm-providers-svn/maven-scm-provider-svnexe/src/main/java/org/apache/maven/scm/provider/svn/svnexe/command/SvnCommandLineUtils.java
index cca583245..5b62bad76 100644
--- 
a/maven-scm-providers/maven-scm-providers-svn/maven-scm-provider-svnexe/src/main/java/org/apache/maven/scm/provider/svn/svnexe/command/SvnCommandLineUtils.java
+++ 
b/maven-scm-providers/maven-scm-providers-svn/maven-scm-provider-svnexe/src/main/java/org/apache/maven/scm/provider/svn/svnexe/command/SvnCommandLineUtils.java
@@ -60,10 +60,9 @@ public final class SvnCommandLineUtils {
         }
 
         File targets = File.createTempFile("maven-scm-", "-targets");
-        PrintStream out = new PrintStream(new FileOutputStream(targets));
-        out.print(sb);
-        out.flush();
-        out.close();
+        try (PrintStream out = new PrintStream(new FileOutputStream(targets))) 
{
+            out.print(sb);
+        }
 
         cl.createArg().setValue("--targets");
         cl.createArg().setValue(targets.getAbsolutePath());
diff --git 
a/maven-scm-test/src/main/java/org/apache/maven/scm/tck/command/branch/BranchCommandTckTest.java
 
b/maven-scm-test/src/main/java/org/apache/maven/scm/tck/command/branch/BranchCommandTckTest.java
index 7eea40aca..a54539356 100644
--- 
a/maven-scm-test/src/main/java/org/apache/maven/scm/tck/command/branch/BranchCommandTckTest.java
+++ 
b/maven-scm-test/src/main/java/org/apache/maven/scm/tck/command/branch/BranchCommandTckTest.java
@@ -19,7 +19,9 @@
 package org.apache.maven.scm.tck.command.branch;
 
 import java.io.File;
-import java.io.FileWriter;
+import java.io.Writer;
+import java.nio.file.Files;
+import java.nio.file.Path;
 
 import org.apache.maven.scm.ScmBranch;
 import org.apache.maven.scm.ScmFileSet;
@@ -28,7 +30,6 @@ import org.apache.maven.scm.command.branch.BranchScmResult;
 import org.apache.maven.scm.command.checkin.CheckInScmResult;
 import org.apache.maven.scm.command.checkout.CheckOutScmResult;
 import org.codehaus.plexus.util.FileUtils;
-import org.codehaus.plexus.util.IOUtil;
 import org.junit.Test;
 
 import static org.junit.Assert.assertEquals;
@@ -65,7 +66,7 @@ public abstract class BranchCommandTckTest extends 
ScmTckTestCase {
         assertEquals("check readme.txt contents", "/readme.txt", 
FileUtils.fileRead(readmeTxt));
 
         this.edit(getWorkingCopy(), "readme.txt", null, getScmRepository());
-        changeReadmeTxt(readmeTxt);
+        changeReadmeTxt(readmeTxt.toPath());
 
         CheckInScmResult checkinResult =
                 getScmManager().checkIn(getScmRepository(), new 
ScmFileSet(getWorkingCopy()), "commit message");
@@ -95,12 +96,9 @@ public abstract class BranchCommandTckTest extends 
ScmTckTestCase {
                 "check readme.txt contents is from branched version", 
"/readme.txt", FileUtils.fileRead(readmeTxt));
     }
 
-    private void changeReadmeTxt(File readmeTxt) throws Exception {
-        FileWriter output = new FileWriter(readmeTxt);
-        try {
+    private void changeReadmeTxt(Path readmeTxt) throws Exception {
+        try (Writer output = Files.newBufferedWriter(readmeTxt)) {
             output.write("changed file");
-        } finally {
-            IOUtil.close(output);
         }
     }
 }
diff --git 
a/maven-scm-test/src/main/java/org/apache/maven/scm/tck/command/checkin/CheckInCommandTckTest.java
 
b/maven-scm-test/src/main/java/org/apache/maven/scm/tck/command/checkin/CheckInCommandTckTest.java
index 53869cd9a..06c952400 100644
--- 
a/maven-scm-test/src/main/java/org/apache/maven/scm/tck/command/checkin/CheckInCommandTckTest.java
+++ 
b/maven-scm-test/src/main/java/org/apache/maven/scm/tck/command/checkin/CheckInCommandTckTest.java
@@ -34,7 +34,6 @@ import 
org.apache.maven.scm.command.checkout.CheckOutScmResult;
 import org.apache.maven.scm.provider.ScmProvider;
 import org.apache.maven.scm.util.FilenameUtils;
 import org.codehaus.plexus.util.FileUtils;
-import org.codehaus.plexus.util.IOUtil;
 import org.junit.Assume;
 import org.junit.Test;
 
@@ -238,10 +237,7 @@ public abstract class CheckInCommandTckTest extends 
ScmTckTestCase {
     }
 
     private void createFooJava(File fooJava) throws Exception {
-        FileWriter output = new FileWriter(fooJava);
-
-        PrintWriter printer = new PrintWriter(output);
-        try {
+        try (PrintWriter printer = new PrintWriter(new FileWriter(fooJava))) {
             printer.println("public class Foo");
             printer.println("{");
 
@@ -251,41 +247,26 @@ public abstract class CheckInCommandTckTest extends 
ScmTckTestCase {
             printer.println("    }");
 
             printer.println("}");
-        } finally {
-            IOUtil.close(output);
-            IOUtil.close(printer);
         }
     }
 
     private void createBarJava(File barJava) throws Exception {
-        FileWriter output = new FileWriter(barJava);
-
-        PrintWriter printer = new PrintWriter(output);
-
-        printer.println("public class Bar");
-        printer.println("{");
-
-        printer.println("    public int bar()");
-        printer.println("    {");
-        printer.println("        return 20;");
-        printer.println("    }");
-
-        printer.println("}");
+        try (PrintWriter printer = new PrintWriter(new FileWriter(barJava))) {
+            printer.println("public class Bar");
+            printer.println("{");
 
-        printer.close();
+            printer.println("    public int bar()");
+            printer.println("    {");
+            printer.println("        return 20;");
+            printer.println("    }");
 
-        output.close();
+            printer.println("}");
+        }
     }
 
     private void changeReadmeTxt(File readmeTxt) throws Exception {
-        FileWriter output = null;
-
-        try {
-            output = new FileWriter(readmeTxt);
-
+        try (FileWriter output = new FileWriter(readmeTxt)) {
             output.write("changed file");
-        } finally {
-            IOUtil.close(output);
         }
     }
 }
diff --git 
a/maven-scm-test/src/main/java/org/apache/maven/scm/tck/command/tag/TagCommandTckTest.java
 
b/maven-scm-test/src/main/java/org/apache/maven/scm/tck/command/tag/TagCommandTckTest.java
index 73f192b4e..206e553be 100644
--- 
a/maven-scm-test/src/main/java/org/apache/maven/scm/tck/command/tag/TagCommandTckTest.java
+++ 
b/maven-scm-test/src/main/java/org/apache/maven/scm/tck/command/tag/TagCommandTckTest.java
@@ -19,7 +19,9 @@
 package org.apache.maven.scm.tck.command.tag;
 
 import java.io.File;
-import java.io.FileWriter;
+import java.io.Writer;
+import java.nio.file.Files;
+import java.nio.file.Path;
 
 import org.apache.maven.scm.ScmFileSet;
 import org.apache.maven.scm.ScmTag;
@@ -28,7 +30,6 @@ import org.apache.maven.scm.command.checkin.CheckInScmResult;
 import org.apache.maven.scm.command.checkout.CheckOutScmResult;
 import org.apache.maven.scm.command.tag.TagScmResult;
 import org.codehaus.plexus.util.FileUtils;
-import org.codehaus.plexus.util.IOUtil;
 import org.junit.Test;
 
 import static org.junit.Assert.assertEquals;
@@ -65,7 +66,7 @@ public abstract class TagCommandTckTest extends 
ScmTckTestCase {
         assertEquals("check readme.txt contents", "/readme.txt", 
FileUtils.fileRead(readmeTxt));
 
         this.edit(getWorkingCopy(), "readme.txt", null, getScmRepository());
-        changeReadmeTxt(readmeTxt);
+        changeReadmeTxt(readmeTxt.toPath());
 
         CheckInScmResult checkinResult =
                 getScmManager().checkIn(getScmRepository(), new 
ScmFileSet(getWorkingCopy()), "commit message");
@@ -94,12 +95,9 @@ public abstract class TagCommandTckTest extends 
ScmTckTestCase {
         assertEquals("check readme.txt contents is from tagged version", 
"/readme.txt", FileUtils.fileRead(readmeTxt));
     }
 
-    private void changeReadmeTxt(File readmeTxt) throws Exception {
-        FileWriter output = new FileWriter(readmeTxt);
-        try {
+    private void changeReadmeTxt(Path readmeTxt) throws Exception {
+        try (Writer output = Files.newBufferedWriter(readmeTxt)) {
             output.write("changed file");
-        } finally {
-            IOUtil.close(output);
         }
     }
 }

Reply via email to