View Javadoc
1   /*
2    *    Copyright 2011-2025 the original author or authors.
3    *
4    *    Licensed under the Apache License, Version 2.0 (the "License");
5    *    you may not use this file except in compliance with the License.
6    *    You may obtain a copy of the License at
7    *
8    *       https://www.apache.org/licenses/LICENSE-2.0
9    *
10   *    Unless required by applicable law or agreed to in writing, software
11   *    distributed under the License is distributed on an "AS IS" BASIS,
12   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *    See the License for the specific language governing permissions and
14   *    limitations under the License.
15   */
16  package com.tunyk.mvn.plugins.htmlcompressor;
17  
18  import java.io.IOException;
19  import java.util.HashMap;
20  import java.util.Map;
21  
22  import org.json.JSONException;
23  import org.junit.jupiter.api.AfterAll;
24  import org.junit.jupiter.api.Assertions;
25  import org.junit.jupiter.api.BeforeAll;
26  import org.junit.jupiter.api.BeforeEach;
27  import org.junit.jupiter.api.Test;
28  import org.slf4j.Logger;
29  import org.slf4j.LoggerFactory;
30  
31  /**
32   * The Class FileToolTest.
33   */
34  class FileToolTest {
35  
36      /** The Constant LOG. */
37      private static final Logger LOG = LoggerFactory.getLogger(FileToolTest.class);
38  
39      /**
40       * Sets the up class.
41       */
42      @BeforeAll
43      static void setUpClass() {
44          LOG.info("Setting up class...");
45      }
46  
47      /**
48       * Tear down class.
49       */
50      @AfterAll
51      static void tearDownClass() {
52          LOG.info("Test finished.");
53      }
54  
55      /**
56       * Sets the up.
57       */
58      @BeforeEach
59      void setUp() {
60          LOG.info("Setting up data for testing...");
61      }
62  
63      /**
64       * Test get files.
65       *
66       * @throws IOException
67       *             Signals that an I/O exception has occurred.
68       */
69      @Test
70      void testGetFiles() throws IOException {
71          LOG.info("Testing getFiles method...");
72  
73          FileTool fileTool = new FileTool("src/test/resources/html", new String[] { "htm", "html" }, true);
74          Map<String, String> map = fileTool.getFiles();
75  
76          Assertions.assertTrue(map.containsKey("templates/Template1.html"));
77          Assertions.assertTrue(map.containsKey("templates/Template2.html"));
78          Assertions.assertTrue(map.containsKey("templates/recursive/Template.html"));
79  
80          // TODO: check if any other files aren't included
81          // TODO: check if file ext filter works
82          // TODO: check if recursion is forking
83          // TODO: test file encoding
84  
85          LOG.info("Passed");
86      }
87  
88      /**
89       * Test write files.
90       *
91       * @throws IOException
92       *             Signals that an I/O exception has occurred.
93       */
94      @Test
95      void testWriteFiles() throws IOException {
96          LOG.info("Testing writeFiles method...");
97  
98          String targetDir = "target/test/filetool";
99          FileTool fileTool = new FileTool(targetDir, new String[] { "htm", "html" }, true);
100         Map<String, String> map = new HashMap<String, String>();
101         map.put("file.html", "root file");
102         map.put("/file2.html", "another root file");
103         map.put("file3.html/", "another root file like folder");
104         map.put("/template/file.html", "template file");
105         map.put("template/file01.html", "template file 01");
106         map.put("/template/subfolder/file.html", "template subfolder file");
107         fileTool.writeFiles(map, targetDir);
108 
109         Map<String, String> files = fileTool.getFiles();
110         Assertions.assertTrue(files.containsKey("file.html"));
111         Assertions.assertTrue(files.containsKey("file2.html"));
112         Assertions.assertTrue(files.containsKey("file3.html"));
113         Assertions.assertTrue(files.containsKey("template/file.html"));
114         Assertions.assertTrue(files.containsKey("template/file01.html"));
115         Assertions.assertTrue(files.containsKey("template/subfolder/file.html"));
116 
117         // TODO: check file exists and its contents is written correctly
118 
119         LOG.info("Passed");
120     }
121 
122     /**
123      * Test write to json file.
124      *
125      * @throws IOException
126      *             Signals that an I/O exception has occurred.
127      * @throws JSONException
128      *             the JSON exception
129      */
130     @Test
131     void testWriteToJsonFile() throws IOException, JSONException {
132         LOG.info("Testing writeToJsonFile method...");
133 
134         String targetDir = "target/test/filetool/";
135         String targetFile = targetDir + "json.js";
136         FileTool fileTool = new FileTool(targetDir, new String[] { "htm", "html" }, true);
137         Map<String, String> map = new HashMap<String, String>();
138         map.put("file.html", "root file");
139         map.put("template/file.html", "template file");
140         map.put("template/subfolder/file.html", "template subfolder file");
141         fileTool.writeToJsonFile(map, targetFile, "var templates = %s;");
142 
143         // TODO: check if file created and contents is right
144 
145         LOG.info("Passed");
146     }
147 }