From 3171417299c66a23f80e6c5de5ab284ddaccfbc8 Mon Sep 17 00:00:00 2001 From: Brett Leslie Porter Date: Sat, 8 Oct 2005 16:48:21 +0000 Subject: [PATCH] PR: MNG-864 Fail build if the model contains an expression that doesn't evaluate git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@307311 13f79535-47bb-0310-9956-ffa450edef68 --- .../ModelInterpolationException.java | 16 +++++++++++----- .../RegexBasedModelInterpolator.java | 17 +++++++++++------ .../RegexBasedModelInterpolatorTest.java | 12 ++++++++++-- 3 files changed, 32 insertions(+), 13 deletions(-) diff --git a/maven-project/src/main/java/org/apache/maven/project/interpolation/ModelInterpolationException.java b/maven-project/src/main/java/org/apache/maven/project/interpolation/ModelInterpolationException.java index 382868ea92..55ae557551 100644 --- a/maven-project/src/main/java/org/apache/maven/project/interpolation/ModelInterpolationException.java +++ b/maven-project/src/main/java/org/apache/maven/project/interpolation/ModelInterpolationException.java @@ -25,13 +25,19 @@ public class ModelInterpolationException extends Exception { private String expression; + private String originalMessage; + public ModelInterpolationException( String message ) + { + super( message ); + } + public ModelInterpolationException( String message, Throwable cause ) { super( message, cause ); } - + public ModelInterpolationException( String expression, String message, Throwable cause ) { super( "The POM expression: " + expression + " could not be evaluated. Reason: " + message, cause ); @@ -39,7 +45,7 @@ public class ModelInterpolationException this.expression = expression; this.originalMessage = message; } - + public ModelInterpolationException( String expression, String message ) { super( "The POM expression: " + expression + " could not be evaluated. Reason: " + message ); @@ -47,15 +53,15 @@ public class ModelInterpolationException this.expression = expression; this.originalMessage = message; } - + public String getExpression() { return expression; } - + public String getOriginalMessage() { return originalMessage; } - + } \ No newline at end of file diff --git a/maven-project/src/main/java/org/apache/maven/project/interpolation/RegexBasedModelInterpolator.java b/maven-project/src/main/java/org/apache/maven/project/interpolation/RegexBasedModelInterpolator.java index 81343c933f..1c1f1153a3 100644 --- a/maven-project/src/main/java/org/apache/maven/project/interpolation/RegexBasedModelInterpolator.java +++ b/maven-project/src/main/java/org/apache/maven/project/interpolation/RegexBasedModelInterpolator.java @@ -28,13 +28,13 @@ import org.codehaus.plexus.util.xml.pull.XmlPullParserException; import java.io.IOException; import java.io.StringReader; import java.io.StringWriter; +import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; -import java.util.Map; /** * Use a regular expression search to find and resolve expressions within the POM. - * + * * @author jdcasey Created on Feb 3, 2005 * @version $Id$ * @todo Consolidate this logic with the PluginParameterExpressionEvaluator, minus deprecations/bans. @@ -46,11 +46,11 @@ public class RegexBasedModelInterpolator private static final Pattern EXPRESSION_PATTERN = Pattern.compile( "\\$\\{(pom\\.|project\\.)?([^}]+)\\}" ); /** - * Serialize the inbound Model instance to a StringWriter, perform the regex replacement to resolve + * Serialize the inbound Model instance to a StringWriter, perform the regex replacement to resolve * POM expressions, then re-parse into the resolved Model instance. *
* NOTE: This will result in a different instance of Model being returned!!! - * + * * @param model The inbound Model instance, to serialize and reference for expression resolution * @param context The other context map to be used during resolution * @return The resolved instance of the inbound Model. This is a different instance! @@ -110,7 +110,7 @@ public class RegexBasedModelInterpolator { value = model.getProperties().getProperty( realExpr ); } - + try { if ( value == null ) @@ -127,7 +127,7 @@ public class RegexBasedModelInterpolator e ); } } - + // if the expression refers to itself, skip it. if ( wholeExpr.equals( value ) ) { @@ -142,6 +142,11 @@ public class RegexBasedModelInterpolator // but this could result in multiple lookups of stringValue, and replaceAll is not correct behaviour matcher.reset( result ); } + else + { + throw new ModelInterpolationException( + "Expression '" + wholeExpr + "' did not evaluate to anything in the model" ); + } } return result; diff --git a/maven-project/src/test/java/org/apache/maven/project/interpolation/RegexBasedModelInterpolatorTest.java b/maven-project/src/test/java/org/apache/maven/project/interpolation/RegexBasedModelInterpolatorTest.java index c13a57879c..62830066d4 100644 --- a/maven-project/src/test/java/org/apache/maven/project/interpolation/RegexBasedModelInterpolatorTest.java +++ b/maven-project/src/test/java/org/apache/maven/project/interpolation/RegexBasedModelInterpolatorTest.java @@ -68,9 +68,17 @@ public class RegexBasedModelInterpolatorTest model.addDependency( dep ); - Model out = new RegexBasedModelInterpolator().interpolate( model, context ); + try + { + Model out = new RegexBasedModelInterpolator().interpolate( model, context ); + fail( "Should have failed to interpolate with invalid reference" ); + } + catch ( ModelInterpolationException expected ) + { + assertTrue( true ); + } - assertEquals( "${something}", ( (Dependency) out.getDependencies().get( 0 ) ).getVersion() ); +// assertEquals( "${something}", ( (Dependency) out.getDependencies().get( 0 ) ).getVersion() ); } public void testTwoReferences()