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.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   * The Class CatalinaLoggerAccessorTest.
36   */
37  class CatalinaLoggerAccessorTest {
38  
39    /** The accessor. */
40    private CatalinaLoggerAccessor accessor;
41  
42    /**
43     * Sets the up.
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     * Test is context.
57     */
58    @Test
59    void testIsContext() {
60      assertTrue(accessor.isContext());
61    }
62  
63    /**
64     * Test get name.
65     */
66    @Test
67    void testGetName() {
68      assertNull(accessor.getName());
69    }
70  
71    /**
72     * Test get log type.
73     */
74    @Test
75    void testGetLogType() {
76      assertEquals("catalina", accessor.getLogType());
77    }
78  
79    /**
80     * Test get file with all fields.
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      // Simulate timestamp field present
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     // Simulate timestamp field present
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       // Should be absolute and include catalina.base
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     // Simulate timestamp field absent
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       // Should be absolute and include catalina.base
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    * Test get file with missing fields.
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 }