1a240b037d4b8924eaf5f7f78de1172dcfd1bc07
[lttng-ust-java-tests.git] / src / org / lttng / ust / agent / integration / log4j / Log4jLegacyApiTest.java
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
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;
21 import org.lttng.ust.agent.utils.LttngSession;
22 import org.lttng.ust.agent.utils.LttngSession.Domain;
23 import org.lttng.ust.agent.utils.TestUtils;
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
33 private LttngSession session;
34
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 */
41 assumeTrue(TestUtils.checkForLog4jLibrary());
42 assumeTrue(TestUtils.checkForLttngTools(Domain.LOG4J));
43 }
44
45 @AfterClass
46 public static void classCleanup() {
47 LttngSession.deleteAllTracee();
48 }
49
50 @Before
51 public void setup() {
52 loggerA = Logger.getLogger(EVENT_NAME_A);
53 LTTngAgent.getLTTngAgent();
54 loggerB = Logger.getLogger(EVENT_NAME_B);
55
56 loggerA.setLevel(Level.ALL);
57 loggerB.setLevel(Level.ALL);
58
59 session = new LttngSession(null, DOMAIN);
60 }
61
62 @After
63 public void tearDown() {
64 session.close();
65
66 LTTngAgent.dispose();
67
68 loggerA = null;
69 loggerB = null;
70 }
71
72 @Test
73 public void testNoEvents() {
74 assertTrue(session.start());
75
76 Log4jTestUtils.send10Events(loggerA);
77 Log4jTestUtils.send10Events(loggerB);
78
79 assertTrue(session.stop());
80
81 List<String> output = session.view();
82 assertNotNull(output);
83 assertTrue(output.isEmpty());
84
85 ILttngHandler handler = getAgentHandler();
86 assertEquals(0, handler.getEventCount());
87 }
88
89 @Test
90 public void testAllEvents() {
91 assertTrue(session.enableAllEvents());
92 assertTrue(session.start());
93
94 Log4jTestUtils.send10Events(loggerA);
95 Log4jTestUtils.send10Events(loggerB);
96
97 assertTrue(session.stop());
98
99 List<String> output = session.view();
100 assertNotNull(output);
101 assertEquals(20, output.size());
102
103 ILttngHandler handler = getAgentHandler();
104 assertEquals(20, handler.getEventCount());
105 }
106
107 @Test
108 public void testSomeEvents() {
109 assertTrue(session.enableEvents(EVENT_NAME_A));
110 assertTrue(session.start());
111
112 Log4jTestUtils.send10Events(loggerA);
113 Log4jTestUtils.send10Events(loggerB);
114
115 assertTrue(session.stop());
116
117 List<String> output = session.view();
118 assertNotNull(output);
119 assertEquals(10, output.size());
120
121 ILttngHandler handler = getAgentHandler();
122 assertEquals(10, handler.getEventCount());
123 }
124
125 /**
126 * Get the singleton Log4j Handler currently managed by the LTTngAgent. It
127 * is not public, so we need reflection to access it.
128 *
129 * @return The agent's Log4j handler
130 */
131 private static ILttngHandler getAgentHandler() {
132 try {
133 Field log4jAppenderField = LTTngAgent.class.getDeclaredField("log4jAppender");
134 log4jAppenderField.setAccessible(true);
135 return (ILttngHandler) log4jAppenderField.get(LTTngAgent.getLTTngAgent());
136 } catch (ReflectiveOperationException | SecurityException e) {
137 fail();
138 return null;
139 }
140 }
141
142 }
143
This page took 0.056501 seconds and 3 git commands to generate.