You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by mi...@apache.org on 2020/12/20 23:01:31 UTC

[maven-deploy-plugin] branch master updated: [MDEPLOY-279] Missing validation of altDeploymentRepository mojo parameter

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

michaelo pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven-deploy-plugin.git


The following commit(s) were added to refs/heads/master by this push:
     new 92d7e74  [MDEPLOY-279] Missing validation of altDeploymentRepository mojo parameter
92d7e74 is described below

commit 92d7e747c2228ed411ae6e9d1639401a6b8fe6cc
Author: Tamas Cservenak <ta...@cservenak.net>
AuthorDate: Thu Dec 10 14:14:16 2020 +0100

    [MDEPLOY-279] Missing validation of altDeploymentRepository mojo parameter
    
    This closes #12
---
 .../apache/maven/plugins/deploy/DeployMojo.java    |  46 +++++--
 .../maven/plugins/deploy/DeployMojoTest.java       | 137 ++++++++++++++++++++-
 2 files changed, 174 insertions(+), 9 deletions(-)

diff --git a/src/main/java/org/apache/maven/plugins/deploy/DeployMojo.java b/src/main/java/org/apache/maven/plugins/deploy/DeployMojo.java
index 5bc471d..ab0c72b 100644
--- a/src/main/java/org/apache/maven/plugins/deploy/DeployMojo.java
+++ b/src/main/java/org/apache/maven/plugins/deploy/DeployMojo.java
@@ -51,8 +51,9 @@ import org.apache.maven.shared.transfer.project.deploy.ProjectDeployerRequest;
 public class DeployMojo
     extends AbstractDeployMojo
 {
+    private static final Pattern ALT_LEGACY_REPO_SYNTAX_PATTERN = Pattern.compile( "(.+?)::(.+?)::(.+)" );
 
-    private static final Pattern ALT_REPO_SYNTAX_PATTERN = Pattern.compile( "(.+)::(.+)" );
+    private static final Pattern ALT_REPO_SYNTAX_PATTERN = Pattern.compile( "(.+?)::(.+)" );
 
     /**
      * When building with multiple threads, reaching the last project doesn't have to mean that all projects are ready
@@ -248,19 +249,48 @@ public class DeployMojo
         {
             getLog().info( "Using alternate deployment repository " + altDeploymentRepo );
 
-            Matcher matcher = ALT_REPO_SYNTAX_PATTERN.matcher( altDeploymentRepo );
+            Matcher matcher = ALT_LEGACY_REPO_SYNTAX_PATTERN.matcher( altDeploymentRepo );
 
-            if ( !matcher.matches() )
+            if ( matcher.matches() )
             {
-                throw new MojoFailureException( altDeploymentRepo, "Invalid syntax for repository.",
-                                                "Invalid syntax for alternative repository. Use \"id::url\"." );
+                String id = matcher.group( 1 ).trim();
+                String layout = matcher.group( 2 ).trim();
+                String url = matcher.group( 3 ).trim();
+
+                if ( "default".equals( layout ) )
+                {
+                    throw new MojoFailureException( altDeploymentRepo,
+                            "Invalid legacy syntax for repository.",
+                            "Invalid legacy syntax for alternative repository. Use \"" + id + "::" + url + "\" instead."
+                    );
+                }
+                else
+                {
+                    throw new MojoFailureException( altDeploymentRepo,
+                            "Invalid legacy syntax and layout for repository.",
+                            "Invalid legacy syntax and layout for alternative repository. Use \""
+                                    + id + "::" + url + "\" instead, and only default layout is supported."
+                    );
+                }
             }
             else
             {
-                String id = matcher.group( 1 ).trim();
-                String url = matcher.group( 2 ).trim();
+                matcher = ALT_REPO_SYNTAX_PATTERN.matcher( altDeploymentRepo );
+
+                if ( !matcher.matches() )
+                {
+                    throw new MojoFailureException( altDeploymentRepo,
+                            "Invalid syntax for repository.",
+                            "Invalid syntax for alternative repository. Use \"id::url\"."
+                    );
+                }
+                else
+                {
+                    String id = matcher.group( 1 ).trim();
+                    String url = matcher.group( 2 ).trim();
 
-                repo = createDeploymentArtifactRepository( id, url );
+                    repo = createDeploymentArtifactRepository( id, url );
+                }
             }
         }
 
diff --git a/src/test/java/org/apache/maven/plugins/deploy/DeployMojoTest.java b/src/test/java/org/apache/maven/plugins/deploy/DeployMojoTest.java
index ee762ef..9a02b1c 100644
--- a/src/test/java/org/apache/maven/plugins/deploy/DeployMojoTest.java
+++ b/src/test/java/org/apache/maven/plugins/deploy/DeployMojoTest.java
@@ -32,6 +32,7 @@ import java.util.Properties;
 import org.apache.maven.artifact.repository.ArtifactRepository;
 import org.apache.maven.execution.MavenSession;
 import org.apache.maven.plugin.MojoExecutionException;
+import org.apache.maven.plugin.MojoFailureException;
 import org.apache.maven.plugin.testing.AbstractMojoTestCase;
 import org.apache.maven.plugin.testing.stubs.MavenProjectStub;
 import org.apache.maven.plugins.deploy.stubs.ArtifactDeployerStub;
@@ -551,7 +552,141 @@ public class DeployMojoTest
         
         FileUtils.deleteDirectory( sshFile );
     }
-    
+
+    public void testLegacyAltDeploymentRepositoryWithDefaultLayout()
+        throws Exception
+    {
+        DeployMojo mojo = spy( new DeployMojo() );
+
+        ArtifactRepository repository = mock( ArtifactRepository.class );
+        when( mojo.createDeploymentArtifactRepository( "altDeploymentRepository", "http://localhost"
+        ) ).thenReturn( repository );
+
+        project.setVersion( "1.0-SNAPSHOT" );
+
+        ProjectDeployerRequest pdr =
+            new ProjectDeployerRequest()
+                .setProject( project )
+                .setAltDeploymentRepository( "altDeploymentRepository::default::http://localhost" );
+        try
+        {
+            mojo.getDeploymentRepository( pdr );
+            fail( "Should throw: Invalid legacy syntax for repository." );
+        }
+        catch( MojoFailureException e )
+        {
+            assertEquals( e.getMessage(), "Invalid legacy syntax for repository.");
+            assertEquals( e.getLongMessage(), "Invalid legacy syntax for alternative repository. Use \"altDeploymentRepository::http://localhost\" instead.");
+        }
+    }
+
+    public void testLegacyAltDeploymentRepositoryWithLegacyLayout()
+        throws Exception
+    {
+        DeployMojo mojo = spy( new DeployMojo() );
+
+        ArtifactRepository repository = mock( ArtifactRepository.class );
+        when( mojo.createDeploymentArtifactRepository( "altDeploymentRepository", "http://localhost"
+        ) ).thenReturn( repository );
+
+        project.setVersion( "1.0-SNAPSHOT" );
+
+        ProjectDeployerRequest pdr =
+            new ProjectDeployerRequest()
+                .setProject( project )
+                .setAltDeploymentRepository( "altDeploymentRepository::legacy::http://localhost" );
+        try
+        {
+            mojo.getDeploymentRepository( pdr );
+            fail( "Should throw: Invalid legacy syntax and layout for repository." );
+        }
+        catch( MojoFailureException e )
+        {
+            assertEquals( e.getMessage(), "Invalid legacy syntax and layout for repository.");
+            assertEquals( e.getLongMessage(), "Invalid legacy syntax and layout for alternative repository. Use \"altDeploymentRepository::http://localhost\" instead, and only default layout is supported.");
+        }
+    }
+
+    public void testInsaneAltDeploymentRepository()
+            throws Exception
+    {
+        DeployMojo mojo = spy( new DeployMojo() );
+
+        ArtifactRepository repository = mock( ArtifactRepository.class );
+        when( mojo.createDeploymentArtifactRepository( "altDeploymentRepository", "http://localhost"
+        ) ).thenReturn( repository );
+
+        project.setVersion( "1.0-SNAPSHOT" );
+
+        ProjectDeployerRequest pdr =
+                new ProjectDeployerRequest()
+                        .setProject( project )
+                        .setAltDeploymentRepository( "altDeploymentRepository::hey::wow::foo::http://localhost" );
+        try
+        {
+            mojo.getDeploymentRepository( pdr );
+            fail( "Should throw: Invalid legacy syntax and layout for repository." );
+        }
+        catch( MojoFailureException e )
+        {
+            assertEquals( e.getMessage(), "Invalid legacy syntax and layout for repository.");
+            assertEquals( e.getLongMessage(), "Invalid legacy syntax and layout for alternative repository. Use \"altDeploymentRepository::wow::foo::http://localhost\" instead, and only default layout is supported.");
+        }
+    }
+
+    public void testDefaultScmSvnAltDeploymentRepository()
+            throws Exception
+    {
+        DeployMojo mojo = spy( new DeployMojo() );
+
+        ArtifactRepository repository = mock( ArtifactRepository.class );
+        when( mojo.createDeploymentArtifactRepository( "altDeploymentRepository", "http://localhost"
+        ) ).thenReturn( repository );
+
+        project.setVersion( "1.0-SNAPSHOT" );
+
+        ProjectDeployerRequest pdr =
+                new ProjectDeployerRequest()
+                        .setProject( project )
+                        .setAltDeploymentRepository( "altDeploymentRepository::default::scm:svn:http://localhost" );
+        try
+        {
+            mojo.getDeploymentRepository( pdr );
+            fail( "Should throw: Invalid legacy syntax for repository." );
+        }
+        catch( MojoFailureException e )
+        {
+            assertEquals( e.getMessage(), "Invalid legacy syntax for repository.");
+            assertEquals( e.getLongMessage(), "Invalid legacy syntax for alternative repository. Use \"altDeploymentRepository::scm:svn:http://localhost\" instead.");
+        }
+    }
+    public void testLegacyScmSvnAltDeploymentRepository()
+            throws Exception
+    {
+        DeployMojo mojo = spy( new DeployMojo() );
+
+        ArtifactRepository repository = mock( ArtifactRepository.class );
+        when( mojo.createDeploymentArtifactRepository( "altDeploymentRepository", "http://localhost"
+        ) ).thenReturn( repository );
+
+        project.setVersion( "1.0-SNAPSHOT" );
+
+        ProjectDeployerRequest pdr =
+                new ProjectDeployerRequest()
+                        .setProject( project )
+                        .setAltDeploymentRepository( "altDeploymentRepository::legacy::scm:svn:http://localhost" );
+        try
+        {
+            mojo.getDeploymentRepository( pdr );
+            fail( "Should throw: Invalid legacy syntax and layout for repository." );
+        }
+        catch( MojoFailureException e )
+        {
+            assertEquals( e.getMessage(), "Invalid legacy syntax and layout for repository.");
+            assertEquals( e.getLongMessage(), "Invalid legacy syntax and layout for alternative repository. Use \"altDeploymentRepository::scm:svn:http://localhost\" instead, and only default layout is supported.");
+        }
+    }
+
     public void testAltSnapshotDeploymentRepository()
         throws Exception
     {