1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
33
34 class FileToolTest {
35
36
37 private static final Logger LOG = LoggerFactory.getLogger(FileToolTest.class);
38
39
40
41
42 @BeforeAll
43 static void setUpClass() {
44 LOG.info("Setting up class...");
45 }
46
47
48
49
50 @AfterAll
51 static void tearDownClass() {
52 LOG.info("Test finished.");
53 }
54
55
56
57
58 @BeforeEach
59 void setUp() {
60 LOG.info("Setting up data for testing...");
61 }
62
63
64
65
66
67
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
81
82
83
84
85 LOG.info("Passed");
86 }
87
88
89
90
91
92
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
118
119 LOG.info("Passed");
120 }
121
122
123
124
125
126
127
128
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
144
145 LOG.info("Passed");
146 }
147 }