1
2
3
4
5
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
26
27 class JSLintCheckerTest {
28
29
30 private File tempJsFile;
31
32
33
34
35
36
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
49
50 @AfterEach
51 void tearDown() {
52 this.tempJsFile.delete();
53 }
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69 @Test
70 void testConstructorCreatesTempFile() throws IOException, NoSuchFieldException, SecurityException,
71 IllegalArgumentException, IllegalAccessException {
72 final var checker = new JSLintChecker();
73 Assertions.assertNotNull(checker);
74
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
83
84
85
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 }