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