001package org.apache.maven.profiles.manager;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *  http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.util.List;
023import java.util.Properties;
024
025import org.apache.maven.model.Activation;
026import org.apache.maven.model.ActivationProperty;
027import org.apache.maven.model.Profile;
028import org.apache.maven.profiles.DefaultProfileManager;
029import org.apache.maven.profiles.ProfileManager;
030import org.codehaus.plexus.PlexusTestCase;
031
032public class DefaultProfileManagerTest
033    extends PlexusTestCase
034{
035
036    public void setUp()
037        throws Exception
038    {
039        super.setUp();
040    }
041
042    public void testShouldActivateDefaultProfile()
043        throws Exception
044    {
045        Profile notActivated = new Profile();
046        notActivated.setId( "notActivated" );
047
048        Activation nonActivation = new Activation();
049
050        nonActivation.setJdk( "19.2" );
051
052        notActivated.setActivation( nonActivation );
053
054        Profile defaultActivated = new Profile();
055        defaultActivated.setId( "defaultActivated" );
056
057        Activation defaultActivation = new Activation();
058
059        defaultActivation.setActiveByDefault( true );
060
061        defaultActivated.setActivation( defaultActivation );
062
063        Properties props = System.getProperties();
064
065        ProfileManager profileManager = new DefaultProfileManager( getContainer(), props );
066
067        profileManager.addProfile( notActivated );
068        profileManager.addProfile( defaultActivated );
069
070        List active = profileManager.getActiveProfiles();
071
072        assertNotNull( active );
073        assertEquals( 1, active.size() );
074        assertEquals( "defaultActivated", ( (Profile) active.get( 0 ) ).getId() );
075    }
076
077    public void testShouldNotActivateDefaultProfile()
078        throws Exception
079    {
080        Profile syspropActivated = new Profile();
081        syspropActivated.setId( "syspropActivated" );
082
083        Activation syspropActivation = new Activation();
084
085        ActivationProperty syspropProperty = new ActivationProperty();
086        syspropProperty.setName( "java.version" );
087
088        syspropActivation.setProperty( syspropProperty );
089
090        syspropActivated.setActivation( syspropActivation );
091
092        Profile defaultActivated = new Profile();
093        defaultActivated.setId( "defaultActivated" );
094
095        Activation defaultActivation = new Activation();
096
097        defaultActivation.setActiveByDefault( true );
098
099        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}