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.jdk;
12  
13  import static org.junit.jupiter.api.Assertions.assertFalse;
14  import static org.junit.jupiter.api.Assertions.assertNull;
15  import static org.junit.jupiter.api.Assertions.assertTrue;
16  import static org.mockito.Mockito.*;
17  
18  import org.junit.jupiter.api.BeforeEach;
19  import org.junit.jupiter.api.Test;
20  
21  /**
22   * The Class Jdk14LoggerAccessorTest.
23   */
24  class Jdk14LoggerAccessorTest {
25  
26    /** The accessor. */
27    private Jdk14LoggerAccessor accessor;
28  
29    /** The mock logger. */
30    private Object mockLogger;
31  
32    /**
33     * Sets the up.
34     */
35    @BeforeEach
36    void setUp() {
37      accessor = new Jdk14LoggerAccessor();
38      mockLogger = mock(Object.class);
39      accessor.setTarget(mockLogger);
40    }
41  
42    /**
43     * Test set and is context.
44     */
45    @Test
46    void testSetAndIsContext() {
47      accessor.setContext(true);
48      assertTrue(accessor.isContext());
49      accessor.setContext(false);
50      assertFalse(accessor.isContext());
51    }
52  
53    /**
54     * Test is root when name is null.
55     */
56    @Test
57    void testIsRootWhenNameIsNull() {
58      Jdk14LoggerAccessor spyAccessor = spy(accessor);
59      doReturn(null).when(spyAccessor).getName();
60      assertTrue(spyAccessor.isRoot());
61    }
62  
63    /**
64     * Test is root when juli root.
65     */
66    @Test
67    void testIsRootWhenJuliRoot() {
68      Jdk14LoggerAccessor spyAccessor = spy(accessor);
69      doReturn("org.apache.juli.ClassLoaderLogManager$RootLogger").when(spyAccessor).getTargetClass();
70      assertTrue(spyAccessor.isRoot());
71    }
72  
73    /**
74     * Test get handler with invalid index.
75     */
76    @Test
77    void testGetHandlerWithInvalidIndex() {
78      Jdk14LoggerAccessor spyAccessor = spy(accessor);
79      Jdk14HandlerAccessor handler = spyAccessor.getHandler("notAnInt");
80      assertNull(handler);
81    }
82  
83    /**
84     * Test set level handles exception.
85     */
86    @Test
87    void testSetLevelHandlesException() {
88      accessor.setLevel("INVALID_LEVEL");
89      // No exception should be thrown
90    }
91  }