View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.profiles.manager;
20  
21  import java.util.List;
22  import java.util.Properties;
23  
24  import org.apache.maven.model.Activation;
25  import org.apache.maven.model.ActivationProperty;
26  import org.apache.maven.model.Profile;
27  import org.apache.maven.profiles.DefaultProfileManager;
28  import org.apache.maven.profiles.ProfileManager;
29  import org.codehaus.plexus.ContainerConfiguration;
30  import org.codehaus.plexus.PlexusConstants;
31  import org.codehaus.plexus.PlexusTestCase;
32  
33  public class DefaultProfileManagerTest extends PlexusTestCase {
34  
35      @Override
36      protected void customizeContainerConfiguration(ContainerConfiguration configuration) {
37          super.customizeContainerConfiguration(configuration);
38          configuration.setAutoWiring(true);
39          configuration.setClassPathScanning(PlexusConstants.SCANNING_ON);
40      }
41  
42      public void testShouldActivateDefaultProfile() throws Exception {
43          Profile notActivated = new Profile();
44          notActivated.setId("notActivated");
45  
46          Activation nonActivation = new Activation();
47  
48          nonActivation.setJdk("19.2");
49  
50          notActivated.setActivation(nonActivation);
51  
52          Profile defaultActivated = new Profile();
53          defaultActivated.setId("defaultActivated");
54  
55          Activation defaultActivation = new Activation();
56  
57          defaultActivation.setActiveByDefault(true);
58  
59          defaultActivated.setActivation(defaultActivation);
60  
61          Properties props = System.getProperties();
62  
63          ProfileManager profileManager = new DefaultProfileManager(getContainer(), props);
64  
65          profileManager.addProfile(notActivated);
66          profileManager.addProfile(defaultActivated);
67  
68          List active = profileManager.getActiveProfiles();
69  
70          assertNotNull(active);
71          assertEquals(1, active.size());
72          assertEquals("defaultActivated", ((Profile) active.get(0)).getId());
73      }
74  
75      public void testShouldNotActivateDefaultProfile() throws Exception {
76          Profile syspropActivated = new Profile();
77          syspropActivated.setId("syspropActivated");
78  
79          Activation syspropActivation = new Activation();
80  
81          ActivationProperty syspropProperty = new ActivationProperty();
82          syspropProperty.setName("java.version");
83  
84          syspropActivation.setProperty(syspropProperty);
85  
86          syspropActivated.setActivation(syspropActivation);
87  
88          Profile defaultActivated = new Profile();
89          defaultActivated.setId("defaultActivated");
90  
91          Activation defaultActivation = new Activation();
92  
93          defaultActivation.setActiveByDefault(true);
94  
95          defaultActivated.setActivation(defaultActivation);
96  
97          Properties props = System.getProperties();
98  
99          ProfileManager profileManager = new DefaultProfileManager(getContainer(), props);
100 
101         profileManager.addProfile(syspropActivated);
102         profileManager.addProfile(defaultActivated);
103 
104         List active = profileManager.getActiveProfiles();
105 
106         assertNotNull(active);
107         assertEquals(1, active.size());
108         assertEquals("syspropActivated", ((Profile) active.get(0)).getId());
109     }
110 
111     public void testShouldNotActivateReversalOfPresentSystemProperty() throws Exception {
112         Profile syspropActivated = new Profile();
113         syspropActivated.setId("syspropActivated");
114 
115         Activation syspropActivation = new Activation();
116 
117         ActivationProperty syspropProperty = new ActivationProperty();
118         syspropProperty.setName("!java.version");
119 
120         syspropActivation.setProperty(syspropProperty);
121 
122         syspropActivated.setActivation(syspropActivation);
123 
124         Properties props = System.getProperties();
125 
126         ProfileManager profileManager = new DefaultProfileManager(getContainer(), props);
127 
128         profileManager.addProfile(syspropActivated);
129 
130         List active = profileManager.getActiveProfiles();
131 
132         assertNotNull(active);
133         assertEquals(0, active.size());
134     }
135 
136     public void testShouldOverrideAndActivateInactiveProfile() throws Exception {
137         Profile syspropActivated = new Profile();
138         syspropActivated.setId("syspropActivated");
139 
140         Activation syspropActivation = new Activation();
141 
142         ActivationProperty syspropProperty = new ActivationProperty();
143         syspropProperty.setName("!java.version");
144 
145         syspropActivation.setProperty(syspropProperty);
146 
147         syspropActivated.setActivation(syspropActivation);
148 
149         Properties props = System.getProperties();
150 
151         ProfileManager profileManager = new DefaultProfileManager(getContainer(), props);
152 
153         profileManager.addProfile(syspropActivated);
154 
155         profileManager.explicitlyActivate("syspropActivated");
156 
157         List active = profileManager.getActiveProfiles();
158 
159         assertNotNull(active);
160         assertEquals(1, active.size());
161         assertEquals("syspropActivated", ((Profile) active.get(0)).getId());
162     }
163 
164     public void testShouldOverrideAndDeactivateActiveProfile() throws Exception {
165         Profile syspropActivated = new Profile();
166         syspropActivated.setId("syspropActivated");
167 
168         Activation syspropActivation = new Activation();
169 
170         ActivationProperty syspropProperty = new ActivationProperty();
171         syspropProperty.setName("java.version");
172 
173         syspropActivation.setProperty(syspropProperty);
174 
175         syspropActivated.setActivation(syspropActivation);
176 
177         Properties props = System.getProperties();
178 
179         ProfileManager profileManager = new DefaultProfileManager(getContainer(), props);
180 
181         profileManager.addProfile(syspropActivated);
182 
183         profileManager.explicitlyDeactivate("syspropActivated");
184 
185         List active = profileManager.getActiveProfiles();
186 
187         assertNotNull(active);
188         assertEquals(0, active.size());
189     }
190     /*
191     public void testOsActivationProfile()
192         throws Exception
193     {
194         Profile osActivated = new Profile();
195         osActivated.setId( "os-profile" );
196 
197         Activation osActivation = new Activation();
198 
199         ActivationOS activationOS = new ActivationOS();
200 
201         activationOS.setName( "!dddd" );
202 
203         osActivation.setOs( activationOS );
204 
205         osActivated.setActivation( osActivation );
206 
207         Properties props = System.getProperties();
208         ProfileActivationContext ctx = new ProfileActivationContext( props, false );
209 
210         ProfileManager profileManager = new DefaultProfileManager( getContainer(), props );
211 
212         profileManager.addProfile( osActivated );
213 
214         List active = profileManager.getActiveProfiles( null );
215 
216         assertNotNull( active );
217         assertEquals( 1, active.size() );
218     }
219     */
220 
221 }