View Javadoc
1   /*
2    * SPDX-License-Identifier: Apache-2.0
3    * See LICENSE file for details.
4    *
5    * Copyright 2012-2026 Hazendaz
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   * Unit tests for {@link SmartSpritesMojo} using the Maven Plugin Testing Harness 3.5.1 (JUnit 5).
28   */
29  @MojoTest
30  class SmartSpritesMojoTest {
31  
32      /**
33       * When {@code skip=true} the mojo must return without executing any processing.
34       *
35       * @param mojo
36       *            the mojo
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       * An unrecognised log level must cause a {@link MojoExecutionException}.
47       *
48       * @param mojo
49       *            the mojo
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       * An unrecognised PNG depth value must cause a {@link MojoExecutionException}.
60       *
61       * @param mojo
62       *            the mojo
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       * An unrecognised working mode must cause a {@link MojoExecutionException}.
73       *
74       * @param mojo
75       *            the mojo
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       * Using rootDirMode with a rootDirPath that does not exist must cause a {@link MojoExecutionException}.
86       *
87       * @param mojo
88       *            the mojo
89       *
90       * @throws IllegalAccessException
91       *             the illegal access exception
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      * Using rootDirMode with an empty directory must complete successfully without processing any sprites.
102      *
103      * @param mojo
104      *            the mojo
105      * @param tempDir
106      *            the temp dir
107      *
108      * @throws IllegalAccessException
109      *             the illegal access exception
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      * Using cssFilesMode with an empty cssFiles list should succeed without processing.
122      *
123      * @param mojo
124      *            the mojo
125      *
126      * @throws IllegalAccessException
127      *             the illegal access exception
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      * Using cssFilesMode with a missing CSS file must fail.
139      *
140      * @param mojo
141      *            the mojo
142      *
143      * @throws IllegalAccessException
144      *             the illegal access exception
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      * Using cssFilesWithOutputDirMode without required configuration must fail.
156      *
157      * @param mojo
158      *            the mojo
159      *
160      * @throws IllegalAccessException
161      *             the illegal access exception
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      * rootDirMode with an explicit null rootDirPath must fail.
175      *
176      * @param mojo
177      *            the mojo
178      *
179      * @throws IllegalAccessException
180      *             the illegal access exception
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      * cssFilesMode with a real CSS file and documentRootDirPath set should succeed.
191      *
192      * @param mojo
193      *            the mojo
194      * @param tempDir
195      *            the temp dir
196      *
197      * @throws IllegalAccessException
198      *             the illegal access exception
199      * @throws IOException
200      *             the io exception
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 }