View Javadoc
1   /*
2    * Licensed under the GPL License. You may not use this file except in compliance with the License.
3    * You may obtain a copy of the License at
4    *
5    *   https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
6    *
7    * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
8    * WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
9    * PURPOSE.
10   */
11  package psiprobe.beans;
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.when;
20  
21  import java.io.File;
22  import java.nio.file.Path;
23  import java.util.Arrays;
24  import java.util.Collections;
25  import java.util.List;
26  
27  import org.junit.jupiter.api.BeforeEach;
28  import org.junit.jupiter.api.Test;
29  import org.mockito.Mockito;
30  
31  import psiprobe.tools.logging.FileLogAccessor;
32  import psiprobe.tools.logging.LogDestination;
33  
34  class LogResolverBeanTest {
35  
36    private LogResolverBean bean;
37    private ContainerWrapperBean containerWrapper;
38  
39    @BeforeEach
40    void setUp() {
41      bean = new LogResolverBean();
42      containerWrapper = mock(ContainerWrapperBean.class);
43      bean.setContainerWrapper(containerWrapper);
44    }
45  
46    @Test
47    void testGetAndSetStdoutFiles() {
48      List<String> files = Arrays.asList("catalina.out", "stdout.log");
49      bean.setStdoutFiles(files);
50      assertEquals(files, bean.getStdoutFiles());
51    }
52  
53    @Test
54    void testGetLogDestinations_Empty() {
55      // Instruments.isInitialized() returns false, so should be empty
56      bean.setStdoutFiles(Collections.emptyList());
57      // Mock Instruments.isInitialized() to return false
58      try (var mocked = org.mockito.Mockito.mockStatic(psiprobe.tools.Instruments.class)) {
59        mocked.when(psiprobe.tools.Instruments::isInitialized).thenReturn(false);
60        List<LogDestination> result = bean.getLogDestinations(true);
61        assertTrue(result.isEmpty());
62      }
63    }
64  
65    @Test
66    void testGetLogSourcesByFile() {
67      Path file = Path.of("test.log");
68      LogDestination dest = mock(LogDestination.class);
69      when(dest.getFile()).thenReturn(file.toFile());
70  
71      LogResolverBean spyBean = Mockito.spy(bean);
72      doReturn(List.of(dest)).when(spyBean).getLogSources();
73  
74      List<LogDestination> result = spyBean.getLogSources(file.toFile());
75      assertEquals(1, result.size());
76      assertEquals(dest, result.get(0));
77    }
78  
79    @Test
80    void testGetLogSources_Empty() {
81      // Instruments.isInitialized() returns false, so should be empty
82      try (var mocked = org.mockito.Mockito.mockStatic(psiprobe.tools.Instruments.class)) {
83        mocked.when(psiprobe.tools.Instruments::isInitialized).thenReturn(false);
84        List<LogDestination> result = bean.getLogSources();
85        assertTrue(result.isEmpty());
86      }
87    }
88  
89    @Test
90    void testGetLogDestination_Stdout() {
91      bean.setStdoutFiles(List.of("stdout.log"));
92      // Create a file that exists
93      File file = mock(File.class);
94      when(file.exists()).thenReturn(true);
95  
96      // Spy to override resolveStdoutLogDestination
97      LogResolverBean spyBean = Mockito.spy(bean);
98      FileLogAccessor accessor = new FileLogAccessor();
99      accessor.setFile(file);
100     accessor.setName("stdout.log");
101     doReturn(accessor).when(spyBean).resolveStdoutLogDestination("stdout.log");
102 
103     LogDestination result =
104         spyBean.getLogDestination("stdout", null, false, false, "stdout.log", null);
105     assertNotNull(result);
106     assertEquals("stdout.log", result.getName());
107   }
108 
109   @Test
110   void testGetLogDestination_UnknownType() {
111     LogDestination result = bean.getLogDestination("unknown", null, false, false, null, null);
112     assertNull(result);
113   }
114 
115   /**
116    * Test set and get container wrapper.
117    */
118   @Test
119   void testSetAndGetContainerWrapper() {
120     ContainerWrapperBean cw = mock(ContainerWrapperBean.class);
121     bean.setContainerWrapper(cw);
122     assertEquals(cw, bean.getContainerWrapper());
123   }
124 
125 }