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 java.util.Arrays;
22  import java.util.Collections;
23  
24  import org.apache.commons.lang3.SystemUtils;
25  import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
26  import org.junit.jupiter.api.BeforeEach;
27  import org.junit.jupiter.api.Test;
28  
29  import static org.assertj.core.api.Assertions.assertThatCode;
30  import static org.assertj.core.api.Assertions.assertThatThrownBy;
31  
32  /**
33   * The Class TestRequireJavaVendor.
34   *
35   * @author Tim Sijstermans
36   */
37  class TestRequireJavaVendor {
38      private static final String NON_MATCHING_VENDOR = "non-matching-vendor";
39  
40      private RequireJavaVendor underTest;
41  
42      @BeforeEach
43      public void prepareTest() {
44          underTest = new RequireJavaVendor();
45      }
46  
47      @Test
48      void matchingInclude() throws EnforcerRuleException {
49          // Set the required vendor to the current system vendor
50          underTest.setIncludes(Collections.singletonList(SystemUtils.JAVA_VENDOR));
51  
52          assertThatCode(() -> underTest.execute()).doesNotThrowAnyException();
53      }
54  
55      @Test
56      void nonMatchingInclude() {
57          // Set the included vendor to something irrelevant
58          underTest.setIncludes(Collections.singletonList(NON_MATCHING_VENDOR));
59  
60          assertThatThrownBy(() -> underTest.execute())
61                  .isInstanceOf(EnforcerRuleException.class)
62                  .hasMessage(
63                          "%s is not an included Required Java Vendor (JAVA_HOME=%s)",
64                          SystemUtils.JAVA_VENDOR, SystemUtils.JAVA_HOME);
65      }
66  
67      @Test
68      void matchingExclude() {
69          // Set the excluded vendor to current vendor name
70          underTest.setExcludes(Collections.singletonList(SystemUtils.JAVA_VENDOR));
71  
72          assertThatThrownBy(() -> underTest.execute())
73                  .isInstanceOf(EnforcerRuleException.class)
74                  .hasMessage(
75                          "%s is an excluded Required Java Vendor (JAVA_HOME=%s)",
76                          SystemUtils.JAVA_VENDOR, SystemUtils.JAVA_HOME);
77      }
78  
79      @Test
80      void nonMatchingExclude() throws EnforcerRuleException {
81          // Set the excluded vendor to something nonsensical
82          underTest.setExcludes(Collections.singletonList(NON_MATCHING_VENDOR));
83  
84          assertThatCode(() -> underTest.execute()).doesNotThrowAnyException();
85      }
86  
87      @Test
88      void matchingIncludeAndMatchingExclude() {
89          underTest.setExcludes(Collections.singletonList(SystemUtils.JAVA_VENDOR));
90          underTest.setIncludes(Collections.singletonList(SystemUtils.JAVA_VENDOR));
91  
92          assertThatThrownBy(() -> underTest.execute())
93                  .isInstanceOf(EnforcerRuleException.class)
94                  .hasMessage(
95                          "%s is an excluded Required Java Vendor (JAVA_HOME=%s)",
96                          SystemUtils.JAVA_VENDOR, SystemUtils.JAVA_HOME);
97      }
98  
99      @Test
100     void matchAnyExclude() {
101         // Set a bunch of excluded vendors
102         underTest.setExcludes(Arrays.asList(SystemUtils.JAVA_VENDOR, SystemUtils.JAVA_VENDOR + "modified"));
103 
104         assertThatThrownBy(() -> underTest.execute())
105                 .isInstanceOf(EnforcerRuleException.class)
106                 .hasMessage(
107                         "%s is an excluded Required Java Vendor (JAVA_HOME=%s)",
108                         SystemUtils.JAVA_VENDOR, SystemUtils.JAVA_HOME);
109     }
110 
111     @Test
112     void matchAnyInclude() throws EnforcerRuleException {
113         // Set a bunch of included vendors
114         underTest.setIncludes(Arrays.asList(SystemUtils.JAVA_VENDOR, SystemUtils.JAVA_VENDOR + "modified"));
115 
116         assertThatCode(() -> underTest.execute()).doesNotThrowAnyException();
117     }
118 
119     @Test
120     void defaultRule() throws EnforcerRuleException {
121 
122         assertThatCode(() -> underTest.execute()).doesNotThrowAnyException();
123     }
124 }