1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package net.alchim31.maven.yuicompressor;
21
22 import java.io.File;
23 import java.io.IOException;
24 import java.nio.charset.StandardCharsets;
25 import java.nio.file.Files;
26 import java.nio.file.StandardOpenOption;
27 import java.util.Arrays;
28 import java.util.Collection;
29 import java.util.HashSet;
30 import java.util.List;
31
32 import org.codehaus.plexus.build.DefaultBuildContext;
33 import org.junit.jupiter.api.Assertions;
34 import org.junit.jupiter.api.Test;
35 import org.junit.jupiter.api.io.TempDir;
36
37
38
39
40
41 public class AggregationTestCase {
42
43
44 @TempDir
45 File dir;
46
47
48 DefaultBuildContext defaultBuildContext = new DefaultBuildContext();
49
50
51
52
53
54
55
56 @Test
57 void test0to1() throws IOException {
58 Aggregation target = new Aggregation();
59 target.setOutput(dir.toPath().resolve("output.js").toFile());
60
61 Assertions.assertFalse(target.getOutput().exists());
62 target.run(null, defaultBuildContext);
63 Assertions.assertFalse(target.getOutput().exists());
64
65 target.setIncludes(new String[] {});
66 Assertions.assertFalse(target.getOutput().exists());
67 target.run(null, defaultBuildContext);
68 Assertions.assertFalse(target.getOutput().exists());
69
70 target.setIncludes(new String[] { "**/*.js" });
71 Assertions.assertFalse(target.getOutput().exists());
72 target.run(null, defaultBuildContext);
73 Assertions.assertFalse(target.getOutput().exists());
74 }
75
76
77
78
79
80
81
82 @Test
83 void test1to1() throws IOException {
84 File f1 = dir.toPath().resolve("01.js").toFile();
85 Files.write(f1.toPath(), "1".getBytes(StandardCharsets.UTF_8), StandardOpenOption.CREATE);
86 Aggregation target = new Aggregation();
87 target.setOutput(dir.toPath().resolve("output.js").toFile());
88 target.setIncludes(new String[] { f1.getName() });
89
90 Assertions.assertFalse(target.getOutput().exists());
91 target.run(null, defaultBuildContext);
92 Assertions.assertTrue(target.getOutput().exists());
93 Assertions.assertEquals(new String(Files.readAllBytes(f1.toPath()), StandardCharsets.UTF_8),
94 new String(Files.readAllBytes(target.getOutput().toPath()), StandardCharsets.UTF_8));
95 }
96
97
98
99
100
101
102
103 @Test
104 void test2to1() throws IOException {
105 File f1 = dir.toPath().resolve("01.js").toFile();
106 Files.write(f1.toPath(), "1".getBytes(StandardCharsets.UTF_8), StandardOpenOption.CREATE);
107
108 File f2 = dir.toPath().resolve("02.js").toFile();
109 Files.write(f2.toPath(), "22\n22".getBytes(StandardCharsets.UTF_8), StandardOpenOption.CREATE);
110
111 Aggregation target = new Aggregation();
112 target.setOutput(dir.toPath().resolve("output.js").toFile());
113
114 target.setIncludes(new String[] { f1.getName(), f2.getName() });
115 Assertions.assertFalse(target.getOutput().exists());
116 target.run(null, defaultBuildContext);
117 Assertions.assertTrue(target.getOutput().exists());
118 Assertions.assertEquals(
119 new String(Files.readAllBytes(f1.toPath()), StandardCharsets.UTF_8)
120 + new String(Files.readAllBytes(f2.toPath()), StandardCharsets.UTF_8),
121 new String(Files.readAllBytes(target.getOutput().toPath()), StandardCharsets.UTF_8));
122
123 target.getOutput().delete();
124 target.setIncludes(new String[] { "*.js" });
125 Assertions.assertFalse(target.getOutput().exists());
126 target.run(null, defaultBuildContext);
127 Assertions.assertTrue(target.getOutput().exists());
128 Assertions.assertEquals(
129 new String(Files.readAllBytes(f1.toPath()), StandardCharsets.UTF_8)
130 + new String(Files.readAllBytes(f2.toPath()), StandardCharsets.UTF_8),
131 new String(Files.readAllBytes(target.getOutput().toPath()), StandardCharsets.UTF_8));
132 }
133
134
135
136
137
138
139
140 @Test
141 void testNoDuplicateAggregation() throws IOException {
142 File f1 = dir.toPath().resolve("01.js").toFile();
143 Files.write(f1.toPath(), "1".getBytes(StandardCharsets.UTF_8), StandardOpenOption.CREATE);
144
145 File f2 = dir.toPath().resolve("02.js").toFile();
146 Files.write(f2.toPath(), "22\n22".getBytes(StandardCharsets.UTF_8), StandardOpenOption.CREATE);
147
148 Aggregation target = new Aggregation();
149 target.setOutput(dir.toPath().resolve("output.js").toFile());
150
151 target.setIncludes(new String[] { f1.getName(), f1.getName(), f2.getName() });
152 Assertions.assertFalse(target.getOutput().exists());
153 target.run(null, defaultBuildContext);
154 Assertions.assertTrue(target.getOutput().exists());
155 Assertions.assertEquals(
156 new String(Files.readAllBytes(f1.toPath()), StandardCharsets.UTF_8)
157 + new String(Files.readAllBytes(f2.toPath()), StandardCharsets.UTF_8),
158 new String(Files.readAllBytes(target.getOutput().toPath()), StandardCharsets.UTF_8));
159
160 target.getOutput().delete();
161 target.setIncludes(new String[] { f1.getName(), "*.js" });
162 Assertions.assertFalse(target.getOutput().exists());
163 target.run(null, defaultBuildContext);
164 Assertions.assertTrue(target.getOutput().exists());
165 Assertions.assertEquals(
166 new String(Files.readAllBytes(f1.toPath()), StandardCharsets.UTF_8)
167 + new String(Files.readAllBytes(f2.toPath()), StandardCharsets.UTF_8),
168 new String(Files.readAllBytes(target.getOutput().toPath()), StandardCharsets.UTF_8));
169 }
170
171
172
173
174
175
176
177 @Test
178 void test2to1Order() throws IOException {
179 File f1 = dir.toPath().resolve("01.js").toFile();
180 Files.write(f1.toPath(), "1".getBytes(StandardCharsets.UTF_8), StandardOpenOption.CREATE);
181
182 File f2 = dir.toPath().resolve("02.js").toFile();
183 Files.write(f2.toPath(), "2".getBytes(StandardCharsets.UTF_8), StandardOpenOption.CREATE);
184
185 Aggregation target = new Aggregation();
186 target.setOutput(dir.toPath().resolve("output.js").toFile());
187
188 target.setIncludes(new String[] { f2.getName(), f1.getName() });
189 Assertions.assertFalse(target.getOutput().exists());
190 target.run(null, defaultBuildContext);
191 Assertions.assertTrue(target.getOutput().exists());
192 Assertions.assertEquals(
193 new String(Files.readAllBytes(f2.toPath()), StandardCharsets.UTF_8)
194 + new String(Files.readAllBytes(f1.toPath()), StandardCharsets.UTF_8),
195 new String(Files.readAllBytes(target.getOutput().toPath()), StandardCharsets.UTF_8));
196 }
197
198
199
200
201
202
203
204 @Test
205 void test2to1WithNewLine() throws IOException {
206 File f1 = dir.toPath().resolve("01.js").toFile();
207 Files.write(f1.toPath(), "1".getBytes(StandardCharsets.UTF_8), StandardOpenOption.CREATE);
208
209 File f2 = dir.toPath().resolve("02.js").toFile();
210 Files.write(f2.toPath(), "22\n22".getBytes(StandardCharsets.UTF_8), StandardOpenOption.CREATE);
211
212 Aggregation target = new Aggregation();
213 target.setOutput(dir.toPath().resolve("output.js").toFile());
214 target.setInsertNewLine(true);
215 target.setIncludes(new String[] { f1.getName(), f2.getName() });
216
217 Assertions.assertFalse(target.getOutput().exists());
218 target.run(null, defaultBuildContext);
219 Assertions.assertTrue(target.getOutput().exists());
220 Assertions.assertEquals(
221 new String(Files.readAllBytes(f1.toPath()), StandardCharsets.UTF_8) + "\n"
222 + new String(Files.readAllBytes(f2.toPath()), StandardCharsets.UTF_8) + "\n",
223 new String(Files.readAllBytes(target.getOutput().toPath()), StandardCharsets.UTF_8));
224 }
225
226
227
228
229
230
231
232 @Test
233 void testAbsolutePathFromInside() throws IOException {
234 File f1 = dir.toPath().resolve("01.js").toFile();
235 Files.write(f1.toPath(), "1".getBytes(StandardCharsets.UTF_8), StandardOpenOption.CREATE);
236
237 File f2 = dir.toPath().resolve("02.js").toFile();
238 Files.write(f2.toPath(), "22\n22".getBytes(StandardCharsets.UTF_8), StandardOpenOption.CREATE);
239
240 Aggregation target = new Aggregation();
241 target.setOutput(dir.toPath().resolve("output.js").toFile());
242
243 target.setIncludes(new String[] { f1.getAbsolutePath(), f2.getName() });
244 Assertions.assertFalse(target.getOutput().exists());
245 target.run(null, defaultBuildContext);
246 Assertions.assertTrue(target.getOutput().exists());
247 Assertions.assertEquals(
248 new String(Files.readAllBytes(f1.toPath()), StandardCharsets.UTF_8)
249 + new String(Files.readAllBytes(f2.toPath()), StandardCharsets.UTF_8),
250 new String(Files.readAllBytes(target.getOutput().toPath()), StandardCharsets.UTF_8));
251 }
252
253
254
255
256
257
258
259 @Test
260 void testAbsolutePathFromOutside() throws IOException {
261 File f1 = File.createTempFile("test-01", ".js");
262 Files.write(f1.toPath(), "1".getBytes(StandardCharsets.UTF_8), StandardOpenOption.CREATE);
263
264 File f2 = dir.toPath().resolve("02.js").toFile();
265 Files.write(f2.toPath(), "22\n22".getBytes(StandardCharsets.UTF_8), StandardOpenOption.CREATE);
266
267 Aggregation target = new Aggregation();
268 target.setOutput(dir.toPath().resolve("output.js").toFile());
269
270 try {
271 target.setIncludes(new String[] { f1.getAbsolutePath(), f2.getName() });
272 Assertions.assertFalse(target.getOutput().exists());
273 target.run(null, defaultBuildContext);
274 Assertions.assertTrue(target.getOutput().exists());
275 Assertions.assertEquals(
276 new String(Files.readAllBytes(f1.toPath()), StandardCharsets.UTF_8)
277 + new String(Files.readAllBytes(f2.toPath()), StandardCharsets.UTF_8),
278 new String(Files.readAllBytes(target.getOutput().toPath()), StandardCharsets.UTF_8));
279 } finally {
280 f1.delete();
281 }
282 }
283
284
285
286
287
288
289
290 @Test
291 void testAutoExcludeWildcards() throws IOException {
292 File f1 = dir.toPath().resolve("01.js").toFile();
293 Files.write(f1.toPath(), "1".getBytes(StandardCharsets.UTF_8), StandardOpenOption.CREATE);
294
295 File f2 = dir.toPath().resolve("02.js").toFile();
296 Files.write(f2.toPath(), "22\n22".getBytes(StandardCharsets.UTF_8), StandardOpenOption.CREATE);
297
298 Aggregation target = new Aggregation();
299 target.setAutoExcludeWildcards(true);
300 target.setOutput(dir.toPath().resolve("output.js").toFile());
301
302 Collection<File> previouslyIncluded = new HashSet<>();
303 previouslyIncluded.add(f1.getCanonicalFile());
304
305 target.setIncludes(new String[] { f1.getName(), f2.getName() });
306 Assertions.assertFalse(target.getOutput().exists());
307
308 List<File> content = target.run(previouslyIncluded, defaultBuildContext);
309 Assertions.assertEquals(2, content.size());
310 Assertions.assertTrue(target.getOutput().exists());
311 Assertions.assertEquals(
312 new String(Files.readAllBytes(f1.toPath()), StandardCharsets.UTF_8)
313 + new String(Files.readAllBytes(f2.toPath()), StandardCharsets.UTF_8),
314 new String(Files.readAllBytes(target.getOutput().toPath()), StandardCharsets.UTF_8));
315
316 target.getOutput().delete();
317 target.setIncludes(new String[] { "*.js" });
318 Assertions.assertFalse(target.getOutput().exists());
319
320 Assertions.assertEquals(target.run(previouslyIncluded, defaultBuildContext),
321 Arrays.asList(f2.getCanonicalFile()));
322 Assertions.assertTrue(target.getOutput().exists());
323 Assertions.assertEquals(new String(Files.readAllBytes(f2.toPath()), StandardCharsets.UTF_8),
324 new String(Files.readAllBytes(target.getOutput().toPath()), StandardCharsets.UTF_8));
325 }
326 }