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

rfscholte pushed a commit to branch maven-wrapper
in repository https://gitbox.apache.org/repos/asf/maven-studies.git


The following commit(s) were added to refs/heads/maven-wrapper by this push:
     new 0345e4f  Remove need of commons-io
0345e4f is described below

commit 0345e4f3fcec98dca9bcdc3493464e4c28cc2072
Author: rfscholte <[email protected]>
AuthorDate: Sat Feb 15 14:58:18 2020 +0100

    Remove need of commons-io
---
 pom.xml                                            |  6 ---
 .../apache/maven/wrapper/DefaultDownloader.java    | 31 +++--------
 .../java/org/apache/maven/wrapper/Installer.java   | 61 ++++++++++-----------
 .../org/apache/maven/wrapper/MavenWrapperMain.java | 16 +++---
 .../maven/wrapper/SystemPropertiesHandler.java     | 13 ++---
 .../org/apache/maven/wrapper/WrapperExecutor.java  |  7 +--
 .../org/apache/maven/wrapper/DownloaderTest.java   | 63 ++++++++++++----------
 .../org/apache/maven/wrapper/InstallerTest.java    | 43 +++++++--------
 .../maven/wrapper/SystemPropertiesHandlerTest.java | 10 ++--
 .../apache/maven/wrapper/WrapperExecutorTest.java  | 19 +++----
 10 files changed, 115 insertions(+), 154 deletions(-)

diff --git a/pom.xml b/pom.xml
index 3bfaa6a..29e82a6 100644
--- a/pom.xml
+++ b/pom.xml
@@ -44,12 +44,6 @@
       <version>2.6</version>
       <scope>test</scope>
     </dependency>
-    <dependency>
-      <groupId>commons-io</groupId>
-      <artifactId>commons-io</artifactId>
-      <version>2.6</version>
-      <scope>test</scope>
-    </dependency>
     <!-- Using zip util class, should be replaced with a zip lib -->
     <dependency>
       <groupId>org.apache.ant</groupId>
diff --git a/src/main/java/org/apache/maven/wrapper/DefaultDownloader.java 
b/src/main/java/org/apache/maven/wrapper/DefaultDownloader.java
index a665d1f..f9333a1 100644
--- a/src/main/java/org/apache/maven/wrapper/DefaultDownloader.java
+++ b/src/main/java/org/apache/maven/wrapper/DefaultDownloader.java
@@ -96,18 +96,15 @@ public class DefaultDownloader
     private void downloadInternal( URI address, File destination )
         throws Exception
     {
-        OutputStream out = null;
-        URLConnection conn;
-        InputStream in = null;
-        try
+        URL url = address.toURL();
+        URLConnection conn = url.openConnection();
+        addBasicAuthentication( address, conn );
+        final String userAgentValue = calculateUserAgent();
+        conn.setRequestProperty( "User-Agent", userAgentValue );
+
+        try ( OutputStream out = new BufferedOutputStream( new 
FileOutputStream( destination ) );
+              InputStream in = conn.getInputStream() )
         {
-            URL url = address.toURL();
-            out = new BufferedOutputStream( new FileOutputStream( destination 
) );
-            conn = url.openConnection();
-            addBasicAuthentication( address, conn );
-            final String userAgentValue = calculateUserAgent();
-            conn.setRequestProperty( "User-Agent", userAgentValue );
-            in = conn.getInputStream();
             byte[] buffer = new byte[BUFFER_SIZE];
             int numRead;
             long progressCounter = 0;
@@ -122,18 +119,6 @@ public class DefaultDownloader
                 out.write( buffer, 0, numRead );
             }
         }
