Skip to content

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

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

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
46 changes: 38 additions & 8 deletions src/main/java/org/apache/maven/plugins/deploy/DeployMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -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,48 @@ 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 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 );
}
}
}

Expand Down
137 changes: 136 additions & 1 deletion src/test/java/org/apache/maven/plugins/deploy/DeployMojoTest.java
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,141 @@ 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 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
{
Expand Down