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