-        finally
-        {
-            Logger.info( "" );
-            if ( in != null )
-            {
-                in.close();
-            }
-            if ( out != null )
-            {
-                out.close();
-            }
-        }
     }
 
     private void addBasicAuthentication( URI address, URLConnection connection 
)
diff --git a/src/main/java/org/apache/maven/wrapper/Installer.java 
b/src/main/java/org/apache/maven/wrapper/Installer.java
index dab1839..6a864f3 100644
--- a/src/main/java/org/apache/maven/wrapper/Installer.java
+++ b/src/main/java/org/apache/maven/wrapper/Installer.java
@@ -151,13 +151,15 @@ public class Installer
             else
             {
                 BufferedReader is = new BufferedReader( new InputStreamReader( 
p.getInputStream() ) );
-                Formatter stdout = new Formatter();
-                String line;
-                while ( ( line = is.readLine() ) != null )
+                try ( Formatter stdout = new Formatter() )
                 {
-                    stdout.format( "%s%n", line );
+                    String line;
+                    while ( ( line = is.readLine() ) != null )
+                    {
+                        stdout.format( "%s%n", line );
+                    }
+                    errorMessage = stdout.toString();
                 }
-                errorMessage = stdout.toString();
             }
         }
         catch ( IOException e )
@@ -207,43 +209,42 @@ public class Installer
     public void unzip( File zip, File dest )
         throws IOException
     {
-        Enumeration entries;
-        ZipFile zipFile;
-
-        zipFile = new ZipFile( zip );
-
-        entries = zipFile.entries();
-
-        while ( entries.hasMoreElements() )
+        
+        try ( ZipFile zipFile = new ZipFile( zip ) )
         {
-            ZipEntry entry = (ZipEntry) entries.nextElement();
+            Enumeration<? extends ZipEntry> entries = zipFile.entries();
 
-            if ( entry.isDirectory() )
+            while ( entries.hasMoreElements() )
             {
-                ( new File( dest, entry.getName() ) ).mkdirs();
-                continue;
-            }
+                ZipEntry entry = entries.nextElement();
+
+                if ( entry.isDirectory() )
+                {
+                    ( new File( dest, entry.getName() ) ).mkdirs();
+                    continue;
+                }
 
-            new File( dest, entry.getName() ).getParentFile().mkdirs();
-            copyInputStream( zipFile.getInputStream( entry ),
-                             new BufferedOutputStream( new FileOutputStream( 
new File( dest, entry.getName() ) ) ) );
+                new File( dest, entry.getName() ).getParentFile().mkdirs();
+                copyInputStream( zipFile.getInputStream( entry ),
+                                new BufferedOutputStream( new 
FileOutputStream( new File( dest, entry.getName() ) ) ) );
+            }
         }
-        zipFile.close();
     }
 
     public void copyInputStream( InputStream in, OutputStream out )
         throws IOException
     {
-        byte[] buffer = new byte[1024];
-        int len;
-
-        while ( ( len = in.read( buffer ) ) >= 0 )
+        try ( InputStream is = in;
+              OutputStream os = out ) 
         {
-            out.write( buffer, 0, len );
-        }
+            byte[] buffer = new byte[1024];
+            int len;
 
-        in.close();
-        out.close();
+            while ( ( len = is.read( buffer ) ) >= 0 )
+            {
+                os.write( buffer, 0, len );
+            }            
+        }
     }
 
 }
