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.commons;
12  
13  import static org.junit.jupiter.api.Assertions.*;
14  
15  import java.util.List;
16  import java.util.UUID;
17  import java.util.logging.ConsoleHandler;
18  import java.util.logging.Logger;
19  
20  import org.junit.jupiter.api.Test;
21  
22  import psiprobe.tools.logging.LogDestination;
23  
24  /** Tests for commons logging visitors and accessor behavior with JUL-backed loggers. */
25  class CommonsLoggerAccessorTest {
26  
27    @Test
28    void getDestinationsCollectsDestinationsFromLoggerHierarchy() {
29      Logger parent = Logger.getLogger("psiprobe.parent." + UUID.randomUUID());
30      Logger child = Logger.getLogger("psiprobe.child." + UUID.randomUUID());
31  
32      parent.setUseParentHandlers(false);
33      child.setUseParentHandlers(false);
34      child.setParent(parent);
35  
36      parent.addHandler(new ConsoleHandler());
37      child.addHandler(new ConsoleHandler());
38  
39      CommonsLoggerAccessor accessor = new CommonsLoggerAccessor();
40      accessor.setTarget(new LoggerHolder(child));
41  
42      List<LogDestination> destinations = accessor.getDestinations();
43  
44      assertTrue(destinations.size() >= 2);
45    }
46  
47    @Test
48    void getDestinationReturnsIndexedDestinationWhenPresent() {
49      Logger logger = Logger.getLogger("psiprobe.single." + UUID.randomUUID());
50      logger.setUseParentHandlers(false);
51      logger.addHandler(new ConsoleHandler());
52  
53      CommonsLoggerAccessor accessor = new CommonsLoggerAccessor();
54      accessor.setTarget(new LoggerHolder(logger));
55  
56      LogDestination destination = accessor.getDestination("0");
57  
58      assertNotNull(destination);
59    }
60  
61    @Test
62    void getDestinationReturnsNullWhenIndexDoesNotExist() {
63      Logger logger = Logger.getLogger("psiprobe.invalid." + UUID.randomUUID());
64      logger.setUseParentHandlers(false);
65      logger.addHandler(new ConsoleHandler());
66  
67      CommonsLoggerAccessor accessor = new CommonsLoggerAccessor();
68      accessor.setTarget(new LoggerHolder(logger));
69  
70      LogDestination destination = accessor.getDestination("5");
71  
72      assertNull(destination);
73    }
74  
75    @Test
76    void getDestinationsReturnsEmptyForUnknownOrMissingLogger() {
77      CommonsLoggerAccessor unknownLoggerAccessor = new CommonsLoggerAccessor();
78      unknownLoggerAccessor.setTarget(new LoggerHolder(new Object()));
79      assertTrue(unknownLoggerAccessor.getDestinations().isEmpty());
80  
81      CommonsLoggerAccessor nullLoggerAccessor = new CommonsLoggerAccessor();
82      nullLoggerAccessor.setTarget(new LoggerHolder(null));
83      assertTrue(nullLoggerAccessor.getDestinations().isEmpty());
84      assertNull(nullLoggerAccessor.getDestination("0"));
85    }
86  
87    private static final class LoggerHolder {
88      @SuppressWarnings("unused")
89      private final Object logger;
90  
91      private LoggerHolder(Object logger) {
92        this.logger = logger;
93      }
94    }
95  }