[MNG-4732] Version string validation

git-svn-id: https://svn.apache.org/repos/asf/maven/maven-3/trunk@965141 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Benjamin Bentmann 2010-07-17 21:53:47 +00:00
parent bcde4d89a4
commit 260867adc2
3 changed files with 61 additions and 0 deletions

View File

@ -61,6 +61,8 @@ public class DefaultModelValidator
private static final String ID_REGEX = "[A-Za-z0-9_\\-.]+";
private static final String ILLEGAL_VERSION_CHARS = "\\/:\"<>|?*";
public void validateRawModel( Model model, ModelBuildingRequest request, ModelProblemCollector problems )
{
Parent parent = model.getParent();
@ -252,6 +254,9 @@ public class DefaultModelValidator
Severity errOn31 = getSeverity( request, ModelBuildingRequest.VALIDATION_LEVEL_MAVEN_3_1 );
validateBannedCharacters( "version", problems, errOn31, model.getVersion(), null, model,
ILLEGAL_VERSION_CHARS );
Build build = model.getBuild();
if ( build != null )
{
@ -689,6 +694,27 @@ public class DefaultModelValidator
return false;
}
private boolean validateBannedCharacters( String fieldName, ModelProblemCollector problems, Severity severity,
String string, String sourceHint, InputLocationTracker tracker,
String banned )
{
if ( string != null )
{
for ( int i = string.length() - 1; i >= 0; i-- )
{
if ( banned.indexOf( string.charAt( i ) ) >= 0 )
{
addViolation( problems, severity, fieldName, sourceHint,
"must not contain any of these characters " + banned + " but found "
+ string.charAt( i ), tracker );
return false;
}
}
}
return true;
}
private boolean validateVersion( String fieldName, ModelProblemCollector problems, Severity severity,
String string, String sourceHint, InputLocationTracker tracker )
{

View File

@ -508,4 +508,14 @@ public class DefaultModelValidatorTest
assertTrue( result.getErrors().get( 0 ).contains( "test:b" ) );
}
public void testBadVersion()
throws Exception
{
SimpleProblemCollector result = validate( "bad-version.xml" );
assertViolations( result, 0, 0, 1 );
assertContains( result.getWarnings().get( 0 ), "'version' must not contain any of these characters" );
}
}

View File

@ -0,0 +1,25 @@
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>foo</groupId>
<artifactId>bar</artifactId>
<version>this\is/bad</version>
</project>