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