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