1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.basepom.mojo.propertyhelper.groups;
16
17 import static org.basepom.mojo.propertyhelper.definitions.DefinitionHelper.propertyDefinition;
18 import static org.basepom.mojo.propertyhelper.definitions.DefinitionHelper.propertyGroupDefinition;
19 import static org.basepom.mojo.propertyhelper.definitions.DefinitionHelper.setProperties;
20 import static org.junit.jupiter.api.Assertions.assertThrows;
21
22 import org.basepom.mojo.propertyhelper.FieldContext;
23 import org.basepom.mojo.propertyhelper.definitions.DefinitionHelper;
24 import org.basepom.mojo.propertyhelper.definitions.PropertyDefinition;
25 import org.basepom.mojo.propertyhelper.definitions.PropertyGroupDefinition;
26
27 import java.util.Collections;
28 import java.util.List;
29
30 import com.google.common.collect.ImmutableMap;
31 import com.google.common.collect.Lists;
32 import org.junit.jupiter.api.Assertions;
33 import org.junit.jupiter.api.Test;
34
35 public class TestPropertyGroup {
36 @Test
37 public void testConstant() {
38 final PropertyGroupDefinition propertyGroupDefinition = propertyGroupDefinition("hello-group");
39 final PropertyDefinition propertyDefinition = propertyDefinition("hello", "world");
40 setProperties(propertyGroupDefinition, propertyDefinition);
41
42 PropertyGroup propertyGroup = propertyGroupDefinition.createGroup(FieldContext.forTesting());
43
44 final List<String> propertyNames = Lists.newArrayList(propertyGroupDefinition.getPropertyNames());
45 Assertions.assertEquals(1, propertyNames.size());
46 Assertions.assertEquals("hello", propertyNames.get(0));
47
48 final String propertyValue = propertyGroup.getPropertyValue(propertyDefinition, Collections.emptyMap());
49 Assertions.assertEquals("world", propertyValue);
50 }
51
52 @Test
53 public void testRenderSingle() {
54 final PropertyGroupDefinition propertyGroupDefinition = propertyGroupDefinition("hello-group");
55 final PropertyDefinition propertyDefinition = propertyDefinition("hello", "@{world}");
56 setProperties(propertyGroupDefinition, propertyDefinition);
57
58 PropertyGroup propertyGroup = propertyGroupDefinition.createGroup(FieldContext.forTesting());
59
60 final List<String> propertyNames = Lists.newArrayList(propertyGroupDefinition.getPropertyNames());
61 Assertions.assertEquals(1, propertyNames.size());
62 Assertions.assertEquals("hello", propertyNames.get(0));
63
64 final String propertyValue = propertyGroup.getPropertyValue(propertyDefinition, ImmutableMap.of("world", "pizza"));
65 Assertions.assertEquals("pizza", propertyValue);
66 }
67
68 @Test
69 public void testRenderEmptyFail() {
70 assertThrows(IllegalStateException.class, () -> {
71 final PropertyGroupDefinition propertyGroupDefinition = propertyGroupDefinition("hello-group");
72 final PropertyDefinition propertyDefinition = propertyDefinition("hello", "@{world}");
73 setProperties(propertyGroupDefinition, propertyDefinition);
74
75 PropertyGroup propertyGroup = propertyGroupDefinition.createGroup(FieldContext.forTesting());
76
77 final List<String> propertyNames = Lists.newArrayList(propertyGroupDefinition.getPropertyNames());
78 Assertions.assertEquals(1, propertyNames.size());
79 Assertions.assertEquals("hello", propertyNames.get(0));
80
81 final String propertyValue = propertyGroup.getPropertyValue(propertyDefinition, Collections.emptyMap());
82 Assertions.assertEquals("", propertyValue);
83 });
84 }
85
86 @Test
87 public void testRenderEmptyOk() {
88 final PropertyGroupDefinition propertyGroupDefinition = propertyGroupDefinition("hello-group");
89 final PropertyDefinition propertyDefinition = propertyDefinition("hello", "nice-@{world}-hat");
90 setProperties(propertyGroupDefinition, propertyDefinition);
91 DefinitionHelper.setOnMissingField(propertyGroupDefinition, "ignore");
92
93 PropertyGroup propertyGroup = propertyGroupDefinition.createGroup(FieldContext.forTesting());
94
95 final List<String> propertyNames = Lists.newArrayList(propertyGroupDefinition.getPropertyNames());
96 Assertions.assertEquals(1, propertyNames.size());
97 Assertions.assertEquals("hello", propertyNames.get(0));
98
99 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 }