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 import java.nio.file.Path;
24 import java.text.SimpleDateFormat;
25 import java.util.Date;
26
27 import org.junit.jupiter.api.AfterEach;
28 import org.junit.jupiter.api.BeforeEach;
29 import org.junit.jupiter.api.Test;
30 import org.mockito.MockedStatic;
31
32 import psiprobe.tools.Instruments;
33
34
35
36
37 class CatalinaLoggerAccessorTest {
38
39
40 private CatalinaLoggerAccessor accessor;
41
42
43
44
45 @BeforeEach
46 void setUp() {
47 accessor = spy(new CatalinaLoggerAccessor());
48 }
49
50 @AfterEach
51 void tearDown() {
52 System.clearProperty("catalina.base");
53 }
54
55
56
57
58 @Test
59 void testIsContext() {
60 assertTrue(accessor.isContext());
61 }
62
63
64
65
66 @Test
67 void testGetName() {
68 assertNull(accessor.getName());
69 }
70
71
72
73
74 @Test
75 void testGetLogType() {
76 assertEquals("catalina", accessor.getLogType());
77 }
78
79
80
81
82 @Test
83 void testGetFileWithAllFields() {
84 Object target = mock(Object.class);
85 doReturn(target).when(accessor).getTarget();
86 doReturn("/tmp").when(accessor).invokeMethod(target, "getDirectory", null, null);
87 doReturn("catalina.").when(accessor).invokeMethod(target, "getPrefix", null, null);
88 doReturn(".log").when(accessor).invokeMethod(target, "getSuffix", null, null);
89
90 try (MockedStatic<Instruments> mocked = mockStatic(Instruments.class)) {
91 mocked.when(() -> Instruments.getField(target, "timestamp")).thenReturn(new Object());
92
93 System.setProperty("catalina.base", "");
94
95 File file = accessor.getFile();
96 assertNotNull(file);
97 assertTrue(file.getName().startsWith("catalina."));
98 assertTrue(file.getName().endsWith(".log"));
99 }
100 }
101
102 @Test
103 void testGetFileWithAllFieldsAndTimestamp() {
104 Object target = mock(Object.class);
105 doReturn(target).when(accessor).getTarget();
106 doReturn("logs").when(accessor).invokeMethod(target, "getDirectory", null, null);
107 doReturn("catalina.").when(accessor).invokeMethod(target, "getPrefix", null, null);
108 doReturn(".log").when(accessor).invokeMethod(target, "getSuffix", null, null);
109
110
111 try (var instrumentsMock = mockStatic(Instruments.class)) {
112 instrumentsMock.when(() -> Instruments.getField(target, "timestamp"))
113 .thenReturn(Boolean.TRUE);
114
115 String date = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
116 System.setProperty("catalina.base", "/base");
117
118 File file = accessor.getFile();
119 assertNotNull(file);
120
121 assertTrue(file.getPath().contains("catalina." + date + ".log"));
122
123 Path catalinaBase = Path.of(System.getProperty("catalina.base")).toAbsolutePath().normalize();
124 Path filePath = file.toPath().toAbsolutePath().normalize();
125
126 assertTrue(filePath.startsWith(catalinaBase));
127 }
128 }
129
130 @Test
131 void testGetFileWithAllFieldsNoTimestamp() {
132 Object target = mock(Object.class);
133 doReturn(target).when(accessor).getTarget();
134 doReturn("logs").when(accessor).invokeMethod(target, "getDirectory", null, null);
135 doReturn("catalina.").when(accessor).invokeMethod(target, "getPrefix", null, null);
136 doReturn(".log").when(accessor).invokeMethod(target, "getSuffix", null, null);
137
138
139 try (var instrumentsMock = mockStatic(Instruments.class)) {
140 instrumentsMock.when(() -> Instruments.getField(target, "timestamp")).thenReturn(null);
141
142 System.setProperty("catalina.base", "/base");
143
144 File file = accessor.getFile();
145 assertNotNull(file);
146
147 assertTrue(file.getPath().contains("catalina..log"));
148
149 Path catalinaBase = Path.of(System.getProperty("catalina.base")).toAbsolutePath().normalize();
150 Path filePath = file.toPath().toAbsolutePath().normalize();
151
152 assertTrue(filePath.startsWith(catalinaBase));
153 }
154 }
155
156
157
158
159 @Test
160 void testGetFileWithMissingFields() {
161 Object target = mock(Object.class);
162 doReturn(target).when(accessor).getTarget();
163 doReturn(null).when(accessor).invokeMethod(target, "getDirectory", null, null);
164 doReturn("catalina.").when(accessor).invokeMethod(target, "getPrefix", null, null);
165 doReturn(".log").when(accessor).invokeMethod(target, "getSuffix", null, null);
166
167 try (var instrumentsMock = mockStatic(Instruments.class)) {
168 instrumentsMock.when(() -> Instruments.getField(target, "timestamp"))
169 .thenReturn(Boolean.TRUE);
170
171 File file = accessor.getFile();
172 assertNull(file);
173 }
174 }
175
176 }