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.model.profile.activation;
20  
21  import java.util.Properties;
22  
23  import org.apache.maven.api.model.Activation;
24  import org.apache.maven.api.model.ActivationOS;
25  import org.apache.maven.api.model.Profile;
26  import org.junit.jupiter.api.BeforeEach;
27  import org.junit.jupiter.api.Test;
28  
29  /**
30   * Tests {@link OperatingSystemProfileActivator}.
31   *
32   */
33  class OperatingSystemProfileActivatorTest extends AbstractProfileActivatorTest<OperatingSystemProfileActivator> {
34  
35      @Override
36      @BeforeEach
37      void setUp() throws Exception {
38          activator = new OperatingSystemProfileActivator();
39      }
40  
41      private Profile newProfile(ActivationOS.Builder activationBuilder) {
42          Activation a = Activation.newBuilder().os(activationBuilder.build()).build();
43  
44          Profile p = Profile.newBuilder().activation(a).build();
45  
46          return p;
47      }
48  
49      private Properties newProperties(String osName, String osVersion, String osArch) {
50          Properties props = new Properties();
51          props.setProperty("os.name", osName);
52          props.setProperty("os.version", osVersion);
53          props.setProperty("os.arch", osArch);
54          return props;
55      }
56  
57      @Test
58      void testVersionStringComparison() throws Exception {
59          Profile profile = newProfile(ActivationOS.newBuilder().version("6.5.0-1014-aws"));
60  
61          assertActivation(true, profile, newContext(null, newProperties("linux", "6.5.0-1014-aws", "amd64")));
62          assertActivation(true, profile, newContext(null, newProperties("windows", "6.5.0-1014-aws", "aarch64")));
63  
64          assertActivation(false, profile, newContext(null, newProperties("linux", "3.1.0", "amd64")));
65      }
66  
67      @Test
68      void testVersionRegexMatching() throws Exception {
69          Profile profile = newProfile(ActivationOS.newBuilder().version("regex:.*aws"));
70  
71          assertActivation(true, profile, newContext(null, newProperties("linux", "6.5.0-1014-aws", "amd64")));
72          assertActivation(true, profile, newContext(null, newProperties("windows", "6.5.0-1014-aws", "aarch64")));
73  
74          assertActivation(false, profile, newContext(null, newProperties("linux", "3.1.0", "amd64")));
75      }
76  
77      @Test
78      void testName() {
79          Profile profile = newProfile(ActivationOS.newBuilder().name("windows"));
80  
81          assertActivation(false, profile, newContext(null, newProperties("linux", "6.5.0-1014-aws", "amd64")));
82          assertActivation(true, profile, newContext(null, newProperties("windows", "6.5.0-1014-aws", "aarch64")));
83      }
84  
85      @Test
86      void testNegatedName() {
87          Profile profile = newProfile(ActivationOS.newBuilder().name("!windows"));
88  
89          assertActivation(true, profile, newContext(null, newProperties("linux", "6.5.0-1014-aws", "amd64")));
90          assertActivation(false, profile, newContext(null, newProperties("windows", "6.5.0-1014-aws", "aarch64")));
91      }
92  
93      @Test
94      void testArch() {
95          Profile profile = newProfile(ActivationOS.newBuilder().arch("amd64"));
96  
97          assertActivation(true, profile, newContext(null, newProperties("linux", "6.5.0-1014-aws", "amd64")));
98          assertActivation(false, profile, newContext(null, newProperties("windows", "6.5.0-1014-aws", "aarch64")));
99      }
100 
101     @Test
102     void testNegatedArch() {
103         Profile profile = newProfile(ActivationOS.newBuilder().arch("!amd64"));
104 
105         assertActivation(false, profile, newContext(null, newProperties("linux", "6.5.0-1014-aws", "amd64")));
106         assertActivation(true, profile, newContext(null, newProperties("windows", "6.5.0-1014-aws", "aarch64")));
107     }
108 
109     @Test
110     void testFamily() {
111         Profile profile = newProfile(ActivationOS.newBuilder().family("windows"));
112 
113         assertActivation(false, profile, newContext(null, newProperties("linux", "6.5.0-1014-aws", "amd64")));
114         assertActivation(true, profile, newContext(null, newProperties("windows", "6.5.0-1014-aws", "aarch64")));
115     }
116 
117     @Test
118     void testNegatedFamily() {
119         Profile profile = newProfile(ActivationOS.newBuilder().family("!windows"));
120 
121         assertActivation(true, profile, newContext(null, newProperties("linux", "6.5.0-1014-aws", "amd64")));
122         assertActivation(false, profile, newContext(null, newProperties("windows", "6.5.0-1014-aws", "aarch64")));
123     }
124 
125     @Test
126     void testAllOsConditions() {
127         Profile profile = newProfile(ActivationOS.newBuilder()
128                 .family("windows")
129                 .name("windows")
130                 .arch("aarch64")
131                 .version("99"));
132 
133         assertActivation(false, profile, newContext(null, newProperties("linux", "6.5.0-1014-aws", "amd64")));
134         assertActivation(false, profile, newContext(null, newProperties("windows", "1", "aarch64")));
135         assertActivation(false, profile, newContext(null, newProperties("windows", "99", "amd64")));
136         assertActivation(true, profile, newContext(null, newProperties("windows", "99", "aarch64")));
137     }
138 
139     @Test
140     public void testCapitalOsName() {
141         Profile profile = newProfile(ActivationOS.newBuilder()
142                 .family("Mac")
143                 .name("Mac OS X")
144                 .arch("aarch64")
145                 .version("14.5"));
146 
147         assertActivation(false, profile, newContext(null, newProperties("linux", "6.5.0-1014-aws", "amd64")));
148         assertActivation(false, profile, newContext(null, newProperties("windows", "1", "aarch64")));
149         assertActivation(false, profile, newContext(null, newProperties("windows", "99", "amd64")));
150         assertActivation(true, profile, newContext(null, newProperties("Mac OS X", "14.5", "aarch64")));
151     }
152 }