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.model.profile.activation;
20  
21  import java.util.Properties;
22  
23  import org.apache.maven.model.Activation;
24  import org.apache.maven.model.ActivationProperty;
25  import org.apache.maven.model.Profile;
26  
27  /**
28   * Tests {@link PropertyProfileActivator}.
29   *
30   * @author Benjamin Bentmann
31   */
32  public class PropertyProfileActivatorTest extends AbstractProfileActivatorTest<PropertyProfileActivator> {
33  
34      public PropertyProfileActivatorTest() {
35          super(PropertyProfileActivator.class);
36      }
37  
38      private Profile newProfile(String key, String value) {
39          ActivationProperty ap = new ActivationProperty();
40          ap.setName(key);
41          ap.setValue(value);
42  
43          Activation a = new Activation();
44          a.setProperty(ap);
45  
46          Profile p = new Profile();
47          p.setActivation(a);
48  
49          return p;
50      }
51  
52      private Properties newProperties(String key, String value) {
53          Properties props = new Properties();
54          props.setProperty(key, value);
55          return props;
56      }
57  
58      public void testNullSafe() throws Exception {
59          Profile p = new Profile();
60  
61          assertActivation(false, p, newContext(null, null));
62  
63          p.setActivation(new Activation());
64  
65          assertActivation(false, p, newContext(null, null));
66      }
67  
68      public void testWithNameOnly_UserProperty() throws Exception {
69          Profile profile = newProfile("prop", null);
70  
71          assertActivation(true, profile, newContext(newProperties("prop", "value"), null));
72  
73          assertActivation(false, profile, newContext(newProperties("prop", ""), null));
74  
75          assertActivation(false, profile, newContext(newProperties("other", "value"), null));
76      }
77  
78      public void testWithNameOnly_SystemProperty() throws Exception {
79          Profile profile = newProfile("prop", null);
80  
81          assertActivation(true, profile, newContext(null, newProperties("prop", "value")));
82  
83          assertActivation(false, profile, newContext(null, newProperties("prop", "")));
84  
85          assertActivation(false, profile, newContext(null, newProperties("other", "value")));
86      }
87  
88      public void testWithNegatedNameOnly_UserProperty() throws Exception {
89          Profile profile = newProfile("!prop", null);
90  
91          assertActivation(false, profile, newContext(newProperties("prop", "value"), null));
92  
93          assertActivation(true, profile, newContext(newProperties("prop", ""), null));
94  
95          assertActivation(true, profile, newContext(newProperties("other", "value"), null));
96      }
97  
98      public void testWithNegatedNameOnly_SystemProperty() throws Exception {
99          Profile profile = newProfile("!prop", null);
100 
101         assertActivation(false, profile, newContext(null, newProperties("prop", "value")));
102 
103         assertActivation(true, profile, newContext(null, newProperties("prop", "")));
104 
105         assertActivation(true, profile, newContext(null, newProperties("other", "value")));
106     }
107 
108     public void testWithValue_UserProperty() throws Exception {
109         Profile profile = newProfile("prop", "value");
110 
111         assertActivation(true, profile, newContext(newProperties("prop", "value"), null));
112 
113         assertActivation(false, profile, newContext(newProperties("prop", "other"), null));
114 
115         assertActivation(false, profile, newContext(newProperties("prop", ""), null));
116     }
117 
118     public void testWithValue_SystemProperty() throws Exception {
119         Profile profile = newProfile("prop", "value");
120 
121         assertActivation(true, profile, newContext(null, newProperties("prop", "value")));
122 
123         assertActivation(false, profile, newContext(null, newProperties("prop", "other")));
124 
125         assertActivation(false, profile, newContext(null, newProperties("other", "")));
126     }
127 
128     public void testWithNegatedValue_UserProperty() throws Exception {
129         Profile profile = newProfile("prop", "!value");
130 
131         assertActivation(false, profile, newContext(newProperties("prop", "value"), null));
132 
133         assertActivation(true, profile, newContext(newProperties("prop", "other"), null));
134 
135         assertActivation(true, profile, newContext(newProperties("prop", ""), null));
136     }
137 
138     public void testWithNegatedValue_SystemProperty() throws Exception {
139         Profile profile = newProfile("prop", "!value");
140 
141         assertActivation(false, profile, newContext(null, newProperties("prop", "value")));
142 
143         assertActivation(true, profile, newContext(null, newProperties("prop", "other")));
144 
145         assertActivation(true, profile, newContext(null, newProperties("other", "")));
146     }
147 
148     public void testWithValue_UserPropertyDominantOverSystemProperty() throws Exception {
149         Profile profile = newProfile("prop", "value");
150 
151         Properties props1 = newProperties("prop", "value");
152         Properties props2 = newProperties("prop", "other");
153 
154         assertActivation(true, profile, newContext(props1, props2));
155 
156         assertActivation(false, profile, newContext(props2, props1));
157     }
158 }