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