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.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   * The Class TestRequireProperty.
35   *
36   * @author Paul Gier
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       * Test rule.
49       *
50       */
51      @Test
52      void testRule() throws Exception {
53  
54          // this property should not be set
55          rule.setProperty("testPropJunk");
56  
57          try {
58              rule.execute();
59              fail("Expected an exception.");
60          } catch (EnforcerRuleException e) {
61              // expected to catch this.
62          }
63  
64          when(evaluator.evaluate("${testProp}")).thenReturn("This is a test.");
65  
66          // this property should be set by the surefire
67          // plugin
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       * Test rule with regex.
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          // This expression should not match the property
87          // value
88          rule.setRegex("[^abc]");
89  
90          try {
91              rule.execute();
92              fail("Expected an exception.");
93          } catch (EnforcerRuleException e) {
94              // expected to catch this.
95          }
96  
97          // this expr should match the property
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      * Test id.
108      */
109     @Test
110     void ruleShouldNotBeCached() {
111         assertThat(rule.getCacheId()).isNull();
112     }
113 }