View Javadoc

1   package org.apache.maven.plugins.enforcer;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import static org.mockito.Mockito.*;
23  
24  import junit.framework.TestCase;
25  
26  import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
27  import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper;
28  import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
29  import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
30  
31  /**
32   * The Class TestEvaluateBeanshell.
33   *
34   * @author hugonnem
35   */
36  public class TestEvaluateBeanshell
37      extends TestCase
38  {
39      private MockProject project;
40  
41      public void setUp()
42      {
43          project = new MockProject();
44          project.setProperty( "env", "\"This is a test.\"" );
45      }
46  
47      /**
48       * Test rule.
49       */
50      public void testRulePass()
51          throws EnforcerRuleException, ExpressionEvaluationException
52      {
53          EvaluateBeanshell rule = new EvaluateBeanshell();
54          // this property should not be set
55          rule.condition = "${env} == \"This is a test.\"";
56          rule.message = "We have a variable : ${env}";
57  
58          EnforcerRuleHelper helper = EnforcerTestUtils.getHelper( project );
59          rule.execute( helper );
60      }
61  
62      public void testRuleFail()
63          throws EnforcerRuleException, ExpressionEvaluationException
64      {
65          EvaluateBeanshell rule = new EvaluateBeanshell();
66          // this property should be set by the surefire
67          // plugin
68          rule.condition = "${env} == null";
69          rule.message = "We have a variable : ${env}";
70  
71          try
72          {
73              EnforcerRuleHelper helper = EnforcerTestUtils.getHelper( project );
74              rule.execute( helper );
75              fail( "Expected an exception." );
76          }
77          catch ( EnforcerRuleException e )
78          {
79              assertEquals( e.getLocalizedMessage(), rule.message );
80          }
81      }
82  
83      public void testRuleFailNoMessage()
84          throws EnforcerRuleException, ExpressionEvaluationException
85      {
86          EvaluateBeanshell rule = new EvaluateBeanshell();
87          // this property should be set by the surefire
88          // plugin
89          rule.condition = "${env} == null";
90          try
91          {
92              EnforcerRuleHelper helper = EnforcerTestUtils.getHelper( project );
93              rule.execute( helper );
94              fail( "Expected an exception." );
95          }
96          catch ( EnforcerRuleException e )
97          {
98              assertEquals( e.getLocalizedMessage(), rule.message );
99              assertTrue( e.getLocalizedMessage().length() > 0 );
100         }
101     }
102 
103     public void testRuleInvalidExpression()
104         throws EnforcerRuleException, ExpressionEvaluationException
105     {
106         EvaluateBeanshell rule = new EvaluateBeanshell();
107         rule.condition = "${env} == null";
108         rule.message = "We have a variable : ${env}";
109 
110         ExpressionEvaluator eval = mock( ExpressionEvaluator.class );
111         when( eval.evaluate( rule.condition ) ).thenThrow( new ExpressionEvaluationException( "expected error" ) );
112         try
113         {
114             EnforcerRuleHelper helper = EnforcerTestUtils.getHelper( project, eval );
115             rule.execute( helper );
116             fail( "Expected an exception." );
117         }
118         catch ( EnforcerRuleException e )
119         {
120             assertFalse( e.getLocalizedMessage().equals( rule.message ) );
121         }
122         verify( eval );
123     }
124 
125     public void testRuleInvalidBeanshell()
126         throws EnforcerRuleException, ExpressionEvaluationException
127     {
128         EvaluateBeanshell rule = new EvaluateBeanshell();
129         rule.condition = "this is not valid beanshell";
130         rule.message = "We have a variable : ${env}";
131         try
132         {
133             EnforcerRuleHelper helper = EnforcerTestUtils.getHelper( project );
134             rule.execute( helper );
135             fail( "Expected an exception." );
136         }
137         catch ( EnforcerRuleException e )
138         {
139             assertFalse( e.getLocalizedMessage().equals( rule.message ) );
140         }
141     }
142 }