From 4cbbc08d5114539f81519db392ad1b4e401e394c Mon Sep 17 00:00:00 2001 From: Romain Bioteau Date: Fri, 25 Nov 2022 14:55:54 +0100 Subject: [PATCH] [MINSTALL-186] Use proper repositorySystemSession When looking for existing files in the local repository, use the proper `repositorySystemSession`. Current behavior ignore the `localRepositoryPath` parameter when checking for file existence and lead to false positive. --- .gitignore | 1 + .../plugins/install/InstallFileMojo.java | 4 +- .../plugins/install/InstallFileMojoTest.java | 86 ++++++++++++++++-- .../plugin-config.xml | 35 +++++++ .../maven-install-test-1.0-SNAPSHOT.jar | Bin 0 -> 2372 bytes 5 files changed, 115 insertions(+), 11 deletions(-) create mode 100644 src/test/resources/unit/install-file-from-local-repository-test/plugin-config.xml create mode 100644 src/test/resources/unit/install-file-from-local-repository-test/target/org/apache/maven/test/maven-install-test/1.0-SNAPSHOT/maven-install-test-1.0-SNAPSHOT.jar diff --git a/.gitignore b/.gitignore index 7495d7e2..3093e958 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ target/ +!src/test/resources/unit/*/target .project .classpath .settings/ diff --git a/src/main/java/org/apache/maven/plugins/install/InstallFileMojo.java b/src/main/java/org/apache/maven/plugins/install/InstallFileMojo.java index a006dace..bdd46c12 100644 --- a/src/main/java/org/apache/maven/plugins/install/InstallFileMojo.java +++ b/src/main/java/org/apache/maven/plugins/install/InstallFileMojo.java @@ -248,8 +248,8 @@ public void execute() ).setFile( file ); installRequest.addArtifact( mainArtifact ); - File artifactLocalFile = getLocalRepositoryFile( session.getRepositorySession(), mainArtifact ); - File pomLocalFile = getPomLocalRepositoryFile( session.getRepositorySession(), mainArtifact ); + File artifactLocalFile = getLocalRepositoryFile( repositorySystemSession, mainArtifact ); + File pomLocalFile = getPomLocalRepositoryFile( repositorySystemSession, mainArtifact ); if ( file.equals( artifactLocalFile ) ) { diff --git a/src/test/java/org/apache/maven/plugins/install/InstallFileMojoTest.java b/src/test/java/org/apache/maven/plugins/install/InstallFileMojoTest.java index 7efd90c0..bcfd69d7 100644 --- a/src/test/java/org/apache/maven/plugins/install/InstallFileMojoTest.java +++ b/src/test/java/org/apache/maven/plugins/install/InstallFileMojoTest.java @@ -58,12 +58,79 @@ public class InstallFileMojoTest private final String LOCAL_REPO = "target/local-repo/"; + private final String SPECIFIC_LOCAL_REPO = "target/specific-local-repo/"; + public void setUp() throws Exception { super.setUp(); FileUtils.deleteDirectory( new File( getBasedir() + "/" + LOCAL_REPO ) ); + FileUtils.deleteDirectory( new File( getBasedir() + "/" + SPECIFIC_LOCAL_REPO ) ); + } + + public void testInstallFileFromLocalRepositoryToLocalRepositoryPath() + throws Exception + { + File localRepository = + new File( getBasedir(), "target/test-classes/unit/install-file-from-local-repository-test/target" ); + + File testPom = new File( localRepository.getParentFile(), "plugin-config.xml" ); + + InstallFileMojo mojo = (InstallFileMojo) lookupMojo( "install-file", testPom ); + + assertNotNull( mojo ); + + setVariableValueToObject( mojo, "session", createMavenSession( localRepository.getAbsolutePath() ) ); + + File specificLocalRepositoryPath = new File( getBasedir() + "/" + SPECIFIC_LOCAL_REPO ); + + setVariableValueToObject( mojo, "localRepositoryPath", specificLocalRepositoryPath ); + + assignValuesForParameter( mojo ); + + mojo.execute(); + + String localPath = getBasedir() + "/" + SPECIFIC_LOCAL_REPO + groupId + "/" + artifactId + "/" + version + "/" + + artifactId + "-" + version; + + File installedArtifact = new File( localPath + "." + "jar" ); + + assertTrue( installedArtifact.exists() ); + + assertEquals( FileUtils.getFiles( new File( SPECIFIC_LOCAL_REPO ), null, null ).toString(), 5, + FileUtils.getFiles( new File( SPECIFIC_LOCAL_REPO ), null, null ).size() ); + } + + public void testInstallFileWithLocalRepositoryPath() + throws Exception + { + File testPom = + new File( getBasedir(), "target/test-classes/unit/install-file-with-checksum/" + "plugin-config.xml" ); + + InstallFileMojo mojo = (InstallFileMojo) lookupMojo( "install-file", testPom ); + + assertNotNull( mojo ); + + setVariableValueToObject( mojo, "session", createMavenSession( LOCAL_REPO ) ); + + File specificLocalRepositoryPath = new File( getBasedir() + "/" + SPECIFIC_LOCAL_REPO ); + + setVariableValueToObject( mojo, "localRepositoryPath", specificLocalRepositoryPath ); + + assignValuesForParameter( mojo ); + + mojo.execute(); + + String localPath = getBasedir() + "/" + SPECIFIC_LOCAL_REPO + groupId + "/" + artifactId + "/" + version + "/" + + artifactId + "-" + version; + + File installedArtifact = new File( localPath + "." + "jar" ); + + assertTrue( installedArtifact.exists() ); + + assertEquals( FileUtils.getFiles( new File( SPECIFIC_LOCAL_REPO ), null, null ).toString(), 5, + FileUtils.getFiles( new File( SPECIFIC_LOCAL_REPO ), null, null ).size() ); } public void testInstallFileTestEnvironment() @@ -73,7 +140,7 @@ public void testInstallFileTestEnvironment() InstallFileMojo mojo = (InstallFileMojo) lookupMojo( "install-file", testPom ); - setVariableValueToObject( mojo, "session", createMavenSession() ); + setVariableValueToObject( mojo, "session", createMavenSession( LOCAL_REPO ) ); assertNotNull( mojo ); } @@ -87,7 +154,7 @@ public void testBasicInstallFile() assertNotNull( mojo ); - setVariableValueToObject( mojo, "session", createMavenSession() ); + setVariableValueToObject( mojo, "session", createMavenSession( LOCAL_REPO ) ); assignValuesForParameter( mojo ); @@ -111,7 +178,7 @@ public void testInstallFileWithClassifier() assertNotNull( mojo ); - setVariableValueToObject( mojo, "session", createMavenSession() ); + setVariableValueToObject( mojo, "session", createMavenSession( LOCAL_REPO ) ); assignValuesForParameter( mojo ); @@ -137,7 +204,7 @@ public void testInstallFileWithGeneratePom() assertNotNull( mojo ); - setVariableValueToObject( mojo, "session", createMavenSession() ); + setVariableValueToObject( mojo, "session", createMavenSession( LOCAL_REPO ) ); assignValuesForParameter( mojo ); @@ -178,7 +245,7 @@ public void testInstallFileWithPomFile() assertNotNull( mojo ); - setVariableValueToObject( mojo, "session", createMavenSession() ); + setVariableValueToObject( mojo, "session", createMavenSession( LOCAL_REPO ) ); assignValuesForParameter( mojo ); @@ -211,7 +278,7 @@ public void testInstallFileWithPomAsPackaging() assertNotNull( mojo ); - setVariableValueToObject( mojo, "session", createMavenSession() ); + setVariableValueToObject( mojo, "session", createMavenSession( LOCAL_REPO ) ); assignValuesForParameter( mojo ); @@ -239,7 +306,7 @@ public void testInstallFile() assertNotNull( mojo ); - setVariableValueToObject( mojo, "session", createMavenSession() ); + setVariableValueToObject( mojo, "session", createMavenSession( LOCAL_REPO ) ); assignValuesForParameter( mojo ); @@ -276,13 +343,14 @@ private String dotToSlashReplacer( String parameter ) return parameter.replace( '.', '/' ); } - private MavenSession createMavenSession() throws NoLocalRepositoryManagerException + private MavenSession createMavenSession( String localRepositoryBaseDir ) + throws NoLocalRepositoryManagerException { MavenSession session = mock( MavenSession.class ); DefaultRepositorySystemSession repositorySession = new DefaultRepositorySystemSession(); repositorySession.setLocalRepositoryManager( new EnhancedLocalRepositoryManagerFactory().newInstance( - repositorySession, new LocalRepository( LOCAL_REPO ) + repositorySession, new LocalRepository( localRepositoryBaseDir ) ) ); ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(); diff --git a/src/test/resources/unit/install-file-from-local-repository-test/plugin-config.xml b/src/test/resources/unit/install-file-from-local-repository-test/plugin-config.xml new file mode 100644 index 00000000..31ddebf6 --- /dev/null +++ b/src/test/resources/unit/install-file-from-local-repository-test/plugin-config.xml @@ -0,0 +1,35 @@ + + + + + + maven-install-plugin + + org.apache.maven.test + maven-install-test + 1.0-SNAPSHOT + jar + ${basedir}/target/test-classes/unit/install-file-from-local-repository-test/target/org/apache/maven/test/maven-install-test/1.0-SNAPSHOT/maven-install-test-1.0-SNAPSHOT.jar + + + + + + diff --git a/src/test/resources/unit/install-file-from-local-repository-test/target/org/apache/maven/test/maven-install-test/1.0-SNAPSHOT/maven-install-test-1.0-SNAPSHOT.jar b/src/test/resources/unit/install-file-from-local-repository-test/target/org/apache/maven/test/maven-install-test/1.0-SNAPSHOT/maven-install-test-1.0-SNAPSHOT.jar new file mode 100644 index 0000000000000000000000000000000000000000..4a0e834e29ba17727a180d6ba70fdf6f845cd84c GIT binary patch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