1
2
3
4
5
6
7 package net.jangaroo.smartsprites.maven;
8
9 import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
10 import static org.junit.jupiter.api.Assertions.assertThrows;
11
12 import java.io.IOException;
13 import java.nio.charset.StandardCharsets;
14 import java.nio.file.Files;
15 import java.nio.file.Path;
16 import java.util.List;
17
18 import org.apache.maven.api.plugin.testing.InjectMojo;
19 import org.apache.maven.api.plugin.testing.MojoExtension;
20 import org.apache.maven.api.plugin.testing.MojoParameter;
21 import org.apache.maven.api.plugin.testing.MojoTest;
22 import org.apache.maven.plugin.MojoExecutionException;
23 import org.junit.jupiter.api.Test;
24 import org.junit.jupiter.api.io.TempDir;
25
26
27
28
29 @MojoTest
30 class SmartSpritesMojoTest {
31
32
33
34
35
36
37
38 @Test
39 @InjectMojo(goal = "smartsprites")
40 @MojoParameter(name = "skip", value = "true")
41 void testSkipExecution(SmartSpritesMojo mojo) {
42 assertDoesNotThrow(mojo::execute);
43 }
44
45
46
47
48
49
50
51 @Test
52 @InjectMojo(goal = "smartsprites")
53 @MojoParameter(name = "logLevel", value = "INVALID_LEVEL")
54 void testInvalidLogLevelThrowsException(SmartSpritesMojo mojo) {
55 assertThrows(MojoExecutionException.class, mojo::execute);
56 }
57
58
59
60
61
62
63
64 @Test
65 @InjectMojo(goal = "smartsprites")
66 @MojoParameter(name = "spritePngDepth", value = "INVALID_DEPTH")
67 void testInvalidSpritePngDepthThrowsException(SmartSpritesMojo mojo) {
68 assertThrows(MojoExecutionException.class, mojo::execute);
69 }
70
71
72
73
74
75
76
77 @Test
78 @InjectMojo(goal = "smartsprites")
79 @MojoParameter(name = "workingMode", value = "invalidMode")
80 void testInvalidWorkingModeThrowsException(SmartSpritesMojo mojo) {
81 assertThrows(MojoExecutionException.class, mojo::execute);
82 }
83
84
85
86
87
88
89
90
91
92
93 @Test
94 @InjectMojo(goal = "smartsprites")
95 void testRootDirPathDoesNotExistThrowsException(SmartSpritesMojo mojo) throws IllegalAccessException {
96 MojoExtension.setVariableValueToObject(mojo, "rootDirPath", Path.of("/non/existent/directory/path").toFile());
97 assertThrows(MojoExecutionException.class, mojo::execute);
98 }
99
100
101
102
103
104
105
106
107
108
109
110
111 @Test
112 @InjectMojo(goal = "smartsprites")
113 void testRootDirModeSucceedsWithEmptyDirectory(SmartSpritesMojo mojo, @TempDir Path tempDir)
114 throws IllegalAccessException {
115 MojoExtension.setVariableValueToObject(mojo, "rootDirPath", tempDir.toFile());
116 MojoExtension.setVariableValueToObject(mojo, "outputDirPath", tempDir.resolve("output").toFile());
117 assertDoesNotThrow(mojo::execute);
118 }
119
120
121
122
123
124
125
126
127
128
129 @Test
130 @InjectMojo(goal = "smartsprites")
131 @MojoParameter(name = "workingMode", value = "cssFilesMode")
132 void testCssFilesModeWithEmptyFilesListSucceeds(SmartSpritesMojo mojo) throws IllegalAccessException {
133 MojoExtension.setVariableValueToObject(mojo, "cssFiles", List.of());
134 assertDoesNotThrow(mojo::execute);
135 }
136
137
138
139
140
141
142
143
144
145
146 @Test
147 @InjectMojo(goal = "smartsprites")
148 void testCssFilesModeWithMissingCssFileThrowsException(SmartSpritesMojo mojo) throws IllegalAccessException {
149 MojoExtension.setVariableValueToObject(mojo, "workingMode", "cssFilesMode");
150 MojoExtension.setVariableValueToObject(mojo, "cssFiles", List.of(Path.of("/non/existent.css").toFile()));
151 assertThrows(MojoExecutionException.class, mojo::execute);
152 }
153
154
155
156
157
158
159
160
161
162
163 @Test
164 @InjectMojo(goal = "smartsprites")
165 void testCssFilesWithOutputDirModeWithoutRequiredValuesThrowsException(SmartSpritesMojo mojo)
166 throws IllegalAccessException {
167 MojoExtension.setVariableValueToObject(mojo, "workingMode", "cssFilesWithOutputDirMode");
168 MojoExtension.setVariableValueToObject(mojo, "cssFiles", List.of());
169 MojoExtension.setVariableValueToObject(mojo, "rootDirPath", null);
170 assertThrows(MojoExecutionException.class, mojo::execute);
171 }
172
173
174
175
176
177
178
179
180
181
182 @Test
183 @InjectMojo(goal = "smartsprites")
184 void testRootDirModeWithNullRootDirPathThrowsException(SmartSpritesMojo mojo) throws IllegalAccessException {
185 MojoExtension.setVariableValueToObject(mojo, "rootDirPath", null);
186 assertThrows(MojoExecutionException.class, mojo::execute);
187 }
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202 @Test
203 @InjectMojo(goal = "smartsprites")
204 void testCssFilesModeSucceedsWithExistingCssFileAndDocumentRoot(SmartSpritesMojo mojo, @TempDir Path tempDir)
205 throws IllegalAccessException, IOException {
206 Path cssFile = tempDir.resolve("style.css");
207 Files.writeString(cssFile, ".sample{color:black;}", StandardCharsets.UTF_8);
208
209 MojoExtension.setVariableValueToObject(mojo, "workingMode", "cssFilesMode");
210 MojoExtension.setVariableValueToObject(mojo, "cssFiles", List.of(cssFile.toFile()));
211 MojoExtension.setVariableValueToObject(mojo, "documentRootDirPath", tempDir.toFile());
212
213 assertDoesNotThrow(mojo::execute);
214 }
215 }