2 * Copyright (C) 2015, EfficiOS Inc., Alexandre Montplaisir <alexmonthy@efficios.com>
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.
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.
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.
19 package org
.lttng
.ust
.agent
.integration
.events
;
21 import static org
.junit
.jupiter
.api
.Assertions
.assertEquals
;
22 import static org
.junit
.jupiter
.api
.Assertions
.assertNotNull
;
23 import static org
.junit
.jupiter
.api
.Assertions
.assertTrue
;
24 import static org
.junit
.jupiter
.api
.Assertions
.fail
;
26 import java
.lang
.reflect
.Field
;
27 import java
.util
.Arrays
;
28 import java
.util
.Collections
;
29 import java
.util
.List
;
30 import java
.util
.logging
.Level
;
31 import java
.util
.logging
.LogManager
;
32 import java
.util
.logging
.Logger
;
34 import org
.junit
.jupiter
.api
.AfterAll
;
35 import org
.junit
.jupiter
.api
.AfterEach
;
36 import org
.junit
.jupiter
.api
.BeforeAll
;
37 import org
.junit
.jupiter
.api
.BeforeEach
;
38 import org
.junit
.jupiter
.api
.Tag
;
39 import org
.junit
.jupiter
.api
.Test
;
40 import org
.junit
.jupiter
.api
.extension
.ExtendWith
;
41 import org
.lttng
.tools
.ILttngSession
;
42 import org
.lttng
.tools
.ILttngSession
.Domain
;
43 import org
.lttng
.ust
.agent
.ILttngHandler
;
44 import org
.lttng
.ust
.agent
.LTTngAgent
;
45 import org
.lttng
.ust
.agent
.utils
.JulTestUtils
;
46 import org
.lttng
.ust
.agent
.utils
.TestPrintExtension
;
49 * Enabled events test for the LTTng-UST JUL log handler, using the legacy API.
51 @ExtendWith(TestPrintExtension
.class)
52 @SuppressWarnings("deprecation")
55 public class JulLegacyApiIT
{
57 private static final Domain DOMAIN
= Domain
.JUL
;
59 private static final String EVENT_NAME_A
= "EventA";
60 private static final String EVENT_NAME_B
= "EventB";
62 private ILttngSession session
;
63 private LTTngAgent agent
;
65 private Logger loggerA
;
66 private Logger loggerB
;
72 public static void julClassSetup() {
73 JulTestUtils
.testClassSetup();
80 public static void julClassCleanup() {
81 JulTestUtils
.testClassCleanup();
89 /* Clear the JUL logger configuration */
90 LogManager
.getLogManager().reset();
93 loggerA
= Logger
.getLogger(EVENT_NAME_A
);
94 agent
= LTTngAgent
.getLTTngAgent();
95 loggerB
= Logger
.getLogger(EVENT_NAME_B
);
97 loggerA
.setLevel(Level
.ALL
);
98 loggerB
.setLevel(Level
.ALL
);
100 session
= ILttngSession
.createSession(null, DOMAIN
);
107 public void tearDown() {
117 * Test tracing with no events enabled in the tracing session.
120 public void testNoEvents() {
121 assertTrue(session
.start());
123 JulTestUtils
.send10EventsTo(loggerA
);
124 JulTestUtils
.send10EventsTo(loggerB
);
126 assertTrue(session
.stop());
128 List
<String
> output
= session
.view();
129 assertNotNull(output
);
130 assertTrue(output
.isEmpty());
132 ILttngHandler handler
= getAgentHandler();
133 assertEquals(0, handler
.getEventCount());
137 * Test tracing with all events enabled (-j -a) in the tracing session.
140 public void testAllEvents() {
141 assertTrue(session
.enableAllEvents());
142 assertTrue(session
.start());
144 JulTestUtils
.send10EventsTo(loggerA
);
145 JulTestUtils
.send10EventsTo(loggerB
);
147 assertTrue(session
.stop());
149 List
<String
> output
= session
.view();
150 assertNotNull(output
);
151 assertEquals(20, output
.size());
153 ILttngHandler handler
= getAgentHandler();
154 assertEquals(20, handler
.getEventCount());
158 * Test tracing with a subset of events enabled in the tracing session.
161 public void testSomeEvents() {
162 assertTrue(session
.enableEvents(EVENT_NAME_A
));
163 assertTrue(session
.start());
165 JulTestUtils
.send10EventsTo(loggerA
);
166 JulTestUtils
.send10EventsTo(loggerB
);
168 assertTrue(session
.stop());
170 List
<String
> output
= session
.view();
171 assertNotNull(output
);
172 assertEquals(10, output
.size());
174 ILttngHandler handler
= getAgentHandler();
175 assertEquals(10, handler
.getEventCount());
179 * Test that the "lttng list" commands lists the expected events.
182 public void testListEvents() {
183 List
<String
> enabledEvents
= session
.listEvents();
184 List
<String
> expectedEvents
= Arrays
.asList(EVENT_NAME_A
, EVENT_NAME_B
);
186 Collections
.sort(enabledEvents
);
187 Collections
.sort(expectedEvents
);
189 assertEquals(expectedEvents
, enabledEvents
);
193 * Get the singleton JUL Handler currently managed by the LTTngAgent. It is
194 * not public, so we need reflection to access it.
196 * @return The agent's JUL handler
198 private static ILttngHandler
getAgentHandler() {
200 Field julHandlerField
= LTTngAgent
.class.getDeclaredField("julHandler");
201 julHandlerField
.setAccessible(true);
202 return (ILttngHandler
) julHandlerField
.get(LTTngAgent
.getLTTngAgent());
203 } catch (ReflectiveOperationException
| SecurityException e
) {
204 fail(e
.getMessage());