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  
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   * The Class CatalinaLoggerAccessorTest.
32   */
33  class CatalinaLoggerAccessorTest {
34  
35    /** The accessor. */
36    private CatalinaLoggerAccessor accessor;
37  
38    /**
39     * Sets the up.
40     */
41    @BeforeEach
42    void setUp() {
43      accessor = spy(new CatalinaLoggerAccessor());
44    }
45  
46    /**
47     * Test is context.
48     */
49    @Test
50    void testIsContext() {
51      assertTrue(accessor.isContext());
52    }
53  
54    /**
55     * Test get name.
56     */
57    @Test
58    void testGetName() {
59      assertNull(accessor.getName());
60    }
61  
62    /**
63     * Test get log type.
64     */
65    @Test
66    void testGetLogType() {
67      assertEquals("catalina", accessor.getLogType());
68    }
69  
70    /**
71     * Test get file with all fields.
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      // Simulate timestamp field present
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     * Test get file with missing fields.
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 }