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.nio.charset.StandardCharsets;
23  import java.nio.file.Files;
24  import java.nio.file.Path;
25  
26  import org.junit.jupiter.api.AfterEach;
27  import org.junit.jupiter.api.Assertions;
28  import org.junit.jupiter.api.BeforeEach;
29  import org.junit.jupiter.api.Test;
30  import org.mockito.Mockito;
31  import org.mozilla.javascript.Context;
32  import org.mozilla.javascript.Function;
33  import org.mozilla.javascript.Scriptable;
34  
35  /**
36   * The Class BasicRhinoShellTest.
37   */
38  class BasicRhinoShellTest {
39  
40      /** The temp file. */
41      private Path tempFile;
42  
43      /**
44       * Sets the up.
45       *
46       * @throws Exception
47       *             the exception
48       */
49      @BeforeEach
50      void setUp() throws Exception {
51          this.tempFile = Files.createTempFile("rhino-test", ".js");
52      }
53  
54      /**
55       * Tear down.
56       *
57       * @throws Exception
58       *             the exception
59       */
60      @AfterEach
61      void tearDown() throws Exception {
62          Files.deleteIfExists(this.tempFile);
63      }
64  
65      /**
66       * Test read file returns content.
67       *
68       * @throws Exception
69       *             the exception
70       */
71      @Test
72      void testReadFileReturnsContent() throws Exception {
73          final var content = "var a = 1;";
74          try (var writer = Files.newBufferedWriter(this.tempFile, StandardCharsets.UTF_8)) {
75              writer.write(content);
76          }
77          final var shell = new BasicRhinoShell();
78          final var result = shell.readFile(this.tempFile.toString());
79          Assertions.assertEquals(content, result.trim());
80      }
81  
82      /**
83       * Test read file throws runtime exception.
84       */
85      @Test
86      void testReadFileThrowsRuntimeException() {
87          final var shell = new BasicRhinoShell();
88          Assertions.assertThrows(RuntimeException.class, () -> shell.readFile("nonexistent.js"));
89      }
90  
91      /**
92       * Test process options version.
93       */
94      @Test
95      void testProcessOptionsVersion() {
96          final var cx = Context.enter();
97          final String[] args = { "-version", "170", "script.js" };
98          final var result = BasicRhinoShell.processOptions(cx, args);
99          Assertions.assertEquals("script.js", result[0]);
100         Assertions.assertEquals(170, cx.getLanguageVersion());
101         Context.exit();
102     }
103 
104     /**
105      * Test help does not throw.
106      */
107     @Test
108     void testHelpDoesNotThrow() {
109         final var shell = new BasicRhinoShell();
110         shell.help(); // Should not throw
111     }
112 
113     /**
114      * Test quit sets flag.
115      *
116      * @throws NoSuchFieldException
117      *             the no such field exception
118      * @throws SecurityException
119      *             the security exception
120      * @throws IllegalArgumentException
121      *             the illegal argument exception
122      * @throws IllegalAccessException
123      *             the illegal access exception
124      */
125     @Test
126     void testQuitSetsFlag()
127             throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
128         final var shell = new BasicRhinoShell();
129         shell.quit();
130         // Reflection to check private field
131         final var field = BasicRhinoShell.class.getDeclaredField("quitting");
132         field.setAccessible(true);
133         Assertions.assertTrue((Boolean) field.get(shell));
134     }
135 
136     /**
137      * Test print logs.
138      */
139     @Test
140     void testPrintLogs() {
141         final var cx = Context.enter();
142         final Scriptable scope = cx.initStandardObjects();
143         final Object[] args = { "Hello", "World" };
144         final var funObj = Mockito.mock(Function.class);
145         // Should not throw
146         BasicRhinoShell.print(cx, scope, args, funObj);
147         Context.exit();
148     }
149 
150     /**
151      * Test warn does not throw.
152      */
153     @Test
154     void testWarnDoesNotThrow() {
155         final var cx = Context.enter();
156         final Scriptable scope = cx.initStandardObjects();
157         final Object[] args = { "Warning", 1, "source.js", 2 };
158         final var funObj = Mockito.mock(Function.class);
159         BasicRhinoShell.warn(cx, scope, args, funObj);
160         Context.exit();
161     }
162 
163     /**
164      * Test version get set.
165      */
166     @Test
167     void testVersionGetSet() {
168         final var cx = Context.enter();
169         final Scriptable scope = cx.initStandardObjects();
170         final Object[] args = { 170 };
171         final var funObj = Mockito.mock(Function.class);
172         final var oldVersion = BasicRhinoShell.version(cx, scope, new Object[0], funObj);
173         BasicRhinoShell.version(cx, scope, args, funObj);
174         Assertions.assertEquals(0.0, oldVersion);
175         Assertions.assertEquals(170, cx.getLanguageVersion());
176         Context.exit();
177     }
178 
179 }