| 1 | /* |
| 2 | * Copyright (C) 2016, EfficiOS Inc., Alexandre Montplaisir <alexmonthy@efficios.com> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License along |
| 15 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 16 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 17 | */ |
| 18 | |
| 19 | package org.lttng.ust.agent.integration.events; |
| 20 | |
| 21 | import java.io.IOException; |
| 22 | import java.util.logging.Handler; |
| 23 | import java.util.logging.LogManager; |
| 24 | import java.util.logging.Logger; |
| 25 | |
| 26 | import org.junit.jupiter.api.AfterAll; |
| 27 | import org.junit.jupiter.api.AfterEach; |
| 28 | import org.junit.jupiter.api.BeforeAll; |
| 29 | import org.junit.jupiter.api.BeforeEach; |
| 30 | import org.junit.jupiter.api.Tag; |
| 31 | import org.lttng.tools.ILttngSession.Domain; |
| 32 | import org.lttng.ust.agent.jul.LttngLogHandler; |
| 33 | import org.lttng.ust.agent.utils.JulTestUtils; |
| 34 | |
| 35 | /** |
| 36 | * Implementation of {@link LoggerHierachyListITBase} for JUL log handlers. |
| 37 | * |
| 38 | * @author Alexandre Montplaisir |
| 39 | */ |
| 40 | @Tag("agent:jul") |
| 41 | @Tag("domain:jul") |
| 42 | public class JulLoggerHierarchyListIT extends LoggerHierachyListITBase { |
| 43 | |
| 44 | private Logger parentLogger; |
| 45 | private Logger childLogger; |
| 46 | |
| 47 | private Handler parentHandler; |
| 48 | private Handler childHandler; |
| 49 | |
| 50 | // ------------------------------------------------------------------------ |
| 51 | // Maintenance |
| 52 | // ------------------------------------------------------------------------ |
| 53 | |
| 54 | /** |
| 55 | * Class setup |
| 56 | */ |
| 57 | @BeforeAll |
| 58 | public static void julClassSetup() { |
| 59 | JulTestUtils.testClassSetup(); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Class cleanup |
| 64 | */ |
| 65 | @AfterAll |
| 66 | public static void julClassCleanup() { |
| 67 | JulTestUtils.testClassCleanup(); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Test setup |
| 72 | */ |
| 73 | @SuppressWarnings("static-method") |
| 74 | @BeforeEach |
| 75 | public void setup() { |
| 76 | /* |
| 77 | * Kind of hackish, but it's the only way to ensure that loggers are |
| 78 | * really removed in-between tests, since LogManager does not provide a |
| 79 | * way to forcibly remove a logger, and it doesn't seem like it will any |
| 80 | * time soon, see http://bugs.java.com/view_bug.do?bug_id=4811930 |
| 81 | */ |
| 82 | LogManager.getLogManager().reset(); |
| 83 | System.gc(); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Test cleanup |
| 88 | */ |
| 89 | @AfterEach |
| 90 | public void cleanup() { |
| 91 | if (parentLogger != null) { |
| 92 | if (parentHandler != null) { |
| 93 | parentLogger.removeHandler(parentHandler); |
| 94 | parentHandler.close(); |
| 95 | parentHandler = null; |
| 96 | } |
| 97 | parentLogger = null; |
| 98 | } |
| 99 | |
| 100 | if (childLogger != null) { |
| 101 | if (childHandler != null) { |
| 102 | childLogger.removeHandler(childHandler); |
| 103 | childHandler.close(); |
| 104 | childHandler = null; |
| 105 | } |
| 106 | childLogger = null; |
| 107 | } |
| 108 | |
| 109 | LogManager.getLogManager().reset(); |
| 110 | System.gc(); |
| 111 | } |
| 112 | |
| 113 | // ------------------------------------------------------------------------ |
| 114 | // Abstract methods |
| 115 | // ------------------------------------------------------------------------ |
| 116 | |
| 117 | @Override |
| 118 | protected Domain getDomain() { |
| 119 | return Domain.JUL; |
| 120 | } |
| 121 | |
| 122 | @Override |
| 123 | protected void activateLoggers(boolean parentLoggerActive, |
| 124 | boolean parentLoggerHasHandler, |
| 125 | boolean childLoggerActive, |
| 126 | boolean childLoggerHasHandler) throws IOException { |
| 127 | if (parentLoggerActive) { |
| 128 | parentLogger = Logger.getLogger(PARENT_LOGGER); |
| 129 | if (parentLoggerHasHandler) { |
| 130 | parentHandler = new LttngLogHandler(); |
| 131 | parentLogger.addHandler(parentHandler); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | if (childLoggerActive) { |
| 136 | childLogger = Logger.getLogger(CHILD_LOGGER); |
| 137 | if (childLoggerHasHandler) { |
| 138 | childHandler = new LttngLogHandler(); |
| 139 | childLogger.addHandler(childHandler); |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | } |