| 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.After; |
| 27 | import org.junit.AfterClass; |
| 28 | import org.junit.Before; |
| 29 | import org.junit.BeforeClass; |
| 30 | import org.junit.runner.RunWith; |
| 31 | import org.junit.runners.Parameterized; |
| 32 | import org.lttng.tools.ILttngSession.Domain; |
| 33 | import org.lttng.ust.agent.jul.LttngLogHandler; |
| 34 | import org.lttng.ust.agent.utils.JulTestUtils; |
| 35 | |
| 36 | /** |
| 37 | * Implementation of {@link LoggerHierachyListITBase} for JUL log handlers. |
| 38 | * |
| 39 | * @author Alexandre Montplaisir |
| 40 | */ |
| 41 | @RunWith(Parameterized.class) |
| 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 | * Test constructor |
| 52 | * |
| 53 | * @param parentLoggerActive |
| 54 | * Parent logger has been instantiated |
| 55 | * @param parentLoggerHasHandler |
| 56 | * Parent logger has a LTTng handler attached to it |
| 57 | * @param childLoggerActive |
| 58 | * Child logger has been instantiated |
| 59 | * @param childLoggerHasHandler |
| 60 | * Child logger has a LTTng handler attached to it |
| 61 | */ |
| 62 | public JulLoggerHierarchyListIT(boolean parentLoggerActive, |
| 63 | boolean parentLoggerHasHandler, |
| 64 | boolean childLoggerActive, |
| 65 | boolean childLoggerHasHandler) { |
| 66 | /* Set by parameters defined in the base class */ |
| 67 | super(parentLoggerActive, |
| 68 | parentLoggerHasHandler, |
| 69 | childLoggerActive, |
| 70 | childLoggerHasHandler); |
| 71 | } |
| 72 | |
| 73 | // ------------------------------------------------------------------------ |
| 74 | // Maintenance |
| 75 | // ------------------------------------------------------------------------ |
| 76 | |
| 77 | /** |
| 78 | * Class setup |
| 79 | */ |
| 80 | @BeforeClass |
| 81 | public static void julClassSetup() { |
| 82 | JulTestUtils.testClassSetup(); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Class cleanup |
| 87 | */ |
| 88 | @AfterClass |
| 89 | public static void julClassCleanup() { |
| 90 | JulTestUtils.testClassCleanup(); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Test setup |
| 95 | */ |
| 96 | @SuppressWarnings("static-method") |
| 97 | @Before |
| 98 | public void setup() { |
| 99 | /* |
| 100 | * Kind of hackish, but it's the only way to ensure that loggers are |
| 101 | * really removed in-between tests, since LogManager does not provide a |
| 102 | * way to forcibly remove a logger, and it doesn't seem like it will any |
| 103 | * time soon, see http://bugs.java.com/view_bug.do?bug_id=4811930 |
| 104 | */ |
| 105 | LogManager.getLogManager().reset(); |
| 106 | System.gc(); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Test cleanup |
| 111 | */ |
| 112 | @After |
| 113 | public void cleanup() { |
| 114 | if (parentLogger != null) { |
| 115 | if (parentHandler != null) { |
| 116 | parentLogger.removeHandler(parentHandler); |
| 117 | parentHandler.close(); |
| 118 | parentHandler = null; |
| 119 | } |
| 120 | parentLogger = null; |
| 121 | } |
| 122 | |
| 123 | if (childLogger != null) { |
| 124 | if (childHandler != null) { |
| 125 | childLogger.removeHandler(childHandler); |
| 126 | childHandler.close(); |
| 127 | childHandler = null; |
| 128 | } |
| 129 | childLogger = null; |
| 130 | } |
| 131 | |
| 132 | LogManager.getLogManager().reset(); |
| 133 | System.gc(); |
| 134 | } |
| 135 | |
| 136 | // ------------------------------------------------------------------------ |
| 137 | // Abstract methods |
| 138 | // ------------------------------------------------------------------------ |
| 139 | |
| 140 | @Override |
| 141 | protected Domain getDomain() { |
| 142 | return Domain.JUL; |
| 143 | } |
| 144 | |
| 145 | @Override |
| 146 | protected void activateLoggers() throws IOException { |
| 147 | if (parentLoggerActive) { |
| 148 | parentLogger = Logger.getLogger(PARENT_LOGGER); |
| 149 | if (parentLoggerHasHandler) { |
| 150 | parentHandler = new LttngLogHandler(); |
| 151 | parentLogger.addHandler(parentHandler); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | if (childLoggerActive) { |
| 156 | childLogger = Logger.getLogger(CHILD_LOGGER); |
| 157 | if (childLoggerHasHandler) { |
| 158 | childHandler = new LttngLogHandler(); |
| 159 | childLogger.addHandler(childHandler); |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | } |