1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package com.github.hazendaz.maven.makeself_maven_plugin;
19
20 import java.lang.reflect.Field;
21 import java.lang.reflect.Method;
22
23 import org.apache.maven.plugin.logging.Log;
24 import org.junit.jupiter.api.Assertions;
25 import org.junit.jupiter.api.Test;
26 import org.junit.jupiter.api.extension.ExtendWith;
27 import org.mockito.Mock;
28 import org.mockito.Mockito;
29 import org.mockito.junit.jupiter.MockitoExtension;
30
31
32
33
34 @ExtendWith(MockitoExtension.class)
35 class HelpMojoTest {
36
37
38 @Mock
39 private Log log;
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54 private static void setField(final Object obj, final String fieldName, final Object value) throws Exception {
55 final Field field = obj.getClass().getDeclaredField(fieldName);
56 field.setAccessible(true);
57 field.set(obj, value);
58 }
59
60
61
62
63
64
65
66 @Test
67 void testExecuteDefault() throws Exception {
68 final HelpMojo mojo = new HelpMojo();
69 mojo.setLog(log);
70 Mockito.when(log.isInfoEnabled()).thenReturn(true);
71
72 mojo.execute();
73
74 Mockito.verify(log).isInfoEnabled();
75 Mockito.verify(log).info(Mockito.anyString());
76 }
77
78
79
80
81
82
83
84 @Test
85 void testExecuteWithDetailTrue() throws Exception {
86 final HelpMojo mojo = new HelpMojo();
87 mojo.setLog(log);
88 setField(mojo, "detail", true);
89 Mockito.when(log.isInfoEnabled()).thenReturn(true);
90
91 mojo.execute();
92
93 Mockito.verify(log).isInfoEnabled();
94 Mockito.verify(log).info(Mockito.anyString());
95 }
96
97
98
99
100
101
102
103 @Test
104 void testExecuteWithGoalMakeself() throws Exception {
105 final HelpMojo mojo = new HelpMojo();
106 mojo.setLog(log);
107 setField(mojo, "goal", "makeself");
108 Mockito.when(log.isInfoEnabled()).thenReturn(true);
109
110 mojo.execute();
111
112 Mockito.verify(log).isInfoEnabled();
113 }
114
115
116
117
118
119
120
121 @Test
122 void testExecuteWithGoalGit() throws Exception {
123 final HelpMojo mojo = new HelpMojo();
124 mojo.setLog(log);
125 setField(mojo, "goal", "git");
126 Mockito.when(log.isInfoEnabled()).thenReturn(true);
127
128 mojo.execute();
129
130 Mockito.verify(log).isInfoEnabled();
131 }
132
133
134
135
136
137
138
139 @Test
140 void testExecuteWithUnknownGoal() throws Exception {
141 final HelpMojo mojo = new HelpMojo();
142 mojo.setLog(log);
143 setField(mojo, "goal", "nonexistent-goal");
144 Mockito.when(log.isInfoEnabled()).thenReturn(true);
145
146 mojo.execute();
147
148 Mockito.verify(log).isInfoEnabled();
149 }
150
151
152
153
154
155
156
157 @Test
158 void testExecuteWithDetailAndGoal() throws Exception {
159 final HelpMojo mojo = new HelpMojo();
160 mojo.setLog(log);
161 setField(mojo, "detail", true);
162 setField(mojo, "goal", "makeself");
163 Mockito.when(log.isInfoEnabled()).thenReturn(true);
164
165 mojo.execute();
166
167 Mockito.verify(log).isInfoEnabled();
168 }
169
170
171
172
173
174
175
176 @Test
177 void testExecuteWithZeroLineLength() throws Exception {
178 final HelpMojo mojo = new HelpMojo();
179 mojo.setLog(log);
180 setField(mojo, "lineLength", 0);
181 Mockito.when(log.isInfoEnabled()).thenReturn(true);
182
183 mojo.execute();
184
185 Mockito.verify(log).warn("The parameter 'lineLength' should be positive, using '80' as default.");
186 }
187
188
189
190
191
192
193
194 @Test
195 void testExecuteWithZeroIndentSize() throws Exception {
196 final HelpMojo mojo = new HelpMojo();
197 mojo.setLog(log);
198 setField(mojo, "indentSize", 0);
199 Mockito.when(log.isInfoEnabled()).thenReturn(true);
200
201 mojo.execute();
202
203 Mockito.verify(log).warn("The parameter 'indentSize' should be positive, using '2' as default.");
204 }
205
206
207
208
209
210
211
212 @Test
213 void testExecuteWithNegativeLineLength() throws Exception {
214 final HelpMojo mojo = new HelpMojo();
215 mojo.setLog(log);
216 setField(mojo, "lineLength", -5);
217 Mockito.when(log.isInfoEnabled()).thenReturn(true);
218
219 mojo.execute();
220
221 Mockito.verify(log).warn("The parameter 'lineLength' should be positive, using '80' as default.");
222 }
223
224
225
226
227
228
229
230 @Test
231 void testExecuteInfoNotEnabledSkipsInfoLog() throws Exception {
232 final HelpMojo mojo = new HelpMojo();
233 mojo.setLog(log);
234 Mockito.when(log.isInfoEnabled()).thenReturn(false);
235
236 mojo.execute();
237
238 Mockito.verify(log).isInfoEnabled();
239 Mockito.verify(log, Mockito.never()).info(Mockito.anyString());
240 }
241
242
243
244
245
246
247
248 @Test
249 void testExecuteDetailWithGitGoal() throws Exception {
250 final HelpMojo mojo = new HelpMojo();
251 mojo.setLog(log);
252 setField(mojo, "detail", true);
253 setField(mojo, "goal", "git");
254 Mockito.when(log.isInfoEnabled()).thenReturn(true);
255
256 mojo.execute();
257
258 Mockito.verify(log).isInfoEnabled();
259 }
260
261
262
263
264
265
266
267 @Test
268 void testGetPropertyFromExpression() throws Exception {
269 final Method method = HelpMojo.class.getDeclaredMethod("getPropertyFromExpression", String.class);
270 method.setAccessible(true);
271
272 Assertions.assertAll(
273
274 () -> Assertions.assertEquals("myProp", method.invoke(null, "${myProp}")),
275
276 () -> Assertions.assertEquals("project.version", method.invoke(null, "${project.version}")),
277
278 () -> Assertions.assertNull(method.invoke(null, (Object) null)),
279
280 () -> Assertions.assertNull(method.invoke(null, "plain-value")),
281
282 () -> Assertions.assertNull(method.invoke(null, "${outer.${inner}}")),
283
284 () -> Assertions.assertNull(method.invoke(null, "${noClosure")));
285 }
286
287
288
289
290
291
292
293 @Test
294 void testIsNotEmpty() throws Exception {
295 final Method method = HelpMojo.class.getDeclaredMethod("isNotEmpty", String.class);
296 method.setAccessible(true);
297
298 Assertions.assertAll(() -> Assertions.assertFalse((boolean) method.invoke(null, (Object) null)),
299 () -> Assertions.assertFalse((boolean) method.invoke(null, "")),
300 () -> Assertions.assertTrue((boolean) method.invoke(null, "hello")));
301 }
302
303
304
305
306
307
308
309 @Test
310 void testRepeat() throws Exception {
311 final Method method = HelpMojo.class.getDeclaredMethod("repeat", String.class, int.class);
312 method.setAccessible(true);
313
314 Assertions.assertAll(() -> Assertions.assertEquals("", method.invoke(null, "ab", 0)),
315 () -> Assertions.assertEquals("ab", method.invoke(null, "ab", 1)),
316 () -> Assertions.assertEquals("ababab", method.invoke(null, "ab", 3)));
317 }
318
319
320
321
322
323
324
325 @Test
326 void testGetIndentLevel() throws Exception {
327 final Method method = HelpMojo.class.getDeclaredMethod("getIndentLevel", String.class);
328 method.setAccessible(true);
329
330 Assertions.assertAll(
331
332 () -> Assertions.assertEquals(0, method.invoke(null, "no indent")),
333
334 () -> Assertions.assertEquals(1, method.invoke(null, "\tsingle")),
335
336 () -> Assertions.assertEquals(2, method.invoke(null, "\t\tdouble")),
337
338 () -> Assertions.assertEquals(2, method.invoke(null, "\t \t")));
339 }
340
341
342
343
344
345
346
347 @Test
348 void testToLinesNonBreakingSpace() throws Exception {
349 final Method method = HelpMojo.class.getDeclaredMethod("toLines", java.util.List.class, String.class, int.class,
350 int.class);
351 method.setAccessible(true);
352
353 final java.util.List<String> lines = new java.util.ArrayList<>();
354
355 method.invoke(null, lines, "hello\u00A0world", 2, 80);
356
357 Assertions.assertFalse(lines.isEmpty(), "toLines should produce at least one output line");
358
359 Assertions.assertTrue(lines.stream().anyMatch(l -> l.contains(" ")),
360 "Output should contain a regular space where the non-breaking space was");
361 }
362
363 }