1
2
3
4
5
6
7
8
9
10
11 package psiprobe.tools.logging.catalina;
12
13 import static org.junit.jupiter.api.Assertions.assertEquals;
14 import static org.junit.jupiter.api.Assertions.assertNotNull;
15 import static org.junit.jupiter.api.Assertions.assertNull;
16 import static org.junit.jupiter.api.Assertions.assertTrue;
17 import static org.mockito.Mockito.doReturn;
18 import static org.mockito.Mockito.mock;
19 import static org.mockito.Mockito.mockStatic;
20 import static org.mockito.Mockito.spy;
21
22 import java.io.File;
23
24 import org.junit.jupiter.api.BeforeEach;
25 import org.junit.jupiter.api.Test;
26 import org.mockito.MockedStatic;
27
28 import psiprobe.tools.Instruments;
29
30
31
32
33 class CatalinaLoggerAccessorTest {
34
35
36 private CatalinaLoggerAccessor accessor;
37
38
39
40
41 @BeforeEach
42 void setUp() {
43 accessor = spy(new CatalinaLoggerAccessor());
44 }
45
46
47
48
49 @Test
50 void testIsContext() {
51 assertTrue(accessor.isContext());
52 }
53
54
55
56
57 @Test
58 void testGetName() {
59 assertNull(accessor.getName());
60 }
61
62
63
64
65 @Test
66 void testGetLogType() {
67 assertEquals("catalina", accessor.getLogType());
68 }
69
70
71
72
73 @Test
74 void testGetFileWithAllFields() {
75 Object target = mock(Object.class);
76 doReturn(target).when(accessor).getTarget();
77 doReturn("/tmp").when(accessor).invokeMethod(target, "getDirectory", null, null);
78 doReturn("catalina.").when(accessor).invokeMethod(target, "getPrefix", null, null);
79 doReturn(".log").when(accessor).invokeMethod(target, "getSuffix", null, null);
80
81 try (MockedStatic<Instruments> mocked = mockStatic(Instruments.class)) {
82 mocked.when(() -> Instruments.getField(target, "timestamp")).thenReturn(new Object());
83
84 System.setProperty("catalina.base", "");
85
86 File file = accessor.getFile();
87 assertNotNull(file);
88 assertTrue(file.getName().startsWith("catalina."));
89 assertTrue(file.getName().endsWith(".log"));
90 }
91 }
92
93
94
95
96 @Test
97 void testGetFileWithMissingFields() {
98 Object target = mock(Object.class);
99 doReturn(target).when(accessor).getTarget();
100 doReturn(null).when(accessor).invokeMethod(target, "getDirectory", null, null);
101 doReturn("catalina.").when(accessor).invokeMethod(target, "getPrefix", null, null);
102 doReturn(".log").when(accessor).invokeMethod(target, "getSuffix", null, null);
103 try (MockedStatic<Instruments> mocked = mockStatic(Instruments.class)) {
104 mocked.when(() -> Instruments.getField(target, "timestamp")).thenReturn(null);
105
106 File file = accessor.getFile();
107 assertNull(file);
108 }
109 }
110
111 }