PR: MNG-801

validate ids in model

git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@307314 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Brett Leslie Porter 2005-10-08 17:12:41 +00:00
parent 3171417299
commit 2715208e74
3 changed files with 44 additions and 6 deletions

View File

@ -37,6 +37,8 @@ import java.util.List;
public class DefaultModelValidator public class DefaultModelValidator
implements ModelValidator implements ModelValidator
{ {
private static final String ID_REGEX = "[A-Za-z0-9_\\-.]+";
/////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////
// ModelValidator Implementation // ModelValidator Implementation
@ -46,9 +48,9 @@ public class DefaultModelValidator
validateStringNotEmpty( "modelVersion", result, model.getModelVersion() ); validateStringNotEmpty( "modelVersion", result, model.getModelVersion() );
validateStringNotEmpty( "groupId", result, model.getGroupId() ); validateId( "groupId", result, model.getGroupId() );
validateStringNotEmpty( "artifactId", result, model.getArtifactId() ); validateId( "artifactId", result, model.getArtifactId() );
validateStringNotEmpty( "packaging", result, model.getPackaging() ); validateStringNotEmpty( "packaging", result, model.getPackaging() );
@ -58,13 +60,13 @@ public class DefaultModelValidator
{ {
Dependency d = (Dependency) it.next(); Dependency d = (Dependency) it.next();
validateSubElementStringNotEmpty( d, "dependencies.dependency.artifactId", result, d.getArtifactId() ); validateId( "dependencies.dependency.artifactId", result, d.getArtifactId() );
validateSubElementStringNotEmpty( d, "dependencies.dependency.groupId", result, d.getGroupId() ); validateId( "dependencies.dependency.groupId", result, d.getGroupId() );
validateSubElementStringNotEmpty( d, "dependencies.dependency.type", result, d.getType() ); validateStringNotEmpty( "dependencies.dependency.type", result, d.getType() );
validateSubElementStringNotEmpty( d, "dependencies.dependency.version", result, d.getVersion() ); validateStringNotEmpty( "dependencies.dependency.version", result, d.getVersion() );
if ( Artifact.SCOPE_SYSTEM.equals( d.getScope() ) && StringUtils.isEmpty( d.getSystemPath() ) ) if ( Artifact.SCOPE_SYSTEM.equals( d.getScope() ) && StringUtils.isEmpty( d.getSystemPath() ) )
{ {
@ -138,6 +140,23 @@ public class DefaultModelValidator
return result; return result;
} }
private boolean validateId( String fieldName, ModelValidationResult result, String id )
{
if ( !validateStringNotEmpty( fieldName, result, id ) )
{
return false;
}
else
{
boolean match = id.matches( ID_REGEX );
if ( !match )
{
result.addMessage( "'" + fieldName + "' with value '" + id + "' does not match a valid id pattern." );
}
return match;
}
}
private void validateRepositories( ModelValidationResult result, List repositories, String prefix ) private void validateRepositories( ModelValidationResult result, List repositories, String prefix )
{ {
for ( Iterator it = repositories.iterator(); it.hasNext(); ) for ( Iterator it = repositories.iterator(); it.hasNext(); )

View File

@ -65,6 +65,18 @@ public class DefaultModelValidatorTest
assertEquals( "'groupId' is missing.", result.getMessage( 0 ) ); assertEquals( "'groupId' is missing.", result.getMessage( 0 ) );
} }
public void testInvalidIds()
throws Exception
{
ModelValidationResult result = validate( "invalid-ids-pom.xml" );
assertEquals( 2, result.getMessageCount() );
assertEquals( "'groupId' with value 'o/a/m' does not match a valid id pattern.", result.getMessage( 0 ) );
assertEquals( "'artifactId' with value 'm$-do$' does not match a valid id pattern.", result.getMessage( 1 ) );
}
public void testMissingType() public void testMissingType()
throws Exception throws Exception
{ {

View File

@ -0,0 +1,7 @@
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>o/a/m</groupId>
<artifactId>m$-do$</artifactId>
<version>99.44</version>
<packaging>bleh</packaging>
</project>