Attach exception messages to JUnit fail() calls
[lttng-ust-java-tests.git] / lttng-ust-java-tests-common / src / main / java / org / lttng / ust / agent / integration / context / AppContextOrderingITBase.java
1 /*
2 * Copyright (C) 2016, 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
19 package org.lttng.ust.agent.integration.context;
20
21 import static org.junit.Assert.assertFalse;
22 import static org.junit.Assert.assertNotNull;
23 import static org.junit.Assert.assertTrue;
24 import static org.junit.Assert.fail;
25
26 import java.io.IOException;
27 import java.util.List;
28
29 import org.junit.After;
30 import org.junit.AfterClass;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 import org.lttng.tools.ILttngSession;
35 import org.lttng.tools.LttngToolsHelper;
36 import org.lttng.tools.ILttngSession.Domain;
37 import org.lttng.ust.agent.ILttngHandler;
38 import org.lttng.ust.agent.context.ContextInfoManager;
39 import org.lttng.ust.agent.context.IContextInfoRetriever;
40 import org.lttng.ust.agent.utils.TestPrintRunner;
41
42 /**
43 * To obtain application contexts in a trace, three steps are required:
44 *
45 * <ul>
46 * <li>Having the Java agent register to the sessiond (Agent)</li>
47 * <li>Registering the application-provided context info retriever (Retriever)</li>
48 * <li>Enabling the contexts in the tracing session (Session)</li>
49 * </ul>
50 *
51 * These three steps however can occur in any order ; this means there are 6
52 * possible cases. The goal of this class is to test all these cases.
53 */
54 @RunWith(TestPrintRunner.class)
55 public abstract class AppContextOrderingITBase {
56
57 protected static final String EVENT_NAME = "EventName";
58
59 private static final IContextInfoRetriever RETRIEVER = ContextInfoRetrieverStubs.STRING_RETRIEVER;
60 private static final String RETRIEVER_NAME = "MyRetriever";
61 private static final String CONTEXT_NAME = ContextInfoRetrieverStubs.CONTEXT_NAME;
62 private static final String CONTEXT_VALUE = ContextInfoRetrieverStubs.STRING_VALUE;
63
64 protected ILttngHandler logHandler;
65
66 private ContextInfoManager cim;
67 private ILttngSession session;
68
69 protected abstract Domain getDomain();
70 protected abstract void sendEventsToLoggers();
71
72 /**
73 * Base test setup
74 */
75 @Before
76 public void testSetup() {
77 try {
78 cim = ContextInfoManager.getInstance();
79 } catch (SecurityException | IOException e) {
80 /* The native library is not available! */
81 fail(e.getMessage());
82 }
83 session = ILttngSession.createSession(null, getDomain());
84 }
85
86 /**
87 * Base test cleanup
88 */
89 @After
90 public void testCleanup() {
91 session.close();
92 assertTrue(cim.unregisterContextInfoRetriever(RETRIEVER_NAME));
93 }
94
95 /**
96 * Base class cleanup
97 */
98 @AfterClass
99 public static void julClassCleanup() {
100 LttngToolsHelper.deleteAllTraces();
101 }
102
103 // ------------------------------------------------------------------------
104 // Utility methods
105 // ------------------------------------------------------------------------
106
107 /**
108 * Instantiate the log handler for the corresponding logging API. This will
109 * also spawn the agent and have it register to the sessiond, so it
110 * corresponds to the "Agent" step.
111 *
112 * This method should set the 'logHandler' field accordingly.
113 */
114 protected abstract void registerAgent();
115
116 /**
117 * Register the context info retriever to UST. This corresponds to the
118 * "Retriever" step.
119 */
120 private void registerRetriever() {
121 assertTrue(cim.registerContextInfoRetriever(RETRIEVER_NAME, RETRIEVER));
122 }
123
124 /**
125 * Enable the contexts in the tracing session. This corresponds to the "Session" step.
126 */
127 private void enableContextInSessions() {
128 assertTrue(session.enableAllEvents());
129 assertTrue(session.enableAppContext(RETRIEVER_NAME, CONTEXT_NAME));
130 }
131
132 /**
133 * Start tracing, send events from the application, and verify that the
134 * output contains the expected context information.
135 *
136 * This should be called only after all 3 steps above are done.
137 */
138 private void traceSendEventsAndVerify() {
139 assertTrue(session.start());
140 sendEventsToLoggers();
141 assertTrue(session.stop());
142
143 List<String> output = session.view();
144 assertNotNull(output);
145 assertFalse(output.isEmpty());
146
147 String expectedString = "_app_" + RETRIEVER_NAME + "_" + CONTEXT_NAME + " = { string = \"" + CONTEXT_VALUE + "\" } }";
148 output.forEach(line -> assertTrue(line.contains(expectedString)));
149 }
150
151 // ------------------------------------------------------------------------
152 // Test methods
153 // ------------------------------------------------------------------------
154
155 /**
156 * Test the sequence Agent -> Retriever -> Session
157 */
158 @Test
159 public void testAgentRetrieverSession() {
160 registerAgent();
161 registerRetriever();
162 enableContextInSessions();
163
164 traceSendEventsAndVerify();
165 }
166
167 /**
168 * Test the sequence Agent -> Session -> Retriever
169 */
170 @Test
171 public void testAgentSessionRetriever() {
172 registerAgent();
173 enableContextInSessions();
174 registerRetriever();
175
176 traceSendEventsAndVerify();
177 }
178
179 /**
180 * Test the sequence Retriever -> Agent -> Session
181 */
182 @Test
183 public void testRetrieverAgentSession() {
184 registerRetriever();
185 registerAgent();
186 enableContextInSessions();
187
188 traceSendEventsAndVerify();
189 }
190
191 /**
192 * Test the sequence Retriever -> Session -> Agent
193 */
194 @Test
195 public void testRetrieverSessionAgent() {
196 registerAgent();
197 registerRetriever();
198 enableContextInSessions();
199
200 traceSendEventsAndVerify();
201 }
202
203 /**
204 * Test the sequence Session -> Agent -> Retriever
205 */
206 @Test
207 public void testSessionAgentRetriever() {
208 registerAgent();
209 registerRetriever();
210 enableContextInSessions();
211
212 traceSendEventsAndVerify();
213 }
214
215 /**
216 * Test the sequence Session -> Retriever -> Agent
217 */
218 @Test
219 public void testSessionRetrieverAgent() {
220 registerAgent();
221 registerRetriever();
222 enableContextInSessions();
223
224 traceSendEventsAndVerify();
225 }
226 }
This page took 0.033981 seconds and 4 git commands to generate.