diff --git a/src/main/java/org/apache/maven/wrapper/MavenWrapperMain.java 
b/src/main/java/org/apache/maven/wrapper/MavenWrapperMain.java
index 13f5daf..3be98a5 100644
--- a/src/main/java/org/apache/maven/wrapper/MavenWrapperMain.java
+++ b/src/main/java/org/apache/maven/wrapper/MavenWrapperMain.java
@@ -122,17 +122,17 @@ public class MavenWrapperMain
 
     static String wrapperVersion()
     {
-        try
+        try 
         {
-            InputStream resourceAsStream =
-                MavenWrapperMain.class.getResourceAsStream( 
"/META-INF/maven/io.takari/maven-wrapper/pom.properties" );
+            try ( InputStream resourceAsStream =
+               MavenWrapperMain.class.getResourceAsStream( 
"/META-INF/maven/io.takari/maven-wrapper/pom.properties" ) )
+            {
+            
             if ( resourceAsStream == null )
             {
                 throw new RuntimeException( "No maven properties found." );
             }
-            Properties mavenProperties = new Properties();
-            try
-            {
+                Properties mavenProperties = new Properties();
                 mavenProperties.load( resourceAsStream );
                 String version = mavenProperties.getProperty( "version" );
                 if ( version == null )
@@ -141,10 +141,6 @@ public class MavenWrapperMain
                 }
                 return version;
             }
-            finally
-            {
-                resourceAsStream.close();
-            }
         }
         catch ( Exception e )
         {
diff --git 
a/src/main/java/org/apache/maven/wrapper/SystemPropertiesHandler.java 
b/src/main/java/org/apache/maven/wrapper/SystemPropertiesHandler.java
index abdc546..e1ecfaa 100644
--- a/src/main/java/org/apache/maven/wrapper/SystemPropertiesHandler.java
+++ b/src/main/java/org/apache/maven/wrapper/SystemPropertiesHandler.java
@@ -41,18 +41,11 @@ public class SystemPropertiesHandler
         {
             return propertyMap;
         }
+
         Properties properties = new Properties();
-        try
+        try ( FileInputStream inStream = new FileInputStream( propertiesFile ) 
)
         {
-            FileInputStream inStream = new FileInputStream( propertiesFile );
-            try
-            {
-                properties.load( inStream );
-            }
-            finally
-            {
-                inStream.close();
-            }
+            properties.load( inStream );
         }
         catch ( IOException e )
         {
diff --git a/src/main/java/org/apache/maven/wrapper/WrapperExecutor.java 
b/src/main/java/org/apache/maven/wrapper/WrapperExecutor.java
index 8484e57..818523d 100644
--- a/src/main/java/org/apache/maven/wrapper/WrapperExecutor.java
+++ b/src/main/java/org/apache/maven/wrapper/WrapperExecutor.java
@@ -121,15 +121,10 @@ public class WrapperExecutor
     private static void loadProperties( File propertiesFile, Properties 
properties )
         throws IOException
     {
-        InputStream inStream = new FileInputStream( propertiesFile );
-        try
+        try ( InputStream inStream = new FileInputStream( propertiesFile ) )
         {
             properties.load( inStream );
         }
-        finally
-        {
-            inStream.close();
-        }
     }
 
     /**
diff --git a/src/test/java/org/apache/maven/wrapper/DownloaderTest.java 
b/src/test/java/org/apache/maven/wrapper/DownloaderTest.java
index 2f72e0c..ae8ea06 100644
--- a/src/test/java/org/apache/maven/wrapper/DownloaderTest.java
+++ b/src/test/java/org/apache/maven/wrapper/DownloaderTest.java
@@ -23,43 +23,50 @@ import static org.junit.Assert.assertEquals;
 
 import java.io.File;
 import java.net.URI;
+import java.nio.file.Files;
+import java.util.Arrays;
+import java.util.stream.Collectors;
 
-import org.apache.commons.io.FileUtils;
 import org.junit.Before;
+import org.junit.Rule;
 import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
 
-public class DownloaderTest {
+public class DownloaderTest
+{
+    @Rule
+    public TemporaryFolder testDir = new TemporaryFolder();
 
-  private DefaultDownloader download;
+    private DefaultDownloader download;
 
-  private File testDir;
+    private File downloadFile;
 
-  private File downloadFile;
+    private File rootDir;
 
-  private File rootDir;
+    private URI sourceRoot;
 
-  private URI sourceRoot;
+    private File remoteFile;
 
-  private File remoteFile;
+    @Before
+    public void setUp()
+        throws Exception
+    {
+        download = new DefaultDownloader( "mvnw", "aVersion" );
+        rootDir = testDir.newFolder( "root" );
+        downloadFile = new File( rootDir, "file" );
+        remoteFile = testDir.newFile( "remoteFile" );
+        Files.write( remoteFile.toPath(), Arrays.asList( "sometext" ) );
+        sourceRoot = remoteFile.toURI();
+    }
 
-  @Before
-  public void setUp() throws Exception {
-    download = new DefaultDownloader("mvnw", "aVersion");
-    testDir = new File("target/test-files/DownloadTest");
-    rootDir = new File(testDir, "root");
-    downloadFile = new File(rootDir, "file");
-    if (downloadFile.exists())
-      downloadFile.delete();
-    remoteFile = new File(testDir, "remoteFile");
-    FileUtils.write(remoteFile, "sometext");
-    sourceRoot = remoteFile.toURI();
-  }
-
-  @Test
-  public void testDownload() throws Exception {
-    assert !downloadFile.exists();
-    download.download(sourceRoot, downloadFile);
-    assert downloadFile.exists();
-    assertEquals("sometext", FileUtils.readFileToString(downloadFile));
-  }
+    @Test
+    public void testDownload()
+        throws Exception
+    {
+        assert !downloadFile.exists();
+        download.download( sourceRoot, downloadFile );
+        assert downloadFile.exists();
+        assertEquals( "sometext",
+                      Files.readAllLines( downloadFile.toPath() 
).stream().collect( Collectors.joining() ) );
+    }
 }
diff --git a/src/test/java/org/apache/maven/wrapper/InstallerTest.java 
b/src/test/java/org/apache/maven/wrapper/InstallerTest.java
index b25599b..9c70c2e 100644
--- a/src/test/java/org/apache/maven/wrapper/InstallerTest.java
+++ b/src/test/java/org/apache/maven/wrapper/InstallerTest.java
@@ -24,30 +24,28 @@ import static org.mockito.Mockito.when;
 
 import java.io.File;
 import java.net.URI;
+import java.nio.file.Files;
+import java.util.Arrays;
 
-import org.apache.commons.io.FileUtils;
 import org.apache.tools.ant.Project;
 import org.apache.tools.ant.taskdefs.Zip;
 import org.junit.Assert;
 import org.junit.Before;
+import org.junit.Rule;
 import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
 
 /**
  * @author Hans Dockter
  */
 public class InstallerTest {
-  private File testDir = new 
File("target/test-files/SystemPropertiesHandlerTest-" + 
System.currentTimeMillis());
+    
+    @Rule
+    public TemporaryFolder temporaryFolder = new TemporaryFolder();
+    
 
   private Installer install;
 
-  private Downloader downloadMock;
-
-  private PathAssembler pathAssemblerMock;
-
-  private boolean downloadCalled;
-
-  private File zip;
-
   private File distributionDir;
 
   private File zipStore;
@@ -66,10 +64,6 @@ public class InstallerTest {
 
   @Before
   public void setup() throws Exception {
-
-    testDir.mkdirs();
-
-    downloadCalled = false;
     configuration.setZipBase(PathAssembler.PROJECT_STRING);
     configuration.setZipPath("someZipPath");
     configuration.setDistributionBase(PathAssembler.MAVEN_USER_HOME_STRING);
@@ -77,9 +71,9 @@ public class InstallerTest {
     configuration.setDistribution(new URI("http://server/maven-0.9.zip";));
     configuration.setAlwaysDownload(false);
     configuration.setAlwaysUnpack(false);
-    distributionDir = new File(testDir, "someDistPath");
+    distributionDir = temporaryFolder.newFolder( "someDistPath" );
     mavenHomeDir = new File(distributionDir, "maven-0.9");
-    zipStore = new File(testDir, "zips");
+    zipStore = temporaryFolder.newFolder( "zips" );
     zipDestination = new File(zipStore, "maven-0.9.zip");
 
     download = mock(Downloader.class);
@@ -95,12 +89,12 @@ public class InstallerTest {
   }
 
   private void createTestZip(File zipDestination) throws Exception {
-    File explodedZipDir = new File(testDir, "explodedZip");
-    explodedZipDir.mkdirs();
+    File explodedZipDir = temporaryFolder.newFolder( "explodedZip");
+
     zipDestination.getParentFile().mkdirs();
     File mavenScript = new File(explodedZipDir, "maven-0.9/bin/mvn");
     mavenScript.getParentFile().mkdirs();
-    FileUtils.write(mavenScript, "something");
+    Files.write( mavenScript.toPath(), Arrays.asList( "something" ) );
 
     zipTo(explodedZipDir, zipDestination);
   }
@@ -123,11 +117,11 @@ public class InstallerTest {
 
   @Test
   public void testCreateDistWithExistingDistribution() throws Exception {
+      Files.createFile( zipDestination.toPath() );
 
-    FileUtils.touch(zipDestination);
     mavenHomeDir.mkdirs();
     File someFile = new File(mavenHomeDir, "some-file");
-    FileUtils.touch(someFile);
+    Files.createFile( someFile.toPath() );
 
     File homeDir = install.createDist(configuration);
 
@@ -147,7 +141,8 @@ public class InstallerTest {
     createTestZip(zipDestination);
     mavenHomeDir.mkdirs();
     File garbage = new File(mavenHomeDir, "garbage");
-    FileUtils.touch(garbage);
+    Files.createFile( garbage.toPath() );
+
     configuration.setAlwaysUnpack(true);
 
     File homeDir = install.createDist(configuration);
@@ -166,8 +161,10 @@ public class InstallerTest {
   public void testCreateDistWithExistingZipAndDistAndAlwaysDownloadTrue() 
throws Exception {
 
     createTestZip(zipDestination);
+    mavenHomeDir.mkdirs();
     File garbage = new File(mavenHomeDir, "garbage");
-    FileUtils.touch(garbage);
+    Files.createFile( garbage.toPath() );
+    
     configuration.setAlwaysUnpack(true);
 
     File homeDir = install.createDist(configuration);
diff --git 
a/src/test/java/org/apache/maven/wrapper/SystemPropertiesHandlerTest.java 
b/src/test/java/org/apache/maven/wrapper/SystemPropertiesHandlerTest.java
index df8c5b9..05a44c6 100644
--- a/src/test/java/org/apache/maven/wrapper/SystemPropertiesHandlerTest.java
+++ b/src/test/java/org/apache/maven/wrapper/SystemPropertiesHandlerTest.java
@@ -28,7 +28,6 @@ import java.util.HashMap;
 import java.util.Map;
 import java.util.Properties;
 
-import org.apache.commons.io.IOUtils;
 import org.junit.Before;
 import org.junit.Test;
 
@@ -49,12 +48,9 @@ public class SystemPropertiesHandlerTest {
     props.put("systemProp.c", "d");
     props.put("systemProp.", "e");
 
-    FileOutputStream fos = null;
-    try {
-      fos = new FileOutputStream(propFile);
-      props.store(fos, "");
-    } finally {
-      IOUtils.closeQuietly(fos);
+    try ( FileOutputStream fos = new FileOutputStream( propFile ) )
+    {
+        props.store(fos, "");
     }
 
     Map<String, String> expected = new HashMap<String, String>();
diff --git a/src/test/java/org/apache/maven/wrapper/WrapperExecutorTest.java 
b/src/test/java/org/apache/maven/wrapper/WrapperExecutorTest.java
index 2c151a0..c122b66 100644
--- a/src/test/java/org/apache/maven/wrapper/WrapperExecutorTest.java
+++ b/src/test/java/org/apache/maven/wrapper/WrapperExecutorTest.java
@@ -29,7 +29,6 @@ import java.io.OutputStream;
 import java.net.URI;
 import java.util.Properties;
 
-import org.apache.commons.io.IOUtils;
 import org.junit.Assert;
 import org.junit.Test;
 import org.mockito.Mockito;
@@ -170,16 +169,14 @@ public class WrapperExecutorTest {
     
Assert.assertTrue(wrapper.getDistribution().getSchemeSpecificPart().endsWith("some/relative/url/to/bin.zip"));
   }
 
-  private void writePropertiesFile(Properties properties, File propertiesFile, 
String message) throws Exception {
+    private void writePropertiesFile( Properties properties, File 
propertiesFile, String message )
+        throws Exception
+    {
+        propertiesFile.getParentFile().mkdirs();
 
-    propertiesFile.getParentFile().mkdirs();
-
-    OutputStream outStream = null;
-    try {
-      outStream = new FileOutputStream(propertiesFile);
-      properties.store(outStream, message);
-    } finally {
-      IOUtils.closeQuietly(outStream);
+        try ( OutputStream outStream = new FileOutputStream( propertiesFile ) )
+        {
+            properties.store( outStream, message );
+        }
     }
-  }
 }

Reply via email to