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.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
39
40 class JSLintCheckerTest {
41
42
43 private File tempJsFile;
44
45
46
47
48
49
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
62
63 @AfterEach
64 void tearDown() {
65 this.tempJsFile.delete();
66 }
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82 @Test
83 void testConstructorCreatesTempFile() throws IOException, NoSuchFieldException, SecurityException,
84 IllegalArgumentException, IllegalAccessException {
85 final var checker = new JSLintChecker();
86 Assertions.assertNotNull(checker);
87
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
96
97
98
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 }