maven-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From br...@apache.org
Subject svn commit: r728846 - in /maven/enforcer/trunk: ./ enforcer-rules/ enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/ enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/
Date Tue, 23 Dec 2008 02:14:36 GMT
Author: brett
Date: Mon Dec 22 18:14:35 2008
New Revision: 728846

URL: http://svn.apache.org/viewvc?rev=728846&view=rev
Log:
[MENFORCER-60] throw an error if beanshell is invalid instead of evaluating to false. Improve
tests for beanshell.

Modified:
    maven/enforcer/trunk/enforcer-rules/pom.xml
    maven/enforcer/trunk/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/EvaluateBeanshell.java
    maven/enforcer/trunk/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/EnforcerTestUtils.java
    maven/enforcer/trunk/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/TestEvaluateBeanshell.java
    maven/enforcer/trunk/pom.xml

Modified: maven/enforcer/trunk/enforcer-rules/pom.xml
URL: http://svn.apache.org/viewvc/maven/enforcer/trunk/enforcer-rules/pom.xml?rev=728846&r1=728845&r2=728846&view=diff
==============================================================================
--- maven/enforcer/trunk/enforcer-rules/pom.xml (original)
+++ maven/enforcer/trunk/enforcer-rules/pom.xml Mon Dec 22 18:14:35 2008
@@ -116,5 +116,9 @@
       <groupId>junit</groupId>
       <artifactId>junit</artifactId>
     </dependency>
+    <dependency>
+      <groupId>easymock</groupId>
+      <artifactId>easymock</artifactId>
+    </dependency>
   </dependencies>
 </project>

Modified: maven/enforcer/trunk/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/EvaluateBeanshell.java
URL: http://svn.apache.org/viewvc/maven/enforcer/trunk/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/EvaluateBeanshell.java?rev=728846&r1=728845&r2=728846&view=diff
==============================================================================
--- maven/enforcer/trunk/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/EvaluateBeanshell.java
(original)
+++ maven/enforcer/trunk/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/EvaluateBeanshell.java
Mon Dec 22 18:14:35 2008
@@ -27,7 +27,6 @@
 import bsh.EvalError;
 import bsh.Interpreter;
 
-// TODO: Auto-generated Javadoc
 /**
  * Rule for Maven Enforcer using Beanshell to evaluate a conditional expression.
  * 
@@ -70,7 +69,7 @@
         }
         catch ( ExpressionEvaluationException e )
         {
-            throw new EnforcerRuleException( "Unable to evaluate an expression", e );
+            throw new EnforcerRuleException( "Unable to evaluate an expression '" + condition
+ "'", e );
         }
     }
 
@@ -80,8 +79,10 @@
      * @param script the expression to be evaluated
      * @param log the logger
      * @return boolean the evaluation of the expression
+     * @throws EnforcerRuleException if the script could not be evaluated
      */
     protected boolean evaluateCondition( String script, Log log )
+        throws EnforcerRuleException
     {
         Boolean evaluation = Boolean.FALSE;
         try
@@ -91,7 +92,7 @@
         }
         catch ( EvalError ex )
         {
-            log.warn( "Couldn't evaluate condition: " + script, ex );
+            throw new EnforcerRuleException( "Couldn't evaluate condition: " + script, ex
);
         }
         return evaluation.booleanValue();
     }

Modified: maven/enforcer/trunk/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/EnforcerTestUtils.java
URL: http://svn.apache.org/viewvc/maven/enforcer/trunk/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/EnforcerTestUtils.java?rev=728846&r1=728845&r2=728846&view=diff
==============================================================================
--- maven/enforcer/trunk/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/EnforcerTestUtils.java
(original)
+++ maven/enforcer/trunk/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/EnforcerTestUtils.java
Mon Dec 22 18:14:35 2008
@@ -35,9 +35,8 @@
  * 
  * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
  */
