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