From 260867adc23d4e8622fd872aa27cdd13b9f6bff5 Mon Sep 17 00:00:00 2001 From: Benjamin Bentmann Date: Sat, 17 Jul 2010 21:53:47 +0000 Subject: [PATCH] [MNG-4732] Version string validation git-svn-id: https://svn.apache.org/repos/asf/maven/maven-3/trunk@965141 13f79535-47bb-0310-9956-ffa450edef68 --- .../validation/DefaultModelValidator.java | 26 +++++++++++++++++++ .../validation/DefaultModelValidatorTest.java | 10 +++++++ .../resources/poms/validation/bad-version.xml | 25 ++++++++++++++++++ 3 files changed, 61 insertions(+) create mode 100644 maven-model-builder/src/test/resources/poms/validation/bad-version.xml diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/validation/DefaultModelValidator.java b/maven-model-builder/src/main/java/org/apache/maven/model/validation/DefaultModelValidator.java index 86db9837c1..e30cb9cb08 100644 --- a/maven-model-builder/src/main/java/org/apache/maven/model/validation/DefaultModelValidator.java +++ b/maven-model-builder/src/main/java/org/apache/maven/model/validation/DefaultModelValidator.java @@ -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 ) { diff --git a/maven-model-builder/src/test/java/org/apache/maven/model/validation/DefaultModelValidatorTest.java b/maven-model-builder/src/test/java/org/apache/maven/model/validation/DefaultModelValidatorTest.java index f613cd85ae..e8d9c42807 100644 --- a/maven-model-builder/src/test/java/org/apache/maven/model/validation/DefaultModelValidatorTest.java +++ b/maven-model-builder/src/test/java/org/apache/maven/model/validation/DefaultModelValidatorTest.java @@ -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" ); + } + } diff --git a/maven-model-builder/src/test/resources/poms/validation/bad-version.xml b/maven-model-builder/src/test/resources/poms/validation/bad-version.xml new file mode 100644 index 0000000000..bf6a1fc81f --- /dev/null +++ b/maven-model-builder/src/test/resources/poms/validation/bad-version.xml @@ -0,0 +1,25 @@ + + + + 4.0.0 + foo + bar + this\is/bad +