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  import org.mockito.ArgumentMatchers;
33  import org.mockito.MockedStatic;
34  import org.mockito.Mockito;
35  import org.mozilla.javascript.ErrorReporter;
36  
37  /**
38   * The Class JSLintCheckerTest.
39   */
40  class JSLintCheckerTest {
41  
42      /** The temp js file. */
43      private File tempJsFile;
44  
45      /**
46       * Sets the up.
47       *
48       * @throws IOException
49       *             Signals that an I/O exception has occurred.
50       */
51      @BeforeEach
52      void setUp() throws IOException {
53          this.tempJsFile = File.createTempFile("test", ".js");
54          this.tempJsFile.deleteOnExit();
55          try (var writer = Files.newBufferedWriter(this.tempJsFile.toPath(), StandardCharsets.UTF_8)) {
56              writer.write("var a = 1;");
57          }
58      }
59  
60      /**
61       * Tear down.
62       */
63      @AfterEach
64      void tearDown() {
65          this.tempJsFile.delete();
66      }
67  
68      /**
69       * Test constructor creates temp file.
70       *
71       * @throws IOException
72       *             Signals that an I/O exception has occurred.
73       * @throws NoSuchFieldException
74       *             the no such field exception
75       * @throws SecurityException
76       *             the security exception
77       * @throws IllegalArgumentException
78       *             the illegal argument exception
79       * @throws IllegalAccessException
80       *             the illegal access exception
81       */
82      @Test
83      void testConstructorCreatesTempFile() throws IOException, NoSuchFieldException, SecurityException,
84              IllegalArgumentException, IllegalAccessException {
85          final var checker = new JSLintChecker();
86          Assertions.assertNotNull(checker);
87          // jslintPath should be set (private, so use reflection)
88          final var field = JSLintChecker.class.getDeclaredField("jslintPath");
89          field.setAccessible(true);
90          final var path = (String) field.get(checker);
91          Assertions.assertTrue(Path.of(path).toFile().exists());
92      }
93  
94      /**
95       * Test check calls basic rhino shell exec.
96       *
97       * @throws Exception
98       *             the exception
99       */
100     @Test
101     void testCheckCallsBasicRhinoShellExec() throws Exception {
102         final var checker = new JSLintChecker();
103         final var reporter = Mockito.mock(ErrorReporter.class);
104         try (MockedStatic<BasicRhinoShell> shellMock = Mockito.mockStatic(BasicRhinoShell.class)) {
105             checker.check(this.tempJsFile, reporter);
106             shellMock.verify(
107                     () -> BasicRhinoShell.exec(ArgumentMatchers.any(String[].class), ArgumentMatchers.eq(reporter)));
108         }
109     }
110 
111 }