1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
42
43
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
59 when(evaluator.evaluate(anyString())).thenAnswer(i -> i.getArgument(0));
60 }
61
62
63
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 }