Add 'log4j2' domain tests to the Log4j 2.x agent
[lttng-ust-java-tests.git] / lttng-ust-java-tests-jul / src / test / java / org / lttng / ust / agent / integration / events / JulLegacyApiIT.java
1 /*
2 * Copyright (C) 2015, 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 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;
25
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;
33
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;
47
48 /**
49 * Enabled events test for the LTTng-UST JUL log handler, using the legacy API.
50 */
51 @ExtendWith(TestPrintExtension.class)
52 @SuppressWarnings("deprecation")
53 @Tag("agent:jul")
54 @Tag("domain:jul")
55 public class JulLegacyApiIT {
56
57 private static final Domain DOMAIN = Domain.JUL;
58
59 private static final String EVENT_NAME_A = "EventA";
60 private static final String EVENT_NAME_B = "EventB";
61
62 private ILttngSession session;
63 private LTTngAgent agent;
64
65 private Logger loggerA;
66 private Logger loggerB;
67
68 /**
69 * Class setup
70 */
71 @BeforeAll
72 public static void julClassSetup() {
73 JulTestUtils.testClassSetup();
74 }
75
76 /**
77 * Class cleanup
78 */
79 @AfterAll
80 public static void julClassCleanup() {
81 JulTestUtils.testClassCleanup();
82 }
83
84 /**
85 * Test setup
86 */
87 @BeforeEach
88 public void setup() {
89 /* Clear the JUL logger configuration */
90 LogManager.getLogManager().reset();
91 System.gc();
92
93 loggerA = Logger.getLogger(EVENT_NAME_A);
94 agent = LTTngAgent.getLTTngAgent();
95 loggerB = Logger.getLogger(EVENT_NAME_B);
96
97 loggerA.setLevel(Level.ALL);
98 loggerB.setLevel(Level.ALL);
99
100 session = ILttngSession.createSession(null, DOMAIN);
101 }
102
103 /**
104 * Test cleanup
105 */
106 @AfterEach
107 public void tearDown() {
108 session.close();
109
110 agent.dispose();
111
112 loggerA = null;
113 loggerB = null;
114 }
115
116 /**
117 * Test tracing with no events enabled in the tracing session.
118 */
119 @Test
120 public void testNoEvents() {
121 assertTrue(session.start());
122
123 JulTestUtils.send10EventsTo(loggerA);
124 JulTestUtils.send10EventsTo(loggerB);
125
126 assertTrue(session.stop());
127
128 List<String> output = session.view();
129 assertNotNull(output);
130 assertTrue(output.isEmpty());
131
132 ILttngHandler handler = getAgentHandler();
133 assertEquals(0, handler.getEventCount());
134 }
135
136 /**
137 * Test tracing with all events enabled (-j -a) in the tracing session.
138 */
139 @Test
140 public void testAllEvents() {
141 assertTrue(session.enableAllEvents());
142 assertTrue(session.start());
143
144 JulTestUtils.send10EventsTo(loggerA);
145 JulTestUtils.send10EventsTo(loggerB);
146
147 assertTrue(session.stop());
148
149 List<String> output = session.view();
150 assertNotNull(output);
151 assertEquals(20, output.size());
152
153 ILttngHandler handler = getAgentHandler();
154 assertEquals(20, handler.getEventCount());
155 }
156
157 /**
158 * Test tracing with a subset of events enabled in the tracing session.
159 */
160 @Test
161 public void testSomeEvents() {
162 assertTrue(session.enableEvents(EVENT_NAME_A));
163 assertTrue(session.start());
164
165 JulTestUtils.send10EventsTo(loggerA);
166 JulTestUtils.send10EventsTo(loggerB);
167
168 assertTrue(session.stop());
169
170 List<String> output = session.view();
171 assertNotNull(output);
172 assertEquals(10, output.size());
173
174 ILttngHandler handler = getAgentHandler();
175 assertEquals(10, handler.getEventCount());
176 }
177
178 /**
179 * Test that the "lttng list" commands lists the expected events.
180 */
181 @Test
182 public void testListEvents() {
183 List<String> enabledEvents = session.listEvents();
184 List<String> expectedEvents = Arrays.asList(EVENT_NAME_A, EVENT_NAME_B);
185
186 Collections.sort(enabledEvents);
187 Collections.sort(expectedEvents);
188
189 assertEquals(expectedEvents, enabledEvents);
190 }
191
192 /**
193 * Get the singleton JUL Handler currently managed by the LTTngAgent. It is
194 * not public, so we need reflection to access it.
195 *
196 * @return The agent's JUL handler
197 */
198 private static ILttngHandler getAgentHandler() {
199 try {
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());
205 return null;
206 }
207 }
208
209 }
210
This page took 0.035018 seconds and 4 git commands to generate.