1
2
3
4
5 package com.qulice.maven;
6
7 import java.util.Arrays;
8 import java.util.List;
9 import java.util.Properties;
10 import org.codehaus.plexus.configuration.xml.XmlPlexusConfiguration;
11 import org.codehaus.plexus.util.xml.Xpp3Dom;
12 import org.hamcrest.MatcherAssert;
13 import org.hamcrest.Matchers;
14 import org.junit.jupiter.api.Assertions;
15 import org.junit.jupiter.api.Test;
16
17
18
19
20
21
22 final class MojoExecutorTest {
23
24
25
26
27
28
29 @Test
30 void rendersCollectionOfPropertiesRecursively() {
31 final Properties dep = new Properties();
32 dep.put("groupId", "org.apache.xmlgraphics");
33 dep.put("artifactId", "batik-ext");
34 dep.put("version", "1.7");
35 final Properties wrapper = new Properties();
36 wrapper.put("dependency", dep);
37 final Properties config = new Properties();
38 config.put("ignoredDependencies", List.of(wrapper));
39 MatcherAssert.assertThat(
40 "Dependency details cannot be rendered as toString()",
41 new MojoExecutor(null, null)
42 .toXppDom(config, "configuration")
43 .getChild("ignoredDependencies")
44 .getChild("dependency")
45 .getChild("groupId")
46 .getValue(),
47 Matchers.equalTo("org.apache.xmlgraphics")
48 );
49 }
50
51
52
53
54
55 @Test
56 void preservesAllKeysOfNestedPropertiesInsideCollection() {
57 final Properties entry = new Properties();
58 entry.put("groupId", "org.example");
59 entry.put("artifactId", "sample");
60 final Properties config = new Properties();
61 config.put("items", List.of(entry));
62 MatcherAssert.assertThat(
63 "All keys of nested Properties must appear as children",
64 new MojoExecutor(null, null)
65 .toXppDom(config, "configuration")
66 .getChild("items").getChildCount(),
67 Matchers.equalTo(2)
68 );
69 }
70
71
72
73
74 @Test
75 void rendersCollectionOfStrings() {
76 final Properties config = new Properties();
77 config.put("patterns", Arrays.asList("alpha", "beta"));
78 MatcherAssert.assertThat(
79 "Collection elements cannot be merged into a single string",
80 new MojoExecutor(null, null)
81 .toXppDom(config, "configuration")
82 .getChild("patterns").getChildCount(),
83 Matchers.equalTo(2)
84 );
85 }
86
87
88
89
90 @Test
91 void rendersStringValueAsLeaf() {
92 final Properties config = new Properties();
93 config.put("encoding", "UTF-8");
94 MatcherAssert.assertThat(
95 "String value cannot be rendered as a leaf node",
96 new MojoExecutor(null, null)
97 .toXppDom(config, "configuration")
98 .getChild("encoding").getValue(),
99 Matchers.equalTo("UTF-8")
100 );
101 }
102
103
104
105
106 @Test
107 void rendersStringArrayAsRepeatedChildren() {
108 final Properties config = new Properties();
109 config.put("excludes", new String[] {"first", "second", "third"});
110 MatcherAssert.assertThat(
111 "String array entries cannot be rendered as separate children",
112 new MojoExecutor(null, null)
113 .toXppDom(config, "configuration")
114 .getChild("excludes").getChildCount(),
115 Matchers.equalTo(3)
116 );
117 }
118
119
120
121
122 @Test
123 void failsOnUnsupportedValueType() {
124 final Properties config = new Properties();
125 config.put("amount", 42);
126 Assertions.assertThrows(
127 IllegalArgumentException.class,
128 () -> new MojoExecutor(null, null)
129 .toXppDom(config, "configuration"),
130 "Unsupported value type must not be silently accepted"
131 );
132 }
133
134
135
136
137 @Test
138 void rendersArbitrarilyNestedProperties() {
139 final Properties java = new Properties();
140 java.put("version", "17");
141 final Properties rules = new Properties();
142 rules.put("requireJavaVersion", java);
143 final Properties config = new Properties();
144 config.put("rules", rules);
145 MatcherAssert.assertThat(
146 "Deeply nested Properties cannot be flattened",
147 new MojoExecutor(null, null)
148 .toXppDom(config, "configuration")
149 .getChild("rules")
150 .getChild("requireJavaVersion")
151 .getChild("version")
152 .getValue(),
153 Matchers.equalTo("17")
154 );
155 }
156
157
158
159
160
161
162 @Test
163 void rendersAttributeOfRootPlexusNode() {
164 MatcherAssert.assertThat(
165 "Attribute of the root node cannot be rendered",
166 MojoExecutorTest.rendered().getAttribute("combine.children"),
167 Matchers.equalTo("append")
168 );
169 }
170
171
172
173
174
175 @Test
176 void rendersAttributeOfNestedPlexusNode() {
177 MatcherAssert.assertThat(
178 "Attribute of the child node cannot be rendered",
179 MojoExecutorTest.rendered()
180 .getChild("dependency")
181 .getAttribute("implementation"),
182 Matchers.equalTo("org.example.Dep")
183 );
184 }
185
186
187
188
189
190 @Test
191 void rendersValueOfNestedPlexusNode() {
192 MatcherAssert.assertThat(
193 "Value of the child node cannot be rendered",
194 MojoExecutorTest.rendered().getChild("dependency").getValue(),
195 Matchers.equalTo("org.example:sample:1.0")
196 );
197 }
198
199 private static Xpp3Dom rendered() {
200 final Xpp3Dom child = new Xpp3Dom("dependency");
201 child.setAttribute("implementation", "org.example.Dep");
202 child.setValue("org.example:sample:1.0");
203 final Xpp3Dom dom = new Xpp3Dom("configuration");
204 dom.setAttribute("combine.children", "append");
205 dom.addChild(child);
206 return new MojoExecutor(null, null)
207 .toXppDom(new XmlPlexusConfiguration(dom));
208 }
209 }