View Javadoc
1   /*
2    * YuiCompressor Maven plugin
3    *
4    * Copyright 2012-2025 Hazendaz.
5    *
6    * Licensed under the GNU Lesser General Public License (LGPL),
7    * version 2.1 or later (the "License").
8    * You may not use this file except in compliance with the License.
9    * You may read the licence in the 'lgpl.txt' file in the root folder of
10   * project or obtain a copy at
11   *
12   *     https://www.gnu.org/licenses/lgpl-2.1.html
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" basis,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
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   * The Class AggregationTestCase.
39   */
40  // Note: public here needed for javadocs to work so don't remove it
41  public class AggregationTestCase {
42  
43      /** The dir. */
44      @TempDir
45      File dir;
46  
47      /** The default build context. */
48      DefaultBuildContext defaultBuildContext = new DefaultBuildContext();
49  
50      /**
51       * Test 0 to 1.
52       *
53       * @throws IOException
54       *             the IO exception
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       * Test 1 to 1.
78       *
79       * @throws IOException
80       *             the IO exception
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       * Test 2 to 1.
99       *
100      * @throws IOException
101      *             the IO exception
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      * Test no duplicate aggregation.
136      *
137      * @throws IOException
138      *             the IO exception
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      * Test 2 to 1 order.
173      *
174      * @throws IOException
175      *             the IO exception
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      * Test 2 to 1 with new line.
200      *
201      * @throws IOException
202      *             the IO exception
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      * Test absolute path from inside.
228      *
229      * @throws IOException
230      *             the IO exception
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      * Test absolute path from outside.
255      *
256      * @throws IOException
257      *             the IO exception
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      * Test auto exclude wildcards.
286      *
287      * @throws IOException
288      *             the IO exception
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         // First call uses path that does not deal with previouslyIncluded so both files are added
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         // f1 was in previouslyIncluded so it is not included
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 }