View Javadoc
1   package org.apache.maven.profiles.manager;
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.List;
23  import java.util.Properties;
24  
25  import org.apache.maven.model.Activation;
26  import org.apache.maven.model.ActivationProperty;
27  import org.apache.maven.model.Profile;
28  import org.apache.maven.profiles.DefaultProfileManager;
29  import org.apache.maven.profiles.ProfileManager;
30  import org.codehaus.plexus.ContainerConfiguration;
31  import org.codehaus.plexus.PlexusConstants;
32  import org.codehaus.plexus.PlexusTestCase;
33  
34  public class DefaultProfileManagerTest
35      extends PlexusTestCase
36  {
37  
38      @Override
39      protected void customizeContainerConfiguration( ContainerConfiguration configuration )
40      {
41          super.customizeContainerConfiguration( configuration );
42          configuration.setAutoWiring( true );
43          configuration.setClassPathScanning( PlexusConstants.SCANNING_ON );
44      }
45  
46      public void testShouldActivateDefaultProfile()
47          throws Exception
48      {
49          Profile notActivated = new Profile();
50          notActivated.setId( "notActivated" );
51  
52          Activation nonActivation = new Activation();
53  
54          nonActivation.setJdk( "19.2" );
55  
56          notActivated.setActivation( nonActivation );
57  
58          Profile defaultActivated = new Profile();
59          defaultActivated.setId( "defaultActivated" );
60  
61          Activation defaultActivation = new Activation();
62  
63          defaultActivation.setActiveByDefault( true );
64  
65          defaultActivated.setActivation( defaultActivation );
66  
67          Properties props = System.getProperties();
68  
69          ProfileManager profileManager = new DefaultProfileManager( getContainer(), props );
70  
71          profileManager.addProfile( notActivated );
72          profileManager.addProfile( defaultActivated );
73  
74          List active = profileManager.getActiveProfiles();
75  
76          assertNotNull( active );
77          assertEquals( 1, active.size() );
78          assertEquals( "defaultActivated", ( (Profile) active.get( 0 ) ).getId() );
79      }
80  
81      public void testShouldNotActivateDefaultProfile()
82          throws Exception
83      {
84          Profile syspropActivated = new Profile();
85          syspropActivated.setId( "syspropActivated" );
86  
87          Activation syspropActivation = new Activation();
88  
89          ActivationProperty syspropProperty = new ActivationProperty();
90          syspropProperty.setName( "java.version" );
91  
92          syspropActivation.setProperty( syspropProperty );
93  
94          syspropActivated.setActivation( syspropActivation );
95  
96          Profile defaultActivated = new Profile();
97          defaultActivated.setId( "defaultActivated" );
98  
99          Activation defaultActivation = new Activation();
100 
101         defaultActivation.setActiveByDefault( true );
102 
103         defaultActivated.setActivation( defaultActivation );
104 
105         Properties props = System.getProperties();
106 
107         ProfileManager profileManager = new DefaultProfileManager( getContainer(), props );
108 
109         profileManager.addProfile( syspropActivated );
110         profileManager.addProfile( defaultActivated );
111 
112         List active = profileManager.getActiveProfiles();
113 
114         assertNotNull( active );
115         assertEquals( 1, active.size() );
116         assertEquals( "syspropActivated", ( (Profile) active.get( 0 ) ).getId() );
117     }
118 
119 
120     public void testShouldNotActivateReversalOfPresentSystemProperty()
121         throws Exception
122     {
123         Profile syspropActivated = new Profile();
124         syspropActivated.setId( "syspropActivated" );
125 
126         Activation syspropActivation = new Activation();
127 
128         ActivationProperty syspropProperty = new ActivationProperty();
129         syspropProperty.setName( "!java.version" );
130 
131         syspropActivation.setProperty( syspropProperty );
132 
133         syspropActivated.setActivation( syspropActivation );
134 
135         Properties props = System.getProperties();
136 
137         ProfileManager profileManager = new DefaultProfileManager( getContainer(), props );
138 
139         profileManager.addProfile( syspropActivated );
140 
141         List active = profileManager.getActiveProfiles();
142 
143         assertNotNull( active );
144         assertEquals( 0, active.size() );
145     }
146 
147     public void testShouldOverrideAndActivateInactiveProfile()
148         throws Exception
149     {
150         Profile syspropActivated = new Profile();
151         syspropActivated.setId( "syspropActivated" );
152 
153         Activation syspropActivation = new Activation();
154 
155         ActivationProperty syspropProperty = new ActivationProperty();
156         syspropProperty.setName( "!java.version" );
157 
158         syspropActivation.setProperty( syspropProperty );
159 
160         syspropActivated.setActivation( syspropActivation );
161 
162         Properties props = System.getProperties();
163 
164         ProfileManager profileManager = new DefaultProfileManager( getContainer(), props );
165 
166         profileManager.addProfile( syspropActivated );
167 
168         profileManager.explicitlyActivate( "syspropActivated" );
169 
170         List active = profileManager.getActiveProfiles();
171 
172         assertNotNull( active );
173         assertEquals( 1, active.size() );
174         assertEquals( "syspropActivated", ( (Profile) active.get( 0 ) ).getId() );
175     }
176 
177     public void testShouldOverrideAndDeactivateActiveProfile()
178         throws Exception
179     {
180         Profile syspropActivated = new Profile();
181         syspropActivated.setId( "syspropActivated" );
182 
183         Activation syspropActivation = new Activation();
184 
185         ActivationProperty syspropProperty = new ActivationProperty();
186         syspropProperty.setName( "java.version" );
187 
188         syspropActivation.setProperty( syspropProperty );
189 
190         syspropActivated.setActivation( syspropActivation );
191 
192         Properties props = System.getProperties();
193 
194         ProfileManager profileManager = new DefaultProfileManager( getContainer(), props );
195 
196         profileManager.addProfile( syspropActivated );
197 
198         profileManager.explicitlyDeactivate( "syspropActivated" );
199 
200         List active = profileManager.getActiveProfiles();
201 
202         assertNotNull( active );
203         assertEquals( 0, active.size() );
204     }
205 /*
206     public void testOsActivationProfile()
207         throws Exception
208     {
209         Profile osActivated = new Profile();
210         osActivated.setId( "os-profile" );
211 
212         Activation osActivation = new Activation();
213 
214         ActivationOS activationOS = new ActivationOS();
215 
216         activationOS.setName( "!dddd" );
217 
218         osActivation.setOs( activationOS );
219 
220         osActivated.setActivation( osActivation );
221 
222         Properties props = System.getProperties();
223         ProfileActivationContext ctx = new ProfileActivationContext( props, false );
224 
225         ProfileManager profileManager = new DefaultProfileManager( getContainer(), props );
226 
227         profileManager.addProfile( osActivated );
228 
229         List active = profileManager.getActiveProfiles( null );
230 
231         assertNotNull( active );
232         assertEquals( 1, active.size() );
233     }
234     */
235 
236 }