Migrate to Junit 5 Jupiter
[lttng-ust-java-tests.git] / lttng-ust-java-tests-common / src / main / java / org / lttng / ust / agent / integration / events / EnabledEventsITBase.java
CommitLineData
2b408e85
AM
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
0e77c728 19package org.lttng.ust.agent.integration.events;
c5524c71 20
7a4f0255
MJ
21import static org.junit.jupiter.api.Assertions.assertEquals;
22import static org.junit.jupiter.api.Assertions.assertNotNull;
23import static org.junit.jupiter.api.Assertions.assertTrue;
c5524c71 24
3fa81377 25import java.util.Arrays;
c5524c71
AM
26import java.util.List;
27
7a4f0255
MJ
28import org.junit.jupiter.api.AfterEach;
29import org.junit.jupiter.api.BeforeEach;
30import org.junit.jupiter.api.Test;
31import org.junit.jupiter.api.extension.ExtendWith;
45d1768c
AM
32import org.lttng.tools.ILttngSession;
33import org.lttng.tools.ILttngSession.Domain;
c5524c71 34import org.lttng.ust.agent.ILttngHandler;
7a4f0255 35import org.lttng.ust.agent.utils.TestPrintExtension;
c5524c71 36
8a0613fa
AM
37/**
38 * Base abstract class to implement all sorts of integration tests verifying the
39 * presence of enabled events in resulting traces.
40 */
7a4f0255 41@ExtendWith(TestPrintExtension.class)
7b82be36 42public abstract class EnabledEventsITBase {
c5524c71
AM
43
44 protected static final String EVENT_NAME_A = "EventA";
45 protected static final String EVENT_NAME_B = "EventAB";
46 protected static final String EVENT_NAME_C = "EventABC";
6c2b24d5 47 protected static final String EVENT_NAME_D = "EventABCDÉ";
c5524c71 48
45d1768c 49 private ILttngSession session;
8576633f 50
c5524c71
AM
51 /* Fields defined by the sub-class */
52 protected ILttngHandler handlerA;
53 protected ILttngHandler handlerB;
54 protected ILttngHandler handlerC;
55
56 protected abstract Domain getDomain();
57
eca1a136
MJ
58 protected abstract boolean closeHandlers();
59
c5524c71
AM
60 protected abstract void sendEventsToLoggers();
61
3fa81377
AM
62 /**
63 * Send one event using a localized API to logger/handler A.
64 */
65 protected abstract void sendLocalizedEvent(String rawString, Object[] params);
66
8a0613fa
AM
67 /**
68 * Base test setup
69 */
7a4f0255 70 @BeforeEach
c5524c71 71 public void testSetup() {
ff620bef 72 session = ILttngSession.createSession(null, getDomain());
c5524c71
AM
73 }
74
8a0613fa
AM
75 /**
76 * Base test teardown
77 */
7a4f0255 78 @AfterEach
c5524c71 79 public void testTeardown() {
8576633f 80 session.close();
c5524c71 81
eca1a136
MJ
82 if (closeHandlers()) {
83 handlerA.close();
84 handlerB.close();
85 handlerC.close();
86 }
c5524c71
AM
87
88 handlerA = null;
89 handlerB = null;
90 handlerC = null;
91 }
92
93 /**
94 * Test sending events on the Java side, but no events enabled in the
95 * tracing session. There should be nothing in the resulting trace, and
96 * handlers should not have logged anything.
97 */
98 @Test
99 public void testNoEvents() {
8576633f 100 assertTrue(session.start());
c5524c71
AM
101
102 sendEventsToLoggers();
103
8576633f 104 assertTrue(session.stop());
c5524c71 105
8576633f 106 List<String> output = session.view();
c5524c71
AM
107 assertNotNull(output);
108 assertTrue(output.isEmpty());
109
c5524c71
AM
110 assertEquals(0, handlerA.getEventCount());
111 assertEquals(0, handlerB.getEventCount());
112 assertEquals(0, handlerC.getEventCount());
113 }
114
115 /**
116 * Test sending events on the Java side, and all events enabled in the
117 * tracing session. All handlers should have sent their events.
118 */
119 @Test
120 public void testAllEvents() {
8576633f
AM
121 assertTrue(session.enableAllEvents());
122 assertTrue(session.start());
c5524c71
AM
123
124 sendEventsToLoggers();
125
8576633f 126 assertTrue(session.stop());
c5524c71 127
8576633f 128 List<String> output = session.view();
c5524c71
AM
129 assertNotNull(output);
130 assertEquals(30, output.size()); // loggerD has no handler attached
131
c5524c71
AM
132 assertEquals(10, handlerA.getEventCount());
133 assertEquals(10, handlerB.getEventCount());
134 assertEquals(10, handlerC.getEventCount());
135 }
136
137 /**
138 * Test sending events on the Java side, with only some of them enabled in
139 * the tracing session. Only the subset that is enabled should be received.
140 */
141 @Test
142 public void testSomeEvents() {
8576633f
AM
143 assertTrue(session.enableEvents(EVENT_NAME_A, EVENT_NAME_C, EVENT_NAME_D));
144 assertTrue(session.start());
c5524c71
AM
145
146 sendEventsToLoggers();
147
8576633f 148 assertTrue(session.stop());
c5524c71 149
8576633f 150 List<String> output = session.view();
c5524c71
AM
151 assertNotNull(output);
152 assertEquals(20, output.size());
153
c5524c71
AM
154 assertEquals(10, handlerA.getEventCount());
155 assertEquals(0, handlerB.getEventCount());
156 assertEquals(10, handlerC.getEventCount());
157 }
158
159 /**
160 * Test with all events enabled (-a), plus some other events added manually.
161 * Events should still be retained, but there should be no duplicates.
162 */
163 @Test
164 public void testAllEventsAndSome() {
8576633f
AM
165 assertTrue(session.enableAllEvents());
166 assertTrue(session.enableEvents(EVENT_NAME_A, EVENT_NAME_B));
167 assertTrue(session.start());
c5524c71
AM
168
169 sendEventsToLoggers();
170
8576633f 171 assertTrue(session.stop());
c5524c71 172
8576633f 173 List<String> output = session.view();
c5524c71
AM
174 assertNotNull(output);
175 assertEquals(30, output.size());
176
c5524c71
AM
177 assertEquals(10, handlerA.getEventCount());
178 assertEquals(10, handlerB.getEventCount());
179 assertEquals(10, handlerC.getEventCount());
180 }
181
182 /**
183 * Same as {@link #testSomeEvents()}, but some events were enabled first,
184 * then disabled. Makes sure the enabled-event refcounting works properly.
185 */
186 @Test
187 public void testSomeEventsAfterDisabling() {
8576633f
AM
188 assertTrue(session.enableEvents(EVENT_NAME_A, EVENT_NAME_C, EVENT_NAME_D));
189 assertTrue(session.disableEvents(EVENT_NAME_C));
190 assertTrue(session.start());
c5524c71
AM
191
192 sendEventsToLoggers();
193
8576633f 194 assertTrue(session.stop());
c5524c71 195
8576633f 196 List<String> output = session.view();
c5524c71
AM
197 assertNotNull(output);
198 assertEquals(10, output.size());
199
c5524c71
AM
200 assertEquals(10, handlerA.getEventCount());
201 assertEquals(0, handlerB.getEventCount());
202 assertEquals(0, handlerC.getEventCount());
203 }
204
205 /**
206 * Test enabling an event prefix, which means an event name ending with a *,
207 * to match all events starting with this name.
208 */
209 @Test
210 public void testEventPrefix() {
8576633f
AM
211 // should match event/loggers B and C, but not A.
212 assertTrue(session.enableEvents("EventAB*"));
213 assertTrue(session.start());
c5524c71
AM
214
215 sendEventsToLoggers();
216
8576633f 217 assertTrue(session.stop());
c5524c71 218
8576633f 219 List<String> output = session.view();
c5524c71
AM
220 assertNotNull(output);
221 assertEquals(20, output.size());
222
c5524c71
AM
223 assertEquals(0, handlerA.getEventCount());
224 assertEquals(10, handlerB.getEventCount());
225 assertEquals(10, handlerC.getEventCount());
226 }
227
228 /**
229 * Same as {@link #testEventPrefix()}, but with multiple prefixes that
230 * overlap. There should not be any duplicate events in the trace or in the
231 * handlers.
232 */
233 @Test
234 public void testEventPrefixOverlapping() {
8576633f
AM
235 // should still match B and C
236 assertTrue(session.enableEvents("EventAB*", "EventABC*"));
237 assertTrue(session.start());
c5524c71
AM
238
239 sendEventsToLoggers();
240
8576633f 241 assertTrue(session.stop());
c5524c71 242
8576633f 243 List<String> output = session.view();
c5524c71
AM
244 assertNotNull(output);
245 assertEquals(20, output.size());
246
c5524c71
AM
247 assertEquals(0, handlerA.getEventCount());
248 assertEquals(10, handlerB.getEventCount());
249 assertEquals(10, handlerC.getEventCount());
250 }
251
252 /**
253 * Test with all events enabled (-a), plus an event prefix. Once again,
254 * there should be no duplicates.
255 */
256 @Test
257 public void testAllEventsAndPrefix() {
8576633f
AM
258 assertTrue(session.enableAllEvents());
259 assertTrue(session.enableEvents("EventAB*"));
260 assertTrue(session.start());
c5524c71
AM
261
262 sendEventsToLoggers();
263
8576633f 264 assertTrue(session.stop());
c5524c71 265
8576633f 266 List<String> output = session.view();
c5524c71
AM
267 assertNotNull(output);
268 assertEquals(30, output.size());
269
c5524c71
AM
270 assertEquals(10, handlerA.getEventCount());
271 assertEquals(10, handlerB.getEventCount());
272 assertEquals(10, handlerC.getEventCount());
273 }
3fa81377
AM
274
275 /**
276 * Test sending a localized message.
277 */
278 @Test
279 public void testLocalizedMessage() {
280 Integer value1 = Integer.valueOf(10);
281 List<Integer> value2 = Arrays.asList(Integer.valueOf(1000), Integer.valueOf(1001), Integer.valueOf(1002));
282
283 assertTrue(session.enableAllEvents());
284 assertTrue(session.start());
285
286 sendLocalizedEvent("Message with a localized value: {0} and some others: {1}",
287 new Object[] { value1, value2 });
288
289 assertTrue(session.stop());
290 assertEquals(1, handlerA.getEventCount());
291
292 List<String> output = session.view();
293
294 assertNotNull(output);
295 assertEquals(1, output.size());
296 assertTrue(output.get(0).contains("msg = \"Message with a localized value: 10 and some others: [1000, 1001, 1002]\""));
297 }
c5524c71 298}
This page took 0.036259 seconds and 4 git commands to generate.