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