1
2
3
4
5
6
7
8
9
10
11 package psiprobe.beans;
12
13 import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
14 import static org.junit.jupiter.api.Assertions.assertEquals;
15 import static org.junit.jupiter.api.Assertions.assertNotNull;
16 import static org.junit.jupiter.api.Assertions.assertNull;
17 import static org.junit.jupiter.api.Assertions.assertTrue;
18 import static org.mockito.Mockito.doReturn;
19 import static org.mockito.Mockito.mock;
20 import static org.mockito.Mockito.when;
21
22 import jakarta.servlet.ServletContext;
23
24 import java.io.File;
25 import java.nio.file.Path;
26 import java.util.List;
27
28 import org.apache.catalina.Context;
29 import org.junit.jupiter.api.BeforeEach;
30 import org.junit.jupiter.api.Test;
31 import org.mockito.Mockito;
32 import org.springframework.test.util.ReflectionTestUtils;
33
34 import psiprobe.TomcatContainer;
35 import psiprobe.tools.logging.FileLogAccessor;
36 import psiprobe.tools.logging.LogDestination;
37
38 class LogResolverBeanTest {
39
40 private LogResolverBean bean;
41 private ContainerWrapperBean containerWrapper;
42
43 @BeforeEach
44 void setUp() {
45 bean = new LogResolverBean();
46 containerWrapper = mock(ContainerWrapperBean.class);
47 ReflectionTestUtils.setField(bean, "containerWrapper", containerWrapper);
48 }
49
50 @Test
51 void testGetAndSetStdoutFiles() {
52 List<String> files = List.of("catalina.out", "stdout.log");
53 bean.setStdoutFiles(files);
54 assertEquals(files, bean.getStdoutFiles());
55 }
56
57 @Test
58 void testGetLogDestinations_Empty() {
59
60 bean.setStdoutFiles(List.of());
61
62 try (var mocked = org.mockito.Mockito.mockStatic(psiprobe.tools.Instruments.class)) {
63 mocked.when(psiprobe.tools.Instruments::isInitialized).thenReturn(false);
64 List<LogDestination> result = bean.getLogDestinations(true);
65 assertTrue(result.isEmpty());
66 }
67 }
68
69 @Test
70 void testGetLogSourcesByFile() {
71 Path file = Path.of("test.log");
72 LogDestination dest = mock(LogDestination.class);
73 when(dest.getFile()).thenReturn(file.toFile());
74
75 LogResolverBean spyBean = Mockito.spy(bean);
76 doReturn(List.of(dest)).when(spyBean).getLogSources();
77
78 List<LogDestination> result = spyBean.getLogSources(file.toFile());
79 assertEquals(1, result.size());
80 assertEquals(dest, result.get(0));
81 }
82
83 @Test
84 void testGetLogSources_Empty() {
85
86 try (var mocked = org.mockito.Mockito.mockStatic(psiprobe.tools.Instruments.class)) {
87 mocked.when(psiprobe.tools.Instruments::isInitialized).thenReturn(false);
88 List<LogDestination> result = bean.getLogSources();
89 assertTrue(result.isEmpty());
90 }
91 }
92
93 @Test
94 void testGetLogDestination_Stdout() {
95 bean.setStdoutFiles(List.of("stdout.log"));
96
97 File file = mock(File.class);
98 when(file.exists()).thenReturn(true);
99
100
101 LogResolverBean spyBean = Mockito.spy(bean);
102 FileLogAccessor accessor = new FileLogAccessor();
103 accessor.setFile(file);
104 accessor.setName("stdout.log");
105 doReturn(accessor).when(spyBean).resolveStdoutLogDestination("stdout.log");
106
107 LogDestination result =
108 spyBean.getLogDestination("stdout", null, false, false, "stdout.log", null);
109 assertNotNull(result);
110 assertEquals("stdout.log", result.getName());
111 }
112
113 @Test
114 void testGetLogDestination_UnknownType() {
115 LogDestination result = bean.getLogDestination("unknown", null, false, false, null, null);
116 assertNull(result);
117 }
118
119
120
121
122 @Test
123 void testGetLogDestinations_NullContextLoader() {
124 System.setProperty("catalina.base", System.getProperty("java.io.tmpdir"));
125 try {
126 Context ctx = mock(Context.class);
127 when(ctx.getLoader()).thenReturn(null);
128 when(ctx.getName()).thenReturn("/testapp");
129 ServletContext servletContext = mock(ServletContext.class);
130 when(ctx.getServletContext()).thenReturn(servletContext);
131
132 TomcatContainer tomcatContainer = mock(TomcatContainer.class);
133 when(tomcatContainer.findContexts()).thenReturn(List.of(ctx));
134 when(containerWrapper.getTomcatContainer()).thenReturn(tomcatContainer);
135
136 try (var mocked = org.mockito.Mockito.mockStatic(psiprobe.tools.Instruments.class)) {
137 mocked.when(psiprobe.tools.Instruments::isInitialized).thenReturn(true);
138 assertDoesNotThrow(() -> bean.getLogDestinations(true));
139 }
140 } finally {
141 System.clearProperty("catalina.base");
142 }
143 }
144
145
146
147
148 @Test
149 void testGetLogDestination_NullContextLoader() {
150 Context ctx = mock(Context.class);
151 when(ctx.getLoader()).thenReturn(null);
152 when(ctx.getName()).thenReturn("/testapp");
153 ServletContext servletContext = mock(ServletContext.class);
154 when(ctx.getServletContext()).thenReturn(servletContext);
155
156 TomcatContainer tomcatContainer = mock(TomcatContainer.class);
157 when(tomcatContainer.findContext("/testapp")).thenReturn(ctx);
158 when(containerWrapper.getTomcatContainer()).thenReturn(tomcatContainer);
159
160
161 assertDoesNotThrow(() -> bean.getLogDestination("jdk", "/testapp", false, true, null, "0"));
162 }
163
164 }