Add 'log4j2' domain tests to the Log4j 2.x agent
[lttng-ust-java-tests.git] / lttng-ust-java-tests-log4j / src / test / java / org / lttng / ust / agent / integration / events / Log4jLegacyApiIT.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.List;
29
30 import org.apache.log4j.Level;
31 import org.apache.log4j.Logger;
32 import org.junit.jupiter.api.AfterAll;
33 import org.junit.jupiter.api.AfterEach;
34 import org.junit.jupiter.api.BeforeAll;
35 import org.junit.jupiter.api.BeforeEach;
36 import org.junit.jupiter.api.Tag;
37 import org.junit.jupiter.api.Test;
38 import org.junit.jupiter.api.extension.ExtendWith;
39 import org.lttng.tools.ILttngSession;
40 import org.lttng.tools.ILttngSession.Domain;
41 import org.lttng.ust.agent.ILttngHandler;
42 import org.lttng.ust.agent.LTTngAgent;
43 import org.lttng.ust.agent.utils.Log4jTestUtils;
44 import org.lttng.ust.agent.utils.TestPrintExtension;
45
46 /**
47 * Enabled events test for the LTTng-UST Log4j log handler, using the legacy
48 * API.
49 */
50 @ExtendWith(TestPrintExtension.class)
51 @SuppressWarnings("deprecation")
52 @Tag("agent:log4j")
53 @Tag("domain:log4j")
54 public class Log4jLegacyApiIT {
55
56 private static final Domain DOMAIN = Domain.LOG4J;
57
58 private static final String EVENT_NAME_A = "EventA";
59 private static final String EVENT_NAME_B = "EventB";
60
61 private ILttngSession session;
62 private LTTngAgent agent;
63
64 private Logger loggerA;
65 private Logger loggerB;
66
67 /**
68 * Class setup
69 */
70 @BeforeAll
71 public static void log4jClassSetup() {
72 Log4jTestUtils.testClassSetup();
73 }
74
75 /**
76 * Class cleanup
77 */
78 @AfterAll
79 public static void log4jClassCleanup() {
80 Log4jTestUtils.testClassCleanup();
81 }
82
83 /**
84 * Test setup
85 */
86 @BeforeEach
87 public void setup() {
88 loggerA = Logger.getLogger(EVENT_NAME_A);
89 agent = LTTngAgent.getLTTngAgent();
90 loggerB = Logger.getLogger(EVENT_NAME_B);
91
92 loggerA.setLevel(Level.ALL);
93 loggerB.setLevel(Level.ALL);
94
95 session = ILttngSession.createSession(null, DOMAIN);
96 }
97
98 /**
99 * Test cleanup
100 */
101 @AfterEach
102 public void tearDown() {
103 session.close();
104
105 agent.dispose();
106
107 loggerA = null;
108 loggerB = null;
109 }
110
111 /**
112 * Test tracing with no events enabled in the tracing session.
113 */
114 @Test
115 public void testNoEvents() {
116 assertTrue(session.start());
117
118 Log4jTestUtils.send10Events(loggerA);
119 Log4jTestUtils.send10Events(loggerB);
120
121 assertTrue(session.stop());
122
123 List<String> output = session.view();
124 assertNotNull(output);
125 assertTrue(output.isEmpty());
126
127 ILttngHandler handler = getAgentHandler();
128 assertEquals(0, handler.getEventCount());
129 }
130
131 /**
132 * Test tracing with all events enabled (-l -a) in the tracing session.
133 */
134 @Test
135 public void testAllEvents() {
136 assertTrue(session.enableAllEvents());
137 assertTrue(session.start());
138
139 Log4jTestUtils.send10Events(loggerA);
140 Log4jTestUtils.send10Events(loggerB);
141
142 assertTrue(session.stop());
143
144 List<String> output = session.view();
145 assertNotNull(output);
146 assertEquals(20, output.size());
147
148 ILttngHandler handler = getAgentHandler();
149 assertEquals(20, handler.getEventCount());
150 }
151
152 /**
153 * Test tracing with a subset of events enabled in the tracing session.
154 */
155 @Test
156 public void testSomeEvents() {
157 assertTrue(session.enableEvents(EVENT_NAME_A));
158 assertTrue(session.start());
159
160 Log4jTestUtils.send10Events(loggerA);
161 Log4jTestUtils.send10Events(loggerB);
162
163 assertTrue(session.stop());
164
165 List<String> output = session.view();
166 assertNotNull(output);
167 assertEquals(10, output.size());
168
169 ILttngHandler handler = getAgentHandler();
170 assertEquals(10, handler.getEventCount());
171 }
172
173 /**
174 * Test that the "lttng list" commands lists the expected events.
175 */
176 @Test
177 public void testListEvents() {
178 List<String> enabledEvents = session.listEvents();
179 List<String> expectedEvents = Arrays.asList(EVENT_NAME_A, EVENT_NAME_B);
180
181 /*
182 * It doesn't seem possible to forcibly remove Loggers with log4j 1.2.
183 * This, coupled with the way the legacy agent works, makes it so
184 * loggers defined in other tests will always "leak" into this one when
185 * running the whole test suite.
186 *
187 * For this test, simply check that the expected names are present, and
188 * let pass the case where other loggers may the present too.
189 */
190 expectedEvents.forEach(event -> assertTrue(enabledEvents.contains(event)));
191 }
192
193 /**
194 * Get the singleton Log4j Handler currently managed by the LTTngAgent. It
195 * is not public, so we need reflection to access it.
196 *
197 * @return The agent's Log4j handler
198 */
199 private static ILttngHandler getAgentHandler() {
200 try {
201 Field log4jAppenderField = LTTngAgent.class.getDeclaredField("log4jAppender");
202 log4jAppenderField.setAccessible(true);
203 return (ILttngHandler) log4jAppenderField.get(LTTngAgent.getLTTngAgent());
204 } catch (ReflectiveOperationException | SecurityException e) {
205 fail(e.getMessage());
206 return null;
207 }
208 }
209
210 }
211
This page took 0.033441 seconds and 4 git commands to generate.