Commit | Line | Data |
---|---|---|
c5524c71 AM |
1 | package org.lttng.ust.agent.integration.log4j; |
2 | ||
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; | |
8 | ||
c5524c71 AM |
9 | import java.lang.reflect.Field; |
10 | import java.util.List; | |
11 | ||
12 | import org.apache.log4j.Level; | |
13 | import org.apache.log4j.Logger; | |
14 | import org.junit.After; | |
15 | import org.junit.AfterClass; | |
16 | import org.junit.Before; | |
17 | import org.junit.BeforeClass; | |
18 | import org.junit.Test; | |
19 | import org.lttng.ust.agent.ILttngHandler; | |
20 | import org.lttng.ust.agent.LTTngAgent; | |
8576633f AM |
21 | import org.lttng.ust.agent.utils.LttngSession; |
22 | import org.lttng.ust.agent.utils.LttngSession.Domain; | |
24b260d9 | 23 | import org.lttng.ust.agent.utils.MiscTestUtils; |
c5524c71 AM |
24 | |
25 | @SuppressWarnings("deprecation") | |
26 | public class Log4jLegacyApiTest { | |
27 | ||
28 | private static final Domain DOMAIN = Domain.LOG4J; | |
29 | ||
30 | private static final String EVENT_NAME_A = "EventA"; | |
31 | private static final String EVENT_NAME_B = "EventB"; | |
32 | ||
8576633f AM |
33 | private LttngSession session; |
34 | ||
c5524c71 AM |
35 | private Logger loggerA; |
36 | private Logger loggerB; | |
37 | ||
38 | @BeforeClass | |
39 | public static void classSetup() { | |
40 | /* Skip tests if we can't find the JNI library or lttng-tools */ | |
24b260d9 AM |
41 | assumeTrue(MiscTestUtils.checkForLog4jLibrary()); |
42 | assumeTrue(MiscTestUtils.checkForLttngTools(Domain.LOG4J)); | |
e41ec02a AM |
43 | |
44 | LttngSession.destroyAllSessions(); | |
c5524c71 AM |
45 | } |
46 | ||
47 | @AfterClass | |
48 | public static void classCleanup() { | |
8576633f | 49 | LttngSession.deleteAllTracee(); |
c5524c71 AM |
50 | } |
51 | ||
52 | @Before | |
53 | public void setup() { | |
54 | loggerA = Logger.getLogger(EVENT_NAME_A); | |
55 | LTTngAgent.getLTTngAgent(); | |
56 | loggerB = Logger.getLogger(EVENT_NAME_B); | |
57 | ||
58 | loggerA.setLevel(Level.ALL); | |
59 | loggerB.setLevel(Level.ALL); | |
8576633f AM |
60 | |
61 | session = new LttngSession(null, DOMAIN); | |
c5524c71 AM |
62 | } |
63 | ||
64 | @After | |
65 | public void tearDown() { | |
8576633f | 66 | session.close(); |
c5524c71 AM |
67 | |
68 | LTTngAgent.dispose(); | |
69 | ||
70 | loggerA = null; | |
71 | loggerB = null; | |
72 | } | |
73 | ||
74 | @Test | |
75 | public void testNoEvents() { | |
8576633f | 76 | assertTrue(session.start()); |
c5524c71 | 77 | |
8576633f AM |
78 | Log4jTestUtils.send10Events(loggerA); |
79 | Log4jTestUtils.send10Events(loggerB); | |
c5524c71 | 80 | |
8576633f | 81 | assertTrue(session.stop()); |
c5524c71 | 82 | |
8576633f | 83 | List<String> output = session.view(); |
c5524c71 AM |
84 | assertNotNull(output); |
85 | assertTrue(output.isEmpty()); | |
86 | ||
c5524c71 AM |
87 | ILttngHandler handler = getAgentHandler(); |
88 | assertEquals(0, handler.getEventCount()); | |
89 | } | |
90 | ||
91 | @Test | |
92 | public void testAllEvents() { | |
8576633f AM |
93 | assertTrue(session.enableAllEvents()); |
94 | assertTrue(session.start()); | |
c5524c71 | 95 | |
8576633f AM |
96 | Log4jTestUtils.send10Events(loggerA); |
97 | Log4jTestUtils.send10Events(loggerB); | |
c5524c71 | 98 | |
8576633f | 99 | assertTrue(session.stop()); |
c5524c71 | 100 | |
8576633f | 101 | List<String> output = session.view(); |
c5524c71 AM |
102 | assertNotNull(output); |
103 | assertEquals(20, output.size()); | |
104 | ||
c5524c71 AM |
105 | ILttngHandler handler = getAgentHandler(); |
106 | assertEquals(20, handler.getEventCount()); | |
107 | } | |
108 | ||
109 | @Test | |
110 | public void testSomeEvents() { | |
8576633f AM |
111 | assertTrue(session.enableEvents(EVENT_NAME_A)); |
112 | assertTrue(session.start()); | |
c5524c71 | 113 | |
8576633f AM |
114 | Log4jTestUtils.send10Events(loggerA); |
115 | Log4jTestUtils.send10Events(loggerB); | |
c5524c71 | 116 | |
8576633f | 117 | assertTrue(session.stop()); |
c5524c71 | 118 | |
8576633f | 119 | List<String> output = session.view(); |
c5524c71 AM |
120 | assertNotNull(output); |
121 | assertEquals(10, output.size()); | |
122 | ||
c5524c71 AM |
123 | ILttngHandler handler = getAgentHandler(); |
124 | assertEquals(10, handler.getEventCount()); | |
125 | } | |
126 | ||
127 | /** | |
128 | * Get the singleton Log4j Handler currently managed by the LTTngAgent. It | |
129 | * is not public, so we need reflection to access it. | |
130 | * | |
131 | * @return The agent's Log4j handler | |
132 | */ | |
8576633f | 133 | private static ILttngHandler getAgentHandler() { |
c5524c71 AM |
134 | try { |
135 | Field log4jAppenderField = LTTngAgent.class.getDeclaredField("log4jAppender"); | |
136 | log4jAppenderField.setAccessible(true); | |
137 | return (ILttngHandler) log4jAppenderField.get(LTTngAgent.getLTTngAgent()); | |
138 | } catch (ReflectiveOperationException | SecurityException e) { | |
139 | fail(); | |
140 | return null; | |
141 | } | |
142 | } | |
143 | ||
144 | } | |
145 |