View Javadoc
1   package org.apache.maven.model.profile.activation;
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.Properties;
23  
24  import org.apache.maven.model.Activation;
25  import org.apache.maven.model.ActivationProperty;
26  import org.apache.maven.model.Profile;
27  
28  /**
29   * Tests {@link PropertyProfileActivator}.
30   *
31   * @author Benjamin Bentmann
32   */
33  public class PropertyProfileActivatorTest
34      extends AbstractProfileActivatorTest<PropertyProfileActivator>
35  {
36  
37      public PropertyProfileActivatorTest()
38      {
39          super( PropertyProfileActivator.class );
40      }
41  
42      private Profile newProfile( String key, String value )
43      {
44          ActivationProperty ap = new ActivationProperty();
45          ap.setName( key );
46          ap.setValue( value );
47  
48          Activation a = new Activation();
49          a.setProperty( ap );
50  
51          Profile p = new Profile();
52          p.setActivation( a );
53  
54          return p;
55      }
56  
57      private Properties newProperties( String key, String value )
58      {
59          Properties props = new Properties();
60          props.setProperty( key, value );
61          return props;
62      }
63  
64      public void testNullSafe()
65          throws Exception
66      {
67          Profile p = new Profile();
68  
69          assertActivation( false, p, newContext( null, null ) );
70  
71          p.setActivation( new Activation() );
72  
73          assertActivation( false, p, newContext( null, null ) );
74      }
75  
76      public void testWithNameOnly_UserProperty()
77          throws Exception
78      {
79          Profile profile = newProfile( "prop", null );
80  
81          assertActivation( true, profile, newContext( newProperties( "prop", "value" ), null ) );
82  
83          assertActivation( false, profile, newContext( newProperties( "prop", "" ), null ) );
84  
85          assertActivation( false, profile, newContext( newProperties( "other", "value" ), null ) );
86      }
87  
88      public void testWithNameOnly_SystemProperty()
89          throws Exception
90      {
91          Profile profile = newProfile( "prop", null );
92  
93          assertActivation( true, profile, newContext( null, newProperties( "prop", "value" ) ) );
94  
95          assertActivation( false, profile, newContext( null, newProperties( "prop", "" ) ) );
96  
97          assertActivation( false, profile, newContext( null, newProperties( "other", "value" ) ) );
98      }
99  
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 }