You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by rf...@apache.org on 2020/02/16 22:08:48 UTC
[maven-studies] branch maven-wrapper updated: Refactor File to Path
Activate Jenkisfile
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 4cfabfd Refactor File to Path Activate Jenkisfile
4cfabfd is described below
commit 4cfabfda857423a31d12bd1b9b18481c20e373de
Author: rfscholte <rf...@apache.org>
AuthorDate: Sun Feb 16 23:08:39 2020 +0100
Refactor File to Path
Activate Jenkisfile
---
.../maven/wrapper/Downloader.java => Jenkinsfile | 16 +-
pom.xml | 12 +-
.../apache/maven/wrapper/BootstrapMainStarter.java | 31 ++-
.../apache/maven/wrapper/DefaultDownloader.java | 49 +---
.../java/org/apache/maven/wrapper/Downloader.java | 4 +-
.../java/org/apache/maven/wrapper/Installer.java | 104 +++-----
.../org/apache/maven/wrapper/MavenWrapperMain.java | 40 +--
.../org/apache/maven/wrapper/PathAssembler.java | 32 ++-
.../maven/wrapper/SystemPropertiesHandler.java | 11 +-
.../org/apache/maven/wrapper/WrapperExecutor.java | 33 ++-
.../org/apache/maven/wrapper/DownloaderTest.java | 24 +-
.../org/apache/maven/wrapper/InstallerTest.java | 268 ++++++++++---------
.../apache/maven/wrapper/PathAssemblerTest.java | 37 ++-
.../maven/wrapper/SystemPropertiesHandlerTest.java | 63 ++---
.../apache/maven/wrapper/WrapperExecutorTest.java | 290 +++++++++++----------
15 files changed, 497 insertions(+), 517 deletions(-)
diff --git a/src/main/java/org/apache/maven/wrapper/Downloader.java b/Jenkinsfile
similarity index 78%
copy from src/main/java/org/apache/maven/wrapper/Downloader.java
copy to Jenkinsfile
index c61931d..09ac70f 100644
--- a/src/main/java/org/apache/maven/wrapper/Downloader.java
+++ b/Jenkinsfile
@@ -1,6 +1,4 @@
-package org.apache.maven.wrapper;
-
-/*
+/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
@@ -19,14 +17,4 @@ package org.apache.maven.wrapper;
* under the License.
*/
-import java.io.File;
-import java.net.URI;
-
-/**
- * @author Hans Dockter
- */
-public interface Downloader
-{
- void download( URI address, File destination )
- throws Exception;
-}
+asfMavenTlpStdBuild()
diff --git a/pom.xml b/pom.xml
index 8266547..4dd34c9 100644
--- a/pom.xml
+++ b/pom.xml
@@ -47,13 +47,13 @@
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
- <version>4.12</version>
+ <version>4.13</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
- <artifactId>hamcrest-all</artifactId>
- <version>1.3</version>
+ <artifactId>hamcrest</artifactId>
+ <version>2.2</version>
<scope>test</scope>
</dependency>
<dependency>
@@ -62,12 +62,6 @@
<version>2.8.47</version>
<scope>test</scope>
</dependency>
- <dependency>
- <groupId>commons-lang</groupId>
- <artifactId>commons-lang</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/BootstrapMainStarter.java b/src/main/java/org/apache/maven/wrapper/BootstrapMainStarter.java
index e18660a..993f2df 100644
--- a/src/main/java/org/apache/maven/wrapper/BootstrapMainStarter.java
+++ b/src/main/java/org/apache/maven/wrapper/BootstrapMainStarter.java
@@ -19,42 +19,41 @@ package org.apache.maven.wrapper;
* under the License.
*/
-import java.io.File;
+import java.io.IOException;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
+import java.nio.file.Files;
+import java.nio.file.Path;
/**
* @author Hans Dockter
*/
public class BootstrapMainStarter
{
- public void start( String[] args, File mavenHome )
+ public void start( String[] args, Path mavenHome )
throws Exception
{
- File mavenJar = findLauncherJar( mavenHome );
- URLClassLoader contextClassLoader = new URLClassLoader( new URL[] { mavenJar.toURI().toURL() },
+ Path mavenJar = findLauncherJar( mavenHome );
+ URLClassLoader contextClassLoader = new URLClassLoader( new URL[] { mavenJar.toUri().toURL() },
ClassLoader.getSystemClassLoader().getParent() );
Thread.currentThread().setContextClassLoader( contextClassLoader );
Class<?> mainClass = contextClassLoader.loadClass( "org.codehaus.plexus.classworlds.launcher.Launcher" );
- System.setProperty( "maven.home", mavenHome.getAbsolutePath() );
- System.setProperty( "classworlds.conf", new File( mavenHome, "/bin/m2.conf" ).getAbsolutePath() );
+ System.setProperty( "maven.home", mavenHome.toAbsolutePath().toString() );
+ System.setProperty( "classworlds.conf", mavenHome.resolve( "/bin/m2.conf" ).toAbsolutePath().toString() );
Method mainMethod = mainClass.getMethod( "main", String[].class );
mainMethod.invoke( null, new Object[] { args } );
}
- private File findLauncherJar( File mavenHome )
+ private Path findLauncherJar( Path mavenHome ) throws RuntimeException, IOException
{
- for ( File file : new File( mavenHome, "boot" ).listFiles() )
- {
- if ( file.getName().matches( "plexus-classworlds-.*\\.jar" ) )
- {
- return file;
- }
- }
- throw new RuntimeException( String.format( "Couldn't locate the Maven launcher JAR in Maven distribution '%s'.",
- mavenHome ) );
+ return Files.list( mavenHome.resolve( "boot" ) )
+ .filter( p -> p.getFileName().toString().matches( "plexus-classworlds-.*\\.jar" ) )
+ .findFirst()
+ .orElseThrow( () -> new RuntimeException(
+ String.format( "Couldn't locate the Maven launcher JAR in Maven distribution '%s'.",
+ mavenHome ) ) );
}
}
diff --git a/src/main/java/org/apache/maven/wrapper/DefaultDownloader.java b/src/main/java/org/apache/maven/wrapper/DefaultDownloader.java
index f9333a1..b4a5a5a 100644
--- a/src/main/java/org/apache/maven/wrapper/DefaultDownloader.java
+++ b/src/main/java/org/apache/maven/wrapper/DefaultDownloader.java
@@ -23,17 +23,18 @@ import static org.apache.maven.wrapper.MavenWrapperMain.MVNW_PASSWORD;
import static org.apache.maven.wrapper.MavenWrapperMain.MVNW_USERNAME;
import java.io.BufferedOutputStream;
-import java.io.File;
-import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
-import java.lang.reflect.Method;
import java.net.Authenticator;
import java.net.PasswordAuthentication;
import java.net.URI;
import java.net.URL;
import java.net.URLConnection;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.Base64;
/**
* @author Hans Dockter
@@ -81,19 +82,20 @@ public class DefaultDownloader
}
}
- public void download( URI address, File destination )
+ @Override
+ public void download( URI address, Path destination )
throws Exception
{
- if ( destination.exists() )
+ if ( Files.exists( destination ) )
{
return;
}
- destination.getParentFile().mkdirs();
+ Files.createDirectories( destination.getParent() );
downloadInternal( address, destination );
}
- private void downloadInternal( URI address, File destination )
+ private void downloadInternal( URI address, Path destination )
throws Exception
{
URL url = address.toURL();
@@ -102,7 +104,7 @@ public class DefaultDownloader
final String userAgentValue = calculateUserAgent();
conn.setRequestProperty( "User-Agent", userAgentValue );
- try ( OutputStream out = new BufferedOutputStream( new FileOutputStream( destination ) );
+ try ( OutputStream out = new BufferedOutputStream( Files.newOutputStream( destination ) );
InputStream in = conn.getInputStream() )
{
byte[] buffer = new byte[BUFFER_SIZE];
@@ -138,10 +140,7 @@ public class DefaultDownloader
}
/**
- * Base64 encode user info for HTTP Basic Authentication. Try to use {@literal java.util.Base64} encoder which is
- * available starting with Java 8. Fallback to {@literal javax.xml.bind.DatatypeConverter} from JAXB which is
- * available starting with Java 6 but is not anymore in Java 9. Fortunately, both of these two Base64 encoders
- * implement the right Base64 flavor, the one that does not split the output in multiple lines.
+ * Base64 encode user info for HTTP Basic Authentication.
*
* @param userInfo user info
* @return Base64 encoded user info
@@ -149,31 +148,7 @@ public class DefaultDownloader
*/
private String base64Encode( String userInfo )
{
- ClassLoader loader = getClass().getClassLoader();
- try
- {
- Method getEncoderMethod = loader.loadClass( "java.util.Base64" ).getMethod( "getEncoder" );
- Method encodeMethod =
- loader.loadClass( "java.util.Base64$Encoder" ).getMethod( "encodeToString", byte[].class );
- Object encoder = getEncoderMethod.invoke( null );
- return (String) encodeMethod.invoke( encoder, new Object[] { userInfo.getBytes( "UTF-8" ) } );
- }
- catch ( Exception java7OrEarlier )
- {
- try
- {
- Method encodeMethod =
- loader.loadClass( "javax.xml.bind.DatatypeConverter" ).getMethod( "printBase64Binary",
- byte[].class );
- return (String) encodeMethod.invoke( null, new Object[] { userInfo.getBytes( "UTF-8" ) } );
- }
- catch ( Exception java5OrEarlier )
- {
- throw new RuntimeException(
- "Downloading Maven distributions with HTTP Basic Authentication is not supported on your JVM.",
- java5OrEarlier );
- }
- }
+ return Base64.getEncoder().encodeToString( userInfo.getBytes( StandardCharsets.UTF_8 ) );
}
private String calculateUserInfo( URI uri )
diff --git a/src/main/java/org/apache/maven/wrapper/Downloader.java b/src/main/java/org/apache/maven/wrapper/Downloader.java
index c61931d..ab51b56 100644
--- a/src/main/java/org/apache/maven/wrapper/Downloader.java
+++ b/src/main/java/org/apache/maven/wrapper/Downloader.java
@@ -19,14 +19,14 @@ package org.apache.maven.wrapper;
* under the License.
*/
-import java.io.File;
import java.net.URI;
+import java.nio.file.Path;
/**
* @author Hans Dockter
*/
public interface Downloader
{
- void download( URI address, File destination )
+ void download( URI address, Path destination )
throws Exception;
}
diff --git a/src/main/java/org/apache/maven/wrapper/Installer.java b/src/main/java/org/apache/maven/wrapper/Installer.java
index 6a864f3..1be51c4 100644
--- a/src/main/java/org/apache/maven/wrapper/Installer.java
+++ b/src/main/java/org/apache/maven/wrapper/Installer.java
@@ -19,15 +19,16 @@ package org.apache.maven.wrapper;
* under the License.
*/
-import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
-import java.io.FileOutputStream;
import java.io.IOException;
-import java.io.InputStream;
import java.io.InputStreamReader;
-import java.io.OutputStream;
import java.net.URI;
+import java.nio.file.FileVisitResult;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.SimpleFileVisitor;
+import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Formatter;
@@ -54,7 +55,7 @@ public class Installer
this.pathAssembler = pathAssembler;
}
- public File createDist( WrapperConfiguration configuration )
+ public Path createDist( WrapperConfiguration configuration )
throws Exception
{
URI distributionUrl;
@@ -74,31 +75,30 @@ public class Installer
PathAssembler.LocalDistribution localDistribution = pathAssembler.getDistribution( configuration );
- File localZipFile = localDistribution.getZipFile();
+ Path localZipFile = localDistribution.getZipFile();
boolean downloaded = false;
- if ( alwaysDownload || !localZipFile.exists() )
+ if ( alwaysDownload || !Files.exists( localZipFile ) )
{
- File tmpZipFile = new File( localZipFile.getParentFile(), localZipFile.getName() + ".part" );
- tmpZipFile.delete();
+ Path tmpZipFile = localZipFile.resolveSibling( localZipFile.getFileName() + ".part" );
+ Files.delete( tmpZipFile );
Logger.info( "Downloading " + distributionUrl );
download.download( distributionUrl, tmpZipFile );
- tmpZipFile.renameTo( localZipFile );
+ Files.move( tmpZipFile, localZipFile );
downloaded = true;
}
- File distDir = localDistribution.getDistributionDir();
- List<File> dirs = listDirs( distDir );
+ Path distDir = localDistribution.getDistributionDir();
+ List<File> dirs = listDirs( distDir.toFile() );
if ( downloaded || alwaysUnpack || dirs.isEmpty() )
{
- for ( File dir : dirs )
- {
- Logger.info( "Deleting directory " + dir.getAbsolutePath() );
- deleteDir( dir );
- }
- Logger.info( "Unzipping " + localZipFile.getAbsolutePath() + " to " + distDir.getAbsolutePath() );
+ Logger.info( "Deleting directory " + distDir.toAbsolutePath() );
+ deleteDir( distDir );
+
+ Logger.info( "Unzipping " + localZipFile.toAbsolutePath() + " to " + distDir.toAbsolutePath() );
unzip( localZipFile, distDir );
- dirs = listDirs( distDir );
+
+ dirs = listDirs( distDir.toFile() );
if ( dirs.isEmpty() )
{
throw new RuntimeException( String.format(
@@ -113,7 +113,7 @@ public class Installer
"Maven distribution '%s' contains too many directories. Expected to find exactly 1 directory.",
distributionUrl ) );
}
- return dirs.get( 0 );
+ return dirs.get( 0 ).toPath();
}
private List<File> listDirs( File distDir )
@@ -162,11 +162,7 @@ public class Installer
}
}
}
- catch ( IOException e )
- {
- errorMessage = e.getMessage();
- }
- catch ( InterruptedException e )
+ catch ( IOException | InterruptedException e )
{
errorMessage = e.getMessage();
}
@@ -187,30 +183,32 @@ public class Installer
return false;
}
- private boolean deleteDir( File dir )
+ private Path deleteDir( Path dir ) throws IOException
{
- if ( dir.isDirectory() )
+ return Files.walkFileTree( dir, new SimpleFileVisitor<Path>()
{
- String[] children = dir.list();
- for ( int i = 0; i < children.length; i++ )
+ @Override
+ public FileVisitResult postVisitDirectory( Path dir, IOException exc )
+ throws IOException
{
- boolean success = deleteDir( new File( dir, children[i] ) );
- if ( !success )
- {
- return false;
- }
+ Files.delete( dir );
+ return FileVisitResult.CONTINUE;
}
- }
- // The directory is now empty so delete it
- return dir.delete();
+ @Override
+ public FileVisitResult visitFile( Path file, BasicFileAttributes attrs )
+ throws IOException
+ {
+ Files.delete( file );
+ return FileVisitResult.CONTINUE;
+ }
+ } );
}
- public void unzip( File zip, File dest )
+ private void unzip( Path zip, Path dest )
throws IOException
{
-
- try ( ZipFile zipFile = new ZipFile( zip ) )
+ try ( ZipFile zipFile = new ZipFile( zip.toFile() ) )
{
Enumeration<? extends ZipEntry> entries = zipFile.entries();
@@ -220,31 +218,15 @@ public class Installer
if ( entry.isDirectory() )
{
- ( new File( dest, entry.getName() ) ).mkdirs();
+ Files.createDirectories( dest.resolve( entry.getName() ) );
continue;
}
- new File( dest, entry.getName() ).getParentFile().mkdirs();
- copyInputStream( zipFile.getInputStream( entry ),
- new BufferedOutputStream( new FileOutputStream( new File( dest, entry.getName() ) ) ) );
- }
- }
- }
-
- public void copyInputStream( InputStream in, OutputStream out )
- throws IOException
- {
- try ( InputStream is = in;
- OutputStream os = out )
- {
- byte[] buffer = new byte[1024];
- int len;
+ Path targetFile = dest.resolve( entry.getName() );
+ Files.createDirectories( targetFile.getParent() );
- while ( ( len = is.read( buffer ) ) >= 0 )
- {
- os.write( buffer, 0, len );
- }
+ Files.copy( zipFile.getInputStream( entry ), targetFile );
+ }
}
}
-
}
diff --git a/src/main/java/org/apache/maven/wrapper/MavenWrapperMain.java b/src/main/java/org/apache/maven/wrapper/MavenWrapperMain.java
index 0acfe4b..572f3d1 100644
--- a/src/main/java/org/apache/maven/wrapper/MavenWrapperMain.java
+++ b/src/main/java/org/apache/maven/wrapper/MavenWrapperMain.java
@@ -19,10 +19,11 @@ package org.apache.maven.wrapper;
* under the License.
*/
-import java.io.File;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
+import java.nio.file.Path;
+import java.nio.file.Paths;
import java.util.Map;
import java.util.Properties;
@@ -58,9 +59,9 @@ public class MavenWrapperMain
public static void main( String[] args )
throws Exception
{
- File wrapperJar = wrapperJar();
- File propertiesFile = wrapperProperties( wrapperJar );
- File rootDir = rootDir( wrapperJar );
+ Path wrapperJar = wrapperJar();
+ Path propertiesFile = wrapperProperties( wrapperJar );
+ Path rootDir = rootDir( wrapperJar );
String wrapperVersion = wrapperVersion();
Logger.info( "Apache Maven Wrapper " + wrapperVersion );
@@ -85,25 +86,26 @@ public class MavenWrapperMain
return converter.convert( commandLineParser.parse( args ) );
}
- private static void addSystemProperties( File rootDir )
+ private static void addSystemProperties( Path rootDir )
{
- System.getProperties().putAll( SystemPropertiesHandler.getSystemProperties( new File( mavenUserHome(),
- "maven.properties" ) ) );
- System.getProperties().putAll( SystemPropertiesHandler.getSystemProperties( new File( rootDir,
- "maven.properties" ) ) );
+ System.getProperties().putAll(
+ SystemPropertiesHandler.getSystemProperties( mavenUserHome().resolve( "maven.properties" ) ) );
+ System.getProperties().putAll(
+ SystemPropertiesHandler.getSystemProperties( rootDir.resolve( "maven.properties" ) ) );
}
- private static File rootDir( File wrapperJar )
+ private static Path rootDir( Path wrapperJar )
{
- return wrapperJar.getParentFile().getParentFile().getParentFile();
+ return wrapperJar.getParent().getParent().getParent();
}
- private static File wrapperProperties( File wrapperJar )
+ private static Path wrapperProperties( Path wrapperJar )
{
- return new File( wrapperJar.getParent(), wrapperJar.getName().replaceFirst( "\\.jar$", ".properties" ) );
+ return wrapperJar().resolveSibling( wrapperJar.getFileName().toString().replaceFirst( "\\.jar$",
+ ".properties" ) );
}
- private static File wrapperJar()
+ private static Path wrapperJar()
{
URI location;
try
@@ -119,7 +121,7 @@ public class MavenWrapperMain
throw new RuntimeException( String.format( "Cannot determine classpath for wrapper Jar from codebase '%s'.",
location ) );
}
- return new File( location.getPath() );
+ return Paths.get( location.getPath() );
}
static String wrapperVersion()
@@ -150,22 +152,22 @@ public class MavenWrapperMain
}
}
- private static File mavenUserHome()
+ private static Path mavenUserHome()
{
String mavenUserHome = System.getProperty( MAVEN_USER_HOME_PROPERTY_KEY );
if ( mavenUserHome != null )
{
- return new File( mavenUserHome );
+ return Paths.get( mavenUserHome );
}
mavenUserHome = System.getenv( MAVEN_USER_HOME_ENV_KEY );
if ( mavenUserHome != null )
{
- return new File( mavenUserHome );
+ return Paths.get( mavenUserHome );
}
else
{
- return new File( DEFAULT_MAVEN_USER_HOME );
+ return Paths.get( DEFAULT_MAVEN_USER_HOME );
}
}
}
diff --git a/src/main/java/org/apache/maven/wrapper/PathAssembler.java b/src/main/java/org/apache/maven/wrapper/PathAssembler.java
index bb2111e..8d544d9 100644
--- a/src/main/java/org/apache/maven/wrapper/PathAssembler.java
+++ b/src/main/java/org/apache/maven/wrapper/PathAssembler.java
@@ -19,9 +19,10 @@ package org.apache.maven.wrapper;
* under the License.
*/
-import java.io.File;
import java.math.BigInteger;
import java.net.URI;
+import java.nio.file.Path;
+import java.nio.file.Paths;
import java.security.MessageDigest;
/**
@@ -33,13 +34,13 @@ public class PathAssembler
public static final String PROJECT_STRING = "PROJECT";
- private File mavenUserHome;
+ private Path mavenUserHome;
public PathAssembler()
{
}
- public PathAssembler( File mavenUserHome )
+ public PathAssembler( Path mavenUserHome )
{
this.mavenUserHome = mavenUserHome;
}
@@ -52,10 +53,13 @@ public class PathAssembler
String baseName = getDistName( configuration.getDistribution() );
String distName = removeExtension( baseName );
String rootDirName = rootDirName( distName, configuration );
- File distDir = new File( getBaseDir( configuration.getDistributionBase() ),
- configuration.getDistributionPath() + "/" + rootDirName );
- File distZip = new File( getBaseDir( configuration.getZipBase() ),
- configuration.getZipPath() + "/" + rootDirName + "/" + baseName );
+ Path distDir = getBaseDir( configuration.getDistributionBase() )
+ .resolve( configuration.getDistributionPath() )
+ .resolve( rootDirName );
+ Path distZip = getBaseDir( configuration.getZipBase() )
+ .resolve( configuration.getZipPath() )
+ .resolve( rootDirName )
+ .resolve( baseName );
return new LocalDistribution( distDir, distZip );
}
@@ -101,7 +105,7 @@ public class PathAssembler
return path.substring( p + 1 );
}
- private File getBaseDir( String base )
+ private Path getBaseDir( String base )
{
if ( base.equals( MAVEN_USER_HOME_STRING ) )
{
@@ -109,7 +113,7 @@ public class PathAssembler
}
else if ( base.equals( PROJECT_STRING ) )
{
- return new File( System.getProperty( "user.dir" ) );
+ return Paths.get( System.getProperty( "user.dir" ) );
}
else
{
@@ -122,11 +126,11 @@ public class PathAssembler
*/
public class LocalDistribution
{
- private final File distZip;
+ private final Path distZip;
- private final File distDir;
+ private final Path distDir;
- public LocalDistribution( File distDir, File distZip )
+ public LocalDistribution( Path distDir, Path distZip )
{
this.distDir = distDir;
this.distZip = distZip;
@@ -135,7 +139,7 @@ public class PathAssembler
/**
* Returns the location to install the distribution into.
*/
- public File getDistributionDir()
+ public Path getDistributionDir()
{
return distDir;
}
@@ -143,7 +147,7 @@ public class PathAssembler
/**
* Returns the location to install the distribution ZIP file to.
*/
- public File getZipFile()
+ public Path getZipFile()
{
return distZip;
}
diff --git a/src/main/java/org/apache/maven/wrapper/SystemPropertiesHandler.java b/src/main/java/org/apache/maven/wrapper/SystemPropertiesHandler.java
index e1ecfaa..770ef60 100644
--- a/src/main/java/org/apache/maven/wrapper/SystemPropertiesHandler.java
+++ b/src/main/java/org/apache/maven/wrapper/SystemPropertiesHandler.java
@@ -19,9 +19,10 @@ package org.apache.maven.wrapper;
* under the License.
*/
-import java.io.File;
-import java.io.FileInputStream;
import java.io.IOException;
+import java.io.InputStream;
+import java.nio.file.Files;
+import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
@@ -34,16 +35,16 @@ import java.util.regex.Pattern;
public class SystemPropertiesHandler
{
- public static Map<String, String> getSystemProperties( File propertiesFile )
+ public static Map<String, String> getSystemProperties( Path propertiesFile )
{
Map<String, String> propertyMap = new HashMap<String, String>();
- if ( !propertiesFile.isFile() )
+ if ( !Files.isRegularFile( propertiesFile ) )
{
return propertyMap;
}
Properties properties = new Properties();
- try ( FileInputStream inStream = new FileInputStream( propertiesFile ) )
+ try ( InputStream inStream = Files.newInputStream( propertiesFile ) )
{
properties.load( inStream );
}
diff --git a/src/main/java/org/apache/maven/wrapper/WrapperExecutor.java b/src/main/java/org/apache/maven/wrapper/WrapperExecutor.java
index 818523d..ccb08f1 100644
--- a/src/main/java/org/apache/maven/wrapper/WrapperExecutor.java
+++ b/src/main/java/org/apache/maven/wrapper/WrapperExecutor.java
@@ -19,12 +19,12 @@ package org.apache.maven.wrapper;
* under the License.
*/
-import java.io.File;
-import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
+import java.nio.file.Files;
+import java.nio.file.Path;
import java.util.Properties;
/**
@@ -44,34 +44,31 @@ public class WrapperExecutor
private final Properties properties;
- private final File propertiesFile;
-
- private final Appendable warningOutput;
+ private final Path propertiesFile;
private final WrapperConfiguration config = new WrapperConfiguration();
- public static WrapperExecutor forProjectDirectory( File projectDir, Appendable warningOutput )
+ public static WrapperExecutor forProjectDirectory( Path projectDir )
{
- return new WrapperExecutor( new File( projectDir, "maven/wrapper/maven-wrapper.properties" ), new Properties(),
- warningOutput );
+ return new WrapperExecutor( projectDir.resolve( "maven/wrapper/maven-wrapper.properties" ),
+ new Properties() );
}
- public static WrapperExecutor forWrapperPropertiesFile( File propertiesFile, Appendable warningOutput )
+ public static WrapperExecutor forWrapperPropertiesFile( Path propertiesFile, Appendable warningOutput )
{
- if ( !propertiesFile.exists() )
+ if ( !Files.exists( propertiesFile ) )
{
throw new RuntimeException( String.format( "Wrapper properties file '%s' does not exist.",
propertiesFile ) );
}
- return new WrapperExecutor( propertiesFile, new Properties(), warningOutput );
+ return new WrapperExecutor( propertiesFile, new Properties() );
}
- WrapperExecutor( File propertiesFile, Properties properties, Appendable warningOutput )
+ WrapperExecutor( Path propertiesFile, Properties properties )
{
this.properties = properties;
this.propertiesFile = propertiesFile;
- this.warningOutput = warningOutput;
- if ( propertiesFile.exists() )
+ if ( Files.exists( propertiesFile ) )
{
try
{
@@ -98,7 +95,7 @@ public class WrapperExecutor
if ( source.getScheme() == null )
{
// no scheme means someone passed a relative url. In our context only file relative urls make sense.
- return new File( propertiesFile.getParentFile(), source.getSchemeSpecificPart() ).toURI();
+ return propertiesFile.getParent().resolve( source.getSchemeSpecificPart() ).toUri();
}
else
{
@@ -118,10 +115,10 @@ public class WrapperExecutor
return null; // previous line will fail
}
- private static void loadProperties( File propertiesFile, Properties properties )
+ private static void loadProperties( Path propertiesFile, Properties properties )
throws IOException
{
- try ( InputStream inStream = new FileInputStream( propertiesFile ) )
+ try ( InputStream inStream = Files.newInputStream( propertiesFile ) )
{
properties.load( inStream );
}
@@ -147,7 +144,7 @@ public class WrapperExecutor
public void execute( String[] args, Installer install, BootstrapMainStarter bootstrapMainStarter )
throws Exception
{
- File mavenHome = install.createDist( config );
+ Path mavenHome = install.createDist( config );
bootstrapMainStarter.start( args, mavenHome );
}
diff --git a/src/test/java/org/apache/maven/wrapper/DownloaderTest.java b/src/test/java/org/apache/maven/wrapper/DownloaderTest.java
index ae8ea06..5d9892c 100644
--- a/src/test/java/org/apache/maven/wrapper/DownloaderTest.java
+++ b/src/test/java/org/apache/maven/wrapper/DownloaderTest.java
@@ -21,9 +21,9 @@ package org.apache.maven.wrapper;
import static org.junit.Assert.assertEquals;
-import java.io.File;
import java.net.URI;
import java.nio.file.Files;
+import java.nio.file.Path;
import java.util.Arrays;
import java.util.stream.Collectors;
@@ -39,34 +39,34 @@ public class DownloaderTest
private DefaultDownloader download;
- private File downloadFile;
+ private Path downloadFile;
- private File rootDir;
+ private Path rootDir;
private URI sourceRoot;
- private File remoteFile;
+ private Path 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();
+ rootDir = testDir.newFolder( "root" ).toPath();
+ downloadFile = rootDir.resolve( "file" );
+ remoteFile = testDir.newFile( "remoteFile" ).toPath();
+ Files.write( remoteFile, Arrays.asList( "sometext" ) );
+ sourceRoot = remoteFile.toUri();
}
@Test
public void testDownload()
throws Exception
{
- assert !downloadFile.exists();
+ assert !Files.exists( downloadFile );
download.download( sourceRoot, downloadFile );
- assert downloadFile.exists();
+ assert Files.exists( downloadFile );
assertEquals( "sometext",
- Files.readAllLines( downloadFile.toPath() ).stream().collect( Collectors.joining() ) );
+ Files.readAllLines( downloadFile ).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 9c70c2e..93647a9 100644
--- a/src/test/java/org/apache/maven/wrapper/InstallerTest.java
+++ b/src/test/java/org/apache/maven/wrapper/InstallerTest.java
@@ -25,6 +25,7 @@ import static org.mockito.Mockito.when;
import java.io.File;
import java.net.URI;
import java.nio.file.Files;
+import java.nio.file.Path;
import java.util.Arrays;
import org.apache.tools.ant.Project;
@@ -38,161 +39,172 @@ import org.junit.rules.TemporaryFolder;
/**
* @author Hans Dockter
*/
-public class InstallerTest {
-
+public class InstallerTest
+{
@Rule
public TemporaryFolder temporaryFolder = new TemporaryFolder();
-
- private Installer install;
+ private Installer install;
- private File distributionDir;
+ private Path distributionDir;
- private File zipStore;
+ private Path zipStore;
- private File mavenHomeDir;
+ private Path mavenHomeDir;
- private File zipDestination;
+ private Path zipDestination;
- private WrapperConfiguration configuration = new WrapperConfiguration();
+ private WrapperConfiguration configuration = new WrapperConfiguration();
- private Downloader download;
+ private Downloader download;
- private PathAssembler pathAssembler;
+ private PathAssembler pathAssembler;
- private PathAssembler.LocalDistribution localDistribution;
+ private PathAssembler.LocalDistribution localDistribution;
- @Before
- public void setup() throws Exception {
- configuration.setZipBase(PathAssembler.PROJECT_STRING);
- configuration.setZipPath("someZipPath");
- configuration.setDistributionBase(PathAssembler.MAVEN_USER_HOME_STRING);
- configuration.setDistributionPath("someDistPath");
- configuration.setDistribution(new URI("http://server/maven-0.9.zip"));
- configuration.setAlwaysDownload(false);
- configuration.setAlwaysUnpack(false);
- distributionDir = temporaryFolder.newFolder( "someDistPath" );
- mavenHomeDir = new File(distributionDir, "maven-0.9");
- zipStore = temporaryFolder.newFolder( "zips" );
- zipDestination = new File(zipStore, "maven-0.9.zip");
+ @Before
+ public void setup()
+ throws Exception
+ {
+ configuration.setZipBase( PathAssembler.PROJECT_STRING );
+ configuration.setZipPath( "someZipPath" );
+ configuration.setDistributionBase( PathAssembler.MAVEN_USER_HOME_STRING );
+ configuration.setDistributionPath( "someDistPath" );
+ configuration.setDistribution( new URI( "http://server/maven-0.9.zip" ) );
+ configuration.setAlwaysDownload( false );
+ configuration.setAlwaysUnpack( false );
+ distributionDir = temporaryFolder.newFolder( "someDistPath" ).toPath();
+ mavenHomeDir = distributionDir.resolve( "maven-0.9" );
+ zipStore = temporaryFolder.newFolder( "zips" ).toPath();
+ zipDestination = zipStore.resolve( "maven-0.9.zip" );
- download = mock(Downloader.class);
- pathAssembler = mock(PathAssembler.class);
- localDistribution = mock(PathAssembler.LocalDistribution.class);
+ download = mock( Downloader.class );
+ pathAssembler = mock( PathAssembler.class );
+ localDistribution = mock( PathAssembler.LocalDistribution.class );
- when(localDistribution.getZipFile()).thenReturn(zipDestination);
- when(localDistribution.getDistributionDir()).thenReturn(distributionDir);
- when(pathAssembler.getDistribution(configuration)).thenReturn(localDistribution);
+ when( localDistribution.getZipFile() ).thenReturn( zipDestination );
+ when( localDistribution.getDistributionDir() ).thenReturn( distributionDir );
+ when( pathAssembler.getDistribution( configuration ) ).thenReturn( localDistribution );
- install = new Installer(download, pathAssembler);
+ install = new Installer( download, pathAssembler );
+ }
- }
+ private void createTestZip( Path zipDestination )
+ throws Exception
+ {
+ Files.createDirectories( zipDestination.getParent() );
- private void createTestZip(File zipDestination) throws Exception {
- File explodedZipDir = temporaryFolder.newFolder( "explodedZip");
+ Path explodedZipDir = temporaryFolder.newFolder( "explodedZip" ).toPath();
- zipDestination.getParentFile().mkdirs();
- File mavenScript = new File(explodedZipDir, "maven-0.9/bin/mvn");
- mavenScript.getParentFile().mkdirs();
- Files.write( mavenScript.toPath(), Arrays.asList( "something" ) );
+ Path mavenScript = explodedZipDir.resolve( "maven-0.9/bin/mvn" );
+ Files.createDirectories( mavenScript.getParent() );
+ Files.write( mavenScript, Arrays.asList( "something" ) );
- zipTo(explodedZipDir, zipDestination);
- }
+ zipTo( explodedZipDir, zipDestination );
+ }
- public void testCreateDist() throws Exception {
- File homeDir = install.createDist(configuration);
+ public void testCreateDist()
+ throws Exception
+ {
+ Path homeDir = install.createDist( configuration );
- Assert.assertEquals(mavenHomeDir, homeDir);
- Assert.assertTrue(homeDir.isDirectory());
- Assert.assertTrue(new File(homeDir, "bin/mvn").exists());
- Assert.assertTrue(zipDestination.exists());
+ Assert.assertEquals( mavenHomeDir, homeDir );
+ Assert.assertTrue( Files.isDirectory( homeDir ) );
+ Assert.assertTrue( Files.exists( homeDir.resolve( "bin/mvn" ) ) );
+ Assert.assertTrue( Files.exists( zipDestination ) );
- Assert.assertEquals(localDistribution, pathAssembler.getDistribution(configuration));
- Assert.assertEquals(distributionDir, localDistribution.getDistributionDir());
- Assert.assertEquals(zipDestination, localDistribution.getZipFile());
+ Assert.assertEquals( localDistribution, pathAssembler.getDistribution( configuration ) );
+ Assert.assertEquals( distributionDir, localDistribution.getDistributionDir() );
+ Assert.assertEquals( zipDestination, localDistribution.getZipFile() );
- // download.download(new URI("http://some/test"), distributionDir);
- // verify(download).download(new URI("http://some/test"), distributionDir);
- }
+ // download.download(new URI("http://some/test"), distributionDir);
+ // verify(download).download(new URI("http://some/test"), distributionDir);
+ }
- @Test
- public void testCreateDistWithExistingDistribution() throws Exception {
- Files.createFile( zipDestination.toPath() );
+ @Test
+ public void testCreateDistWithExistingDistribution()
+ throws Exception
+ {
+ Files.createFile( zipDestination );
- mavenHomeDir.mkdirs();
- File someFile = new File(mavenHomeDir, "some-file");
- Files.createFile( someFile.toPath() );
+ Files.createDirectories( mavenHomeDir );
+ Path someFile = mavenHomeDir.resolve( "some-file" );
+ Files.createFile( someFile );
- File homeDir = install.createDist(configuration);
+ Path homeDir = install.createDist( configuration );
- Assert.assertEquals(mavenHomeDir, homeDir);
- Assert.assertTrue(mavenHomeDir.isDirectory());
- Assert.assertTrue(new File(homeDir, "some-file").exists());
- Assert.assertTrue(zipDestination.exists());
+ Assert.assertEquals( mavenHomeDir, homeDir );
+ Assert.assertTrue( Files.isDirectory( mavenHomeDir ) );
+ Assert.assertTrue( Files.exists( homeDir.resolve( "some-file" ) ) );
+ Assert.assertTrue( Files.exists( zipDestination ) );
- Assert.assertEquals(localDistribution, pathAssembler.getDistribution(configuration));
- Assert.assertEquals(distributionDir, localDistribution.getDistributionDir());
- Assert.assertEquals(zipDestination, localDistribution.getZipFile());
- }
-
- @Test
- public void testCreateDistWithExistingDistAndZipAndAlwaysUnpackTrue() throws Exception {
-
- createTestZip(zipDestination);
- mavenHomeDir.mkdirs();
- File garbage = new File(mavenHomeDir, "garbage");
- Files.createFile( garbage.toPath() );
-
- configuration.setAlwaysUnpack(true);
-
- File homeDir = install.createDist(configuration);
-
- Assert.assertEquals(mavenHomeDir, homeDir);
- Assert.assertTrue(mavenHomeDir.isDirectory());
- Assert.assertFalse(new File(homeDir, "garbage").exists());
- Assert.assertTrue(zipDestination.exists());
-
- Assert.assertEquals(localDistribution, pathAssembler.getDistribution(configuration));
- Assert.assertEquals(distributionDir, localDistribution.getDistributionDir());
- Assert.assertEquals(zipDestination, localDistribution.getZipFile());
- }
-
- @Test
- public void testCreateDistWithExistingZipAndDistAndAlwaysDownloadTrue() throws Exception {
-
- createTestZip(zipDestination);
- mavenHomeDir.mkdirs();
- File garbage = new File(mavenHomeDir, "garbage");
- Files.createFile( garbage.toPath() );
-
- configuration.setAlwaysUnpack(true);
-
- File homeDir = install.createDist(configuration);
-
- Assert.assertEquals(mavenHomeDir, homeDir);
- Assert.assertTrue(mavenHomeDir.isDirectory());
- Assert.assertTrue(new File(homeDir, "bin/mvn").exists());
- Assert.assertFalse(new File(homeDir, "garbage").exists());
- Assert.assertTrue(zipDestination.exists());
-
- Assert.assertEquals(localDistribution, pathAssembler.getDistribution(configuration));
- Assert.assertEquals(distributionDir, localDistribution.getDistributionDir());
- Assert.assertEquals(zipDestination, localDistribution.getZipFile());
-
- // download.download(new URI("http://some/test"), distributionDir);
- // verify(download).download(new URI("http://some/test"), distributionDir);
- }
-
- public void zipTo(File directoryToZip, File zipFile) {
- Zip zip = new Zip();
- zip.setBasedir(directoryToZip);
- zip.setDestFile(zipFile);
- zip.setProject(new Project());
-
- Zip.WhenEmpty whenEmpty = new Zip.WhenEmpty();
- whenEmpty.setValue("create");
- zip.setWhenempty(whenEmpty);
- zip.execute();
- }
+ Assert.assertEquals( localDistribution, pathAssembler.getDistribution( configuration ) );
+ Assert.assertEquals( distributionDir, localDistribution.getDistributionDir() );
+ Assert.assertEquals( zipDestination, localDistribution.getZipFile() );
+ }
+ @Test
+ public void testCreateDistWithExistingDistAndZipAndAlwaysUnpackTrue()
+ throws Exception
+ {
+
+ createTestZip( zipDestination );
+ Files.createDirectories( mavenHomeDir );
+ File garbage = mavenHomeDir.resolve( "garbage" ).toFile();
+ Files.createFile( garbage.toPath() );
+
+ configuration.setAlwaysUnpack( true );
+
+ Path homeDir = install.createDist( configuration );
+
+ Assert.assertEquals( mavenHomeDir, homeDir );
+ Assert.assertTrue( Files.isDirectory( mavenHomeDir ) );
+ Assert.assertFalse( Files.exists( homeDir.resolve( "garbage" ) ) );
+ Assert.assertTrue( Files.exists( zipDestination ) );
+
+ Assert.assertEquals( localDistribution, pathAssembler.getDistribution( configuration ) );
+ Assert.assertEquals( distributionDir, localDistribution.getDistributionDir() );
+ Assert.assertEquals( zipDestination, localDistribution.getZipFile() );
+ }
+
+ @Test
+ public void testCreateDistWithExistingZipAndDistAndAlwaysDownloadTrue()
+ throws Exception
+ {
+
+ createTestZip( zipDestination );
+ Files.createDirectories( mavenHomeDir );
+ File garbage = mavenHomeDir.resolve( "garbage" ).toFile();
+ Files.createFile( garbage.toPath() );
+
+ configuration.setAlwaysUnpack( true );
+
+ Path homeDir = install.createDist( configuration );
+
+ Assert.assertEquals( mavenHomeDir, homeDir );
+ Assert.assertTrue( Files.isDirectory( mavenHomeDir ) );
+ Assert.assertTrue( Files.exists( homeDir.resolve( "bin/mvn" ) ) );
+ Assert.assertFalse( Files.exists( homeDir.resolve( "garbage" ) ) );
+ Assert.assertTrue( Files.exists( zipDestination ) );
+
+ Assert.assertEquals( localDistribution, pathAssembler.getDistribution( configuration ) );
+ Assert.assertEquals( distributionDir, localDistribution.getDistributionDir() );
+ Assert.assertEquals( zipDestination, localDistribution.getZipFile() );
+
+ // download.download(new URI("http://some/test"), distributionDir);
+ // verify(download).download(new URI("http://some/test"), distributionDir);
+ }
+
+ public void zipTo( Path directoryToZip, Path zipFile )
+ {
+ Zip zip = new Zip();
+ zip.setBasedir( directoryToZip.toFile() );
+ zip.setDestFile( zipFile.toFile() );
+ zip.setProject( new Project() );
+
+ Zip.WhenEmpty whenEmpty = new Zip.WhenEmpty();
+ whenEmpty.setValue( "create" );
+ zip.setWhenempty( whenEmpty );
+ zip.execute();
+ }
}
diff --git a/src/test/java/org/apache/maven/wrapper/PathAssemblerTest.java b/src/test/java/org/apache/maven/wrapper/PathAssemblerTest.java
index d895648..9bd3916 100644
--- a/src/test/java/org/apache/maven/wrapper/PathAssemblerTest.java
+++ b/src/test/java/org/apache/maven/wrapper/PathAssemblerTest.java
@@ -20,8 +20,9 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
-import java.io.File;
import java.net.URI;
+import java.nio.file.Path;
+import java.nio.file.Paths;
import java.util.regex.Pattern;
import org.hamcrest.BaseMatcher;
@@ -36,7 +37,7 @@ import org.junit.Test;
public class PathAssemblerTest {
public static final String TEST_MAVEN_USER_HOME = "someUserHome";
- private PathAssembler pathAssembler = new PathAssembler(new File(TEST_MAVEN_USER_HOME));
+ private PathAssembler pathAssembler = new PathAssembler( Paths.get( TEST_MAVEN_USER_HOME ) );
final WrapperConfiguration configuration = new WrapperConfiguration();
@@ -52,9 +53,9 @@ public class PathAssemblerTest {
public void distributionDirWithMavenUserHomeBase() throws Exception {
configuration.setDistribution(new URI("http://server/dist/maven-0.9-bin.zip"));
- File distributionDir = pathAssembler.getDistribution(configuration).getDistributionDir();
- assertThat(distributionDir.getName(), matchesRegexp("[a-z0-9]+"));
- assertThat(distributionDir.getParentFile(), equalTo(file(TEST_MAVEN_USER_HOME + "/somePath/maven-0.9-bin")));
+ Path distributionDir = pathAssembler.getDistribution(configuration).getDistributionDir();
+ assertThat(distributionDir.getFileName().toString(), matchesRegexp("[a-z0-9]+"));
+ assertThat(distributionDir.getParent(), equalTo(Paths.get(TEST_MAVEN_USER_HOME, "/somePath/maven-0.9-bin")));
}
@Test
@@ -62,9 +63,9 @@ public class PathAssemblerTest {
configuration.setDistributionBase(PathAssembler.PROJECT_STRING);
configuration.setDistribution(new URI("http://server/dist/maven-0.9-bin.zip"));
- File distributionDir = pathAssembler.getDistribution(configuration).getDistributionDir();
- assertThat(distributionDir.getName(), matchesRegexp("[a-z0-9]+"));
- assertThat(distributionDir.getParentFile(), equalTo(file(currentDirPath() + "/somePath/maven-0.9-bin")));
+ Path distributionDir = pathAssembler.getDistribution(configuration).getDistributionDir();
+ assertThat(distributionDir.getFileName().toString(), matchesRegexp("[a-z0-9]+"));
+ assertThat(distributionDir.getParent(), equalTo(Paths.get(currentDirPath(), "/somePath/maven-0.9-bin")));
}
@Test
@@ -84,10 +85,10 @@ public class PathAssemblerTest {
public void distZipWithMavenUserHomeBase() throws Exception {
configuration.setDistribution(new URI("http://server/dist/maven-1.0.zip"));
- File dist = pathAssembler.getDistribution(configuration).getZipFile();
- assertThat(dist.getName(), equalTo("maven-1.0.zip"));
- assertThat(dist.getParentFile().getName(), matchesRegexp("[a-z0-9]+"));
- assertThat(dist.getParentFile().getParentFile(), equalTo(file(TEST_MAVEN_USER_HOME + "/somePath/maven-1.0")));
+ Path dist = pathAssembler.getDistribution(configuration).getZipFile();
+ assertThat(dist.getFileName().toString(), equalTo("maven-1.0.zip"));
+ assertThat(dist.getParent().getFileName().toString(), matchesRegexp("[a-z0-9]+"));
+ assertThat(dist.getParent().getParent(), equalTo(Paths.get(TEST_MAVEN_USER_HOME, "/somePath/maven-1.0")));
}
@Test
@@ -95,14 +96,10 @@ public class PathAssemblerTest {
configuration.setZipBase(PathAssembler.PROJECT_STRING);
configuration.setDistribution(new URI("http://server/dist/maven-1.0.zip"));
- File dist = pathAssembler.getDistribution(configuration).getZipFile();
- assertThat(dist.getName(), equalTo("maven-1.0.zip"));
- assertThat(dist.getParentFile().getName(), matchesRegexp("[a-z0-9]+"));
- assertThat(dist.getParentFile().getParentFile(), equalTo(file(currentDirPath() + "/somePath/maven-1.0")));
- }
-
- private File file(String path) {
- return new File(path);
+ Path dist = pathAssembler.getDistribution(configuration).getZipFile();
+ assertThat(dist.getFileName().toString(), equalTo("maven-1.0.zip"));
+ assertThat(dist.getParent().getFileName().toString(), matchesRegexp("[a-z0-9]+"));
+ assertThat(dist.getParent().getParent(), equalTo(Paths.get(currentDirPath(), "/somePath/maven-1.0")));
}
private String currentDirPath() {
diff --git a/src/test/java/org/apache/maven/wrapper/SystemPropertiesHandlerTest.java b/src/test/java/org/apache/maven/wrapper/SystemPropertiesHandlerTest.java
index 05a44c6..40d2256 100644
--- a/src/test/java/org/apache/maven/wrapper/SystemPropertiesHandlerTest.java
+++ b/src/test/java/org/apache/maven/wrapper/SystemPropertiesHandlerTest.java
@@ -22,46 +22,49 @@ package org.apache.maven.wrapper;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
-import java.io.File;
-import java.io.FileOutputStream;
+import java.io.OutputStream;
+import java.nio.file.Files;
+import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
-import org.junit.Before;
+import org.junit.Rule;
import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
-public class SystemPropertiesHandlerTest {
+public class SystemPropertiesHandlerTest
+{
- private File tmpDir = new File("target/test-files/SystemPropertiesHandlerTest");
+ @Rule
+ public TemporaryFolder tmpDir = new TemporaryFolder();
- @Before
- public void setupTempDir() {
- tmpDir.mkdirs();
- }
-
- @Test
- public void testParsePropertiesFile() throws Exception {
- File propFile = new File(tmpDir, "props");
- Properties props = new Properties();
- props.put("a", "b");
- props.put("systemProp.c", "d");
- props.put("systemProp.", "e");
-
- try ( FileOutputStream fos = new FileOutputStream( propFile ) )
+ @Test
+ public void testParsePropertiesFile()
+ throws Exception
{
- props.store(fos, "");
- }
+ Path propFile = tmpDir.newFile( "props" ).toPath();
+ Properties props = new Properties();
+ props.put( "a", "b" );
+ props.put( "systemProp.c", "d" );
+ props.put( "systemProp.", "e" );
- Map<String, String> expected = new HashMap<String, String>();
- expected.put("c", "d");
+ try (OutputStream fos = Files.newOutputStream( propFile ))
+ {
+ props.store( fos, "" );
+ }
- assertThat(SystemPropertiesHandler.getSystemProperties(propFile), equalTo(expected));
- }
+ Map<String, String> expected = new HashMap<String, String>();
+ expected.put( "c", "d" );
- @Test
- public void ifNoPropertyFileExistShouldReturnEmptyMap() {
- Map<String, String> expected = new HashMap<String, String>();
- assertThat(SystemPropertiesHandler.getSystemProperties(new File(tmpDir, "unknown")), equalTo(expected));
- }
+ assertThat( SystemPropertiesHandler.getSystemProperties( propFile ), equalTo( expected ) );
+ }
+
+ @Test
+ public void ifNoPropertyFileExistShouldReturnEmptyMap()
+ {
+ Map<String, String> expected = new HashMap<String, String>();
+ assertThat( SystemPropertiesHandler.getSystemProperties( tmpDir.getRoot().toPath().resolve( "unknown" ) ),
+ equalTo( expected ) );
+ }
}
diff --git a/src/test/java/org/apache/maven/wrapper/WrapperExecutorTest.java b/src/test/java/org/apache/maven/wrapper/WrapperExecutorTest.java
index c122b66..ed8a6ad 100644
--- a/src/test/java/org/apache/maven/wrapper/WrapperExecutorTest.java
+++ b/src/test/java/org/apache/maven/wrapper/WrapperExecutorTest.java
@@ -23,158 +23,184 @@ import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
-import java.io.File;
-import java.io.FileOutputStream;
import java.io.OutputStream;
import java.net.URI;
+import java.nio.file.Files;
+import java.nio.file.Path;
import java.util.Properties;
import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Rule;
import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
import org.mockito.Mockito;
-public class WrapperExecutorTest {
- private final Installer install;
-
- private final BootstrapMainStarter start;
-
- private File propertiesFile;
-
- private Properties properties = new Properties();
-
- private File testDir = new File("target/test-files/SystemPropertiesHandlerTest-" + System.currentTimeMillis());
-
- private File mockInstallDir = new File(testDir, "mock-dir");
-
- public WrapperExecutorTest() throws Exception {
- install = mock(Installer.class);
- when(install.createDist(Mockito.any(WrapperConfiguration.class))).thenReturn(mockInstallDir);
- start = mock(BootstrapMainStarter.class);
-
- testDir.mkdirs();
- propertiesFile = new File(testDir, "maven/wrapper/maven-wrapper.properties");
-
- properties.put("distributionUrl", "http://server/test/maven.zip");
- properties.put("distributionBase", "testDistBase");
- properties.put("distributionPath", "testDistPath");
- properties.put("zipStoreBase", "testZipBase");
- properties.put("zipStorePath", "testZipPath");
-
- writePropertiesFile(properties, propertiesFile, "header");
-
- }
-
- @Test
- public void loadWrapperMetadataFromFile() throws Exception {
- WrapperExecutor wrapper = WrapperExecutor.forWrapperPropertiesFile(propertiesFile, System.out);
-
- Assert.assertEquals(new URI("http://server/test/maven.zip"), wrapper.getDistribution());
- Assert.assertEquals(new URI("http://server/test/maven.zip"), wrapper.getConfiguration().getDistribution());
- Assert.assertEquals("testDistBase", wrapper.getConfiguration().getDistributionBase());
- Assert.assertEquals("testDistPath", wrapper.getConfiguration().getDistributionPath());
- Assert.assertEquals("testZipBase", wrapper.getConfiguration().getZipBase());
- Assert.assertEquals("testZipPath", wrapper.getConfiguration().getZipPath());
- }
-
- @Test
- public void loadWrapperMetadataFromDirectory() throws Exception {
- WrapperExecutor wrapper = WrapperExecutor.forProjectDirectory(testDir, System.out);
-
- Assert.assertEquals(new URI("http://server/test/maven.zip"), wrapper.getDistribution());
- Assert.assertEquals(new URI("http://server/test/maven.zip"), wrapper.getConfiguration().getDistribution());
- Assert.assertEquals("testDistBase", wrapper.getConfiguration().getDistributionBase());
- Assert.assertEquals("testDistPath", wrapper.getConfiguration().getDistributionPath());
- Assert.assertEquals("testZipBase", wrapper.getConfiguration().getZipBase());
- Assert.assertEquals("testZipPath", wrapper.getConfiguration().getZipPath());
- }
-
- @Test
- public void useDefaultMetadataNoProeprtiesFile() throws Exception {
- WrapperExecutor wrapper = WrapperExecutor.forProjectDirectory(new File(testDir, "unknown"), System.out);
-
- Assert.assertNull(wrapper.getDistribution());
- Assert.assertNull(wrapper.getConfiguration().getDistribution());
- Assert.assertEquals(PathAssembler.MAVEN_USER_HOME_STRING, wrapper.getConfiguration().getDistributionBase());
- Assert.assertEquals(Installer.DEFAULT_DISTRIBUTION_PATH, wrapper.getConfiguration().getDistributionPath());
- Assert.assertEquals(PathAssembler.MAVEN_USER_HOME_STRING, wrapper.getConfiguration().getZipBase());
- Assert.assertEquals(Installer.DEFAULT_DISTRIBUTION_PATH, wrapper.getConfiguration().getZipPath());
- }
-
- @Test
- public void propertiesFileOnlyContainsDistURL() throws Exception {
-
- properties = new Properties();
- properties.put("distributionUrl", "http://server/test/maven.zip");
- writePropertiesFile(properties, propertiesFile, "header");
-
- WrapperExecutor wrapper = WrapperExecutor.forWrapperPropertiesFile(propertiesFile, System.out);
-
- Assert.assertEquals(new URI("http://server/test/maven.zip"), wrapper.getDistribution());
- Assert.assertEquals(new URI("http://server/test/maven.zip"), wrapper.getConfiguration().getDistribution());
- Assert.assertEquals(PathAssembler.MAVEN_USER_HOME_STRING, wrapper.getConfiguration().getDistributionBase());
- Assert.assertEquals(Installer.DEFAULT_DISTRIBUTION_PATH, wrapper.getConfiguration().getDistributionPath());
- Assert.assertEquals(PathAssembler.MAVEN_USER_HOME_STRING, wrapper.getConfiguration().getZipBase());
- Assert.assertEquals(Installer.DEFAULT_DISTRIBUTION_PATH, wrapper.getConfiguration().getZipPath());
- }
-
- @Test
- public void executeInstallAndLaunch() throws Exception {
- WrapperExecutor wrapper = WrapperExecutor.forProjectDirectory(propertiesFile, System.out);
-
- wrapper.execute(new String[] {
- "arg"
- }, install, start);
- verify(install).createDist(Mockito.any(WrapperConfiguration.class));
- verify(start).start(new String[] {
- "arg"
- }, mockInstallDir);
- }
-
- @Test()
- public void failWhenDistNotSetInProperties() throws Exception {
- properties = new Properties();
- writePropertiesFile(properties, propertiesFile, "header");
-
- try {
- WrapperExecutor.forWrapperPropertiesFile(propertiesFile, System.out);
- Assert.fail("Expected RuntimeException");
- } catch (RuntimeException e) {
- Assert.assertEquals("Could not load wrapper properties from '" + propertiesFile + "'.", e.getMessage());
- Assert.assertEquals("No value with key 'distributionUrl' specified in wrapper properties file '" + propertiesFile + "'.", e.getCause().getMessage());
+public class WrapperExecutorTest
+{
+ @Rule
+ public TemporaryFolder testDir = new TemporaryFolder();
+
+ private Installer install;
+
+ private BootstrapMainStarter start;
+
+ private Path propertiesFile;
+
+ private Properties properties = new Properties();
+
+ private Path mockInstallDir;
+
+ @Before
+ public void setUp()
+ throws Exception
+ {
+ mockInstallDir = testDir.newFolder( "mock-dir" ).toPath();
+
+ install = mock( Installer.class );
+ when( install.createDist( Mockito.any( WrapperConfiguration.class ) ) ).thenReturn( mockInstallDir );
+ start = mock( BootstrapMainStarter.class );
+
+ propertiesFile = testDir.newFolder( "maven", "wrapper" ).toPath().resolve( "maven-wrapper.properties" );
+
+ properties.put( "distributionUrl", "http://server/test/maven.zip" );
+ properties.put( "distributionBase", "testDistBase" );
+ properties.put( "distributionPath", "testDistPath" );
+ properties.put( "zipStoreBase", "testZipBase" );
+ properties.put( "zipStorePath", "testZipPath" );
+
+ writePropertiesFile( properties, propertiesFile, "header" );
+ }
+
+ @Test
+ public void loadWrapperMetadataFromFile()
+ throws Exception
+ {
+ WrapperExecutor wrapper = WrapperExecutor.forWrapperPropertiesFile( propertiesFile, System.out );
+
+ Assert.assertEquals( new URI( "http://server/test/maven.zip" ), wrapper.getDistribution() );
+ Assert.assertEquals( new URI( "http://server/test/maven.zip" ), wrapper.getConfiguration().getDistribution() );
+ Assert.assertEquals( "testDistBase", wrapper.getConfiguration().getDistributionBase() );
+ Assert.assertEquals( "testDistPath", wrapper.getConfiguration().getDistributionPath() );
+ Assert.assertEquals( "testZipBase", wrapper.getConfiguration().getZipBase() );
+ Assert.assertEquals( "testZipPath", wrapper.getConfiguration().getZipPath() );
+ }
+
+ @Test
+ public void loadWrapperMetadataFromDirectory()
+ throws Exception
+ {
+ WrapperExecutor wrapper = WrapperExecutor.forProjectDirectory( testDir.getRoot().toPath() );
+
+ Assert.assertEquals( new URI( "http://server/test/maven.zip" ), wrapper.getDistribution() );
+ Assert.assertEquals( new URI( "http://server/test/maven.zip" ), wrapper.getConfiguration().getDistribution() );
+ Assert.assertEquals( "testDistBase", wrapper.getConfiguration().getDistributionBase() );
+ Assert.assertEquals( "testDistPath", wrapper.getConfiguration().getDistributionPath() );
+ Assert.assertEquals( "testZipBase", wrapper.getConfiguration().getZipBase() );
+ Assert.assertEquals( "testZipPath", wrapper.getConfiguration().getZipPath() );
+ }
+
+ @Test
+ public void useDefaultMetadataNoProeprtiesFile()
+ throws Exception
+ {
+ WrapperExecutor wrapper = WrapperExecutor.forProjectDirectory( testDir.getRoot().toPath().resolve( "unknown" ) );
+
+ Assert.assertNull( wrapper.getDistribution() );
+ Assert.assertNull( wrapper.getConfiguration().getDistribution() );
+ Assert.assertEquals( PathAssembler.MAVEN_USER_HOME_STRING, wrapper.getConfiguration().getDistributionBase() );
+ Assert.assertEquals( Installer.DEFAULT_DISTRIBUTION_PATH, wrapper.getConfiguration().getDistributionPath() );
+ Assert.assertEquals( PathAssembler.MAVEN_USER_HOME_STRING, wrapper.getConfiguration().getZipBase() );
+ Assert.assertEquals( Installer.DEFAULT_DISTRIBUTION_PATH, wrapper.getConfiguration().getZipPath() );
+ }
+
+ @Test
+ public void propertiesFileOnlyContainsDistURL()
+ throws Exception
+ {
+
+ properties = new Properties();
+ properties.put( "distributionUrl", "http://server/test/maven.zip" );
+ writePropertiesFile( properties, propertiesFile, "header" );
+
+ WrapperExecutor wrapper = WrapperExecutor.forWrapperPropertiesFile( propertiesFile, System.out );
+
+ Assert.assertEquals( new URI( "http://server/test/maven.zip" ), wrapper.getDistribution() );
+ Assert.assertEquals( new URI( "http://server/test/maven.zip" ), wrapper.getConfiguration().getDistribution() );
+ Assert.assertEquals( PathAssembler.MAVEN_USER_HOME_STRING, wrapper.getConfiguration().getDistributionBase() );
+ Assert.assertEquals( Installer.DEFAULT_DISTRIBUTION_PATH, wrapper.getConfiguration().getDistributionPath() );
+ Assert.assertEquals( PathAssembler.MAVEN_USER_HOME_STRING, wrapper.getConfiguration().getZipBase() );
+ Assert.assertEquals( Installer.DEFAULT_DISTRIBUTION_PATH, wrapper.getConfiguration().getZipPath() );
}
- }
+ @Test
+ public void executeInstallAndLaunch()
+ throws Exception
+ {
+ WrapperExecutor wrapper = WrapperExecutor.forProjectDirectory( propertiesFile );
+
+ wrapper.execute( new String[] { "arg" }, install, start );
+ verify( install ).createDist( Mockito.any( WrapperConfiguration.class ) );
+ verify( start ).start( new String[] { "arg" }, mockInstallDir );
+ }
+
+ @Test( )
+ public void failWhenDistNotSetInProperties()
+ throws Exception
+ {
+ properties = new Properties();
+ writePropertiesFile( properties, propertiesFile, "header" );
+
+ try
+ {
+ WrapperExecutor.forWrapperPropertiesFile( propertiesFile, System.out );
+ Assert.fail( "Expected RuntimeException" );
+ }
+ catch ( RuntimeException e )
+ {
+ Assert.assertEquals( "Could not load wrapper properties from '" + propertiesFile + "'.", e.getMessage() );
+ Assert.assertEquals( "No value with key 'distributionUrl' specified in wrapper properties file '"
+ + propertiesFile + "'.", e.getCause().getMessage() );
+ }
+
+ }
- @Test
- public void failWhenPropertiesFileDoesNotExist() {
- propertiesFile = new File(testDir, "unknown.properties");
+ @Test
+ public void failWhenPropertiesFileDoesNotExist()
+ {
+ propertiesFile = testDir.getRoot().toPath().resolve( "unknown.properties" );
- try {
- WrapperExecutor.forWrapperPropertiesFile(propertiesFile, System.out);
- Assert.fail("Expected RuntimeException");
- } catch (RuntimeException e) {
- Assert.assertEquals("Wrapper properties file '" + propertiesFile + "' does not exist.", e.getMessage());
+ try
+ {
+ WrapperExecutor.forWrapperPropertiesFile( propertiesFile, System.out );
+ Assert.fail( "Expected RuntimeException" );
+ }
+ catch ( RuntimeException e )
+ {
+ Assert.assertEquals( "Wrapper properties file '" + propertiesFile + "' does not exist.", e.getMessage() );
+ }
}
- }
- @Test
- public void testRelativeDistUrl() throws Exception {
+ @Test
+ public void testRelativeDistUrl()
+ throws Exception
+ {
- properties = new Properties();
- properties.put("distributionUrl", "some/relative/url/to/bin.zip");
- writePropertiesFile(properties, propertiesFile, "header");
+ properties = new Properties();
+ properties.put( "distributionUrl", "some/relative/url/to/bin.zip" );
+ writePropertiesFile( properties, propertiesFile, "header" );
- WrapperExecutor wrapper = WrapperExecutor.forWrapperPropertiesFile(propertiesFile, System.out);
- Assert.assertNotEquals("some/relative/url/to/bin.zip", wrapper.getDistribution().getSchemeSpecificPart());
- Assert.assertTrue(wrapper.getDistribution().getSchemeSpecificPart().endsWith("some/relative/url/to/bin.zip"));
- }
+ WrapperExecutor wrapper = WrapperExecutor.forWrapperPropertiesFile( propertiesFile, System.out );
+ Assert.assertNotEquals( "some/relative/url/to/bin.zip", wrapper.getDistribution().getSchemeSpecificPart() );
+ Assert.assertTrue( wrapper.getDistribution().getSchemeSpecificPart().endsWith( "some/relative/url/to/bin.zip" ) );
+ }
- private void writePropertiesFile( Properties properties, File propertiesFile, String message )
+ private void writePropertiesFile( Properties properties, Path propertiesFile, String message )
throws Exception
{
- propertiesFile.getParentFile().mkdirs();
+ Files.createDirectories( propertiesFile.getParent() );
- try ( OutputStream outStream = new FileOutputStream( propertiesFile ) )
+ try ( OutputStream outStream = Files.newOutputStream( propertiesFile ) )
{
properties.store( outStream, message );
}