Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MDEPLOY-279] Better validation for alt deploy repository mojo parameter #12

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 33 additions & 15 deletions src/main/java/org/apache/maven/plugins/deploy/DeployMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,6 @@
* under the License.
*/

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.maven.artifact.ArtifactUtils;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.plugin.MojoExecutionException;
Expand All @@ -41,6 +34,13 @@
import org.apache.maven.shared.transfer.project.deploy.ProjectDeployer;
import org.apache.maven.shared.transfer.project.deploy.ProjectDeployerRequest;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* Deploys an artifact to remote repository.
*
Expand All @@ -51,8 +51,9 @@
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
Expand Down Expand Up @@ -248,19 +249,36 @@ else if ( !ArtifactUtils.isSnapshot( project.getVersion() ) && altReleaseDeploym
{
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 url = matcher.group( 3 ).trim();

throw new MojoFailureException( altDeploymentRepo,
"Invalid legacy syntax for repository.",
"Invalid legacy syntax for alternative repository. Use \"" + id + "::" + url + "\" instead."
);
}
else
{
String id = matcher.group( 1 ).trim();
String url = matcher.group( 2 ).trim();
matcher = ALT_REPO_SYNTAX_PATTERN.matcher( altDeploymentRepo );

repo = createDeploymentArtifactRepository( id, url );
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 );
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
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;
Expand Down Expand Up @@ -551,7 +552,88 @@ public void _testBasicDeployWithScpAsProtocol()

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 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 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 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 testAltSnapshotDeploymentRepository()
throws Exception
{
Expand Down