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