1   package org.apache.maven.profiles;
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.io.File;
23  import java.io.IOException;
24  import java.util.List;
25  
26  import org.apache.maven.model.Activation;
27  import org.apache.maven.model.ActivationOS;
28  import org.apache.maven.model.ActivationProperty;
29  import org.apache.maven.model.Profile;
30  import org.apache.maven.profiles.activation.ProfileActivationException;
31  import org.apache.maven.settings.Settings;
32  import org.codehaus.plexus.PlexusTestCase;
33  
34  public class DefaultProfileManagerTest
35      extends PlexusTestCase
36  {
37      public void testShouldActivateNonExistantSettingsProfile()
38          throws ProfileActivationException
39      {
40          // MNG-2234 - we should activate profiles that don't exist yet so that they can be defined by a POM.
41          Settings settings = new Settings();
42          settings.addActiveProfile( "testProfile" );
43  
44          ProfileManager profileManager = new DefaultProfileManager( getContainer(), settings );
45  
46          List activeIds = profileManager.getExplicitlyActivatedIds();
47  
48          assertNotNull( activeIds );
49          assertEquals( 1, activeIds.size() );
50          assertEquals( "testProfile", activeIds.get( 0 ) );
51  
52          List activeProfiles = profileManager.getActiveProfiles();
53          assertTrue( activeProfiles.isEmpty() );
54  
55          Profile p = new Profile();
56          p.setId( "testProfile" );
57          profileManager.addProfile( p );
58  
59          activeProfiles = profileManager.getActiveProfiles();
60  
61          assertNotNull( activeProfiles );
62          assertEquals( 1, activeProfiles.size() );
63          assertEquals( "testProfile", ( (Profile) activeProfiles.get( 0 ) ).getId() );
64      }
65  
66      public void testShouldActivateDefaultProfile()
67          throws ProfileActivationException
68      {
69          Profile notActivated = new Profile();
70          notActivated.setId( "notActivated" );
71  
72          Activation nonActivation = new Activation();
73  
74          nonActivation.setJdk( "19.2" );
75  
76          notActivated.setActivation( nonActivation );
77  
78          Profile defaultActivated = new Profile();
79          defaultActivated.setId( "defaultActivated" );
80  
81          Activation defaultActivation = new Activation();
82  
83          defaultActivation.setActiveByDefault( true );
84  
85          defaultActivated.setActivation( defaultActivation );
86  
87          ProfileManager profileManager = new DefaultProfileManager( getContainer() );
88  
89          profileManager.addProfile( notActivated );
90          profileManager.addProfile( defaultActivated );
91  
92          List active = profileManager.getActiveProfiles();
93  
94          assertNotNull( active );
95          assertEquals( 1, active.size() );
96          assertEquals( "defaultActivated", ( (Profile) active.get( 0 ) ).getId() );
97      }
98  
99      public void testShouldNotActivateDefaultProfile()
100         throws ProfileActivationException
101     {
102         Profile syspropActivated = new Profile();
103         syspropActivated.setId( "syspropActivated" );
104 
105         Activation syspropActivation = new Activation();
106 
107         ActivationProperty syspropProperty = new ActivationProperty();
108         syspropProperty.setName( "java.version" );
109 
110         syspropActivation.setProperty( syspropProperty );
111 
112         syspropActivated.setActivation( syspropActivation );
113 
114         Profile defaultActivated = new Profile();
115         defaultActivated.setId( "defaultActivated" );
116 
117         Activation defaultActivation = new Activation();
118 
119         defaultActivation.setActiveByDefault( true );
120 
121         defaultActivated.setActivation( defaultActivation );
122 
123         ProfileManager profileManager = new DefaultProfileManager( getContainer() );
124 
125         profileManager.addProfile( syspropActivated );
126         profileManager.addProfile( defaultActivated );
127 
128         List active = profileManager.getActiveProfiles();
129 
130         assertNotNull( active );
131         assertEquals( 1, active.size() );
132         assertEquals( "syspropActivated", ( (Profile) active.get( 0 ) ).getId() );
133     }
134 
135     public void testShouldNotActivateReversalOfPresentSystemProperty()
136         throws ProfileActivationException
137     {
138         Profile syspropActivated = new Profile();
139         syspropActivated.setId( "syspropActivated" );
140 
141         Activation syspropActivation = new Activation();
142 
143         ActivationProperty syspropProperty = new ActivationProperty();
144         syspropProperty.setName( "!java.version" );
145 
146         syspropActivation.setProperty( syspropProperty );
147 
148         syspropActivated.setActivation( syspropActivation );
149 
150         ProfileManager profileManager = new DefaultProfileManager( getContainer() );
151 
152         profileManager.addProfile( syspropActivated );
153 
154         List active = profileManager.getActiveProfiles();
155 
156         assertNotNull( active );
157         assertEquals( 0, active.size() );
158     }
159 
160     public void testShouldOverrideAndActivateInactiveProfile()
161         throws ProfileActivationException
162     {
163         Profile syspropActivated = new Profile();
164         syspropActivated.setId( "syspropActivated" );
165 
166         Activation syspropActivation = new Activation();
167 
168         ActivationProperty syspropProperty = new ActivationProperty();
169         syspropProperty.setName( "!java.version" );
170 
171         syspropActivation.setProperty( syspropProperty );
172 
173         syspropActivated.setActivation( syspropActivation );
174 
175         ProfileManager profileManager = new DefaultProfileManager( getContainer() );
176 
177         profileManager.addProfile( syspropActivated );
178 
179         profileManager.explicitlyActivate( "syspropActivated" );
180 
181         List active = profileManager.getActiveProfiles();
182 
183         assertNotNull( active );
184         assertEquals( 1, active.size() );
185         assertEquals( "syspropActivated", ( (Profile) active.get( 0 ) ).getId() );
186     }
187 
188     public void testShouldOverrideAndDeactivateActiveProfile()
189         throws ProfileActivationException
190     {
191         Profile syspropActivated = new Profile();
192         syspropActivated.setId( "syspropActivated" );
193 
194         Activation syspropActivation = new Activation();
195 
196         ActivationProperty syspropProperty = new ActivationProperty();
197         syspropProperty.setName( "java.version" );
198 
199         syspropActivation.setProperty( syspropProperty );
200 
201         syspropActivated.setActivation( syspropActivation );
202 
203         ProfileManager profileManager = new DefaultProfileManager( getContainer() );
204 
205         profileManager.addProfile( syspropActivated );
206 
207         profileManager.explicitlyDeactivate( "syspropActivated" );
208 
209         List active = profileManager.getActiveProfiles();
210 
211         assertNotNull( active );
212         assertEquals( 0, active.size() );
213     }
214 
215     public void testOsActivationProfile()
216         throws ProfileActivationException
217     {
218         Profile osActivated = new Profile();
219         osActivated.setId( "os-profile" );
220 
221         Activation osActivation = new Activation();
222 
223         ActivationOS activationOS = new ActivationOS();
224 
225         activationOS.setName( "!dddd" );
226 
227         osActivation.setOs( activationOS );
228 
229         osActivated.setActivation( osActivation );
230 
231         ProfileManager profileManager = new DefaultProfileManager( getContainer() );
232 
233         profileManager.addProfile( osActivated );
234 
235         List active = profileManager.getActiveProfiles();
236 
237         assertNotNull( active );
238         assertEquals( 1, active.size() );
239     }
240 
241     public void testFileActivationProfile()
242         throws ProfileActivationException, IOException
243     {
244         Profile osActivated = new Profile();
245         osActivated.setId( "os-profile" );
246 
247         Activation fileActivation = new Activation();
248 
249         org.apache.maven.model.ActivationFile activationFile = new org.apache.maven.model.ActivationFile();
250 
251         File f = File.createTempFile( "activationTest", null );
252         f.createNewFile();
253         activationFile.setExists( f.getAbsolutePath() );
254 
255         fileActivation.setFile( activationFile );
256 
257         osActivated.setActivation( fileActivation );
258 
259         ProfileManager profileManager = new DefaultProfileManager( getContainer() );
260 
261         profileManager.addProfile( osActivated );
262 
263         List active = profileManager.getActiveProfiles();
264 
265         assertNotNull( active );
266         assertEquals( 1, active.size() );
267         
268         try
269         {
270             f.delete();
271         }
272         catch (Exception e)
273         {
274             //don't care. I tried.
275         }
276     }
277 }