001    package org.apache.maven.model.profile.activation;
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    
022    import java.util.Properties;
023    
024    import org.apache.maven.model.Activation;
025    import org.apache.maven.model.ActivationProperty;
026    import org.apache.maven.model.Profile;
027    
028    /**
029     * Tests {@link PropertyProfileActivator}.
030     * 
031     * @author Benjamin Bentmann
032     */
033    public class PropertyProfileActivatorTest
034        extends AbstractProfileActivatorTest<PropertyProfileActivator>
035    {
036    
037        public PropertyProfileActivatorTest()
038        {
039            super( PropertyProfileActivator.class );
040        }
041    
042        private Profile newProfile( String key, String value )
043        {
044            ActivationProperty ap = new ActivationProperty();
045            ap.setName( key );
046            ap.setValue( value );
047    
048            Activation a = new Activation();
049            a.setProperty( ap );
050    
051            Profile p = new Profile();
052            p.setActivation( a );
053    
054            return p;
055        }
056    
057        private Properties newProperties( String key, String value )
058        {
059            Properties props = new Properties();
060            props.setProperty( key, value );
061            return props;
062        }
063    
064        public void testNullSafe()
065            throws Exception
066        {
067            Profile p = new Profile();
068    
069            assertActivation( false, p, newContext( null, null ) );
070    
071            p.setActivation( new Activation() );
072    
073            assertActivation( false, p, newContext( null, null ) );
074        }
075    
076        public void testWithNameOnly_UserProperty()
077            throws Exception
078        {
079            Profile profile = newProfile( "prop", null );
080    
081            assertActivation( true, profile, newContext( newProperties( "prop", "value" ), null ) );
082    
083            assertActivation( false, profile, newContext( newProperties( "prop", "" ), null ) );
084    
085            assertActivation( false, profile, newContext( newProperties( "other", "value" ), null ) );
086        }
087    
088        public void testWithNameOnly_SystemProperty()
089            throws Exception
090        {
091            Profile profile = newProfile( "prop", null );
092    
093            assertActivation( true, profile, newContext( null, newProperties( "prop", "value" ) ) );
094    
095            assertActivation( false, profile, newContext( null, newProperties( "prop", "" ) ) );
096    
097            assertActivation( false, profile, newContext( null, newProperties( "other", "value" ) ) );
098        }
099    
100        public void testWithNegatedNameOnly_UserProperty()
101            throws Exception
102        {
103            Profile profile = newProfile( "!prop", null );
104    
105            assertActivation( false, profile, newContext( newProperties( "prop", "value" ), null ) );
106    
107            assertActivation( true, profile, newContext( newProperties( "prop", "" ), null ) );
108    
109            assertActivation( true, profile, newContext( newProperties( "other", "value" ), null ) );
110        }
111    
112        public void testWithNegatedNameOnly_SystemProperty()
113            throws Exception
114        {
115            Profile profile = newProfile( "!prop", null );
116    
117            assertActivation( false, profile, newContext( null, newProperties( "prop", "value" ) ) );
118    
119            assertActivation( true, profile, newContext( null, newProperties( "prop", "" ) ) );
120    
121            assertActivation( true, profile, newContext( null, newProperties( "other", "value" ) ) );
122        }
123    
124        public void testWithValue_UserProperty()
125            throws Exception
126        {
127            Profile profile = newProfile( "prop", "value" );
128    
129            assertActivation( true, profile, newContext( newProperties( "prop", "value" ), null ) );
130    
131            assertActivation( false, profile, newContext( newProperties( "prop", "other" ), null ) );
132    
133            assertActivation( false, profile, newContext( newProperties( "prop", "" ), null ) );
134        }
135    
136        public void testWithValue_SystemProperty()
137            throws Exception
138        {
139            Profile profile = newProfile( "prop", "value" );
140    
141            assertActivation( true, profile, newContext( null, newProperties( "prop", "value" ) ) );
142    
143            assertActivation( false, profile, newContext( null, newProperties( "prop", "other" ) ) );
144    
145            assertActivation( false, profile, newContext( null, newProperties( "other", "" ) ) );
146        }
147    
148        public void testWithNegatedValue_UserProperty()
149            throws Exception
150        {
151            Profile profile = newProfile( "prop", "!value" );
152    
153            assertActivation( false, profile, newContext( newProperties( "prop", "value" ), null ) );
154    
155            assertActivation( true, profile, newContext( newProperties( "prop", "other" ), null ) );
156    
157            assertActivation( true, profile, newContext( newProperties( "prop", "" ), null ) );
158        }
159    
160        public void testWithNegatedValue_SystemProperty()
161            throws Exception
162        {
163            Profile profile = newProfile( "prop", "!value" );
164    
165            assertActivation( false, profile, newContext( null, newProperties( "prop", "value" ) ) );
166    
167            assertActivation( true, profile, newContext( null, newProperties( "prop", "other" ) ) );
168    
169            assertActivation( true, profile, newContext( null, newProperties( "other", "" ) ) );
170        }
171    
172        public void testWithValue_UserPropertyDominantOverSystemProperty()
173            throws Exception
174        {
175            Profile profile = newProfile( "prop", "value" );
176    
177            Properties props1 = newProperties( "prop", "value" );
178            Properties props2 = newProperties( "prop", "other" );
179    
180            assertActivation( true, profile, newContext( props1, props2 ) );
181    
182            assertActivation( false, profile, newContext( props2, props1 ) );
183        }
184    
185    }