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.assertj.core.api.Assertions;
23  import org.junit.jupiter.api.Test;
24  
25  import static org.junit.jupiter.api.Assertions.fail;
26  
27  /**
28   * Unit test for {@link RequireEnvironmentVariable}}
29   *
30   * @author <a href='mailto:marvin[at]marvinformatics[dot]com'>Marvin Froeder</a>
31   */
32  class TestRequireEnvironmentVariable {
33  
34      private RequireEnvironmentVariable rule = new RequireEnvironmentVariable();
35      /**
36       * Test rule.
37       *
38       */
39      @Test
40      void testRule() {
41          // this env variable should not be set
42          rule.setVariableName("JUNK");
43  
44          try {
45              rule.execute();
46              fail("Expected an exception.");
47          } catch (EnforcerRuleException e) {
48              // expected to catch this.
49          }
50  
51          // PATH shall be common to windows and linux
52          rule.setVariableName("PATH");
53          try {
54              rule.execute();
55          } catch (EnforcerRuleException e) {
56              fail("This should not throw an exception");
57          }
58      }
59  
60      /**
61       * Test rule with regex.
62       *
63       */
64      @Test
65      void testRuleWithRegex() {
66  
67          rule.setVariableName("PATH");
68          // This expression should not match the property
69          // value
70          rule.setRegex("[^abc]");
71  
72          try {
73              rule.execute();
74              fail("Expected an exception.");
75          } catch (EnforcerRuleException e) {
76              // expected to catch this.
77          }
78  
79          // can't really predict what a PATH will looks like, just enforce it ain't empty
80          rule.setRegex(".{1,}");
81          try {
82              rule.execute();
83          } catch (EnforcerRuleException e) {
84              fail("This should not throw an exception");
85          }
86      }
87  
88      @Test
89      void ruleShouldBeCached() {
90          rule.setVariableName("TEST");
91          Assertions.assertThat(rule.getCacheId()).isNotEmpty();
92      }
93  }