1
2
3
4
5
6
7 package com.github.hazendaz.maven.yuicompressor_maven_plugin;
8
9 import java.lang.reflect.Field;
10
11 import org.apache.maven.plugin.MojoExecutionException;
12 import org.junit.jupiter.api.Assertions;
13 import org.junit.jupiter.api.Test;
14
15
16
17
18
19
20
21
22 class HelpMojoTest {
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43 private HelpMojo createMojo(String goal, boolean detail, int lineLength, int indentSize) throws Exception {
44 final var mojo = new HelpMojo();
45 setField(mojo, "goal", goal);
46 setField(mojo, "detail", detail);
47 setField(mojo, "lineLength", lineLength);
48 setField(mojo, "indentSize", indentSize);
49 return mojo;
50 }
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65 private static void setField(Object target, String fieldName, Object value) throws Exception {
66 Class<?> clazz = target.getClass();
67 Field field = null;
68 while (clazz != null) {
69 try {
70 field = clazz.getDeclaredField(fieldName);
71 break;
72 } catch (NoSuchFieldException e) {
73 clazz = clazz.getSuperclass();
74 }
75 }
76 if (field == null) {
77 throw new NoSuchFieldException(fieldName);
78 }
79 field.setAccessible(true);
80 field.set(target, value);
81 }
82
83
84
85
86
87
88
89
90
91 @Test
92 void testExecute_defaultParams_listsAllGoals() throws Exception {
93 final var mojo = createMojo(null, false, 80, 2);
94 Assertions.assertDoesNotThrow(mojo::execute, "execute() with default params should not throw");
95 }
96
97
98
99
100
101
102
103 @Test
104 void testExecute_withSpecificGoal_compress() throws Exception {
105 final var mojo = createMojo("compress", false, 80, 2);
106 Assertions.assertDoesNotThrow(mojo::execute, "execute() with goal='compress' should not throw");
107 }
108
109
110
111
112
113
114
115 @Test
116 void testExecute_withSpecificGoal_jslint() throws Exception {
117 final var mojo = createMojo("jslint", false, 80, 2);
118 Assertions.assertDoesNotThrow(mojo::execute, "execute() with goal='jslint' should not throw");
119 }
120
121
122
123
124
125
126
127 @Test
128 void testExecute_withSpecificGoal_help() throws Exception {
129 final var mojo = createMojo("help", false, 80, 2);
130 Assertions.assertDoesNotThrow(mojo::execute, "execute() with goal='help' should not throw");
131 }
132
133
134
135
136
137
138
139 @Test
140 void testExecute_detailTrue_allGoals() throws Exception {
141 final var mojo = createMojo(null, true, 80, 2);
142 Assertions.assertDoesNotThrow(mojo::execute, "execute() with detail=true should not throw");
143 }
144
145
146
147
148
149
150
151 @Test
152 void testExecute_detailTrue_specificGoal() throws Exception {
153 final var mojo = createMojo("compress", true, 80, 2);
154 Assertions.assertDoesNotThrow(mojo::execute, "execute() with detail=true and goal='compress' should not throw");
155 }
156
157
158
159
160
161
162
163 @Test
164 void testExecute_detailTrue_jslintGoal() throws Exception {
165 final var mojo = createMojo("jslint", true, 80, 2);
166 Assertions.assertDoesNotThrow(mojo::execute, "execute() with detail=true and goal='jslint' should not throw");
167 }
168
169
170
171
172
173
174
175 @Test
176 void testExecute_invalidLineLength_usesDefault() throws Exception {
177 final var mojo = createMojo(null, false, 0, 2);
178 Assertions.assertDoesNotThrow(mojo::execute, "execute() with lineLength=0 should not throw");
179 }
180
181
182
183
184
185
186
187 @Test
188 void testExecute_invalidIndentSize_usesDefault() throws Exception {
189 final var mojo = createMojo(null, false, 80, 0);
190 Assertions.assertDoesNotThrow(mojo::execute, "execute() with indentSize=0 should not throw");
191 }
192
193
194
195
196
197
198
199 @Test
200 void testExecute_bothInvalid_usesDefaults() throws Exception {
201 final var mojo = createMojo(null, false, -1, -1);
202 Assertions.assertDoesNotThrow(mojo::execute,
203 "execute() with both lineLength and indentSize invalid should not throw");
204 }
205
206
207
208
209
210
211
212 @Test
213 void testExecute_unknownGoal_noOutput() throws Exception {
214 final var mojo = createMojo("nonexistent-goal", false, 80, 2);
215 Assertions.assertDoesNotThrow(mojo::execute, "execute() with unknown goal should not throw");
216 }
217
218
219
220
221
222
223
224 @Test
225 void testExecute_shortLineLength_wrapsLines() throws Exception {
226 final var mojo = createMojo(null, false, 40, 2);
227 Assertions.assertDoesNotThrow(mojo::execute, "execute() with short lineLength should not throw");
228 }
229
230
231
232
233
234
235
236 @Test
237 void testExecute_largeIndentSize_detail() throws Exception {
238 final var mojo = createMojo("compress", true, 120, 4);
239 Assertions.assertDoesNotThrow(mojo::execute, "execute() with large indent and detail should not throw");
240 }
241
242
243
244
245
246
247
248 @Test
249 void testExecute_emptyGoal_listsAllGoals() throws Exception {
250 final var mojo = createMojo("", false, 80, 2);
251 Assertions.assertDoesNotThrow(mojo::execute, "execute() with empty goal should not throw");
252 }
253
254
255
256
257
258
259
260 @Test
261 void testExecute_pluginHelpXmlPresent_doesNotThrow() throws MojoExecutionException {
262 final var mojo = new HelpMojo();
263 Assertions.assertDoesNotThrow(mojo::execute,
264 "Normal execute should succeed when plugin-help.xml is on classpath");
265 }
266 }