|
| 1 | +/* |
| 2 | + * Copyright (C) 2017, École Polytechnique de Montréal, Geneviève Bastien <[email protected]> |
| 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 static org.junit.Assert.assertEquals; |
| 22 | +import static org.junit.Assert.assertFalse; |
| 23 | +import static org.junit.Assert.assertNotNull; |
| 24 | +import static org.junit.Assert.assertTrue; |
| 25 | + |
| 26 | +import java.io.IOException; |
| 27 | +import java.util.Arrays; |
| 28 | +import java.util.List; |
| 29 | +import java.util.logging.Handler; |
| 30 | +import java.util.logging.Level; |
| 31 | +import java.util.logging.LogManager; |
| 32 | +import java.util.logging.Logger; |
| 33 | + |
| 34 | +import org.junit.After; |
| 35 | +import org.junit.AfterClass; |
| 36 | +import org.junit.Before; |
| 37 | +import org.junit.BeforeClass; |
| 38 | +import org.junit.Test; |
| 39 | +import org.junit.runner.RunWith; |
| 40 | +import org.junit.runners.Parameterized; |
| 41 | +import org.junit.runners.Parameterized.Parameters; |
| 42 | +import org.lttng.tools.ILttngSession; |
| 43 | +import org.lttng.tools.ILttngSession.Domain; |
| 44 | +import org.lttng.ust.agent.jul.LttngLogHandler; |
| 45 | +import org.lttng.ust.agent.utils.JulTestUtils; |
| 46 | + |
| 47 | +/** |
| 48 | + * Test the {@link LttngLogHandler} log output with source log enabled or not |
| 49 | + * |
| 50 | + * @author Geneviève Bastien |
| 51 | + */ |
| 52 | +@RunWith(Parameterized.class) |
| 53 | +public class JulHandlerLogSourceIT { |
| 54 | + |
| 55 | + private static final Domain DOMAIN = Domain.JUL; |
| 56 | + |
| 57 | + private static final String LOGGER_NAME = "org.lttng.test"; |
| 58 | + |
| 59 | + private ILttngSession session; |
| 60 | + |
| 61 | + private Logger logger; |
| 62 | + |
| 63 | + private final Boolean logSource; |
| 64 | + private final String expectedString; |
| 65 | + |
| 66 | + // ------------------------------------------------------------------------ |
| 67 | + // Test parameter definition |
| 68 | + // ------------------------------------------------------------------------ |
| 69 | + |
| 70 | + /** |
| 71 | + * Get the test parameters, with / without source logging as well as the |
| 72 | + * string that should be present in the events as a result of the event |
| 73 | + * logging. |
| 74 | + * |
| 75 | + * @return The test parameters |
| 76 | + */ |
| 77 | + @Parameters(name = "{index}: logSource={0}") |
| 78 | + public static Iterable<Object[]> testCases() { |
| 79 | + return Arrays.asList(new Object[][] { |
| 80 | + { Boolean.TRUE, "class_name = \"org.lttng.ust.agent.utils.JulTestUtils\", method_name = \"send10EventsTo\"" }, |
| 81 | + { Boolean.FALSE, "class_name = \"\", method_name = \"\"" }, |
| 82 | + }); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Test constructor |
| 87 | + * |
| 88 | + * @param logSource |
| 89 | + * Should source information be logged with the event |
| 90 | + * @param expectedString |
| 91 | + * A string that should be present in the event |
| 92 | + */ |
| 93 | + public JulHandlerLogSourceIT(boolean logSource, |
| 94 | + String expectedString) { |
| 95 | + this.logSource = logSource; |
| 96 | + this.expectedString = expectedString; |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Class setup |
| 101 | + */ |
| 102 | + @BeforeClass |
| 103 | + public static void julClassSetup() { |
| 104 | + JulTestUtils.testClassSetup(); |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Class cleanup |
| 109 | + */ |
| 110 | + @AfterClass |
| 111 | + public static void julClassCleanup() { |
| 112 | + JulTestUtils.testClassCleanup(); |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Test setup |
| 117 | + * |
| 118 | + * @throws IOException |
| 119 | + * Exceptions thrown by handler |
| 120 | + * @throws SecurityException |
| 121 | + * Exceptions thrown by handler |
| 122 | + */ |
| 123 | + @Before |
| 124 | + public void setup() throws SecurityException, IOException { |
| 125 | + /* Clear the JUL logger configuration */ |
| 126 | + LogManager.getLogManager().reset(); |
| 127 | + System.gc(); |
| 128 | + |
| 129 | + /* Initialize the logger */ |
| 130 | + logger = Logger.getLogger(LOGGER_NAME); |
| 131 | + logger.setLevel(Level.ALL); |
| 132 | + |
| 133 | + /* Initialize the handler */ |
| 134 | + LttngLogHandler handler = new LttngLogHandler(); |
| 135 | + handler.setLogSource(logSource); |
| 136 | + logger.addHandler(handler); |
| 137 | + |
| 138 | + /* Create the lttng session */ |
| 139 | + session = ILttngSession.createSession(null, DOMAIN); |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * Test cleanup |
| 144 | + */ |
| 145 | + @After |
| 146 | + public void tearDown() { |
| 147 | + session.close(); |
| 148 | + Handler handler = logger.getHandlers()[0]; |
| 149 | + logger.removeHandler(handler); |
| 150 | + handler.close(); |
| 151 | + |
| 152 | + logger = null; |
| 153 | + } |
| 154 | + |
| 155 | + /** |
| 156 | + * Test tracing some events and see if the output contains the expected string |
| 157 | + */ |
| 158 | + @Test |
| 159 | + public void testHandlerOutput() { |
| 160 | + assertTrue(session.enableAllEvents()); |
| 161 | + assertTrue(session.start()); |
| 162 | + |
| 163 | + JulTestUtils.send10EventsTo(logger); |
| 164 | + |
| 165 | + assertTrue(session.stop()); |
| 166 | + |
| 167 | + List<String> output = session.view(); |
| 168 | + assertNotNull(output); |
| 169 | + assertFalse(output.isEmpty()); |
| 170 | + |
| 171 | + assertEquals(10, output.size()); |
| 172 | + for (String str : output) { |
| 173 | + assertTrue("Validating event string : " + str, str.contains(expectedString)); |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | +} |
| 178 | + |
0 commit comments