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.Path;
27  
28  import org.junit.jupiter.api.AfterEach;
29  import org.junit.jupiter.api.Assertions;
30  import org.junit.jupiter.api.BeforeEach;
31  import org.junit.jupiter.api.Test;
32  
33  /**
34   * The Class SourceFileTest.
35   */
36  class SourceFileTest {
37  
38      /** The src root. */
39      private Path srcRoot;
40  
41      /** The dest root. */
42      private Path destRoot;
43  
44      /**
45       * Sets the up.
46       *
47       * @throws IOException
48       *             Signals that an I/O exception has occurred.
49       */
50      @BeforeEach
51      void setUp() throws IOException {
52          this.srcRoot = Files.createTempDirectory("srcRoot");
53          this.destRoot = Files.createTempDirectory("destRoot");
54      }
55  
56      /**
57       * Tear down.
58       *
59       * @throws IOException
60       *             Signals that an I/O exception has occurred.
61       */
62      @AfterEach
63      void tearDown() throws IOException {
64          try (var srcStream = Files.walk(this.srcRoot)) {
65              srcStream.map(Path::toFile).forEach(File::delete);
66          }
67          try (var destStream = Files.walk(this.destRoot)) {
68              destStream.map(Path::toFile).forEach(File::delete);
69          }
70      }
71  
72      /**
73       * Test constructor with extension.
74       */
75      @Test
76      void testConstructorWithExtension() {
77          final var sf = new SourceFile(this.srcRoot.toFile(), this.destRoot.toFile(), "foo.js", false);
78          Assertions.assertEquals(".js", sf.getExtension());
79      }
80  
81      /**
82       * Test constructor without extension.
83       */
84      @Test
85      void testConstructorWithoutExtension() {
86          final var sf = new SourceFile(this.srcRoot.toFile(), this.destRoot.toFile(), "foo", false);
87          Assertions.assertEquals("", sf.getExtension());
88      }
89  
90      /**
91       * Test to file src root.
92       *
93       * @throws IOException
94       *             Signals that an I/O exception has occurred.
95       */
96      @Test
97      void testToFileSrcRoot() throws IOException {
98          final var filePath = this.srcRoot.resolve("bar.js");
99          Files.write(filePath, "test".getBytes(StandardCharsets.UTF_8));
100         final var sf = new SourceFile(this.srcRoot.toFile(), this.destRoot.toFile(), "bar.js", false);
101         Assertions.assertEquals(filePath.toFile(), sf.toFile());
102     }
103 
104     /**
105      * Test to file dest root when dest as source and file exists.
106      *
107      * @throws IOException
108      *             Signals that an I/O exception has occurred.
109      */
110     @Test
111     void testToFileDestRootWhenDestAsSourceAndFileExists() throws IOException {
112         final var filePath = this.destRoot.resolve("baz.js");
113         Files.write(filePath, "test".getBytes(StandardCharsets.UTF_8));
114         final var sf = new SourceFile(this.srcRoot.toFile(), this.destRoot.toFile(), "baz.js", true);
115         Assertions.assertEquals(filePath.toFile(), sf.toFile());
116     }
117 
118     /**
119      * Test to file dest root when dest as source and file does not exist.
120      */
121     @Test
122     void testToFileDestRootWhenDestAsSourceAndFileDoesNotExist() {
123         final var sf = new SourceFile(this.srcRoot.toFile(), this.destRoot.toFile(), "qux.js", true);
124         // Should fall back to srcRoot
125         final var expected = this.srcRoot.resolve("qux.js").toFile();
126         Assertions.assertEquals(expected, sf.toFile());
127     }
128 
129     /**
130      * Test to dest file with suffix.
131      */
132     @Test
133     void testToDestFileWithSuffix() {
134         final var sf = new SourceFile(this.srcRoot.toFile(), this.destRoot.toFile(), "foo.js", false);
135         final var expected = this.destRoot.resolve("foo-min.js").toFile();
136         Assertions.assertEquals(expected, sf.toDestFile("-min"));
137     }
138 
139     /**
140      * Test to dest file with suffix no extension.
141      */
142     @Test
143     void testToDestFileWithSuffixNoExtension() {
144         final var sf = new SourceFile(this.srcRoot.toFile(), this.destRoot.toFile(), "foo", false);
145         final var expected = this.destRoot.resolve("foo-min").toFile();
146         Assertions.assertEquals(expected, sf.toDestFile("-min"));
147     }
148 
149 }