1
2
3
4
5
6
7
8
9
10
11 package psiprobe.tools.logging.jdk;
12
13 import static org.junit.jupiter.api.Assertions.assertEquals;
14 import static org.mockito.Mockito.doReturn;
15 import static org.mockito.Mockito.mockStatic;
16 import static org.mockito.Mockito.spy;
17
18 import java.io.File;
19 import java.nio.file.Path;
20
21 import org.junit.jupiter.api.BeforeEach;
22 import org.junit.jupiter.api.Test;
23 import org.mockito.MockedStatic;
24
25 import psiprobe.tools.Instruments;
26
27
28
29
30 class JuliHandlerAccessorTest {
31
32
33 private JuliHandlerAccessor accessor;
34
35
36
37
38 @BeforeEach
39 void setUp() {
40 accessor = spy(new JuliHandlerAccessor());
41 }
42
43
44
45
46
47 @Test
48 void testGetFile_AllFieldsPresent() {
49 Object target = new Object();
50 doReturn(target).when(accessor).getTarget();
51
52 try (MockedStatic<Instruments> instrumentsMock = mockStatic(Instruments.class)) {
53 instrumentsMock.when(() -> Instruments.getField(target, "directory")).thenReturn("logs");
54 instrumentsMock.when(() -> Instruments.getField(target, "prefix")).thenReturn("app-");
55 instrumentsMock.when(() -> Instruments.getField(target, "suffix")).thenReturn(".log");
56 instrumentsMock.when(() -> Instruments.getField(target, "date")).thenReturn("20240607");
57
58 File expected = Path.of("logs", "app-20240607.log").toFile();
59 File actual = accessor.getFile();
60
61 assertEquals(expected, actual);
62 }
63 }
64
65
66
67
68 @Test
69 void testGetFile_MissingField_ReturnsStdoutFile() {
70 Object target = new Object();
71 doReturn(target).when(accessor).getTarget();
72
73 try (MockedStatic<Instruments> instrumentsMock = mockStatic(Instruments.class)) {
74 instrumentsMock.when(() -> Instruments.getField(target, "directory")).thenReturn(null);
75 instrumentsMock.when(() -> Instruments.getField(target, "prefix")).thenReturn("app-");
76 instrumentsMock.when(() -> Instruments.getField(target, "suffix")).thenReturn(".log");
77 instrumentsMock.when(() -> Instruments.getField(target, "date")).thenReturn("20240607");
78
79 Path stdoutFile = Path.of("stdout.log");
80 doReturn(stdoutFile.toFile()).when(accessor).getStdoutFile();
81
82 File actual = accessor.getFile();
83
84 assertEquals(stdoutFile.toFile(), actual);
85 }
86 }
87
88 }