1
2
3
4
5
6
7 package com.github.hazendaz.maven.smartsprites_maven_plugin;
8
9 import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
10 import static org.junit.jupiter.api.Assertions.assertEquals;
11 import static org.junit.jupiter.api.Assertions.assertNull;
12 import static org.junit.jupiter.api.Assertions.assertThrows;
13 import static org.junit.jupiter.api.Assertions.assertTrue;
14
15 import java.lang.reflect.Field;
16 import java.lang.reflect.Method;
17 import java.util.List;
18
19 import javax.xml.parsers.DocumentBuilderFactory;
20
21 import org.apache.maven.plugin.MojoExecutionException;
22 import org.junit.jupiter.api.Test;
23 import org.w3c.dom.Document;
24 import org.w3c.dom.Element;
25 import org.w3c.dom.Node;
26
27
28
29
30 public class HelpMojoTest {
31
32
33
34
35 @Test
36 void testExecuteWithDefaultParameters() {
37 HelpMojo mojo = new HelpMojo();
38 assertDoesNotThrow(mojo::execute);
39 }
40
41
42
43
44
45
46
47 @Test
48 void testExecuteWithDetailAndGoal() throws ReflectiveOperationException {
49 HelpMojo mojo = new HelpMojo();
50 setField(mojo, "detail", true);
51 setField(mojo, "goal", "smartsprites");
52 setField(mojo, "lineLength", 64);
53 setField(mojo, "indentSize", 2);
54
55 assertDoesNotThrow(mojo::execute);
56 }
57
58
59
60
61
62
63
64 @Test
65 void testGetSingleChildThrowsForMissingAndMultiple() throws Exception {
66 Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
67 Element root = document.createElement("root");
68 document.appendChild(root);
69 root.appendChild(document.createElement("a"));
70 root.appendChild(document.createElement("a"));
71
72 Method getSingleChild = method("getSingleChild", Node.class, String.class);
73 assertThrows(MojoExecutionException.class, () -> invoke(getSingleChild, null, root, "missing"));
74 assertThrows(MojoExecutionException.class, () -> invoke(getSingleChild, null, root, "a"));
75 }
76
77
78
79
80
81
82
83 @Test
84 void testFindSingleChildReturnsNullWhenMissing() throws Exception {
85 Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
86 Element root = document.createElement("root");
87 document.appendChild(root);
88
89 Method findSingleChild = method("findSingleChild", Node.class, String.class);
90 Object child = invoke(findSingleChild, null, root, "absent");
91
92 assertNull(child);
93 }
94
95
96
97
98
99
100
101 @Test
102 void testGetPropertyFromExpressionVariants() throws Exception {
103 Method method = method("getPropertyFromExpression", String.class);
104 assertEquals("demo.property", invoke(method, null, "${demo.property}"));
105 assertNull(invoke(method, null, "${a${b}}"));
106 assertNull(invoke(method, null, "plain-value"));
107 }
108
109
110
111
112
113
114
115 @Test
116 @SuppressWarnings("unchecked")
117 void testToLinesWrapsAndNormalizesWhitespace() throws Exception {
118 Method toLines = method("toLines", String.class, int.class, int.class, int.class);
119 List<String> lines = (List<String>) invoke(toLines, null, "\tword\u00A0word longlongtoken", 1, 2, 12);
120
121 assertTrue(lines.size() > 1);
122 assertTrue(lines.stream().noneMatch(line -> line.contains("\u00A0")));
123 }
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138 private static Method method(String name, Class<?>... parameterTypes) throws ReflectiveOperationException {
139 Method method = HelpMojo.class.getDeclaredMethod(name, parameterTypes);
140 method.setAccessible(true);
141 return method;
142 }
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159 private static Object invoke(Method method, Object target, Object... args) throws Exception {
160 try {
161 return method.invoke(target, args);
162 } catch (ReflectiveOperationException e) {
163 if (e.getCause() instanceof Exception) {
164 throw (Exception) e.getCause();
165 }
166 throw e;
167 }
168 }
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183 private static void setField(Object target, String fieldName, Object value) throws ReflectiveOperationException {
184 Field field = target.getClass().getDeclaredField(fieldName);
185 field.setAccessible(true);
186 field.set(target, value);
187 }
188 }