View Javadoc
1   /*
2    * SPDX-License-Identifier: Apache-2.0
3    * See LICENSE file for details.
4    *
5    * Copyright 2018-2026 Hazendaz
6    * Copyright 2011-2018 tunyk
7    */
8   package com.tunyk.mvn.plugins.htmlcompressor;
9   
10  import java.nio.file.Files;
11  import java.nio.file.Path;
12  
13  import org.apache.maven.plugin.MojoExecutionException;
14  import org.junit.jupiter.api.AfterAll;
15  import org.junit.jupiter.api.Assertions;
16  import org.junit.jupiter.api.BeforeAll;
17  import org.junit.jupiter.api.BeforeEach;
18  import org.junit.jupiter.api.Test;
19  import org.junit.jupiter.api.io.TempDir;
20  import org.slf4j.Logger;
21  import org.slf4j.LoggerFactory;
22  
23  /**
24   * The Class XmlCompressorMojoTest.
25   */
26  class XmlCompressorMojoTest {
27  
28      /** The Constant LOG. */
29      private static final Logger LOG = LoggerFactory.getLogger(XmlCompressorMojoTest.class);
30  
31      /**
32       * Sets the up class.
33       */
34      @BeforeAll
35      static void setUpClass() {
36          LOG.info("Setting up class...");
37      }
38  
39      /**
40       * Tear down class.
41       */
42      @AfterAll
43      static void tearDownClass() {
44          LOG.info("Mojo test finished.");
45      }
46  
47      /**
48       * Sets the up.
49       */
50      @BeforeEach
51      void setUp() {
52          LOG.info("Setting up data for testing...");
53      }
54  
55      /**
56       * Test execute with default settings produces output files.
57       *
58       * @throws MojoExecutionException
59       *             the mojo execution exception
60       */
61      @Test
62      void testExecute() throws MojoExecutionException {
63          LOG.info("Testing mojo execution...");
64  
65          XmlCompressorMojo xmlCompressorMojo = new XmlCompressorMojo();
66          xmlCompressorMojo.setSrcFolder("src/test/resources/xml");
67          xmlCompressorMojo.setTargetFolder("target/htmlcompressor/xml");
68          xmlCompressorMojo.execute();
69  
70          // Verify output files were produced
71          Assertions.assertTrue(Files.exists(Path.of("target/htmlcompressor/xml/file.xml")),
72                  "file.xml should be created in target folder");
73          Assertions.assertTrue(Files.exists(Path.of("target/htmlcompressor/xml/recursive/file.xml")),
74                  "recursive/file.xml should be created in target folder");
75  
76          LOG.info("Passed");
77      }
78  
79      /**
80       * Test execute when skip is true produces no output.
81       *
82       * @param tempDir
83       *            the temp dir provided by JUnit
84       *
85       * @throws MojoExecutionException
86       *             the mojo execution exception
87       */
88      @Test
89      void testExecuteSkip(@TempDir Path tempDir) throws MojoExecutionException {
90          LOG.info("Testing mojo execution with skip=true...");
91  
92          XmlCompressorMojo xmlCompressorMojo = new XmlCompressorMojo();
93          xmlCompressorMojo.setSrcFolder("src/test/resources/xml");
94          xmlCompressorMojo.setTargetFolder(tempDir.toString());
95          xmlCompressorMojo.setSkip(true);
96          xmlCompressorMojo.execute();
97  
98          // No files should be created in the target directory when skipped
99          Assertions.assertFalse(Files.exists(tempDir.resolve("file.xml")),
100                 "No output files should be created when skip=true");
101 
102         LOG.info("Passed");
103     }
104 
105     /**
106      * Test execute when enabled is false produces no output.
107      *
108      * @param tempDir
109      *            the temp dir provided by JUnit
110      *
111      * @throws MojoExecutionException
112      *             the mojo execution exception
113      */
114     @Test
115     void testExecuteDisabled(@TempDir Path tempDir) throws MojoExecutionException {
116         LOG.info("Testing mojo execution with enabled=false...");
117 
118         XmlCompressorMojo xmlCompressorMojo = new XmlCompressorMojo();
119         xmlCompressorMojo.setSrcFolder("src/test/resources/xml");
120         xmlCompressorMojo.setTargetFolder(tempDir.toString());
121         xmlCompressorMojo.setEnabled(false);
122         xmlCompressorMojo.execute();
123 
124         // No files should be created when compression is disabled
125         Assertions.assertFalse(Files.exists(tempDir.resolve("file.xml")),
126                 "No output files should be created when enabled=false");
127 
128         LOG.info("Passed");
129     }
130 
131     /**
132      * Test execute with deprecated fileExt parameter.
133      *
134      * @param tempDir
135      *            the temp dir provided by JUnit
136      *
137      * @throws MojoExecutionException
138      *             the mojo execution exception
139      */
140     @Test
141     @SuppressWarnings("deprecation")
142     void testExecuteWithDeprecatedFileExt(@TempDir Path tempDir) throws MojoExecutionException {
143         LOG.info("Testing mojo execution with deprecated fileExt parameter...");
144 
145         XmlCompressorMojo xmlCompressorMojo = new XmlCompressorMojo();
146         xmlCompressorMojo.setSrcFolder("src/test/resources/xml");
147         xmlCompressorMojo.setTargetFolder(tempDir.toString());
148         // Use deprecated fileExt (should fall through to fileExtensions when fileExtensions is null)
149         xmlCompressorMojo.setFileExt(new String[] { "xml" });
150         xmlCompressorMojo.execute();
151 
152         // Verify output files were produced using the deprecated extension list
153         Assertions.assertTrue(Files.exists(tempDir.resolve("file.xml")),
154                 "file.xml should be in output when using deprecated fileExt");
155 
156         LOG.info("Passed");
157     }
158 
159     /**
160      * Test execute compresses XML and removes comments.
161      *
162      * @param tempDir
163      *            the temp dir provided by JUnit
164      *
165      * @throws MojoExecutionException
166      *             the mojo execution exception
167      */
168     @Test
169     void testExecuteCompressesXml(@TempDir Path tempDir) throws MojoExecutionException {
170         LOG.info("Testing mojo execution compresses XML content...");
171 
172         XmlCompressorMojo xmlCompressorMojo = new XmlCompressorMojo();
173         xmlCompressorMojo.setSrcFolder("src/test/resources/xml");
174         xmlCompressorMojo.setTargetFolder(tempDir.toString());
175         xmlCompressorMojo.execute();
176 
177         // Verify compression occurred (comments should be removed by default)
178         Assertions.assertTrue(Files.exists(tempDir.resolve("file.xml")), "file.xml should exist in target");
179         Assertions.assertDoesNotThrow(() -> {
180             String compressedContent = Files.readString(tempDir.resolve("file.xml"));
181             Assertions.assertFalse(compressedContent.contains("<!--"), "Compressed XML should not contain comments");
182             Assertions.assertTrue(compressedContent.contains("sometag"),
183                     "Compressed XML should retain element content");
184         });
185 
186         LOG.info("Passed");
187     }
188 
189     /**
190      * Test XmlCompressorMojo getters and setters for all configurable parameters.
191      */
192     @Test
193     @SuppressWarnings("deprecation")
194     void testXmlMojoGettersSetters() {
195         LOG.info("Testing XmlCompressorMojo getters and setters...");
196 
197         XmlCompressorMojo mojo = new XmlCompressorMojo();
198 
199         // Skip
200         Assertions.assertFalse(mojo.isSkip());
201         mojo.setSkip(true);
202         Assertions.assertTrue(mojo.isSkip());
203 
204         // Enabled
205         Assertions.assertTrue(mojo.getEnabled());
206         mojo.setEnabled(false);
207         Assertions.assertFalse(mojo.getEnabled());
208 
209         // Remove comments
210         Assertions.assertTrue(mojo.getRemoveComments());
211         mojo.setRemoveComments(false);
212         Assertions.assertFalse(mojo.getRemoveComments());
213 
214         // Remove intertag spaces
215         Assertions.assertTrue(mojo.getRemoveIntertagSpaces());
216         mojo.setRemoveIntertagSpaces(false);
217         Assertions.assertFalse(mojo.getRemoveIntertagSpaces());
218 
219         // Source folder
220         mojo.setSrcFolder("src/main/resources");
221         Assertions.assertEquals("src/main/resources", mojo.getSrcFolder());
222 
223         // Target folder
224         mojo.setTargetFolder("target/classes");
225         Assertions.assertEquals("target/classes", mojo.getTargetFolder());
226 
227         // File extensions
228         mojo.setFileExtensions(new String[] { "xml", "xsd" });
229         Assertions.assertArrayEquals(new String[] { "xml", "xsd" }, mojo.getFileExtensions());
230 
231         // Deprecated fileExt getter/setter
232         mojo.setFileExt(new String[] { "xml" });
233         Assertions.assertArrayEquals(new String[] { "xml" }, mojo.getFileExt());
234 
235         LOG.info("Passed");
236     }
237 
238     /**
239      * Test execute with remove comments disabled retains comments.
240      *
241      * @param tempDir
242      *            the temp dir provided by JUnit
243      *
244      * @throws MojoExecutionException
245      *             the mojo execution exception
246      */
247     @Test
248     void testExecuteWithCommentsRetained(@TempDir Path tempDir) throws MojoExecutionException {
249         LOG.info("Testing mojo execution with removeComments=false...");
250 
251         XmlCompressorMojo xmlCompressorMojo = new XmlCompressorMojo();
252         xmlCompressorMojo.setSrcFolder("src/test/resources/xml");
253         xmlCompressorMojo.setTargetFolder(tempDir.toString());
254         xmlCompressorMojo.setRemoveComments(false);
255         xmlCompressorMojo.execute();
256 
257         Assertions.assertTrue(Files.exists(tempDir.resolve("file.xml")), "file.xml should exist in target");
258         Assertions.assertDoesNotThrow(() -> {
259             String content = Files.readString(tempDir.resolve("file.xml"));
260             // With removeComments=false, comments should be preserved
261             Assertions.assertTrue(content.contains("<!--"),
262                     "XML comments should be retained when removeComments=false");
263         });
264 
265         LOG.info("Passed");
266     }
267 }