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.property;
20
21 import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
22 import org.apache.maven.enforcer.rules.utils.ExpressionEvaluator;
23 import org.junit.jupiter.api.Test;
24 import org.junit.jupiter.api.extension.ExtendWith;
25 import org.mockito.InjectMocks;
26 import org.mockito.Mock;
27 import org.mockito.junit.jupiter.MockitoExtension;
28
29 import static org.assertj.core.api.Assertions.assertThat;
30 import static org.junit.jupiter.api.Assertions.fail;
31 import static org.mockito.Mockito.when;
32
33
34
35
36
37
38 @ExtendWith(MockitoExtension.class)
39 class TestRequireProperty {
40
41 @Mock
42 private ExpressionEvaluator evaluator;
43
44 @InjectMocks
45 private RequireProperty rule;
46
47
48
49
50
51 @Test
52 void testRule() throws Exception {
53
54
55 rule.setProperty("testPropJunk");
56
57 try {
58 rule.execute();
59 fail("Expected an exception.");
60 } catch (EnforcerRuleException e) {
61
62 }
63
64 when(evaluator.evaluate("${testProp}")).thenReturn("This is a test.");
65
66
67
68 rule.setProperty("testProp");
69 try {
70 rule.execute();
71 } catch (EnforcerRuleException e) {
72 fail("This should not throw an exception");
73 }
74 }
75
76
77
78
79
80 @Test
81 void testRuleWithRegex() throws Exception {
82
83 when(evaluator.evaluate("${testProp}")).thenReturn("This is a test.");
84
85 rule.setProperty("testProp");
86
87
88 rule.setRegex("[^abc]");
89
90 try {
91 rule.execute();
92 fail("Expected an exception.");
93 } catch (EnforcerRuleException e) {
94
95 }
96
97
98 rule.setRegex("[This].*[.]");
99 try {
100 rule.execute();
101 } catch (EnforcerRuleException e) {
102 fail("This should not throw an exception");
103 }
104 }
105
106
107
108
109 @Test
110 void ruleShouldNotBeCached() {
111 assertThat(rule.getCacheId()).isNull();
112 }
113 }