Add log4j and legacy-API tests
[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.io.IOException;
10 import java.lang.reflect.Field;
11 import java.util.List;
12
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;
25
26 @SuppressWarnings("deprecation")
27 public class Log4jLegacyApiTest {
28
29 private static final Domain DOMAIN = Domain.LOG4J;
30
31 private static final String EVENT_NAME_A = "EventA";
32 private static final String EVENT_NAME_B = "EventB";
33
34 private Logger loggerA;
35 private Logger loggerB;
36
37 @BeforeClass
38 public static void classSetup() {
39 /* Skip tests if we can't find the JNI library or lttng-tools */
40 try {
41 LttngLogAppender testAppender = new LttngLogAppender();
42 testAppender.close();
43 } catch (SecurityException | IOException e) {
44 assumeTrue(false);
45 }
46
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());
54 }
55
56 @AfterClass
57 public static void classCleanup() {
58 LttngSessionControl.deleteAllTracee();
59 }
60
61 @Before
62 public void setup() {
63 loggerA = Logger.getLogger(EVENT_NAME_A);
64 LTTngAgent.getLTTngAgent();
65 loggerB = Logger.getLogger(EVENT_NAME_B);
66
67 loggerA.setLevel(Level.ALL);
68 loggerB.setLevel(Level.ALL);
69 }
70
71 @After
72 public void tearDown() {
73 /* In case the test fails before destroying the session */
74 LttngSessionControl.tryDestroySession(null);
75
76 LTTngAgent.dispose();
77
78 loggerA = null;
79 loggerB = null;
80 }
81
82 @Test
83 public void testNoEvents() {
84 assertTrue(LttngSessionControl.setupSession(null, DOMAIN));
85
86 Log4jEnabledEventsTest.send10Events(loggerA);
87 Log4jEnabledEventsTest.send10Events(loggerB);
88
89 assertTrue(LttngSessionControl.stopSession(null));
90
91 List<String> output = LttngSessionControl.viewSession(null);
92 assertNotNull(output);
93 assertTrue(output.isEmpty());
94
95 assertTrue(LttngSessionControl.destroySession(null));
96
97 ILttngHandler handler = getAgentHandler();
98 assertEquals(0, handler.getEventCount());
99 }
100
101 @Test
102 public void testAllEvents() {
103 assertTrue(LttngSessionControl.setupSessionAllEvents(null, DOMAIN));
104
105 Log4jEnabledEventsTest.send10Events(loggerA);
106 Log4jEnabledEventsTest.send10Events(loggerB);
107
108 assertTrue(LttngSessionControl.stopSession(null));
109
110 List<String> output = LttngSessionControl.viewSession(null);
111 assertNotNull(output);
112 assertEquals(20, output.size());
113
114 assertTrue(LttngSessionControl.destroySession(null));
115
116 ILttngHandler handler = getAgentHandler();
117 assertEquals(20, handler.getEventCount());
118 }
119
120 @Test
121 public void testSomeEvents() {
122 assertTrue(LttngSessionControl.setupSession(null, DOMAIN,
123 EVENT_NAME_A));
124
125 Log4jEnabledEventsTest.send10Events(loggerA);
126 Log4jEnabledEventsTest.send10Events(loggerB);
127
128 assertTrue(LttngSessionControl.stopSession(null));
129
130 List<String> output = LttngSessionControl.viewSession(null);
131 assertNotNull(output);
132 assertEquals(10, output.size());
133
134 assertTrue(LttngSessionControl.destroySession(null));
135
136 ILttngHandler handler = getAgentHandler();
137 assertEquals(10, handler.getEventCount());
138 }
139
140 /**
141 * Get the singleton Log4j Handler currently managed by the LTTngAgent. It
142 * is not public, so we need reflection to access it.
143 *
144 * @return The agent's Log4j handler
145 */
146 private ILttngHandler getAgentHandler() {
147 try {
148 Field log4jAppenderField = LTTngAgent.class.getDeclaredField("log4jAppender");
149 log4jAppenderField.setAccessible(true);
150 return (ILttngHandler) log4jAppenderField.get(LTTngAgent.getLTTngAgent());
151 } catch (ReflectiveOperationException | SecurityException e) {
152 fail();
153 return null;
154 }
155 }
156
157 }
158
This page took 0.032214 seconds and 4 git commands to generate.