1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
37
38 class BasicRhinoShellTest {
39
40
41 private Path tempFile;
42
43
44
45
46
47
48
49 @BeforeEach
50 void setUp() throws Exception {
51 this.tempFile = Files.createTempFile("rhino-test", ".js");
52 }
53
54
55
56
57
58
59
60 @AfterEach
61 void tearDown() throws Exception {
62 Files.deleteIfExists(this.tempFile);
63 }
64
65
66
67
68
69
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
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
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
106
107 @Test
108 void testHelpDoesNotThrow() {
109 final var shell = new BasicRhinoShell();
110 shell.help();
111 }
112
113
114
115
116
117
118
119
120
121
122
123
124
125 @Test
126 void testQuitSetsFlag()
127 throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
128 final var shell = new BasicRhinoShell();
129 shell.quit();
130
131 final var field = BasicRhinoShell.class.getDeclaredField("quitting");
132 field.setAccessible(true);
133 Assertions.assertTrue((Boolean) field.get(shell));
134 }
135
136
137
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
146 BasicRhinoShell.print(cx, scope, args, funObj);
147 Context.exit();
148 }
149
150
151
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
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 }