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.Iterator;
22  
23  import org.apache.maven.enforcer.rule.api.EnforcerLogger;
24  import org.apache.maven.enforcer.rule.api.EnforcerRuleError;
25  import org.apache.maven.model.profile.activation.OperatingSystemProfileActivator;
26  import org.apache.maven.plugin.logging.Log;
27  import org.apache.maven.plugin.logging.SystemStreamLog;
28  import org.codehaus.plexus.util.Os;
29  import org.junit.jupiter.api.Test;
30  import org.mockito.Mockito;
31  
32  import static org.assertj.core.api.Assertions.assertThat;
33  import static org.assertj.core.api.Assertions.assertThatCode;
34  
35  /**
36   * Exhaustively check the OS mojo.
37   *
38   * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
39   */
40  class TestRequireOS {
41  
42      /**
43       * Test os.
44       */
45      @Test
46      public void testOS() {
47          Log log = new SystemStreamLog();
48  
49          RequireOS rule = new RequireOS(new OperatingSystemProfileActivator());
50  
51          Iterator<String> iter = Os.getValidFamilies().iterator();
52          String validFamily;
53          String invalidFamily = null;
54          while (iter.hasNext()) {
55              String fam = iter.next();
56              if (!Os.isFamily(fam)) {
57                  invalidFamily = fam;
58                  break;
59              }
60          }
61  
62          validFamily = Os.OS_FAMILY;
63  
64          log.info("Testing Mojo Using Valid Family: " + validFamily + " Invalid Family: " + invalidFamily);
65  
66          rule.setFamily(validFamily);
67          assertThat(rule.isAllowed()).isTrue();
68  
69          rule.setFamily(invalidFamily);
70          assertThat(rule.isAllowed()).isFalse();
71  
72          rule.setFamily("!" + invalidFamily);
73          assertThat(rule.isAllowed()).isTrue();
74  
75          rule.setFamily(null);
76          rule.setArch(Os.OS_ARCH);
77          assertThat(rule.isAllowed()).isTrue();
78  
79          rule.setArch("somecrazyarch");
80          assertThat(rule.isAllowed()).isFalse();
81  
82          rule.setArch("!somecrazyarch");
83          assertThat(rule.isAllowed()).isTrue();
84  
85          rule.setArch(null);
86  
87          rule.setName(Os.OS_NAME);
88          assertThat(rule.isAllowed()).isTrue();
89  
90          rule.setName("somecrazyname");
91          assertThat(rule.isAllowed()).isFalse();
92  
93          rule.setName("!somecrazyname");
94          assertThat(rule.isAllowed()).isTrue();
95  
96          rule.setName(null);
97  
98          rule.setVersion(Os.OS_VERSION);
99          assertThat(rule.isAllowed()).isTrue();
100 
101         rule.setVersion("somecrazyversion");
102         assertThat(rule.isAllowed()).isFalse();
103 
104         rule.setVersion("!somecrazyversion");
105         assertThat(rule.isAllowed()).isTrue();
106     }
107 
108     @Test
109     void testInvalidFamily() {
110         RequireOS rule = new RequireOS(new OperatingSystemProfileActivator());
111         rule.setLog(Mockito.mock(EnforcerLogger.class));
112 
113         rule.setFamily("junk");
114         assertThatCode(rule::execute)
115                 .isInstanceOf(EnforcerRuleError.class)
116                 .hasMessageStartingWith("Invalid Family type used. Valid family types are: ");
117     }
118 
119     @Test
120     void testId() {
121         RequireOS rule = new RequireOS(new OperatingSystemProfileActivator());
122         rule.setVersion("1.2");
123         assertThat(rule.getCacheId()).isNotEmpty();
124     }
125 }