-public class EnforcerTestUtils
+public final class EnforcerTestUtils
 {
-
     /**
      * Gets the maven session.
      * 
@@ -104,6 +103,19 @@
     }
 
     /**
+     * Gets the helper.
+     * 
+     * @param project the project
+     * @param eval the expression evaluator to use
+     * @return the helper
+     */
+    public static EnforcerRuleHelper getHelper( MavenProject project, ExpressionEvaluator
eval )
+    {
+        MavenSession session = getMavenSession();
+        return new DefaultEnforcementRuleHelper( session, eval, new SystemStreamLog(), null
);
+    }
+
+    /**
      * New plugin.
      * 
      * @param groupId the group id

Modified: maven/enforcer/trunk/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/TestEvaluateBeanshell.java
URL: http://svn.apache.org/viewvc/maven/enforcer/trunk/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/TestEvaluateBeanshell.java?rev=728846&r1=728845&r2=728846&view=diff
==============================================================================
--- maven/enforcer/trunk/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/TestEvaluateBeanshell.java
(original)
+++ maven/enforcer/trunk/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/TestEvaluateBeanshell.java
Mon Dec 22 18:14:35 2008
@@ -22,8 +22,10 @@
 
 import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
 import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper;
+import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
+import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
+import org.easymock.MockControl;
 
-// TODO: Auto-generated Javadoc
 /**
  * The Class TestEvaluateBeanshell.
  * 
@@ -32,38 +34,111 @@
 public class TestEvaluateBeanshell
     extends TestCase
 {
+    private MockProject project;
+
+    public void setUp()
+    {
+        project = new MockProject();
+        project.setProperty( "env", "\"This is a test.\"" );
+    }
 
     /**
      * Test rule.
-     * 
-     * @throws EnforcerRuleException the enforcer rule exception
      */
-    public void testRule()
-        throws EnforcerRuleException
+    public void testRulePass()
+        throws EnforcerRuleException, ExpressionEvaluationException
     {
-        MockProject project = new MockProject();
-        project.setProperty( "env", "\"This is a test.\"" );
-        EnforcerRuleHelper helper = EnforcerTestUtils.getHelper( project );
-
         EvaluateBeanshell rule = new EvaluateBeanshell();
         // this property should not be set
         rule.condition = "${env} == \"This is a test.\"";
         rule.message = "We have a variable : ${env}";
 
+        EnforcerRuleHelper helper = EnforcerTestUtils.getHelper( project );
         rule.execute( helper );
+    }
+
+    public void testRuleFail()
+        throws EnforcerRuleException, ExpressionEvaluationException
+    {
+        EvaluateBeanshell rule = new EvaluateBeanshell();
+        // this property should be set by the surefire
+        // plugin
+        rule.condition = "${env} == null";
+        rule.message = "We have a variable : ${env}";
+
+        try
+        {
+            EnforcerRuleHelper helper = EnforcerTestUtils.getHelper( project );
+            rule.execute( helper );
+            fail( "Expected an exception." );
+        }
+        catch ( EnforcerRuleException e )
+        {
+            assertEquals( e.getLocalizedMessage(), rule.message );
+        }
+    }
 
+    public void testRuleFailNoMessage()
+        throws EnforcerRuleException, ExpressionEvaluationException
+    {
+        EvaluateBeanshell rule = new EvaluateBeanshell();
         // this property should be set by the surefire
         // plugin
         rule.condition = "${env} == null";
         try
         {
+            EnforcerRuleHelper helper = EnforcerTestUtils.getHelper( project );
             rule.execute( helper );
             fail( "Expected an exception." );
         }
         catch ( EnforcerRuleException e )
         {
-            System.out.println( "Caught expected exception:" + e.getLocalizedMessage() );
+            assertEquals( e.getLocalizedMessage(), rule.message );
+            assertTrue( e.getLocalizedMessage().length() > 0 );
         }
     }
 
+    public void testRuleInvalidExpression()
+        throws EnforcerRuleException, ExpressionEvaluationException
+    {
+        EvaluateBeanshell rule = new EvaluateBeanshell();
+        rule.condition = "${env} == null";
+        rule.message = "We have a variable : ${env}";
+        MockControl evalControl = MockControl.createControl( ExpressionEvaluator.class );
+        try
+        {
+            ExpressionEvaluator eval = (ExpressionEvaluator) evalControl.getMock();
+            eval.evaluate( rule.condition );
+            evalControl.expectAndDefaultThrow( null, new ExpressionEvaluationException( "expected
error" ) );
+            evalControl.replay();
+
+            EnforcerRuleHelper helper = EnforcerTestUtils.getHelper( project, eval );
+            rule.execute( helper );
+            fail( "Expected an exception." );
+        }
+        catch ( EnforcerRuleException e )
+        {
+            assertFalse( e.getLocalizedMessage().equals( rule.message ) );
+        }
+
+        evalControl.verify();
+    }
+
+    public void testRuleInvalidBeanshell()
+        throws EnforcerRuleException, ExpressionEvaluationException
+    {
+        EvaluateBeanshell rule = new EvaluateBeanshell();
+        rule.condition = "this is not valid beanshell";
+        rule.message = "We have a variable : ${env}";
+        try
+        {
+            EnforcerRuleHelper helper = EnforcerTestUtils.getHelper( project );
+            rule.execute( helper );
+            fail( "Expected an exception." );
+        }
+        catch ( EnforcerRuleException e )
+        {
+            assertFalse( e.getLocalizedMessage().equals( rule.message ) );
+        }
+    }
 }

Modified: maven/enforcer/trunk/pom.xml
URL: http://svn.apache.org/viewvc/maven/enforcer/trunk/pom.xml?rev=728846&r1=728845&r2=728846&view=diff
==============================================================================
--- maven/enforcer/trunk/pom.xml (original)
+++ maven/enforcer/trunk/pom.xml Mon Dec 22 18:14:35 2008
@@ -233,6 +233,12 @@
         <scope>test</scope>
       </dependency>
       <dependency>
+        <groupId>easymock</groupId>
+        <artifactId>easymock</artifactId>
+        <version>1.2_Java1.3</version>
+        <scope>test</scope>
+      </dependency>
+      <dependency>
         <groupId>commons-lang</groupId>
         <artifactId>commons-lang</artifactId>
         <version>2.3</version>



Mime
View raw message