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.Profile;
25  import org.junit.jupiter.api.BeforeEach;
26  import org.junit.jupiter.api.Test;
27  
28  /**
29   * Tests {@link JdkVersionProfileActivator}.
30   *
31   */
32  class JdkVersionProfileActivatorTest extends AbstractProfileActivatorTest<JdkVersionProfileActivator> {
33  
34      @Override
35      @BeforeEach
36      void setUp() throws Exception {
37          activator = new JdkVersionProfileActivator();
38      }
39  
40      private Profile newProfile(String jdkVersion) {
41          Activation a = Activation.newBuilder().jdk(jdkVersion).build();
42  
43          Profile p = Profile.newBuilder().activation(a).build();
44  
45          return p;
46      }
47  
48      private Properties newProperties(String javaVersion) {
49          Properties props = new Properties();
50          props.setProperty("java.version", javaVersion);
51          return props;
52      }
53  
54      @Test
55      void testNullSafe() throws Exception {
56          Profile p = Profile.newInstance();
57  
58          assertActivation(false, p, newContext(null, null));
59  
60          p = p.withActivation(Activation.newInstance());
61  
62          assertActivation(false, p, newContext(null, null));
63      }
64  
65      @Test
66      void testPrefix() throws Exception {
67          Profile profile = newProfile("1.4");
68  
69          assertActivation(true, profile, newContext(null, newProperties("1.4")));
70          assertActivation(true, profile, newContext(null, newProperties("1.4.2")));
71          assertActivation(true, profile, newContext(null, newProperties("1.4.2_09")));
72          assertActivation(true, profile, newContext(null, newProperties("1.4.2_09-b03")));
73  
74          assertActivation(false, profile, newContext(null, newProperties("1.3")));
75  
76          assertActivation(false, profile, newContext(null, newProperties("1.5")));
77      }
78  
79      @Test
80      void testPrefixNegated() throws Exception {
81          Profile profile = newProfile("!1.4");
82  
83          assertActivation(false, profile, newContext(null, newProperties("1.4")));
84          assertActivation(false, profile, newContext(null, newProperties("1.4.2")));
85          assertActivation(false, profile, newContext(null, newProperties("1.4.2_09")));
86          assertActivation(false, profile, newContext(null, newProperties("1.4.2_09-b03")));
87  
88          assertActivation(true, profile, newContext(null, newProperties("1.3")));
89  
90          assertActivation(true, profile, newContext(null, newProperties("1.5")));
91      }
92  
93      @Test
94      void testVersionRangeInclusiveBounds() throws Exception {
95          Profile profile = newProfile("[1.5,1.6]");
96  
97          assertActivation(false, profile, newContext(null, newProperties("1.4")));
98          assertActivation(false, profile, newContext(null, newProperties("1.4.2")));
99          assertActivation(false, profile, newContext(null, newProperties("1.4.2_09")));
100         assertActivation(false, profile, newContext(null, newProperties("1.4.2_09-b03")));
101 
102         assertActivation(true, profile, newContext(null, newProperties("1.5")));
103         assertActivation(true, profile, newContext(null, newProperties("1.5.0")));
104         assertActivation(true, profile, newContext(null, newProperties("1.5.0_09")));
105         assertActivation(true, profile, newContext(null, newProperties("1.5.0_09-b03")));
106         assertActivation(true, profile, newContext(null, newProperties("1.5.1")));
107 
108         assertActivation(true, profile, newContext(null, newProperties("1.6")));
109         assertActivation(true, profile, newContext(null, newProperties("1.6.0")));
110         assertActivation(true, profile, newContext(null, newProperties("1.6.0_09")));
111         assertActivation(true, profile, newContext(null, newProperties("1.6.0_09-b03")));
112     }
113 
114     @Test
115     void testVersionRangeExclusiveBounds() throws Exception {
116         Profile profile = newProfile("(1.3,1.6)");
117 
118         assertActivation(false, profile, newContext(null, newProperties("1.3")));
119         assertActivation(false, profile, newContext(null, newProperties("1.3.0")));
120         assertActivation(false, profile, newContext(null, newProperties("1.3.0_09")));
121         assertActivation(false, profile, newContext(null, newProperties("1.3.0_09-b03")));
122 
123         assertActivation(true, profile, newContext(null, newProperties("1.3.1")));
124         assertActivation(true, profile, newContext(null, newProperties("1.3.1_09")));
125         assertActivation(true, profile, newContext(null, newProperties("1.3.1_09-b03")));
126 
127         assertActivation(true, profile, newContext(null, newProperties("1.5")));
128         assertActivation(true, profile, newContext(null, newProperties("1.5.0")));
129         assertActivation(true, profile, newContext(null, newProperties("1.5.0_09")));
130         assertActivation(true, profile, newContext(null, newProperties("1.5.0_09-b03")));
131         assertActivation(true, profile, newContext(null, newProperties("1.5.1")));
132 
133         assertActivation(false, profile, newContext(null, newProperties("1.6")));
134     }
135 
136     @Test
137     void testVersionRangeInclusiveLowerBound() throws Exception {
138         Profile profile = newProfile("[1.5,)");
139 
140         assertActivation(false, profile, newContext(null, newProperties("1.4")));
141         assertActivation(false, profile, newContext(null, newProperties("1.4.2")));
142         assertActivation(false, profile, newContext(null, newProperties("1.4.2_09")));
143         assertActivation(false, profile, newContext(null, newProperties("1.4.2_09-b03")));
144 
145         assertActivation(true, profile, newContext(null, newProperties("1.5")));
146         assertActivation(true, profile, newContext(null, newProperties("1.5.0")));
147         assertActivation(true, profile, newContext(null, newProperties("1.5.0_09")));
148         assertActivation(true, profile, newContext(null, newProperties("1.5.0_09-b03")));
149         assertActivation(true, profile, newContext(null, newProperties("1.5.1")));
150 
151         assertActivation(true, profile, newContext(null, newProperties("1.6")));
152         assertActivation(true, profile, newContext(null, newProperties("1.6.0")));
153         assertActivation(true, profile, newContext(null, newProperties("1.6.0_09")));
154         assertActivation(true, profile, newContext(null, newProperties("1.6.0_09-b03")));
155     }
156 
157     @Test
158     void testVersionRangeExclusiveUpperBound() throws Exception {
159         Profile profile = newProfile("(,1.6)");
160 
161         assertActivation(true, profile, newContext(null, newProperties("1.5")));
162         assertActivation(true, profile, newContext(null, newProperties("1.5.0")));
163         assertActivation(true, profile, newContext(null, newProperties("1.5.0_09")));
164         assertActivation(true, profile, newContext(null, newProperties("1.5.0_09-b03")));
165         assertActivation(true, profile, newContext(null, newProperties("1.5.1")));
166 
167         assertActivation(false, profile, newContext(null, newProperties("1.6")));
168         assertActivation(false, profile, newContext(null, newProperties("1.6.0")));
169         assertActivation(false, profile, newContext(null, newProperties("1.6.0_09")));
170         assertActivation(false, profile, newContext(null, newProperties("1.6.0_09-b03")));
171     }
172 }