001/*
002 * Licensed under the Apache License, Version 2.0 (the "License");
003 * you may not use this file except in compliance with the License.
004 * You may obtain a copy of the License at
005 *
006 * http://www.apache.org/licenses/LICENSE-2.0
007 *
008 * Unless required by applicable law or agreed to in writing, software
009 * distributed under the License is distributed on an "AS IS" BASIS,
010 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
011 * See the License for the specific language governing permissions and
012 * limitations under the License.
013 */
014
015package org.basepom.mojo.propertyhelper.fields;
016
017import static org.basepom.mojo.propertyhelper.definitions.DefinitionHelper.setBlankIsValid;
018import static org.basepom.mojo.propertyhelper.definitions.DefinitionHelper.setOnMissingFileProperty;
019import static org.basepom.mojo.propertyhelper.definitions.DefinitionHelper.setOnMissingValue;
020import static org.basepom.mojo.propertyhelper.definitions.DefinitionHelper.setValues;
021import static org.basepom.mojo.propertyhelper.definitions.DefinitionHelper.stringDefinition;
022import static org.junit.jupiter.api.Assertions.assertEquals;
023import static org.junit.jupiter.api.Assertions.assertThrows;
024
025import org.basepom.mojo.propertyhelper.ValueCache;
026import org.basepom.mojo.propertyhelper.ValueProvider;
027import org.basepom.mojo.propertyhelper.ValueProvider.PropertyBackedValueAdapter;
028import org.basepom.mojo.propertyhelper.definitions.DefinitionHelper;
029import org.basepom.mojo.propertyhelper.definitions.StringDefinition;
030
031import java.util.Properties;
032
033import com.google.common.collect.ImmutableMap;
034import org.junit.jupiter.api.Test;
035
036public class TestStringField {
037
038    @Test
039    public void testSimple() {
040        final StringDefinition f1 = DefinitionHelper.stringDefinition("hello");
041        setValues(f1, "foo");
042
043        f1.check();
044
045        final StringField sf1 = StringField.forTesting(f1, ValueProvider.NULL_PROVIDER);
046        assertEquals("foo", sf1.getValue());
047    }
048
049    @Test
050    public void testTwoValues() {
051        final StringDefinition f1 = DefinitionHelper.stringDefinition("hello");
052        setValues(f1, "foo", "bar", "baz");
053
054        f1.check();
055
056        final StringField sf1 = StringField.forTesting(f1, ValueProvider.NULL_PROVIDER);
057        assertEquals("foo", sf1.getValue());
058    }
059
060    @Test
061    public void testIgnoreBlank() {
062        final StringDefinition f1 = DefinitionHelper.stringDefinition("hello");
063        setValues(f1, "", "      ", "baz");
064        setBlankIsValid(f1, false);
065
066        f1.check();
067
068        final StringField sf1 = StringField.forTesting(f1, ValueProvider.NULL_PROVIDER);
069        assertEquals("baz", sf1.getValue());
070    }
071
072    @Test
073    public void testAcceptBlank() {
074        final StringDefinition f1 = DefinitionHelper.stringDefinition("hello");
075        setValues(f1, "      ", "baz");
076        setBlankIsValid(f1, true);
077
078        f1.check();
079
080        final StringField sf1 = StringField.forTesting(f1, ValueProvider.NULL_PROVIDER);
081        assertEquals("      ", sf1.getValue());
082    }
083
084    @Test
085    public void testAcceptEmpty() {
086        final StringDefinition f1 = DefinitionHelper.stringDefinition("hello");
087        setValues(f1, "", "baz");
088        setBlankIsValid(f1, true);
089
090        f1.check();
091
092        final StringField sf1 = StringField.forTesting(f1, ValueProvider.NULL_PROVIDER);
093        assertEquals("", sf1.getValue());
094    }
095
096    @Test
097    public void testNullValueIsEmptyString() {
098        final StringDefinition f1 = DefinitionHelper.stringDefinition("hello");
099        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}