From 52bfff25ffcccca1061000dc60395090e233d371 Mon Sep 17 00:00:00 2001 From: rfscholte Date: Sun, 4 Mar 2018 17:49:22 +0100 Subject: [PATCH] Switch to Paths, which are more reliable and powerful compared to Files --- .../phase/AbstractRewritePomsPhase.java | 9 +- .../release/phase/CheckoutProjectFromScm.java | 2 +- .../phase/RewritePomVersionsPhase.java | 4 +- .../phase/RewritePomsForBranchPhase.java | 9 +- .../phase/RewritePomsForDevelopmentPhase.java | 4 +- .../phase/RewritePomsForReleasePhase.java | 10 +- .../shared/release/util/ReleaseUtil.java | 78 ++------ .../shared/release/util/ReleaseUtilTest.java | 185 +++++++++--------- 8 files changed, 132 insertions(+), 169 deletions(-) diff --git a/maven-release-manager/src/main/java/org/apache/maven/shared/release/phase/AbstractRewritePomsPhase.java b/maven-release-manager/src/main/java/org/apache/maven/shared/release/phase/AbstractRewritePomsPhase.java index d068ce40a..066c79662 100644 --- a/maven-release-manager/src/main/java/org/apache/maven/shared/release/phase/AbstractRewritePomsPhase.java +++ b/maven-release-manager/src/main/java/org/apache/maven/shared/release/phase/AbstractRewritePomsPhase.java @@ -21,6 +21,7 @@ import java.io.File; import java.io.IOException; +import java.nio.file.Path; import java.util.ArrayList; import java.util.Collection; import java.util.List; @@ -169,7 +170,7 @@ private void transform( ReleaseDescriptor releaseDescriptor, ReleaseEnvironment List reactorProjects, boolean simulate, ReleaseResult result ) throws ReleaseExecutionException, ReleaseFailureException { - String commonBasedir; + Path commonBasedir; try { commonBasedir = ReleaseUtil.getCommonBasedir( reactorProjects ); @@ -189,7 +190,7 @@ private void transform( ReleaseDescriptor releaseDescriptor, ReleaseEnvironment } private void transformProject( MavenProject project, ReleaseDescriptor releaseDescriptor, - ReleaseEnvironment releaseEnvironment, String commonBasedir, + ReleaseEnvironment releaseEnvironment, Path commonBasedir, boolean simulate, ReleaseResult result ) throws ReleaseExecutionException, ReleaseFailureException { @@ -244,7 +245,7 @@ private void transformProject( MavenProject project, ReleaseDescriptor releaseDe } private void transformDocument( MavenProject project, Model modelTarget, ReleaseDescriptor releaseDescriptor, - String commonBasedir, ScmRepository scmRepository, + Path commonBasedir, ScmRepository scmRepository, ReleaseResult result, boolean simulate ) throws ReleaseExecutionException, ReleaseFailureException { @@ -568,7 +569,7 @@ protected abstract String getOriginalVersion( ReleaseDescriptor releaseDescripto protected abstract void transformScm( MavenProject project, Model modelTarget, ReleaseDescriptor releaseDescriptor, String projectId, ScmRepository scmRepository, - ReleaseResult result, String commonBasedir ) + ReleaseResult result, Path commonBasedir ) throws ReleaseExecutionException; /** diff --git a/maven-release-manager/src/main/java/org/apache/maven/shared/release/phase/CheckoutProjectFromScm.java b/maven-release-manager/src/main/java/org/apache/maven/shared/release/phase/CheckoutProjectFromScm.java index 33f953778..60f78b8c9 100644 --- a/maven-release-manager/src/main/java/org/apache/maven/shared/release/phase/CheckoutProjectFromScm.java +++ b/maven-release-manager/src/main/java/org/apache/maven/shared/release/phase/CheckoutProjectFromScm.java @@ -224,7 +224,7 @@ private ReleaseResult performCheckout( ReleaseDescriptor releaseDescriptor, Rele String basedir; try { - basedir = ReleaseUtil.getCommonBasedir( reactorProjects ); + basedir = ReleaseUtil.getCommonBasedir( reactorProjects ).toString(); } catch ( IOException e ) { diff --git a/maven-release-manager/src/main/java/org/apache/maven/shared/release/phase/RewritePomVersionsPhase.java b/maven-release-manager/src/main/java/org/apache/maven/shared/release/phase/RewritePomVersionsPhase.java index 0ef834e64..2bbd7b676 100644 --- a/maven-release-manager/src/main/java/org/apache/maven/shared/release/phase/RewritePomVersionsPhase.java +++ b/maven-release-manager/src/main/java/org/apache/maven/shared/release/phase/RewritePomVersionsPhase.java @@ -19,6 +19,8 @@ * under the License. */ +import java.nio.file.Path; + import org.apache.maven.model.Model; import org.apache.maven.project.MavenProject; import org.apache.maven.scm.repository.ScmRepository; @@ -45,7 +47,7 @@ protected final String getPomSuffix() @Override protected void transformScm( MavenProject project, Model modelTarget, ReleaseDescriptor releaseDescriptor, String projectId, ScmRepository scmRepository, ReleaseResult result, - String commonBasedir ) + Path commonBasedir ) throws ReleaseExecutionException { // We are only updating versions no mods to scm needed diff --git a/maven-release-manager/src/main/java/org/apache/maven/shared/release/phase/RewritePomsForBranchPhase.java b/maven-release-manager/src/main/java/org/apache/maven/shared/release/phase/RewritePomsForBranchPhase.java index 586e97d98..840f89a66 100644 --- a/maven-release-manager/src/main/java/org/apache/maven/shared/release/phase/RewritePomsForBranchPhase.java +++ b/maven-release-manager/src/main/java/org/apache/maven/shared/release/phase/RewritePomsForBranchPhase.java @@ -20,6 +20,7 @@ */ import java.io.IOException; +import java.nio.file.Path; import org.apache.maven.artifact.ArtifactUtils; import org.apache.maven.model.Model; @@ -52,7 +53,7 @@ protected final String getPomSuffix() @Override protected void transformScm( MavenProject project, Model modelTarget, ReleaseDescriptor releaseDescriptor, String projectId, ScmRepository scmRepository, ReleaseResult result, - String commonBasedir ) + Path commonBasedir ) throws ReleaseExecutionException { // If SCM is null in original model, it is inherited, no mods needed @@ -105,7 +106,7 @@ protected void transformScm( MavenProject project, Model modelTarget, ReleaseDes } private boolean translateScm( MavenProject project, ReleaseDescriptor releaseDescriptor, Scm scmTarget, - ScmRepository scmRepository, ReleaseResult relResult, String commonBasedir ) + ScmRepository scmRepository, ReleaseResult relResult, Path commonBasedir ) throws IOException { ScmTranslator translator = getScmTranslators().get( scmRepository.getProvider() ); @@ -127,9 +128,7 @@ private boolean translateScm( MavenProject project, ReleaseDescriptor releaseDes branchBase = "scm:svn:" + branchBase; } - String workingDirectory = - ReleaseUtil.isSymlink( project.getBasedir() ) ? project.getBasedir().getCanonicalPath() - : project.getBasedir().getAbsolutePath(); + Path workingDirectory = project.getBasedir().toPath().toRealPath(); int count = ReleaseUtil.getBaseWorkingDirectoryParentCount( commonBasedir, workingDirectory ); diff --git a/maven-release-manager/src/main/java/org/apache/maven/shared/release/phase/RewritePomsForDevelopmentPhase.java b/maven-release-manager/src/main/java/org/apache/maven/shared/release/phase/RewritePomsForDevelopmentPhase.java index eb5ff4d39..3c6aa8262 100644 --- a/maven-release-manager/src/main/java/org/apache/maven/shared/release/phase/RewritePomsForDevelopmentPhase.java +++ b/maven-release-manager/src/main/java/org/apache/maven/shared/release/phase/RewritePomsForDevelopmentPhase.java @@ -19,6 +19,8 @@ * under the License. */ +import java.nio.file.Path; + import org.apache.maven.model.Model; import org.apache.maven.model.Scm; import org.apache.maven.project.MavenProject; @@ -47,7 +49,7 @@ protected final String getPomSuffix() @Override protected void transformScm( MavenProject project, Model modelTarget, ReleaseDescriptor releaseDescriptor, String projectId, ScmRepository scmRepository, ReleaseResult result, - String commonBasedir ) + Path commonBasedir ) throws ReleaseExecutionException { // If SCM is null in original model, it is inherited, no mods needed diff --git a/maven-release-manager/src/main/java/org/apache/maven/shared/release/phase/RewritePomsForReleasePhase.java b/maven-release-manager/src/main/java/org/apache/maven/shared/release/phase/RewritePomsForReleasePhase.java index dc774a190..4e4bea43f 100644 --- a/maven-release-manager/src/main/java/org/apache/maven/shared/release/phase/RewritePomsForReleasePhase.java +++ b/maven-release-manager/src/main/java/org/apache/maven/shared/release/phase/RewritePomsForReleasePhase.java @@ -20,6 +20,7 @@ */ import java.io.IOException; +import java.nio.file.Path; import org.apache.maven.artifact.ArtifactUtils; import org.apache.maven.model.Model; @@ -51,7 +52,7 @@ protected final String getPomSuffix() @Override protected void transformScm( MavenProject project, Model modelTarget, ReleaseDescriptor releaseDescriptor, String projectId, ScmRepository scmRepository, ReleaseResult result, - String commonBasedir ) + Path commonBasedir ) throws ReleaseExecutionException { // If SCM is null in original model, it is inherited, no mods needed @@ -103,7 +104,7 @@ protected void transformScm( MavenProject project, Model modelTarget, ReleaseDes } private boolean translateScm( MavenProject project, ReleaseDescriptor releaseDescriptor, Scm scmTarget, - ScmRepository scmRepository, ReleaseResult relResult, String commonBasedir ) + ScmRepository scmRepository, ReleaseResult relResult, Path commonBasedir ) throws IOException { ScmTranslator translator = getScmTranslators().get( scmRepository.getProvider() ); @@ -125,9 +126,8 @@ private boolean translateScm( MavenProject project, ReleaseDescriptor releaseDes tagBase = "scm:svn:" + tagBase; } - String workingDirectory = - ReleaseUtil.isSymlink( project.getBasedir() ) ? project.getBasedir().getCanonicalPath() - : project.getBasedir().getAbsolutePath(); + Path workingDirectory = project.getBasedir().toPath().toRealPath(); + int count = ReleaseUtil.getBaseWorkingDirectoryParentCount( commonBasedir, workingDirectory ); if ( scm.getConnection() != null ) diff --git a/maven-release-manager/src/main/java/org/apache/maven/shared/release/util/ReleaseUtil.java b/maven-release-manager/src/main/java/org/apache/maven/shared/release/util/ReleaseUtil.java index 00f2c1ed8..587a50c30 100644 --- a/maven-release-manager/src/main/java/org/apache/maven/shared/release/util/ReleaseUtil.java +++ b/maven-release-manager/src/main/java/org/apache/maven/shared/release/util/ReleaseUtil.java @@ -26,9 +26,7 @@ import java.nio.file.Paths; import java.util.Arrays; import java.util.List; -import java.util.Locale; -import org.apache.commons.io.FilenameUtils; import org.apache.commons.lang3.StringUtils; import org.apache.maven.model.Model; import org.apache.maven.project.MavenProject; @@ -42,7 +40,6 @@ import org.codehaus.plexus.interpolation.PrefixAwareRecursionInterceptor; import org.codehaus.plexus.interpolation.PrefixedObjectValueSource; import org.codehaus.plexus.interpolation.StringSearchInterpolator; -import org.codehaus.plexus.util.FileUtils; import org.codehaus.plexus.util.IOUtil; import org.codehaus.plexus.util.ReaderFactory; @@ -58,8 +55,6 @@ public class ReleaseUtil @SuppressWarnings( "checkstyle:constantname" ) public static final String POMv4 = "pom.xml"; - private static final String FS = File.separator; - /** * The line separator to use. */ @@ -172,7 +167,7 @@ public static ReleaseDescriptor createBasedirAlignedReleaseDescriptor( ReleaseDe List reactorProjects ) throws ReleaseExecutionException { - String basedir; + Path basedir; try { basedir = getCommonBasedir( reactorProjects ); @@ -185,82 +180,37 @@ public static ReleaseDescriptor createBasedirAlignedReleaseDescriptor( ReleaseDe int parentLevels = getBaseWorkingDirectoryParentCount( basedir, - FileUtils.normalize( releaseDescriptor.getWorkingDirectory() ) ); + Paths.get( releaseDescriptor.getWorkingDirectory() ) ); String url = releaseDescriptor.getScmSourceUrl(); url = realignScmUrl( parentLevels, url ); ReleaseDescriptorBuilder builder = new ReleaseDescriptorBuilder(); - builder.setWorkingDirectory( basedir ); + builder.setWorkingDirectory( basedir.toFile().getAbsolutePath() ); builder.setScmSourceUrl( url ); return ReleaseUtils.buildReleaseDescriptor( builder ); } - public static String getCommonBasedir( List reactorProjects ) - throws IOException - { - return getCommonBasedir( reactorProjects, FS ); - } - - public static String getCommonBasedir( List reactorProjects, String separator ) + public static Path getCommonBasedir( List reactorProjects ) throws IOException { - String[] baseDirs = new String[reactorProjects.size()]; - int idx = 0; - for ( MavenProject p : reactorProjects ) + Path basePath = reactorProjects.get( 0 ).getBasedir().toPath(); + + for ( MavenProject reactorProject : reactorProjects ) { - String dir = p.getBasedir().getCanonicalPath(); - - // always end with separator so that we know what is a path and what is a partial directory name in the - // next call - if ( !dir.endsWith( separator ) ) + Path matchPath = reactorProject.getBasedir().toPath(); + while ( !basePath.startsWith( matchPath ) ) { - dir = dir + separator; + matchPath = matchPath.getParent(); } - baseDirs[idx++] = dir; + basePath = matchPath; } - - String basedir = StringUtils.getCommonPrefix( baseDirs ); - - int separatorPos = basedir.lastIndexOf( separator ); - if ( !basedir.endsWith( separator ) && separatorPos >= 0 ) - { - basedir = basedir.substring( 0, separatorPos ); - } - - if ( basedir.endsWith( separator ) && basedir.length() > 1 ) - { - basedir = basedir.substring( 0, basedir.length() - 1 ); - } - - return basedir; + return basePath; } - public static int getBaseWorkingDirectoryParentCount( String basedir, String workingDirectory ) + public static int getBaseWorkingDirectoryParentCount( final Path baseDirectory, final Path workingDirectory ) { - int num = 0; - - // we can safely assume case-insensitivity as we are just backtracking, not comparing. This helps with issues - // on Windows with C: vs c: - workingDirectory = FilenameUtils.normalize( workingDirectory.toLowerCase( Locale.ENGLISH ) ); - basedir = FilenameUtils.normalize( basedir.toLowerCase( Locale.ENGLISH ) ); - - // MRELEASE-663 - // For Windows is does matter if basedir ends with a file-separator or not to be able to compare. - // Using the parent of a dummy file makes it possible to compare them OS-independent - Path workingDirectoryFile = Paths.get( workingDirectory, ".tmp" ).getParent(); - Path basedirFile = Paths.get( basedir, ".tmp" ).getParent(); - - if ( !workingDirectoryFile.equals( basedirFile ) && workingDirectory.startsWith( basedir ) ) - { - do - { - workingDirectoryFile = workingDirectoryFile.getParent(); - num++; - } - while ( !workingDirectoryFile.equals( basedirFile ) ); - } - return num; + return Math.max( 0, workingDirectory.normalize().getNameCount() - baseDirectory.normalize().getNameCount() ); } public static String realignScmUrl( int parentLevels, String url ) diff --git a/maven-release-manager/src/test/java/org/apache/maven/shared/release/util/ReleaseUtilTest.java b/maven-release-manager/src/test/java/org/apache/maven/shared/release/util/ReleaseUtilTest.java index 6b063c9ef..3cdb7e450 100644 --- a/maven-release-manager/src/test/java/org/apache/maven/shared/release/util/ReleaseUtilTest.java +++ b/maven-release-manager/src/test/java/org/apache/maven/shared/release/util/ReleaseUtilTest.java @@ -25,6 +25,8 @@ import static org.junit.Assume.*; import java.io.File; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.Arrays; import java.util.Collections; @@ -50,122 +52,129 @@ public void testProjectIsNull() @Test public void testGetCommonBasedirSingleProject() throws Exception { - assertEquals( "/working/directory/flat-multi-module/project", ReleaseUtil.getCommonBasedir( - Collections.singletonList( createProject( "/working/directory/flat-multi-module/project" ) ), "/" ) ); + assertEquals( Paths.get( "/working/directory/flat-multi-module/project" ), + ReleaseUtil.getCommonBasedir( Collections.singletonList( createProject( "/working/directory/flat-multi-module/project" ) ) ) ); } @Test public void testGetCommonBasedirSingleProjectWindows() throws Exception { - assertEquals( "C:\\working\\directory\\flat-multi-module\\project", ReleaseUtil.getCommonBasedir( - Collections.singletonList( createProject( "C:\\working\\directory\\flat-multi-module\\project" ) ), - "\\" ) ); + assertEquals( Paths.get( "C:\\working\\directory\\flat-multi-module\\project" ), + ReleaseUtil.getCommonBasedir( Collections.singletonList( createProject( "C:\\working\\directory\\flat-multi-module\\project" ) ) ) ); } @Test public void testGetCommonBasedirOfFlatMultiModule() throws Exception { - assertEquals( "/working/directory/flat-multi-module", ReleaseUtil.getCommonBasedir( Arrays.asList( - new MavenProject[]{createProject( "/working/directory/flat-multi-module/root-project" ), - createProject( "/working/directory/flat-multi-module/core" ), - createProject( "/working/directory/flat-multi-module/webapp" )} ), "/" ) ); + assertEquals( Paths.get( "/working/directory/flat-multi-module" ), + ReleaseUtil.getCommonBasedir( Arrays.asList( new MavenProject[] { + createProject( "/working/directory/flat-multi-module/root-project" ), + createProject( "/working/directory/flat-multi-module/core" ), + createProject( "/working/directory/flat-multi-module/webapp" ) } ) ) ); } @Test public void testGetCommonBasedirOfFlatMultiModuleWindows() throws Exception { - assertEquals( "C:\\working\\directory\\flat-multi-module", ReleaseUtil.getCommonBasedir( Arrays.asList( - new MavenProject[]{createProject( "C:\\working\\directory\\flat-multi-module\\root-project" ), - createProject( "C:\\working\\directory\\flat-multi-module\\core" ), - createProject( "C:\\working\\directory\\flat-multi-module\\webapp" )} ), "\\" ) ); + assertEquals( Paths.get( "C:\\working\\directory\\flat-multi-module" ), + ReleaseUtil.getCommonBasedir( Arrays.asList( new MavenProject[] { + createProject( "C:\\working\\directory\\flat-multi-module\\root-project" ), + createProject( "C:\\working\\directory\\flat-multi-module\\core" ), + createProject( "C:\\working\\directory\\flat-multi-module\\webapp" ) } ) ) ); } @Test public void testGetCommonBasedirUppercaseLowerCaseWindows() throws Exception { - assertEquals( "C:\\WORKING\\root", ReleaseUtil.getCommonBasedir( Arrays.asList( - new MavenProject[]{createProject( "c:\\WORKING\\root", "C:\\WORKING\\root" ), - createProject( "c:\\working\\root\\project1", "C:\\WORKING\\root\\project1" ), - createProject( "C:\\WORKING\\root\\project2", "C:\\WORKING\\root\\project2" )} ), "\\" ) ); + assertEquals( Paths.get( "C:\\WORKING\\root" ), + ReleaseUtil.getCommonBasedir( Arrays.asList( new MavenProject[] { + createProject( "c:\\WORKING\\root", "C:\\WORKING\\root" ), + createProject( "c:\\working\\root\\project1", "C:\\WORKING\\root\\project1" ), + createProject( "C:\\WORKING\\root\\project2", "C:\\WORKING\\root\\project2" ) } ) ) ); } @Test public void testGetCommonBasedirOfFlatMultiModuleSimilarArtifactIds() throws Exception { - assertEquals( "/working/directory/flat-multi-module", ReleaseUtil.getCommonBasedir( Arrays.asList( - new MavenProject[]{createProject( "/working/directory/flat-multi-module/release-parent" ), - createProject( "/working/directory/flat-multi-module/release-module1" ), - createProject( "/working/directory/flat-multi-module/release-module2" )} ), "/" ) ); + assertEquals( Paths.get( "/working/directory/flat-multi-module" ), + ReleaseUtil.getCommonBasedir( Arrays.asList( new MavenProject[] { + createProject( "/working/directory/flat-multi-module/release-parent" ), + createProject( "/working/directory/flat-multi-module/release-module1" ), + createProject( "/working/directory/flat-multi-module/release-module2" ) } ) ) ); } @Test public void testGetCommonBasedirOfFlatMultiModuleSimilarArtifactIdsWindows() throws Exception { - assertEquals( "c:\\working\\directory\\flat-multi-module", ReleaseUtil.getCommonBasedir( Arrays.asList( - new MavenProject[]{createProject( "c:\\working\\directory\\flat-multi-module\\release-parent" ), - createProject( "c:\\working\\directory\\flat-multi-module\\release-module1" ), - createProject( "c:\\working\\directory\\flat-multi-module\\release-module2" )} ), "\\" ) ); + assertEquals( Paths.get( "c:\\working\\directory\\flat-multi-module" ), + ReleaseUtil.getCommonBasedir( Arrays.asList( new MavenProject[] { + createProject( "c:\\working\\directory\\flat-multi-module\\release-parent" ), + createProject( "c:\\working\\directory\\flat-multi-module\\release-module1" ), + createProject( "c:\\working\\directory\\flat-multi-module\\release-module2" ) } ) ) ); } @Test public void testGetCommonBasedirOfRegularMultiModule() throws Exception { - assertEquals( "/working/directory/flat-multi-module", ReleaseUtil.getCommonBasedir( Arrays.asList( - new MavenProject[]{createProject( "/working/directory/flat-multi-module" ), - createProject( "/working/directory/flat-multi-module/core" ), - createProject( "/working/directory/flat-multi-module/webapp" )} ), "/" ) ); + assertEquals( Paths.get( "/working/directory/flat-multi-module" ), + ReleaseUtil.getCommonBasedir( Arrays.asList( new MavenProject[] { + createProject( "/working/directory/flat-multi-module" ), + createProject( "/working/directory/flat-multi-module/core" ), + createProject( "/working/directory/flat-multi-module/webapp" ) } ) ) ); } @Test public void testGetCommonBasedirOfRegularMultiModuleParentNotBeeingFirstInReactor() throws Exception { - assertEquals( "/working/directory/flat-multi-module", ReleaseUtil.getCommonBasedir( Arrays.asList( - new MavenProject[]{ - createProject( "/working/directory/flat-multi-module/core" ), - createProject( "/working/directory/flat-multi-module" ), - createProject( "/working/directory/flat-multi-module/webapp" )} ), "/" ) ); + assertEquals( Paths.get( "/working/directory/flat-multi-module" ), + ReleaseUtil.getCommonBasedir( Arrays.asList( new MavenProject[] { + createProject( "/working/directory/flat-multi-module/core" ), + createProject( "/working/directory/flat-multi-module" ), + createProject( "/working/directory/flat-multi-module/webapp" ) } ) ) ); } @Test public void testGetCommonBasedirOfRegularMultiModuleWindowsPath() throws Exception { - assertEquals( "c:\\working\\directory\\flat-multi-module", ReleaseUtil.getCommonBasedir( Arrays.asList( - new MavenProject[]{ - createProject( "c:\\working\\directory\\flat-multi-module\\core" ), - createProject( "c:\\working\\directory\\flat-multi-module" ), - createProject( "c:\\working\\directory\\flat-multi-module\\webapp" )} ), "\\" ) ); + assertEquals( Paths.get( "c:\\working\\directory\\flat-multi-module" ), + ReleaseUtil.getCommonBasedir( Arrays.asList( new MavenProject[] { + createProject( "c:\\working\\directory\\flat-multi-module\\core" ), + createProject( "c:\\working\\directory\\flat-multi-module" ), + createProject( "c:\\working\\directory\\flat-multi-module\\webapp" ) } ) ) ); } @Test public void testGetCommonBasedirOfFlatMultiModuleWithMultipleLevels() throws Exception { - assertEquals( "/working/directory/flat-multi-module", ReleaseUtil.getCommonBasedir( Arrays.asList( - new MavenProject[]{createProject( "/working/directory/flat-multi-module/root-project" ), - createProject( "/working/directory/flat-multi-module/core" ), - createProject( "/working/directory/flat-multi-module/common/utils" ), - createProject( "/working/directory/flat-multi-module/common/xml" ), - createProject( "/working/directory/flat-multi-module/webapp" )} ), "/" ) ); + assertEquals( Paths.get( "/working/directory/flat-multi-module" ), + ReleaseUtil.getCommonBasedir( Arrays.asList( new MavenProject[] { + createProject( "/working/directory/flat-multi-module/root-project" ), + createProject( "/working/directory/flat-multi-module/core" ), + createProject( "/working/directory/flat-multi-module/common/utils" ), + createProject( "/working/directory/flat-multi-module/common/xml" ), + createProject( "/working/directory/flat-multi-module/webapp" ) } ) ) ); } @Test public void testGetCommonBasedirOfFlatMultiModuleWithDescendingHierarchy() throws Exception { - assertEquals( "/working/directory/flat-multi-module", ReleaseUtil.getCommonBasedir( Arrays.asList( - new MavenProject[]{createProject( "/working/directory/flat-multi-module/level/1/2/3" ), - createProject( "/working/directory/flat-multi-module/level/1/2" ), - createProject( "/working/directory/flat-multi-module/level/1" ), - createProject( "/working/directory/flat-multi-module/level" ), - createProject( "/working/directory/flat-multi-module/other" )} ), "/" ) ); + assertEquals( Paths.get( "/working/directory/flat-multi-module" ), + ReleaseUtil.getCommonBasedir( Arrays.asList( new MavenProject[] { + createProject( "/working/directory/flat-multi-module/level/1/2/3" ), + createProject( "/working/directory/flat-multi-module/level/1/2" ), + createProject( "/working/directory/flat-multi-module/level/1" ), + createProject( "/working/directory/flat-multi-module/level" ), + createProject( "/working/directory/flat-multi-module/other" ) } ) ) ); } @Test @@ -232,90 +241,90 @@ public void testGetBaseScmUrlOfFlatMultiModuleMultipleLevels() @Test public void testGetBaseWorkingDirectoryParentCountSameDirectory() { - String workingDirectory = "/working/directory/maven/release"; - String basedir = "/working/directory/maven/release"; + Path workingDirectory = Paths.get( "/working/directory/maven/release" ); + Path basedir = Paths.get( "/working/directory/maven/release" ); assertEquals( 0, ReleaseUtil.getBaseWorkingDirectoryParentCount( basedir, workingDirectory ) ); } @Test public void testGetBaseWorkingDirectoryParentCountSameDirectoryDotCharacter() { - String workingDirectory = new File( "/working/directory/maven/release/." ).getAbsolutePath(); - assertTrue( workingDirectory.contains( "." ) ); - String basedir = new File( "/working/directory/maven/release" ).getAbsolutePath(); + Path workingDirectory = Paths.get( "/working/directory/maven/release/." ).toAbsolutePath(); + assertTrue( workingDirectory.toString().contains( "." ) ); + Path basedir = Paths.get( "/working/directory/maven/release" ).toAbsolutePath(); assertEquals( 0, ReleaseUtil.getBaseWorkingDirectoryParentCount( basedir, workingDirectory ) ); // finish with slash - workingDirectory = new File( "/working/directory/maven/release/./" ).getAbsolutePath(); - assertTrue( workingDirectory.contains( "." ) ); - basedir = new File( "/working/directory/maven/release" ).getAbsolutePath(); + workingDirectory = Paths.get( "/working/directory/maven/release/./" ).toAbsolutePath(); + assertTrue( workingDirectory.toString().contains( "." ) ); + basedir = Paths.get( "/working/directory/maven/release" ).toAbsolutePath(); assertEquals( 0, ReleaseUtil.getBaseWorkingDirectoryParentCount( basedir, workingDirectory ) ); } @Test public void testGetBaseWorkingDirectoryParentCountSubdirectory() { - String workingDirectory = new File( "/working/directory/maven/release" ).getAbsolutePath(); - String basedir = new File( "/working/directory/maven/release/maven-release-manager" ).getAbsolutePath(); + Path workingDirectory = Paths.get( "/working/directory/maven/release" ).toAbsolutePath(); + Path basedir = Paths.get( "/working/directory/maven/release/maven-release-manager" ).toAbsolutePath(); assertEquals( 0, ReleaseUtil.getBaseWorkingDirectoryParentCount( basedir, workingDirectory ) ); } @Test public void testGetBaseWorkingDirectoryParentCountParentDirectory() { - String workingDirectory = - new File( "/working/directory/maven/release/maven-release-manager" ).getAbsolutePath(); - String basedir = new File( "/working/directory/maven/release" ).getAbsolutePath(); + Path workingDirectory = + Paths.get( "/working/directory/maven/release/maven-release-manager" ).toAbsolutePath(); + Path basedir = Paths.get( "/working/directory/maven/release" ).toAbsolutePath(); assertEquals( 1, ReleaseUtil.getBaseWorkingDirectoryParentCount( basedir, workingDirectory ) ); } @Test public void testGetBaseWorkingDirectoryParentCountParentDirectoryDotCharacter() { - String workingDirectory = - new File( "/working/directory/maven/release/maven-release-manager/." ).getAbsolutePath(); - assertTrue( workingDirectory.contains( "." ) ); - String basedir = new File( "/working/directory/maven/release" ).getAbsolutePath(); + Path workingDirectory = + Paths.get( "/working/directory/maven/release/maven-release-manager/." ).toAbsolutePath(); + assertTrue( workingDirectory.toString().contains( "." ) ); + Path basedir = Paths.get( "/working/directory/maven/release" ).toAbsolutePath(); assertEquals( 1, ReleaseUtil.getBaseWorkingDirectoryParentCount( basedir, workingDirectory ) ); // finish with slash - workingDirectory = new File( "/working/directory/maven/release/maven-release-manager/./" ).getAbsolutePath(); - assertTrue( workingDirectory.contains( "." ) ); - basedir = new File( "/working/directory/maven/release" ).getAbsolutePath(); + workingDirectory = Paths.get( "/working/directory/maven/release/maven-release-manager/./" ).toAbsolutePath(); + assertTrue( workingDirectory.toString().contains( "." ) ); + basedir = Paths.get( "/working/directory/maven/release" ).toAbsolutePath(); assertEquals( 1, ReleaseUtil.getBaseWorkingDirectoryParentCount( basedir, workingDirectory ) ); } @Test public void testGetBaseWorkingDirectoryParentCountParentDirectoryMultiple() { - String workingDirectory = - new File( "/working/directory/maven/release/maven-release-manager" ).getAbsolutePath(); - String basedir = new File( "/working/directory" ).getAbsolutePath(); + Path workingDirectory = + Paths.get( "/working/directory/maven/release/maven-release-manager" ).toAbsolutePath(); + Path basedir = Paths.get( "/working/directory" ).toAbsolutePath(); assertEquals( 3, ReleaseUtil.getBaseWorkingDirectoryParentCount( basedir, workingDirectory ) ); } @Test public void testGetBaseWorkingDirectoryParentCountParentDirectoryMultipleDotCharacter() { - String workingDirectory = - new File( "/working/directory/maven/release/maven-release-manager/./." ).getAbsolutePath(); - assertTrue( workingDirectory.contains( "." ) ); - String basedir = new File( "/working/directory" ).getAbsolutePath(); + Path workingDirectory = + Paths.get( "/working/directory/maven/release/maven-release-manager/./." ).toAbsolutePath(); + assertTrue( workingDirectory.toString().contains( "." ) ); + Path basedir = Paths.get( "/working/directory" ).toAbsolutePath(); assertEquals( 3, ReleaseUtil.getBaseWorkingDirectoryParentCount( basedir, workingDirectory ) ); // finish with slash - workingDirectory = new File( "/working/directory/maven/release/maven-release-manager/././" ).getAbsolutePath(); - assertTrue( workingDirectory.contains( "." ) ); - basedir = new File( "/working/directory" ).getAbsolutePath(); + workingDirectory = Paths.get( "/working/directory/maven/release/maven-release-manager/././" ).toAbsolutePath(); + assertTrue( workingDirectory.toString().contains( "." ) ); + basedir = Paths.get( "/working/directory" ).toAbsolutePath(); assertEquals( 3, ReleaseUtil.getBaseWorkingDirectoryParentCount( basedir, workingDirectory ) ); } @Test public void testGetBaseWorkingDirectoryParentCountDifferentCase() { - String workingDirectory = - new File( "/Working/Directory/maven/release/maven-release-manager" ).getAbsolutePath(); - String basedir = new File( "/working/directory" ).getAbsolutePath(); + Path workingDirectory = + Paths.get( "/Working/Directory/maven/release/maven-release-manager" ).toAbsolutePath(); + Path basedir = Paths.get( "/working/directory" ).toAbsolutePath(); assertEquals( 3, ReleaseUtil.getBaseWorkingDirectoryParentCount( basedir, workingDirectory ) ); } @@ -327,10 +336,10 @@ public void testGetWindowsRootBaseWorkingDirectoryParentCountDifferentCase() { assumeTrue( Os.isFamily( Os.FAMILY_WINDOWS ) ); - assertEquals( 2, ReleaseUtil.getBaseWorkingDirectoryParentCount( "C:", "C:\\working\\directory" ) ); - assertEquals( 2, ReleaseUtil.getBaseWorkingDirectoryParentCount( "C:", "C:\\working\\directory\\" ) ); - assertEquals( 2, ReleaseUtil.getBaseWorkingDirectoryParentCount( "C:\\", "C:\\working\\directory" ) ); - assertEquals( 2, ReleaseUtil.getBaseWorkingDirectoryParentCount( "C:\\", "C:\\working\\directory\\" ) ); + assertEquals( 2, ReleaseUtil.getBaseWorkingDirectoryParentCount( Paths.get( "C:" ), Paths.get( "C:\\working\\directory" ) ) ); + assertEquals( 2, ReleaseUtil.getBaseWorkingDirectoryParentCount( Paths.get( "C:" ), Paths.get( "C:\\working\\directory\\" ) ) ); + assertEquals( 2, ReleaseUtil.getBaseWorkingDirectoryParentCount( Paths.get( "C:\\" ), Paths.get( "C:\\working\\directory" ) ) ); + assertEquals( 2, ReleaseUtil.getBaseWorkingDirectoryParentCount( Paths.get( "C:\\" ), Paths.get( "C:\\working\\directory\\" ) ) ); } private static MavenProject createProject( String basedir )