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.charset.StandardCharsets;
11  import java.nio.file.Files;
12  import java.nio.file.Path;
13  
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 XmlCompressorTest.
25   */
26  class XmlCompressorTest {
27  
28      /** The Constant LOG. */
29      private static final Logger LOG = LoggerFactory.getLogger(XmlCompressorTest.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("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 compress with default settings produces output files.
57       *
58       * @throws Exception
59       *             the exception
60       */
61      @Test
62      void testCompress() throws Exception {
63          LOG.info("Testing compress method...");
64  
65          XmlCompressor xmlCompressor = new XmlCompressor("src/test/resources/xml", "target/test/xmlcompressor/0");
66          xmlCompressor.compress();
67  
68          // Verify output files were created
69          Assertions.assertTrue(Files.exists(Path.of("target/test/xmlcompressor/0/file.xml")),
70                  "file.xml should be created in output");
71          Assertions.assertTrue(Files.exists(Path.of("target/test/xmlcompressor/0/recursive/file.xml")),
72                  "recursive/file.xml should be created in output");
73  
74          // Verify content is compressed (comments removed by default)
75          String compressed = Files.readString(Path.of("target/test/xmlcompressor/0/file.xml"));
76          Assertions.assertFalse(compressed.contains("<!--"), "Compressed output should not contain XML comments");
77          Assertions.assertTrue(compressed.contains("sometag"),
78                  "Compressed output should retain the XML element content");
79  
80          xmlCompressor = new XmlCompressor("src/test/resources/xml", "target/test/xmlcompressor/1");
81          com.googlecode.htmlcompressor.compressor.XmlCompressor xmlCompressorHandler = new com.googlecode.htmlcompressor.compressor.XmlCompressor();
82          xmlCompressorHandler.setEnabled(false);
83          xmlCompressor.setXmlCompressor(xmlCompressorHandler);
84          xmlCompressor.compress();
85  
86          // When compressor is disabled, output should match input (content not compressed)
87          String disabledOutput = Files.readString(Path.of("target/test/xmlcompressor/1/file.xml"));
88          Assertions.assertTrue(disabledOutput.contains("<!--"),
89                  "Output with disabled compressor should retain XML comments");
90  
91          LOG.info("Passed");
92      }
93  
94      /**
95       * Test compress reduces file size compared to input.
96       *
97       * @param tempDir
98       *            the temp dir provided by JUnit
99       *
100      * @throws Exception
101      *             the exception
102      */
103     @Test
104     void testCompressReducesFileSize(@TempDir Path tempDir) throws Exception {
105         LOG.info("Testing that compress reduces file size...");
106 
107         XmlCompressor xmlCompressor = new XmlCompressor("src/test/resources/xml", tempDir.toString());
108         xmlCompressor.compress();
109 
110         Path originalFile = Path.of("src/test/resources/xml/file.xml");
111         Path compressedFile = tempDir.resolve("file.xml");
112 
113         long originalSize = Files.size(originalFile);
114         long compressedSize = Files.size(compressedFile);
115 
116         Assertions.assertTrue(compressedSize < originalSize,
117                 "Compressed file should be smaller than the original (original: " + originalSize + "B, compressed: "
118                         + compressedSize + "B)");
119 
120         LOG.info("Passed");
121     }
122 
123     /**
124      * Test compress with custom file extensions processes only specified types.
125      *
126      * @param tempDir
127      *            the temp dir provided by JUnit
128      *
129      * @throws Exception
130      *             the exception
131      */
132     @Test
133     void testCompressWithCustomFileExtensions(@TempDir Path tempDir) throws Exception {
134         LOG.info("Testing compress with custom file extensions...");
135 
136         // Create test files with different extensions
137         Path srcDir = tempDir.resolve("src");
138         Files.createDirectories(srcDir);
139         Files.writeString(srcDir.resolve("data.xml"),
140                 "<?xml version=\"1.0\"?>\n<!-- comment -->\n<root><child/></root>");
141         Files.writeString(srcDir.resolve("config.cfg"), "setting=value");
142 
143         Path targetDir = tempDir.resolve("target");
144         XmlCompressor xmlCompressor = new XmlCompressor(srcDir.toString(), targetDir.toString());
145         xmlCompressor.setFileExtensions(new String[] { "xml" });
146         xmlCompressor.compress();
147 
148         // Only the .xml file should be processed
149         Assertions.assertTrue(Files.exists(targetDir.resolve("data.xml")), "XML file should be in output");
150         Assertions.assertFalse(Files.exists(targetDir.resolve("config.cfg")),
151                 "CFG file should not be in output with XML-only filter");
152 
153         LOG.info("Passed");
154     }
155 
156     /**
157      * Test XmlCompressor getters and setters.
158      */
159     @Test
160     void testXmlCompressorGettersSetters() {
161         LOG.info("Testing XmlCompressor getters and setters...");
162 
163         XmlCompressor xmlCompressor = new XmlCompressor("src/test/resources/xml", "target/test");
164 
165         // Test srcDirPath getter/setter
166         Assertions.assertEquals("src/test/resources/xml", xmlCompressor.getSrcDirPath());
167         xmlCompressor.setSrcDirPath("src/main/resources");
168         Assertions.assertEquals("src/main/resources", xmlCompressor.getSrcDirPath());
169 
170         // Test targetDirPath getter/setter
171         Assertions.assertEquals("target/test", xmlCompressor.getTargetDirPath());
172         xmlCompressor.setTargetDirPath("target/other");
173         Assertions.assertEquals("target/other", xmlCompressor.getTargetDirPath());
174 
175         // Test fileEncoding getter/setter
176         xmlCompressor.setFileEncoding(StandardCharsets.UTF_8);
177         Assertions.assertEquals(StandardCharsets.UTF_8, xmlCompressor.getFileEncoding());
178 
179         // Test fileExtensions getter/setter
180         xmlCompressor.setFileExtensions(new String[] { "xml", "xsd" });
181         Assertions.assertArrayEquals(new String[] { "xml", "xsd" }, xmlCompressor.getFileExtensions());
182 
183         // Test xmlCompressor handler getter/setter
184         com.googlecode.htmlcompressor.compressor.XmlCompressor handler = new com.googlecode.htmlcompressor.compressor.XmlCompressor();
185         xmlCompressor.setXmlCompressor(handler);
186         Assertions.assertSame(handler, xmlCompressor.getXmlCompressor());
187 
188         LOG.info("Passed");
189     }
190 
191     /**
192      * Test compress with comments removed produces smaller output.
193      *
194      * @param tempDir
195      *            the temp dir provided by JUnit
196      *
197      * @throws Exception
198      *             the exception
199      */
200     @Test
201     void testCompressRemovesComments(@TempDir Path tempDir) throws Exception {
202         LOG.info("Testing that compress removes XML comments...");
203 
204         Path srcDir = tempDir.resolve("src");
205         Files.createDirectories(srcDir);
206         String xmlWithComments = "<?xml version=\"1.0\"?>\n<!-- This is a long comment that should be removed -->\n<root>\n  <!-- Another comment -->\n  <child>value</child>\n</root>";
207         Files.writeString(srcDir.resolve("test.xml"), xmlWithComments);
208 
209         Path targetDir = tempDir.resolve("target");
210         XmlCompressor xmlCompressor = new XmlCompressor(srcDir.toString(), targetDir.toString());
211         xmlCompressor.compress();
212 
213         String compressed = Files.readString(targetDir.resolve("test.xml"));
214         Assertions.assertFalse(compressed.contains("<!--"), "Comments should be removed by default");
215         Assertions.assertTrue(compressed.contains("child"), "Element content should be preserved");
216         Assertions.assertTrue(compressed.contains("value"), "Text content should be preserved");
217 
218         LOG.info("Passed");
219     }
220 }