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
CommitLineData
2b408e85
AM
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
4821eac9 19package org.lttng.ust.agent.integration.events;
c5524c71 20
7a4f0255
MJ
21import static org.junit.jupiter.api.Assertions.assertEquals;
22import static org.junit.jupiter.api.Assertions.assertNotNull;
23import static org.junit.jupiter.api.Assertions.assertTrue;
24import static org.junit.jupiter.api.Assertions.fail;
c5524c71 25
c5524c71 26import java.lang.reflect.Field;
bda25414
AM
27import java.util.Arrays;
28import java.util.Collections;
c5524c71
AM
29import java.util.List;
30import java.util.logging.Level;
9d06e3eb 31import java.util.logging.LogManager;
c5524c71
AM
32import java.util.logging.Logger;
33
7a4f0255
MJ
34import org.junit.jupiter.api.AfterAll;
35import org.junit.jupiter.api.AfterEach;
36import org.junit.jupiter.api.BeforeAll;
37import org.junit.jupiter.api.BeforeEach;
9db2c69a 38import org.junit.jupiter.api.Tag;
7a4f0255
MJ
39import org.junit.jupiter.api.Test;
40import org.junit.jupiter.api.extension.ExtendWith;
45d1768c
AM
41import org.lttng.tools.ILttngSession;
42import org.lttng.tools.ILttngSession.Domain;
c5524c71
AM
43import org.lttng.ust.agent.ILttngHandler;
44import org.lttng.ust.agent.LTTngAgent;
4821eac9 45import org.lttng.ust.agent.utils.JulTestUtils;
7a4f0255 46import org.lttng.ust.agent.utils.TestPrintExtension;
c5524c71 47
8a0613fa
AM
48/**
49 * Enabled events test for the LTTng-UST JUL log handler, using the legacy API.
50 */
7a4f0255 51@ExtendWith(TestPrintExtension.class)
c5524c71 52@SuppressWarnings("deprecation")
9db2c69a
MJ
53@Tag("agent:jul")
54@Tag("domain:jul")
7b82be36 55public class JulLegacyApiIT {
c5524c71
AM
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
45d1768c 62 private ILttngSession session;
3e1f7ff4 63 private LTTngAgent agent;
8576633f 64
c5524c71
AM
65 private Logger loggerA;
66 private Logger loggerB;
67
8a0613fa
AM
68 /**
69 * Class setup
70 */
7a4f0255 71 @BeforeAll
c5524c71 72 public static void julClassSetup() {
eca27046 73 JulTestUtils.testClassSetup();
c5524c71
AM
74 }
75
8a0613fa
AM
76 /**
77 * Class cleanup
78 */
7a4f0255 79 @AfterAll
c5524c71 80 public static void julClassCleanup() {
eca27046 81 JulTestUtils.testClassCleanup();
c5524c71
AM
82 }
83
8a0613fa
AM
84 /**
85 * Test setup
86 */
7a4f0255 87 @BeforeEach
c5524c71 88 public void setup() {
9d06e3eb
AM
89 /* Clear the JUL logger configuration */
90 LogManager.getLogManager().reset();
91 System.gc();
92
c5524c71 93 loggerA = Logger.getLogger(EVENT_NAME_A);
3e1f7ff4 94 agent = LTTngAgent.getLTTngAgent();
c5524c71
AM
95 loggerB = Logger.getLogger(EVENT_NAME_B);
96
97 loggerA.setLevel(Level.ALL);
98 loggerB.setLevel(Level.ALL);
8576633f 99
ff620bef 100 session = ILttngSession.createSession(null, DOMAIN);
c5524c71
AM
101 }
102
8a0613fa
AM
103 /**
104 * Test cleanup
105 */
7a4f0255 106 @AfterEach
c5524c71 107 public void tearDown() {
8576633f 108 session.close();
c5524c71 109
3e1f7ff4 110 agent.dispose();
c5524c71
AM
111
112 loggerA = null;
113 loggerB = null;
114 }
115
8a0613fa
AM
116 /**
117 * Test tracing with no events enabled in the tracing session.
118 */
c5524c71
AM
119 @Test
120 public void testNoEvents() {
8576633f 121 assertTrue(session.start());
c5524c71 122
8576633f
AM
123 JulTestUtils.send10EventsTo(loggerA);
124 JulTestUtils.send10EventsTo(loggerB);
c5524c71 125
8576633f 126 assertTrue(session.stop());
c5524c71 127
8576633f 128 List<String> output = session.view();
c5524c71
AM
129 assertNotNull(output);
130 assertTrue(output.isEmpty());
131
c5524c71
AM
132 ILttngHandler handler = getAgentHandler();
133 assertEquals(0, handler.getEventCount());
134 }
135
8a0613fa
AM
136 /**
137 * Test tracing with all events enabled (-j -a) in the tracing session.
138 */
c5524c71
AM
139 @Test
140 public void testAllEvents() {
8576633f
AM
141 assertTrue(session.enableAllEvents());
142 assertTrue(session.start());
c5524c71 143
8576633f
AM
144 JulTestUtils.send10EventsTo(loggerA);
145 JulTestUtils.send10EventsTo(loggerB);
c5524c71 146
8576633f 147 assertTrue(session.stop());
c5524c71 148
8576633f 149 List<String> output = session.view();
c5524c71
AM
150 assertNotNull(output);
151 assertEquals(20, output.size());
152
c5524c71
AM
153 ILttngHandler handler = getAgentHandler();
154 assertEquals(20, handler.getEventCount());
155 }
156
8a0613fa
AM
157 /**
158 * Test tracing with a subset of events enabled in the tracing session.
159 */
c5524c71
AM
160 @Test
161 public void testSomeEvents() {
8576633f
AM
162 assertTrue(session.enableEvents(EVENT_NAME_A));
163 assertTrue(session.start());
c5524c71 164
8576633f
AM
165 JulTestUtils.send10EventsTo(loggerA);
166 JulTestUtils.send10EventsTo(loggerB);
c5524c71 167
8576633f 168 assertTrue(session.stop());
c5524c71 169
8576633f 170 List<String> output = session.view();
c5524c71
AM
171 assertNotNull(output);
172 assertEquals(10, output.size());
173
c5524c71
AM
174 ILttngHandler handler = getAgentHandler();
175 assertEquals(10, handler.getEventCount());
176 }
177
bda25414
AM
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
c5524c71
AM
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 */
8576633f 198 private static ILttngHandler getAgentHandler() {
c5524c71
AM
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) {
1f3dd43c 204 fail(e.getMessage());
c5524c71
AM
205 return null;
206 }
207 }
208
209}
210
This page took 0.032731 seconds and 4 git commands to generate.