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.Profile;
25  import org.apache.maven.model.building.SimpleProblemCollector;
26  import org.apache.maven.model.profile.ProfileActivationContext;
27  
28  /**
29   * Tests {@link JdkVersionProfileActivator}.
30   *
31   * @author Benjamin Bentmann
32   */
33  public class JdkVersionProfileActivatorTest extends AbstractProfileActivatorTest<JdkVersionProfileActivator> {
34  
35      public JdkVersionProfileActivatorTest() {
36          super(JdkVersionProfileActivator.class);
37      }
38  
39      private Profile newProfile(String jdkVersion) {
40          Activation a = new Activation();
41          a.setJdk(jdkVersion);
42  
43          Profile p = new Profile();
44          p.setActivation(a);
45  
46          return p;
47      }
48  
49      private Properties newProperties(String javaVersion) {
50          Properties props = new Properties();
51          props.setProperty("java.version", javaVersion);
52          return props;
53      }
54  
55      public void testNullSafe() throws Exception {
56          Profile p = new Profile();
57  
58          assertActivation(false, p, newContext(null, null));
59  
60          p.setActivation(new Activation());
61  
62          assertActivation(false, p, newContext(null, null));
63      }
64  
65      public void testPrefix() throws Exception {
66          Profile profile = newProfile("1.4");
67  
68          assertActivation(true, profile, newContext(null, newProperties("1.4")));
69          assertActivation(true, profile, newContext(null, newProperties("1.4.2")));
70          assertActivation(true, profile, newContext(null, newProperties("1.4.2_09")));
71          assertActivation(true, profile, newContext(null, newProperties("1.4.2_09-b03")));
72  
73          assertActivation(false, profile, newContext(null, newProperties("1.3")));
74  
75          assertActivation(false, profile, newContext(null, newProperties("1.5")));
76      }
77  
78      public void testPrefixNegated() throws Exception {
79          Profile profile = newProfile("!1.4");
80  
81          assertActivation(false, profile, newContext(null, newProperties("1.4")));
82          assertActivation(false, profile, newContext(null, newProperties("1.4.2")));
83          assertActivation(false, profile, newContext(null, newProperties("1.4.2_09")));
84          assertActivation(false, profile, newContext(null, newProperties("1.4.2_09-b03")));
85  
86          assertActivation(true, profile, newContext(null, newProperties("1.3")));
87  
88          assertActivation(true, profile, newContext(null, newProperties("1.5")));
89      }
90  
91      public void testVersionRangeInclusiveBounds() throws Exception {
92          Profile profile = newProfile("[1.5,1.6]");
93  
94          assertActivation(false, profile, newContext(null, newProperties("1.4")));
95          assertActivation(false, profile, newContext(null, newProperties("1.4.2")));
96          assertActivation(false, profile, newContext(null, newProperties("1.4.2_09")));
97          assertActivation(false, profile, newContext(null, newProperties("1.4.2_09-b03")));
98  
99          assertActivation(true, profile, newContext(null, newProperties("1.5")));
100         assertActivation(true, profile, newContext(null, newProperties("1.5.0")));
101         assertActivation(true, profile, newContext(null, newProperties("1.5.0_09")));
102         assertActivation(true, profile, newContext(null, newProperties("1.5.0_09-b03")));
103         assertActivation(true, profile, newContext(null, newProperties("1.5.1")));
104 
105         assertActivation(true, profile, newContext(null, newProperties("1.6")));
106         assertActivation(true, profile, newContext(null, newProperties("1.6.0")));
107         assertActivation(true, profile, newContext(null, newProperties("1.6.0_09")));
108         assertActivation(true, profile, newContext(null, newProperties("1.6.0_09-b03")));
109     }
110 
111     public void testVersionRangeExclusiveBounds() throws Exception {
112         Profile profile = newProfile("(1.3,1.6)");
113 
114         assertActivation(false, profile, newContext(null, newProperties("1.3")));
115         assertActivation(false, profile, newContext(null, newProperties("1.3.0")));
116         assertActivation(false, profile, newContext(null, newProperties("1.3.0_09")));
117         assertActivation(false, profile, newContext(null, newProperties("1.3.0_09-b03")));
118 
119         assertActivation(true, profile, newContext(null, newProperties("1.3.1")));
120         assertActivation(true, profile, newContext(null, newProperties("1.3.1_09")));
121         assertActivation(true, profile, newContext(null, newProperties("1.3.1_09-b03")));
122 
123         assertActivation(true, profile, newContext(null, newProperties("1.5")));
124         assertActivation(true, profile, newContext(null, newProperties("1.5.0")));
125         assertActivation(true, profile, newContext(null, newProperties("1.5.0_09")));
126         assertActivation(true, profile, newContext(null, newProperties("1.5.0_09-b03")));
127         assertActivation(true, profile, newContext(null, newProperties("1.5.1")));
128 
129         assertActivation(false, profile, newContext(null, newProperties("1.6")));
130     }
131 
132     public void testVersionRangeInclusiveLowerBound() throws Exception {
133         Profile profile = newProfile("[1.5,)");
134 
135         assertActivation(false, profile, newContext(null, newProperties("1.4")));
136         assertActivation(false, profile, newContext(null, newProperties("1.4.2")));
137         assertActivation(false, profile, newContext(null, newProperties("1.4.2_09")));
138         assertActivation(false, profile, newContext(null, newProperties("1.4.2_09-b03")));
139 
140         assertActivation(true, profile, newContext(null, newProperties("1.5")));
141         assertActivation(true, profile, newContext(null, newProperties("1.5.0")));
142         assertActivation(true, profile, newContext(null, newProperties("1.5.0_09")));
143         assertActivation(true, profile, newContext(null, newProperties("1.5.0_09-b03")));
144         assertActivation(true, profile, newContext(null, newProperties("1.5.1")));
145 
146         assertActivation(true, profile, newContext(null, newProperties("1.6")));
147         assertActivation(true, profile, newContext(null, newProperties("1.6.0")));
148         assertActivation(true, profile, newContext(null, newProperties("1.6.0_09")));
149         assertActivation(true, profile, newContext(null, newProperties("1.6.0_09-b03")));
150     }
151 
152     public void testVersionRangeExclusiveUpperBound() throws Exception {
153         Profile profile = newProfile("(,1.6)");
154 
155         assertActivation(true, profile, newContext(null, newProperties("1.5")));
156         assertActivation(true, profile, newContext(null, newProperties("1.5.0")));
157         assertActivation(true, profile, newContext(null, newProperties("1.5.0_09")));
158         assertActivation(true, profile, newContext(null, newProperties("1.5.0_09-b03")));
159         assertActivation(true, profile, newContext(null, newProperties("1.5.1")));
160 
161         assertActivation(false, profile, newContext(null, newProperties("1.6")));
162         assertActivation(false, profile, newContext(null, newProperties("1.6.0")));
163         assertActivation(false, profile, newContext(null, newProperties("1.6.0_09")));
164         assertActivation(false, profile, newContext(null, newProperties("1.6.0_09-b03")));
165     }
166 
167     public void testRubbishJavaVersion() {
168         Profile profile = newProfile("[1.8,)");
169 
170         assertActivationWithProblems(profile, newContext(null, newProperties("PÅ«teketeke")), "invalid JDK version");
171         assertActivationWithProblems(profile, newContext(null, newProperties("rubbish")), "invalid JDK version");
172         assertActivationWithProblems(profile, newContext(null, newProperties("1.a.0_09")), "invalid JDK version");
173         assertActivationWithProblems(profile, newContext(null, newProperties("1.a.2.b")), "invalid JDK version");
174     }
175 
176     private void assertActivationWithProblems(
177             Profile profile, ProfileActivationContext context, String warningContains) {
178         SimpleProblemCollector problems = new SimpleProblemCollector();
179 
180         assertEquals(false, activator.isActive(profile, context, problems));
181 
182         assertEquals(problems.getErrors().toString(), 0, problems.getErrors().size());
183         assertEquals(
184                 problems.getWarnings().toString(), 1, problems.getWarnings().size());
185         assertTrue(problems.getWarnings().get(0), problems.getWarnings().get(0).contains(warningContains));
186     }
187 }