1   package org.apache.maven.profiles.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 org.apache.maven.model.Activation;
23  import org.apache.maven.model.Profile;
24  
25  import junit.framework.TestCase;
26  
27  public class JdkPrefixProfileActivatorTest
28      extends TestCase
29  {
30      public void testDetectionWithoutActivationElement()
31      {
32          JdkPrefixProfileActivator activator = createActivator();
33  
34          Profile profile = new Profile();
35  
36          assertFalse( activator.canDetectActivation( profile ) );
37      }
38  
39      public void testDetectionWithoutJdkElement()
40      {
41          JdkPrefixProfileActivator activator = createActivator();
42  
43          Profile profile = createProfile( null );
44  
45          assertFalse( activator.canDetectActivation( profile ) );
46      }
47  
48      public void testDetectionWithJdkElement()
49      {
50          JdkPrefixProfileActivator activator = createActivator();
51  
52          Profile profile = createProfile( "1.5" );
53  
54          assertTrue( activator.canDetectActivation( profile ) );
55      }
56  
57      public void testActivationWithMatchingJdkVersion()
58          throws ProfileActivationException
59      {
60          JdkPrefixProfileActivator activator = createActivator( "1.5.0_15" );
61  
62          Profile profile = createProfile( "1.5" );
63  
64          assertTrue( activator.isActive( profile ) );
65      }
66  
67      public void testActivationWithDifferentJdkVersion()
68          throws ProfileActivationException
69      {
70          JdkPrefixProfileActivator activator = createActivator( "1.5.0_15" );
71  
72          Profile profile = createProfile( "1.4" );
73  
74          assertFalse( activator.isActive( profile ) );
75      }
76  
77      public void testActivationWithMatchingNegatedJdkVersion()
78          throws ProfileActivationException
79      {
80          JdkPrefixProfileActivator activator = createActivator( "1.5.0_15" );
81  
82          Profile profile = createProfile( "!1.4" );
83  
84          assertTrue( activator.isActive( profile ) );
85      }
86  
87      public void testActivationWithDifferentNegatedJdkVersion()
88          throws ProfileActivationException
89      {
90          JdkPrefixProfileActivator activator = createActivator( "1.5.0_15" );
91  
92          Profile profile = createProfile( "!1.5" );
93  
94          assertFalse( activator.isActive( profile ) );
95      }
96  
97      public void testActivationWithMatchingRangeJdkVersion()
98          throws ProfileActivationException
99      {
100         JdkPrefixProfileActivator activator = createActivator( "1.5.0_15" );
101 
102         Profile profile = createProfile( "[1.4,1.6)" );
103 
104         assertTrue( activator.isActive( profile ) );
105     }
106 
107     public void testActivationWithDifferentRangeJdkVersion()
108         throws ProfileActivationException
109     {
110         JdkPrefixProfileActivator activator = createActivator( "1.5.0_15" );
111 
112         Profile profile = createProfile( "[1.4,1.5]" );
113 
114         assertFalse( activator.isActive( profile ) );
115     }
116 
117     public void testActivationWithBadRangeJdkVersion()
118         throws ProfileActivationException
119     {
120         JdkPrefixProfileActivator activator = createActivator( "1.5.0_15" );
121 
122         Profile profile = createProfile( "[1.4," );
123         try
124         {
125             activator.isActive( profile );
126             fail( "Should have raised an exception for invalid format" );
127         }
128         catch ( ProfileActivationException e )
129         {
130             assertTrue( true );
131         }
132     }
133 
134     private static JdkPrefixProfileActivator createActivator()
135     {
136         return new JdkPrefixProfileActivator();
137     }
138 
139     private static JdkPrefixProfileActivator createActivator( final String testJdkVersion )
140     {
141         return new JdkPrefixProfileActivator()
142         {
143             protected String getJdkVersion()
144             {
145                 return testJdkVersion;
146             }
147         };
148     }
149 
150     private static Profile createProfile( String jdk )
151     {
152         Profile profile = new Profile();
153         Activation activation = new Activation();
154         activation.setJdk( jdk );
155         profile.setActivation( activation );
156         return profile;
157     }
158 }