View Javadoc
1   package org.apache.maven.model.profile.activation;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.Properties;
23  
24  import org.apache.maven.api.model.Activation;
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 JdkVersionProfileActivator}.
31   *
32   * @author Benjamin Bentmann
33   */
34  public class JdkVersionProfileActivatorTest
35      extends AbstractProfileActivatorTest<JdkVersionProfileActivator>
36  {
37  
38      @Override
39      @BeforeEach
40      void setUp() throws Exception
41      {
42          activator = new JdkVersionProfileActivator();
43      }
44  
45      private Profile newProfile(String jdkVersion )
46      {
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      {
56          Properties props = new Properties();
57          props.setProperty( "java.version", javaVersion );
58          return props;
59      }
60  
61      @Test
62      public void testNullSafe()
63          throws Exception
64      {
65          Profile p = Profile.newInstance();
66  
67          assertActivation( false, p, newContext( null, null ) );
68  
69          p = p.withActivation( Activation.newInstance() );
70  
71          assertActivation( false, p, newContext( null, null ) );
72      }
73  
74      @Test
75      public void testPrefix()
76          throws Exception
77      {
78          Profile profile = newProfile( "1.4" );
79  
80          assertActivation( true, profile, newContext( null, newProperties( "1.4" ) ) );
81          assertActivation( true, profile, newContext( null, newProperties( "1.4.2" ) ) );
82          assertActivation( true, profile, newContext( null, newProperties( "1.4.2_09" ) ) );
83          assertActivation( true, profile, newContext( null, newProperties( "1.4.2_09-b03" ) ) );
84  
85          assertActivation( false, profile, newContext( null, newProperties( "1.3" ) ) );
86  
87          assertActivation( false, profile, newContext( null, newProperties( "1.5" ) ) );
88      }
89  
90      @Test
91      public void testPrefixNegated()
92          throws Exception
93      {
94          Profile profile = newProfile( "!1.4" );
95  
96          assertActivation( false, profile, newContext( null, newProperties( "1.4" ) ) );
97          assertActivation( false, profile, newContext( null, newProperties( "1.4.2" ) ) );
98          assertActivation( false, profile, newContext( null, newProperties( "1.4.2_09" ) ) );
99          assertActivation( false, profile, newContext( null, newProperties( "1.4.2_09-b03" ) ) );
100 
101         assertActivation( true, profile, newContext( null, newProperties( "1.3" ) ) );
102 
103         assertActivation( true, profile, newContext( null, newProperties( "1.5" ) ) );
104     }
105 
106     @Test
107     public void testVersionRangeInclusiveBounds()
108         throws Exception
109     {
110         Profile profile = newProfile( "[1.5,1.6]" );
111 
112         assertActivation( false, profile, newContext( null, newProperties( "1.4" ) ) );
113         assertActivation( false, profile, newContext( null, newProperties( "1.4.2" ) ) );
114         assertActivation( false, profile, newContext( null, newProperties( "1.4.2_09" ) ) );
115         assertActivation( false, profile, newContext( null, newProperties( "1.4.2_09-b03" ) ) );
116 
117         assertActivation( true, profile, newContext( null, newProperties( "1.5" ) ) );
118         assertActivation( true, profile, newContext( null, newProperties( "1.5.0" ) ) );
119         assertActivation( true, profile, newContext( null, newProperties( "1.5.0_09" ) ) );
120         assertActivation( true, profile, newContext( null, newProperties( "1.5.0_09-b03" ) ) );
121         assertActivation( true, profile, newContext( null, newProperties( "1.5.1" ) ) );
122 
123         assertActivation( true, profile, newContext( null, newProperties( "1.6" ) ) );
124         assertActivation( true, profile, newContext( null, newProperties( "1.6.0" ) ) );
125         assertActivation( true, profile, newContext( null, newProperties( "1.6.0_09" ) ) );
126         assertActivation( true, profile, newContext( null, newProperties( "1.6.0_09-b03" ) ) );
127     }
128 
129     @Test
130     public void testVersionRangeExclusiveBounds()
131         throws Exception
132     {
133         Profile profile = newProfile( "(1.3,1.6)" );
134 
135         assertActivation( false, profile, newContext( null, newProperties( "1.3" ) ) );
136         assertActivation( false, profile, newContext( null, newProperties( "1.3.0" ) ) );
137         assertActivation( false, profile, newContext( null, newProperties( "1.3.0_09" ) ) );
138         assertActivation( false, profile, newContext( null, newProperties( "1.3.0_09-b03" ) ) );
139 
140         assertActivation( true, profile, newContext( null, newProperties( "1.3.1" ) ) );
141         assertActivation( true, profile, newContext( null, newProperties( "1.3.1_09" ) ) );
142         assertActivation( true, profile, newContext( null, newProperties( "1.3.1_09-b03" ) ) );
143 
144         assertActivation( true, profile, newContext( null, newProperties( "1.5" ) ) );
145         assertActivation( true, profile, newContext( null, newProperties( "1.5.0" ) ) );
146         assertActivation( true, profile, newContext( null, newProperties( "1.5.0_09" ) ) );
147         assertActivation( true, profile, newContext( null, newProperties( "1.5.0_09-b03" ) ) );
148         assertActivation( true, profile, newContext( null, newProperties( "1.5.1" ) ) );
149 
150         assertActivation( false, profile, newContext( null, newProperties( "1.6" ) ) );
151     }
152 
153     @Test
154     public void testVersionRangeInclusiveLowerBound()
155         throws Exception
156     {
157         Profile profile = newProfile( "[1.5,)" );
158 
159         assertActivation( false, profile, newContext( null, newProperties( "1.4" ) ) );
160         assertActivation( false, profile, newContext( null, newProperties( "1.4.2" ) ) );
161         assertActivation( false, profile, newContext( null, newProperties( "1.4.2_09" ) ) );
162         assertActivation( false, profile, newContext( null, newProperties( "1.4.2_09-b03" ) ) );
163 
164         assertActivation( true, profile, newContext( null, newProperties( "1.5" ) ) );
165         assertActivation( true, profile, newContext( null, newProperties( "1.5.0" ) ) );
166         assertActivation( true, profile, newContext( null, newProperties( "1.5.0_09" ) ) );
167         assertActivation( true, profile, newContext( null, newProperties( "1.5.0_09-b03" ) ) );
168         assertActivation( true, profile, newContext( null, newProperties( "1.5.1" ) ) );
169 
170         assertActivation( true, profile, newContext( null, newProperties( "1.6" ) ) );
171         assertActivation( true, profile, newContext( null, newProperties( "1.6.0" ) ) );
172         assertActivation( true, profile, newContext( null, newProperties( "1.6.0_09" ) ) );
173         assertActivation( true, profile, newContext( null, newProperties( "1.6.0_09-b03" ) ) );
174     }
175 
176     @Test
177     public void testVersionRangeExclusiveUpperBound()
178         throws Exception
179     {
180         Profile profile = newProfile( "(,1.6)" );
181 
182         assertActivation( true, profile, newContext( null, newProperties( "1.5" ) ) );
183         assertActivation( true, profile, newContext( null, newProperties( "1.5.0" ) ) );
184         assertActivation( true, profile, newContext( null, newProperties( "1.5.0_09" ) ) );
185         assertActivation( true, profile, newContext( null, newProperties( "1.5.0_09-b03" ) ) );
186         assertActivation( true, profile, newContext( null, newProperties( "1.5.1" ) ) );
187 
188         assertActivation( false, profile, newContext( null, newProperties( "1.6" ) ) );
189         assertActivation( false, profile, newContext( null, newProperties( "1.6.0" ) ) );
190         assertActivation( false, profile, newContext( null, newProperties( "1.6.0_09" ) ) );
191         assertActivation( false, profile, newContext( null, newProperties( "1.6.0_09-b03" ) ) );
192     }
193 
194 }