View Javadoc
1   /*
2    * Copyright (c) 2011-2023 Alex Tunyk <alex at tunyk.com>.
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   * See the NOTICE file distributed with this work for additional information
17   * regarding copyright ownership.
18   */
19  package com.tunyk.mvn.plugins.htmlcompressor;
20  
21  import java.io.IOException;
22  import java.util.HashMap;
23  import java.util.Map;
24  
25  import org.json.JSONException;
26  import org.junit.jupiter.api.AfterAll;
27  import org.junit.jupiter.api.Assertions;
28  import org.junit.jupiter.api.BeforeAll;
29  import org.junit.jupiter.api.BeforeEach;
30  import org.junit.jupiter.api.Test;
31  import org.slf4j.Logger;
32  import org.slf4j.LoggerFactory;
33  
34  /**
35   * The Class FileToolTest.
36   */
37  class FileToolTest {
38  
39      /** The Constant LOG. */
40      private static final Logger LOG = LoggerFactory.getLogger(FileToolTest.class);
41  
42      /**
43       * Sets the up class.
44       */
45      @BeforeAll
46      static void setUpClass() {
47          LOG.info("Setting up class...");
48      }
49  
50      /**
51       * Tear down class.
52       */
53      @AfterAll
54      static void tearDownClass() {
55          LOG.info("Test finished.");
56      }
57  
58      /**
59       * Sets the up.
60       */
61      @BeforeEach
62      void setUp() {
63          LOG.info("Setting up data for testing...");
64      }
65  
66      /**
67       * Test get files.
68       *
69       * @throws IOException
70       *             Signals that an I/O exception has occurred.
71       */
72      @Test
73      void testGetFiles() throws IOException {
74          LOG.info("Testing getFiles method...");
75  
76          FileTool fileTool = new FileTool("src/test/resources/html", new String[] { "htm", "html" }, true);
77          Map<String, String> map = fileTool.getFiles();
78  
79          Assertions.assertTrue(map.containsKey("templates/Template1.html"));
80          Assertions.assertTrue(map.containsKey("templates/Template2.html"));
81          Assertions.assertTrue(map.containsKey("templates/recursive/Template.html"));
82  
83          // TODO: check if any other files aren't included
84          // TODO: check if file ext filter works
85          // TODO: check if recursion is forking
86          // TODO: test file encoding
87  
88          LOG.info("Passed");
89      }
90  
91      /**
92       * Test write files.
93       *
94       * @throws IOException
95       *             Signals that an I/O exception has occurred.
96       */
97      @Test
98      void testWriteFiles() throws IOException {
99          LOG.info("Testing writeFiles method...");
100 
101         String targetDir = "target/test/filetool";
102         FileTool fileTool = new FileTool(targetDir, new String[] { "htm", "html" }, true);
103         Map<String, String> map = new HashMap<String, String>();
104         map.put("file.html", "root file");
105         map.put("/file2.html", "another root file");
106         map.put("file3.html/", "another root file like folder");
107         map.put("/template/file.html", "template file");
108         map.put("template/file01.html", "template file 01");
109         map.put("/template/subfolder/file.html", "template subfolder file");
110         fileTool.writeFiles(map, targetDir);
111 
112         Map<String, String> files = fileTool.getFiles();
113         Assertions.assertTrue(files.containsKey("file.html"));
114         Assertions.assertTrue(files.containsKey("file2.html"));
115         Assertions.assertTrue(files.containsKey("file3.html"));
116         Assertions.assertTrue(files.containsKey("template/file.html"));
117         Assertions.assertTrue(files.containsKey("template/file01.html"));
118         Assertions.assertTrue(files.containsKey("template/subfolder/file.html"));
119 
120         // TODO: check file exists and its contents is written correctly
121 
122         LOG.info("Passed");
123     }
124 
125     /**
126      * Test write to json file.
127      *
128      * @throws IOException
129      *             Signals that an I/O exception has occurred.
130      * @throws JSONException
131      *             the JSON exception
132      */
133     @Test
134     void testWriteToJsonFile() throws IOException, JSONException {
135         LOG.info("Testing writeToJsonFile method...");
136 
137         String targetDir = "target/test/filetool/";
138         String targetFile = targetDir + "json.js";
139         FileTool fileTool = new FileTool(targetDir, new String[] { "htm", "html" }, true);
140         Map<String, String> map = new HashMap<String, String>();
141         map.put("file.html", "root file");
142         map.put("template/file.html", "template file");
143         map.put("template/subfolder/file.html", "template subfolder file");
144         fileTool.writeToJsonFile(map, targetFile, "var templates = %s;");
145 
146         // TODO: check if file created and contents is right
147 
148         LOG.info("Passed");
149     }
150 }