Skip to content

Commit 92d7e74

Browse files
cstamasmichael-o
authored andcommitted
[MDEPLOY-279] Missing validation of altDeploymentRepository mojo parameter
This closes #12
1 parent 46a5f40 commit 92d7e74

File tree

2 files changed

+174
-9
lines changed

2 files changed

+174
-9
lines changed

src/main/java/org/apache/maven/plugins/deploy/DeployMojo.java

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,9 @@
5151
public class DeployMojo
5252
extends AbstractDeployMojo
5353
{
54+
private static final Pattern ALT_LEGACY_REPO_SYNTAX_PATTERN = Pattern.compile( "(.+?)::(.+?)::(.+)" );
5455

55-
private static final Pattern ALT_REPO_SYNTAX_PATTERN = Pattern.compile( "(.+)::(.+)" );
56+
private static final Pattern ALT_REPO_SYNTAX_PATTERN = Pattern.compile( "(.+?)::(.+)" );
5657

5758
/**
5859
* When building with multiple threads, reaching the last project doesn't have to mean that all projects are ready
@@ -248,19 +249,48 @@ else if ( !ArtifactUtils.isSnapshot( project.getVersion() ) && altReleaseDeploym
248249
{
249250
getLog().info( "Using alternate deployment repository " + altDeploymentRepo );
250251

251-
Matcher matcher = ALT_REPO_SYNTAX_PATTERN.matcher( altDeploymentRepo );
252+
Matcher matcher = ALT_LEGACY_REPO_SYNTAX_PATTERN.matcher( altDeploymentRepo );
252253

253-
if ( !matcher.matches() )
254+
if ( matcher.matches() )
254255
{
255-
throw new MojoFailureException( altDeploymentRepo, "Invalid syntax for repository.",
256-
"Invalid syntax for alternative repository. Use \"id::url\"." );
256+
String id = matcher.group( 1 ).trim();
257+
String layout = matcher.group( 2 ).trim();
258+
String url = matcher.group( 3 ).trim();
259+
260+
if ( "default".equals( layout ) )
261+
{
262+
throw new MojoFailureException( altDeploymentRepo,
263+
"Invalid legacy syntax for repository.",
264+
"Invalid legacy syntax for alternative repository. Use \"" + id + "::" + url + "\" instead."
265+
);
266+
}
267+
else
268+
{
269+
throw new MojoFailureException( altDeploymentRepo,
270+
"Invalid legacy syntax and layout for repository.",
271+
"Invalid legacy syntax and layout for alternative repository. Use \""
272+
+ id + "::" + url + "\" instead, and only default layout is supported."
273+
);
274+
}
257275
}
258276
else
259277
{
260-
String id = matcher.group( 1 ).trim();
261-
String url = matcher.group( 2 ).trim();
278+
matcher = ALT_REPO_SYNTAX_PATTERN.matcher( altDeploymentRepo );
279+
280+
if ( !matcher.matches() )
281+
{
282+
throw new MojoFailureException( altDeploymentRepo,
283+
"Invalid syntax for repository.",
284+
"Invalid syntax for alternative repository. Use \"id::url\"."
285+
);
286+
}
287+
else
288+
{
289+
String id = matcher.group( 1 ).trim();
290+
String url = matcher.group( 2 ).trim();
262291

263-
repo = createDeploymentArtifactRepository( id, url );
292+
repo = createDeploymentArtifactRepository( id, url );
293+
}
264294
}
265295
}
266296

src/test/java/org/apache/maven/plugins/deploy/DeployMojoTest.java

Lines changed: 136 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.apache.maven.artifact.repository.ArtifactRepository;
3333
import org.apache.maven.execution.MavenSession;
3434
import org.apache.maven.plugin.MojoExecutionException;
35+
import org.apache.maven.plugin.MojoFailureException;
3536
import org.apache.maven.plugin.testing.AbstractMojoTestCase;
3637
import org.apache.maven.plugin.testing.stubs.MavenProjectStub;
3738
import org.apache.maven.plugins.deploy.stubs.ArtifactDeployerStub;
@@ -551,7 +552,141 @@ public void _testBasicDeployWithScpAsProtocol()
551552

552553
FileUtils.deleteDirectory( sshFile );
553554
}
554-
555+
556+
public void testLegacyAltDeploymentRepositoryWithDefaultLayout()
557+
throws Exception
558+
{
559+
DeployMojo mojo = spy( new DeployMojo() );
560+
561+
ArtifactRepository repository = mock( ArtifactRepository.class );
562+
when( mojo.createDeploymentArtifactRepository( "altDeploymentRepository", "http://localhost"
563+
) ).thenReturn( repository );
564+
565+
project.setVersion( "1.0-SNAPSHOT" );
566+
567+
ProjectDeployerRequest pdr =
568+
new ProjectDeployerRequest()
569+
.setProject( project )
570+
.setAltDeploymentRepository( "altDeploymentRepository::default::http://localhost" );
571+
try
572+
{
573+
mojo.getDeploymentRepository( pdr );
574+
fail( "Should throw: Invalid legacy syntax for repository." );
575+
}
576+
catch( MojoFailureException e )
577+
{
578+
assertEquals( e.getMessage(), "Invalid legacy syntax for repository.");
579+
assertEquals( e.getLongMessage(), "Invalid legacy syntax for alternative repository. Use \"altDeploymentRepository::http://localhost\" instead.");
580+
}
581+
}
582+
583+
public void testLegacyAltDeploymentRepositoryWithLegacyLayout()
584+
throws Exception
585+
{
586+
DeployMojo mojo = spy( new DeployMojo() );
587+
588+
ArtifactRepository repository = mock( ArtifactRepository.class );
589+
when( mojo.createDeploymentArtifactRepository( "altDeploymentRepository", "http://localhost"
590+
) ).thenReturn( repository );
591+
592+
project.setVersion( "1.0-SNAPSHOT" );
593+
594+
ProjectDeployerRequest pdr =
595+
new ProjectDeployerRequest()
596+
.setProject( project )
597+
.setAltDeploymentRepository( "altDeploymentRepository::legacy::http://localhost" );
598+
try
599+
{
600+
mojo.getDeploymentRepository( pdr );
601+
fail( "Should throw: Invalid legacy syntax and layout for repository." );
602+
}
603+
catch( MojoFailureException e )
604+
{
605+
assertEquals( e.getMessage(), "Invalid legacy syntax and layout for repository.");
606+
assertEquals( e.getLongMessage(), "Invalid legacy syntax and layout for alternative repository. Use \"altDeploymentRepository::http://localhost\" instead, and only default layout is supported.");
607+
}
608+
}
609+
610+
public void testInsaneAltDeploymentRepository()
611+
throws Exception
612+
{
613+
DeployMojo mojo = spy( new DeployMojo() );
614+
615+
ArtifactRepository repository = mock( ArtifactRepository.class );
616+
when( mojo.createDeploymentArtifactRepository( "altDeploymentRepository", "http://localhost"
617+
) ).thenReturn( repository );
618+
619+
project.setVersion( "1.0-SNAPSHOT" );
620+
621+
ProjectDeployerRequest pdr =
622+
new ProjectDeployerRequest()
623+
.setProject( project )
624+
.setAltDeploymentRepository( "altDeploymentRepository::hey::wow::foo::http://localhost" );
625+
try
626+
{
627+
mojo.getDeploymentRepository( pdr );
628+
fail( "Should throw: Invalid legacy syntax and layout for repository." );
629+
}
630+
catch( MojoFailureException e )
631+
{
632+
assertEquals( e.getMessage(), "Invalid legacy syntax and layout for repository.");
633+
assertEquals( e.getLongMessage(), "Invalid legacy syntax and layout for alternative repository. Use \"altDeploymentRepository::wow::foo::http://localhost\" instead, and only default layout is supported.");
634+
}
635+
}
636+
637+
public void testDefaultScmSvnAltDeploymentRepository()
638+
throws Exception
639+
{
640+
DeployMojo mojo = spy( new DeployMojo() );
641+
642+
ArtifactRepository repository = mock( ArtifactRepository.class );
643+
when( mojo.createDeploymentArtifactRepository( "altDeploymentRepository", "http://localhost"
644+
) ).thenReturn( repository );
645+
646+
project.setVersion( "1.0-SNAPSHOT" );
647+
648+
ProjectDeployerRequest pdr =
649+
new ProjectDeployerRequest()
650+
.setProject( project )
651+
.setAltDeploymentRepository( "altDeploymentRepository::default::scm:svn:http://localhost" );
652+
try
653+
{
654+
mojo.getDeploymentRepository( pdr );
655+
fail( "Should throw: Invalid legacy syntax for repository." );
656+
}
657+
catch( MojoFailureException e )
658+
{
659+
assertEquals( e.getMessage(), "Invalid legacy syntax for repository.");
660+
assertEquals( e.getLongMessage(), "Invalid legacy syntax for alternative repository. Use \"altDeploymentRepository::scm:svn:http://localhost\" instead.");
661+
}
662+
}
663+
public void testLegacyScmSvnAltDeploymentRepository()
664+
throws Exception
665+
{
666+
DeployMojo mojo = spy( new DeployMojo() );
667+
668+
ArtifactRepository repository = mock( ArtifactRepository.class );
669+
when( mojo.createDeploymentArtifactRepository( "altDeploymentRepository", "http://localhost"
670+
) ).thenReturn( repository );
671+
672+
project.setVersion( "1.0-SNAPSHOT" );
673+
674+
ProjectDeployerRequest pdr =
675+
new ProjectDeployerRequest()
676+
.setProject( project )
677+
.setAltDeploymentRepository( "altDeploymentRepository::legacy::scm:svn:http://localhost" );
678+
try
679+
{
680+
mojo.getDeploymentRepository( pdr );
681+
fail( "Should throw: Invalid legacy syntax and layout for repository." );
682+
}
683+
catch( MojoFailureException e )
684+
{
685+
assertEquals( e.getMessage(), "Invalid legacy syntax and layout for repository.");
686+
assertEquals( e.getLongMessage(), "Invalid legacy syntax and layout for alternative repository. Use \"altDeploymentRepository::scm:svn:http://localhost\" instead, and only default layout is supported.");
687+
}
688+
}
689+
555690
public void testAltSnapshotDeploymentRepository()
556691
throws Exception
557692
{

0 commit comments

Comments
 (0)