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