1 package org
.lttng
.ust
.agent
.integration
.log4j
;
3 import static org
.junit
.Assert
.assertEquals
;
4 import static org
.junit
.Assert
.assertNotNull
;
5 import static org
.junit
.Assert
.assertTrue
;
6 import static org
.junit
.Assert
.fail
;
7 import static org
.junit
.Assume
.assumeTrue
;
9 import java
.io
.IOException
;
10 import java
.lang
.reflect
.Field
;
11 import java
.util
.List
;
13 import org
.apache
.log4j
.Level
;
14 import org
.apache
.log4j
.Logger
;
15 import org
.junit
.After
;
16 import org
.junit
.AfterClass
;
17 import org
.junit
.Before
;
18 import org
.junit
.BeforeClass
;
19 import org
.junit
.Test
;
20 import org
.lttng
.ust
.agent
.ILttngHandler
;
21 import org
.lttng
.ust
.agent
.LTTngAgent
;
22 import org
.lttng
.ust
.agent
.log4j
.LttngLogAppender
;
23 import org
.lttng
.ust
.agent
.utils
.LttngSessionControl
;
24 import org
.lttng
.ust
.agent
.utils
.LttngSessionControl
.Domain
;
26 @SuppressWarnings("deprecation")
27 public class Log4jLegacyApiTest
{
29 private static final Domain DOMAIN
= Domain
.LOG4J
;
31 private static final String EVENT_NAME_A
= "EventA";
32 private static final String EVENT_NAME_B
= "EventB";
34 private Logger loggerA
;
35 private Logger loggerB
;
38 public static void classSetup() {
39 /* Skip tests if we can't find the JNI library or lttng-tools */
41 LttngLogAppender testAppender
= new LttngLogAppender();
43 } catch (SecurityException
| IOException e
) {
47 boolean ret1
= LttngSessionControl
.setupSession(null, DOMAIN
);
48 boolean ret2
= LttngSessionControl
.stopSession(null);
49 /* "lttng view" also tests that Babeltrace is installed and working */
50 List
<String
> contents
= LttngSessionControl
.viewSession(null);
51 boolean ret3
= LttngSessionControl
.destroySession(null);
52 assumeTrue(ret1
&& ret2
&& ret3
);
53 assumeTrue(contents
.isEmpty());
57 public static void classCleanup() {
58 LttngSessionControl
.deleteAllTracee();
63 loggerA
= Logger
.getLogger(EVENT_NAME_A
);
64 LTTngAgent
.getLTTngAgent();
65 loggerB
= Logger
.getLogger(EVENT_NAME_B
);
67 loggerA
.setLevel(Level
.ALL
);
68 loggerB
.setLevel(Level
.ALL
);
72 public void tearDown() {
73 /* In case the test fails before destroying the session */
74 LttngSessionControl
.tryDestroySession(null);
83 public void testNoEvents() {
84 assertTrue(LttngSessionControl
.setupSession(null, DOMAIN
));
86 Log4jEnabledEventsTest
.send10Events(loggerA
);
87 Log4jEnabledEventsTest
.send10Events(loggerB
);
89 assertTrue(LttngSessionControl
.stopSession(null));
91 List
<String
> output
= LttngSessionControl
.viewSession(null);
92 assertNotNull(output
);
93 assertTrue(output
.isEmpty());
95 assertTrue(LttngSessionControl
.destroySession(null));
97 ILttngHandler handler
= getAgentHandler();
98 assertEquals(0, handler
.getEventCount());
102 public void testAllEvents() {
103 assertTrue(LttngSessionControl
.setupSessionAllEvents(null, DOMAIN
));
105 Log4jEnabledEventsTest
.send10Events(loggerA
);
106 Log4jEnabledEventsTest
.send10Events(loggerB
);
108 assertTrue(LttngSessionControl
.stopSession(null));
110 List
<String
> output
= LttngSessionControl
.viewSession(null);
111 assertNotNull(output
);
112 assertEquals(20, output
.size());
114 assertTrue(LttngSessionControl
.destroySession(null));
116 ILttngHandler handler
= getAgentHandler();
117 assertEquals(20, handler
.getEventCount());
121 public void testSomeEvents() {
122 assertTrue(LttngSessionControl
.setupSession(null, DOMAIN
,
125 Log4jEnabledEventsTest
.send10Events(loggerA
);
126 Log4jEnabledEventsTest
.send10Events(loggerB
);
128 assertTrue(LttngSessionControl
.stopSession(null));
130 List
<String
> output
= LttngSessionControl
.viewSession(null);
131 assertNotNull(output
);
132 assertEquals(10, output
.size());
134 assertTrue(LttngSessionControl
.destroySession(null));
136 ILttngHandler handler
= getAgentHandler();
137 assertEquals(10, handler
.getEventCount());
141 * Get the singleton Log4j Handler currently managed by the LTTngAgent. It
142 * is not public, so we need reflection to access it.
144 * @return The agent's Log4j handler
146 private ILttngHandler
getAgentHandler() {
148 Field log4jAppenderField
= LTTngAgent
.class.getDeclaredField("log4jAppender");
149 log4jAppenderField
.setAccessible(true);
150 return (ILttngHandler
) log4jAppenderField
.get(LTTngAgent
.getLTTngAgent());
151 } catch (ReflectiveOperationException
| SecurityException e
) {