View Javadoc
1   /*
2    * SPDX-License-Identifier: LGPL-2.1-or-later
3    * See LICENSE file for details.
4    *
5    * Copyright 2012-2026 Hazendaz.
6    */
7   package net.alchim31.maven.yuicompressor;
8   
9   import java.io.File;
10  import java.io.IOException;
11  import java.nio.charset.StandardCharsets;
12  import java.nio.file.Files;
13  import java.nio.file.Path;
14  
15  import org.junit.jupiter.api.AfterEach;
16  import org.junit.jupiter.api.Assertions;
17  import org.junit.jupiter.api.BeforeEach;
18  import org.junit.jupiter.api.Test;
19  
20  /**
21   * The Class SourceFileTest.
22   */
23  class SourceFileTest {
24  
25      /** The src root. */
26      private Path srcRoot;
27  
28      /** The dest root. */
29      private Path destRoot;
30  
31      /**
32       * Sets the up.
33       *
34       * @throws IOException
35       *             Signals that an I/O exception has occurred.
36       */
37      @BeforeEach
38      void setUp() throws IOException {
39          this.srcRoot = Files.createTempDirectory("srcRoot");
40          this.destRoot = Files.createTempDirectory("destRoot");
41      }
42  
43      /**
44       * Tear down.
45       *
46       * @throws IOException
47       *             Signals that an I/O exception has occurred.
48       */
49      @AfterEach
50      void tearDown() throws IOException {
51          try (var srcStream = Files.walk(this.srcRoot)) {
52              srcStream.map(Path::toFile).forEach(File::delete);
53          }
54          try (var destStream = Files.walk(this.destRoot)) {
55              destStream.map(Path::toFile).forEach(File::delete);
56          }
57      }
58  
59      /**
60       * Test constructor with extension.
61       */
62      @Test
63      void testConstructorWithExtension() {
64          final var sf = new SourceFile(this.srcRoot.toFile(), this.destRoot.toFile(), "foo.js", false);
65          Assertions.assertEquals(".js", sf.getExtension());
66      }
67  
68      /**
69       * Test constructor without extension.
70       */
71      @Test
72      void testConstructorWithoutExtension() {
73          final var sf = new SourceFile(this.srcRoot.toFile(), this.destRoot.toFile(), "foo", false);
74          Assertions.assertEquals("", sf.getExtension());
75      }
76  
77      /**
78       * Test to file src root.
79       *
80       * @throws IOException
81       *             Signals that an I/O exception has occurred.
82       */
83      @Test
84      void testToFileSrcRoot() throws IOException {
85          final var filePath = this.srcRoot.resolve("bar.js");
86          Files.write(filePath, "test".getBytes(StandardCharsets.UTF_8));
87          final var sf = new SourceFile(this.srcRoot.toFile(), this.destRoot.toFile(), "bar.js", false);
88          Assertions.assertEquals(filePath.toFile(), sf.toFile());
89      }
90  
91      /**
92       * Test to file dest root when dest as source and file exists.
93       *
94       * @throws IOException
95       *             Signals that an I/O exception has occurred.
96       */
97      @Test
98      void testToFileDestRootWhenDestAsSourceAndFileExists() throws IOException {
99          final var filePath = this.destRoot.resolve("baz.js");
100         Files.write(filePath, "test".getBytes(StandardCharsets.UTF_8));
101         final var sf = new SourceFile(this.srcRoot.toFile(), this.destRoot.toFile(), "baz.js", true);
102         Assertions.assertEquals(filePath.toFile(), sf.toFile());
103     }
104 
105     /**
106      * Test to file dest root when dest as source and file does not exist.
107      */
108     @Test
109     void testToFileDestRootWhenDestAsSourceAndFileDoesNotExist() {
110         final var sf = new SourceFile(this.srcRoot.toFile(), this.destRoot.toFile(), "qux.js", true);
111         // Should fall back to srcRoot
112         final var expected = this.srcRoot.resolve("qux.js").toFile();
113         Assertions.assertEquals(expected, sf.toFile());
114     }
115 
116     /**
117      * Test to dest file with suffix.
118      */
119     @Test
120     void testToDestFileWithSuffix() {
121         final var sf = new SourceFile(this.srcRoot.toFile(), this.destRoot.toFile(), "foo.js", false);
122         final var expected = this.destRoot.resolve("foo-min.js").toFile();
123         Assertions.assertEquals(expected, sf.toDestFile("-min"));
124     }
125 
126     /**
127      * Test to dest file with suffix no extension.
128      */
129     @Test
130     void testToDestFileWithSuffixNoExtension() {
131         final var sf = new SourceFile(this.srcRoot.toFile(), this.destRoot.toFile(), "foo", false);
132         final var expected = this.destRoot.resolve("foo-min").toFile();
133         Assertions.assertEquals(expected, sf.toDestFile("-min"));
134     }
135 
136 }