View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.enforcer.rules;
20  
21  import org.apache.maven.enforcer.rule.api.EnforcerLogger;
22  import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
23  import org.apache.maven.enforcer.rules.utils.ExpressionEvaluator;
24  import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
25  import org.junit.jupiter.api.BeforeEach;
26  import org.junit.jupiter.api.Test;
27  import org.junit.jupiter.api.extension.ExtendWith;
28  import org.mockito.InjectMocks;
29  import org.mockito.Mock;
30  import org.mockito.Mockito;
31  import org.mockito.junit.jupiter.MockitoExtension;
32  
33  import static org.junit.jupiter.api.Assertions.assertEquals;
34  import static org.junit.jupiter.api.Assertions.assertNotEquals;
35  import static org.junit.jupiter.api.Assertions.assertTrue;
36  import static org.junit.jupiter.api.Assertions.fail;
37  import static org.mockito.ArgumentMatchers.anyString;
38  import static org.mockito.Mockito.when;
39  
40  /**
41   * The Class TestEvaluateBeanshell.
42   *
43   * @author hugonnem
44   */
45  @ExtendWith(MockitoExtension.class)
46  class TestEvaluateBeanshell {
47  
48      @Mock
49      private ExpressionEvaluator evaluator;
50  
51      @InjectMocks
52      private EvaluateBeanshell rule;
53  
54      @BeforeEach
55      public void setUp() throws Exception {
56          rule.setLog(Mockito.mock(EnforcerLogger.class));
57  
58          // we need not testing ExpressionEvaluator
59          when(evaluator.evaluate(anyString())).thenAnswer(i -> i.getArgument(0));
60      }
61  
62      /**
63       * Test rule.
64       */
65      @Test
66      void testRulePass() throws Exception {
67  
68          rule.setCondition("\"This is a test.\" == \"This is a test.\"");
69          rule.execute();
70      }
71  
72      @Test
73      void testRuleFail() {
74          rule.setCondition("\"Test\" == null");
75          rule.setMessage("We have a variable : ${env}");
76  
77          try {
78              rule.execute();
79              fail("Expected an exception.");
80          } catch (EnforcerRuleException e) {
81              assertEquals(e.getMessage(), rule.getMessage());
82          }
83      }
84  
85      @Test
86      void testRuleFailNoMessage() {
87          rule.setCondition("\"Test\" == null");
88          try {
89              rule.execute();
90              fail("Expected an exception.");
91          } catch (EnforcerRuleException e) {
92              assertEquals("The expression \"\"Test\" == null\" is not true.", e.getLocalizedMessage());
93              assertTrue(e.getLocalizedMessage().length() > 0);
94          }
95      }
96  
97      @Test
98      void testRuleInvalidExpression() throws Exception {
99          rule.setCondition("${env} == null");
100         rule.setMessage("We have a variable : ${env}");
101 
102         when(evaluator.evaluate(rule.getCondition())).thenThrow(new ExpressionEvaluationException("expected error"));
103         try {
104             rule.execute();
105             fail("Expected an exception.");
106         } catch (EnforcerRuleException e) {
107             assertNotEquals(e.getLocalizedMessage(), rule.getMessage());
108         }
109     }
110 
111     @Test
112     void testRuleInvalidBeanshell() {
113         rule.setCondition("this is not valid beanshell");
114         rule.setMessage("We have a variable : ${env}");
115         try {
116             rule.execute();
117             fail("Expected an exception.");
118         } catch (EnforcerRuleException e) {
119             assertNotEquals(e.getLocalizedMessage(), rule.getMessage());
120         }
121     }
122 }