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.groups;
016
017import static org.basepom.mojo.propertyhelper.definitions.DefinitionHelper.propertyDefinition;
018import static org.basepom.mojo.propertyhelper.definitions.DefinitionHelper.propertyGroupDefinition;
019import static org.basepom.mojo.propertyhelper.definitions.DefinitionHelper.setProperties;
020import static org.junit.jupiter.api.Assertions.assertThrows;
021
022import org.basepom.mojo.propertyhelper.FieldContext;
023import org.basepom.mojo.propertyhelper.definitions.DefinitionHelper;
024import org.basepom.mojo.propertyhelper.definitions.PropertyDefinition;
025import org.basepom.mojo.propertyhelper.definitions.PropertyGroupDefinition;
026
027import java.util.Collections;
028import java.util.List;
029
030import com.google.common.collect.ImmutableMap;
031import com.google.common.collect.Lists;
032import org.junit.jupiter.api.Assertions;
033import org.junit.jupiter.api.Test;
034
035public class TestPropertyGroup {
036    @Test
037    public void testConstant() {
038        final PropertyGroupDefinition propertyGroupDefinition = propertyGroupDefinition("hello-group");
039        final PropertyDefinition propertyDefinition = propertyDefinition("hello", "world");
040        setProperties(propertyGroupDefinition, propertyDefinition);
041
042        PropertyGroup propertyGroup = propertyGroupDefinition.createGroup(FieldContext.forTesting());
043
044        final List<String> propertyNames = Lists.newArrayList(propertyGroupDefinition.getPropertyNames());
045        Assertions.assertEquals(1, propertyNames.size());
046        Assertions.assertEquals("hello", propertyNames.get(0));
047
048        final String propertyValue = propertyGroup.getPropertyValue(propertyDefinition, Collections.emptyMap());
049        Assertions.assertEquals("world", propertyValue);
050    }
051
052    @Test
053    public void testRenderSingle() {
054        final PropertyGroupDefinition propertyGroupDefinition = propertyGroupDefinition("hello-group");
055        final PropertyDefinition propertyDefinition = propertyDefinition("hello", "@{world}");
056        setProperties(propertyGroupDefinition, propertyDefinition);
057
058        PropertyGroup propertyGroup = propertyGroupDefinition.createGroup(FieldContext.forTesting());
059
060        final List<String> propertyNames = Lists.newArrayList(propertyGroupDefinition.getPropertyNames());
061        Assertions.assertEquals(1, propertyNames.size());
062        Assertions.assertEquals("hello", propertyNames.get(0));
063
064        final String propertyValue = propertyGroup.getPropertyValue(propertyDefinition, ImmutableMap.of("world", "pizza"));
065        Assertions.assertEquals("pizza", propertyValue);
066    }
067
068    @Test
069    public void testRenderEmptyFail() {
070        assertThrows(IllegalStateException.class, () -> {
071            final PropertyGroupDefinition propertyGroupDefinition = propertyGroupDefinition("hello-group");
072            final PropertyDefinition propertyDefinition = propertyDefinition("hello", "@{world}");
073            setProperties(propertyGroupDefinition, propertyDefinition);
074
075            PropertyGroup propertyGroup = propertyGroupDefinition.createGroup(FieldContext.forTesting());
076
077            final List<String> propertyNames = Lists.newArrayList(propertyGroupDefinition.getPropertyNames());
078            Assertions.assertEquals(1, propertyNames.size());
079            Assertions.assertEquals("hello", propertyNames.get(0));
080
081            final String propertyValue = propertyGroup.getPropertyValue(propertyDefinition, Collections.emptyMap());
082            Assertions.assertEquals("", propertyValue);
083        });
084    }
085
086    @Test
087    public void testRenderEmptyOk() {
088        final PropertyGroupDefinition propertyGroupDefinition = propertyGroupDefinition("hello-group");
089        final PropertyDefinition propertyDefinition = propertyDefinition("hello", "nice-@{world}-hat");
090        setProperties(propertyGroupDefinition, propertyDefinition);
091        DefinitionHelper.setOnMissingField(propertyGroupDefinition, "ignore");
092
093        PropertyGroup propertyGroup = propertyGroupDefinition.createGroup(FieldContext.forTesting());
094
095        final List<String> propertyNames = Lists.newArrayList(propertyGroupDefinition.getPropertyNames());
096        Assertions.assertEquals(1, propertyNames.size());
097        Assertions.assertEquals("hello", propertyNames.get(0));
098
099        final String propertyValue = propertyGroup.getPropertyValue(propertyDefinition, Collections.emptyMap());
100        Assertions.assertEquals("nice--hat", propertyValue);
101    }
102
103    @Test
104    public void testRenderIsReluctant() {
105        final PropertyGroupDefinition propertyGroupDefinition = propertyGroupDefinition("hello-group");
106        final PropertyDefinition propertyDefinition = propertyDefinition("hello", "nice-@{first}-@{world}-hat");
107        setProperties(propertyGroupDefinition, propertyDefinition);
108        DefinitionHelper.setOnMissingField(propertyGroupDefinition, "ignore");
109
110        PropertyGroup propertyGroup = propertyGroupDefinition.createGroup(FieldContext.forTesting());
111
112        final List<String> propertyNames = Lists.newArrayList(propertyGroupDefinition.getPropertyNames());
113        Assertions.assertEquals(1, propertyNames.size());
114        Assertions.assertEquals("hello", propertyNames.get(0));
115
116        final String propertyValue = propertyGroup.getPropertyValue(propertyDefinition, Collections.emptyMap());
117        Assertions.assertEquals("nice---hat", propertyValue);
118    }
119
120    @Test
121    public void testRenderFriendOfAFriend() throws Exception {
122        final PropertyGroupDefinition propertyGroupDefinition = propertyGroupDefinition("hello-group");
123        final PropertyDefinition propertyDefinition = propertyDefinition("hello", "nice-@{whatWorld}-@{world}-hat");
124        setProperties(propertyGroupDefinition, propertyDefinition);
125        DefinitionHelper.setOnMissingField(propertyGroupDefinition, "ignore");
126
127        PropertyGroup propertyGroup = propertyGroupDefinition.createGroup(FieldContext.forTesting());
128
129        final List<String> propertyNames = Lists.newArrayList(propertyGroupDefinition.getPropertyNames());
130        Assertions.assertEquals(1, propertyNames.size());
131        Assertions.assertEquals("hello", propertyNames.get(0));
132
133        final String propertyValue = propertyGroup.getPropertyValue(propertyDefinition,
134            ImmutableMap.of("whatWorld", "@{first}", "first", "decadent", "world", "rome"));
135        Assertions.assertEquals("nice-decadent-rome-hat", propertyValue);
136    }
137
138
139    @Test
140    public void testRenderDotsAreCool() {
141        final PropertyGroupDefinition propertyGroupDefinition = propertyGroupDefinition("hello-group");
142        final PropertyDefinition propertyDefinition = propertyDefinition("hello", "nice-@{foo.bar.world}-hat");
143        setProperties(propertyGroupDefinition, propertyDefinition);
144        DefinitionHelper.setOnMissingField(propertyGroupDefinition, "ignore");
145
146        PropertyGroup propertyGroup = propertyGroupDefinition.createGroup(FieldContext.forTesting());
147
148        final List<String> propertyNames = Lists.newArrayList(propertyGroupDefinition.getPropertyNames());
149        Assertions.assertEquals(1, propertyNames.size());
150        Assertions.assertEquals("hello", propertyNames.get(0));
151
152        final String propertyValue = propertyGroup.getPropertyValue(propertyDefinition, ImmutableMap.of("foo.bar.world", "strange"));
153        Assertions.assertEquals("nice-strange-hat", propertyValue);
154    }
155
156}