View Javadoc
1   /*
2    * Licensed under the Apache License, Version 2.0 (the "License");
3    * you may not use this file except in compliance with the License.
4    * You may obtain a copy of the License at
5    *
6    * http://www.apache.org/licenses/LICENSE-2.0
7    *
8    * Unless required by applicable law or agreed to in writing, software
9    * distributed under the License is distributed on an "AS IS" BASIS,
10   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11   * See the License for the specific language governing permissions and
12   * limitations under the License.
13   */
14  
15  package org.basepom.mojo.propertyhelper.fields;
16  
17  import static org.basepom.mojo.propertyhelper.definitions.DefinitionHelper.setBlankIsValid;
18  import static org.basepom.mojo.propertyhelper.definitions.DefinitionHelper.setOnMissingFileProperty;
19  import static org.basepom.mojo.propertyhelper.definitions.DefinitionHelper.setOnMissingValue;
20  import static org.basepom.mojo.propertyhelper.definitions.DefinitionHelper.setValues;
21  import static org.basepom.mojo.propertyhelper.definitions.DefinitionHelper.stringDefinition;
22  import static org.junit.jupiter.api.Assertions.assertEquals;
23  import static org.junit.jupiter.api.Assertions.assertThrows;
24  
25  import org.basepom.mojo.propertyhelper.ValueCache;
26  import org.basepom.mojo.propertyhelper.ValueProvider;
27  import org.basepom.mojo.propertyhelper.ValueProvider.PropertyBackedValueAdapter;
28  import org.basepom.mojo.propertyhelper.definitions.DefinitionHelper;
29  import org.basepom.mojo.propertyhelper.definitions.StringDefinition;
30  
31  import java.util.Properties;
32  
33  import com.google.common.collect.ImmutableMap;
34  import org.junit.jupiter.api.Test;
35  
36  public class TestStringField {
37  
38      @Test
39      public void testSimple() {
40          final StringDefinition f1 = DefinitionHelper.stringDefinition("hello");
41          setValues(f1, "foo");
42  
43          f1.check();
44  
45          final StringField sf1 = StringField.forTesting(f1, ValueProvider.NULL_PROVIDER);
46          assertEquals("foo", sf1.getValue());
47      }
48  
49      @Test
50      public void testTwoValues() {
51          final StringDefinition f1 = DefinitionHelper.stringDefinition("hello");
52          setValues(f1, "foo", "bar", "baz");
53  
54          f1.check();
55  
56          final StringField sf1 = StringField.forTesting(f1, ValueProvider.NULL_PROVIDER);
57          assertEquals("foo", sf1.getValue());
58      }
59  
60      @Test
61      public void testIgnoreBlank() {
62          final StringDefinition f1 = DefinitionHelper.stringDefinition("hello");
63          setValues(f1, "", "      ", "baz");
64          setBlankIsValid(f1, false);
65  
66          f1.check();
67  
68          final StringField sf1 = StringField.forTesting(f1, ValueProvider.NULL_PROVIDER);
69          assertEquals("baz", sf1.getValue());
70      }
71  
72      @Test
73      public void testAcceptBlank() {
74          final StringDefinition f1 = DefinitionHelper.stringDefinition("hello");
75          setValues(f1, "      ", "baz");
76          setBlankIsValid(f1, true);
77  
78          f1.check();
79  
80          final StringField sf1 = StringField.forTesting(f1, ValueProvider.NULL_PROVIDER);
81          assertEquals("      ", sf1.getValue());
82      }
83  
84      @Test
85      public void testAcceptEmpty() {
86          final StringDefinition f1 = DefinitionHelper.stringDefinition("hello");
87          setValues(f1, "", "baz");
88          setBlankIsValid(f1, true);
89  
90          f1.check();
91  
92          final StringField sf1 = StringField.forTesting(f1, ValueProvider.NULL_PROVIDER);
93          assertEquals("", sf1.getValue());
94      }
95  
96      @Test
97      public void testNullValueIsEmptyString() {
98          final StringDefinition f1 = DefinitionHelper.stringDefinition("hello");
99          setValues(f1, null, "wibble");
100         setBlankIsValid(f1, true);
101 
102         f1.check();
103 
104         final StringField sf1 = StringField.forTesting(f1, ValueProvider.NULL_PROVIDER);
105         assertEquals("", sf1.getValue());
106     }
107 
108     @Test
109     public void testSimpleProperty() {
110         final StringDefinition f1 = DefinitionHelper.stringDefinition("hello");
111 
112         f1.check();
113 
114         final Properties props = new Properties();
115         props.setProperty("hello", "foo");
116         final StringField sf1 = StringField.forTesting(f1, new PropertyBackedValueAdapter(props, f1.getId()));
117         assertEquals("foo", sf1.getValue());
118     }
119 
120     @Test
121     public void testSimplePropertyWithDefault() {
122         final StringDefinition f1 = DefinitionHelper.stringDefinition("hello");
123         setValues(f1, "baz");
124 
125         f1.check();
126 
127         final Properties props = new Properties();
128         props.setProperty("hello", "foo");
129         final StringField sf1 = StringField.forTesting(f1, new PropertyBackedValueAdapter(props, f1.getId()));
130         assertEquals("foo", sf1.getValue());
131     }
132 
133     @Test
134     public void testNoProperty() {
135         final StringDefinition f1 = DefinitionHelper.stringDefinition("hello");
136         setValues(f1, "baz");
137 
138         f1.check();
139 
140         final Properties props = new Properties();
141         props.setProperty("hello2", "foo");
142         final StringField sf1 = StringField.forTesting(f1, new PropertyBackedValueAdapter(props, f1.getId()));
143         assertEquals("baz", sf1.getValue());
144     }
145 
146     @Test
147     public void testIgnoreBlankProperty() {
148         final StringDefinition f1 = DefinitionHelper.stringDefinition("hello");
149         setBlankIsValid(f1, false);
150         setValues(f1, "baz");
151 
152         f1.check();
153 
154         final Properties props = new Properties();
155         props.setProperty("hello", "");
156         final StringField sf1 = StringField.forTesting(f1, new PropertyBackedValueAdapter(props, f1.getId()));
157         assertEquals("baz", sf1.getValue());
158     }
159 
160     @Test
161     public void testNothing() {
162 
163         assertThrows(IllegalStateException.class, () -> {
164             final StringDefinition f1 = stringDefinition("hello");
165             setOnMissingValue(f1, "fail");
166             setBlankIsValid(f1, true);
167             f1.check();
168 
169             final StringField sf1 = StringField.forTesting(f1, ValueProvider.NULL_PROVIDER);
170             assertEquals("baz", sf1.getValue());
171         });
172     }
173 
174     @Test
175     public void testNothingIgnore() {
176         final StringDefinition f1 = DefinitionHelper.stringDefinition("hello");
177         setOnMissingValue(f1, "ignore");
178         setBlankIsValid(f1, true);
179 
180         f1.check();
181 
182         final StringField sf1 = StringField.forTesting(f1, ValueProvider.NULL_PROVIDER);
183         assertEquals("", sf1.getValue());
184     }
185 
186     @Test
187     public void testMissingProperty() {
188         ValueCache valueCache = new ValueCache();
189 
190         assertThrows(IllegalStateException.class, () -> {
191             final StringDefinition f1 = stringDefinition("hello");
192             setOnMissingValue(f1, "ignore");
193             setOnMissingFileProperty(f1, "fail");
194             setBlankIsValid(f1, true);
195 
196             f1.check();
197 
198             final ValueProvider provider = valueCache.findCurrentValueProvider(ImmutableMap.<String, String>of(), f1);
199 
200             final StringField sf1 = StringField.forTesting(f1, provider);
201             assertEquals("", sf1.getValue());
202         });
203     }
204 
205     @Test
206     public void testBlankPropertyValue() {
207         ValueCache valueCache = new ValueCache();
208 
209         final StringDefinition f1 = DefinitionHelper.stringDefinition("hello");
210         setBlankIsValid(f1, true);
211 
212         f1.check();
213 
214         final ImmutableMap<String, String> props = ImmutableMap.of("hello", "");
215         final ValueProvider provider = valueCache.findCurrentValueProvider(props, f1);
216 
217         final StringField sf1 = StringField.forTesting(f1, provider);
218         assertEquals("", sf1.getValue());
219     }
220 
221     @Test
222     public void testBlankValue() {
223         final StringDefinition f1 = DefinitionHelper.stringDefinition("hello");
224         setValues(f1, "", "foo");
225         setBlankIsValid(f1, true);
226 
227         f1.check();
228 
229         final StringField sf1 = StringField.forTesting(f1, ValueProvider.NULL_PROVIDER);
230         assertEquals("", sf1.getValue());
231     }
232 }