lttng: Fix ControlViewTest
[deliverable/tracecompass.git] / lttng / org.eclipse.tracecompass.lttng2.control.ui.tests / stubs / org / eclipse / tracecompass / internal / lttng2 / control / stubs / shells / LTTngToolsFileShell.java
1 /**********************************************************************
2 * Copyright (c) 2012, 2015 Ericsson
3 *
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *
9 * Contributors:
10 * Bernd Hufmann - Initial API and implementation
11 * Markus Schorn - Bug 448058: Use org.eclipse.remote in favor of RSE
12 **********************************************************************/
13 package org.eclipse.tracecompass.internal.lttng2.control.stubs.shells;
14
15 import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
16
17 import java.io.BufferedReader;
18 import java.io.DataInputStream;
19 import java.io.File;
20 import java.io.FileInputStream;
21 import java.io.IOException;
22 import java.io.InputStreamReader;
23 import java.io.PrintWriter;
24 import java.util.ArrayList;
25 import java.util.Collection;
26 import java.util.HashMap;
27 import java.util.Iterator;
28 import java.util.List;
29 import java.util.Map;
30 import java.util.regex.Matcher;
31 import java.util.regex.Pattern;
32
33 import org.eclipse.core.commands.ExecutionException;
34 import org.eclipse.core.resources.IWorkspace;
35 import org.eclipse.core.resources.ResourcesPlugin;
36 import org.eclipse.core.runtime.IProgressMonitor;
37 import org.eclipse.jdt.annotation.NonNull;
38 import org.eclipse.tracecompass.internal.tmf.remote.core.stubs.shells.TestCommandShell;
39 import org.eclipse.tracecompass.tmf.remote.core.shell.ICommandInput;
40 import org.eclipse.tracecompass.tmf.remote.core.shell.ICommandOutputListener;
41 import org.eclipse.tracecompass.tmf.remote.core.shell.ICommandResult;
42
43 @SuppressWarnings("javadoc")
44 public class LTTngToolsFileShell extends TestCommandShell {
45
46 // ------------------------------------------------------------------------
47 // CONSTANTS
48 // ------------------------------------------------------------------------
49 private final static String SCENARIO_KEY = "<SCENARIO>";
50 private final static String SCENARIO_END_KEY = "</SCENARIO>";
51 private final static String INPUT_KEY = "<COMMAND_INPUT>";
52 private final static String INPUT_END_KEY = "</COMMAND_INPUT>";
53 private final static String RESULT_KEY = "<COMMAND_RESULT>";
54 private final static String OUTPUT_KEY = "<COMMAND_OUTPUT>";
55 private final static String OUTPUT_END_KEY = "</COMMAND_OUTPUT>";
56 private final static String ERROR_OUTPUT_KEY = "<COMMAND_ERROR_OUTPUT>";
57 private final static String ERROR_OUTPUT_END_KEY = "</COMMAND_ERROR_OUTPUT>";
58 private final static String COMMENT_KEY = "#.*";
59
60 private final static Pattern LTTNG_LIST_SESSION_PATTERN = Pattern.compile("lttng\\s+list\\s+(.+)");
61 private final static String LTTNG_LIST_PROVIDER_PATTERN = "lttng\\s+list\\s+(-u|-k|-j|-l|-p).*";
62
63 private final static Pattern LTTNG_LIST_SESSION_MI_PATTERN = Pattern.compile("lttng\\s+--mi xml\\s+list\\s+(.+)");
64 private final static String LTTNG_LIST_PROVIDER_MI_PATTERN = "lttng\\s+--mi xml\\s+list\\s+(-u|-k|-j|-l|-p).*";
65
66 private final static String LTTNG_USER_HOME_PATTERN = "\\$\\{userhome\\}";
67 private final static String LTTNG_WORKSPACE_PATTERN = "\\$\\{workspace\\}";
68 private final static String SESSION_NAME_PATTERN = "\\$\\{sessionname\\}";
69
70 private final static String USER_HOME = System.getProperty("user.home");
71 private final static String WORKSPACE_HOME;
72
73 private final static Pattern LTTNG_SAVE_MI_PATTERN = Pattern.compile("lttng\\s+--mi xml\\s+save\\s+-f");
74
75 private final static String PROFILE_PATH_STRING = USER_HOME + '/' + ".lttng" + '/' + "sessions";
76
77 static {
78 IWorkspace workspace = ResourcesPlugin.getWorkspace();
79 //get location of workspace (java.io.File)
80 File workspaceDirectory = workspace.getRoot().getLocation().toFile();
81 WORKSPACE_HOME = workspaceDirectory.toString();
82 }
83
84 // ------------------------------------------------------------------------
85 // Attributes
86 // ------------------------------------------------------------------------
87 private String fScenariofile;
88 private String fScenario;
89 private String fProfileName = null;
90 private File fProfileFile = null;
91 private String fSessionName = null;
92
93 private final Map<String, Map<String, ICommandResult>> fScenarioMap = new HashMap<>();
94 private final Map<String, Integer> fSessionNameMap = new HashMap<>();
95
96 /**
97 * Parse a scenario file with the format:
98 *
99 * <pre>
100 * &lt;SCENARIO&gt;
101 * ScenarioName
102 *
103 * &lt;COMMAND_INPUT&gt;
104 * Command
105 * &lt;/COMMAND_INPUT&gt;
106 *
107 * &lt;COMMAND_RESULT&gt;
108 * CommandResult
109 * &lt;/COMMAND_RESULT&gt;
110 *
111 * &lt;COMMAND_OUTPUT&gt;
112 * CommandOutput
113 * &lt;COMMAND_ERROR_OUTPUT&gt;
114 * CommandErrorOutput
115 * &lt;/COMMAND_ERROR_OUTPUT&gt;
116 * &lt;/COMMAND_OUTPUT&gt;
117 *
118 * &lt;/SCENARIO&gt;
119 *
120 * Where: ScenarioName - is the scenario name
121 * Command - the command line string
122 * CommandResult - the result integer of the command (0 for success, 1 for failure)
123 * CommandOutput - the command output string (multi-line possible)
124 * CommandErrorOutput - the command error output string (multi-line possible)
125 *
126 * Note: 1) There can be many scenarios per file
127 * 2) There can be many (Command-CommandResult-CommandOutput) triples per scenario
128 * 3) Lines starting with # will be ignored (comments)
129 *
130 * <pre>
131 * @param scenariofile - path to scenario file
132 */
133 public synchronized void loadScenarioFile(String scenariofile) {
134 fScenariofile = scenariofile;
135
136 // clean up map
137 Collection<Map<String, ICommandResult>> values = fScenarioMap.values();
138 for (Iterator<Map<String, ICommandResult>> iterator = values.iterator(); iterator.hasNext();) {
139 Map<String, ICommandResult> map = iterator.next();
140 map.clear();
141 }
142 fScenarioMap.clear();
143
144 // load from file
145
146 // Open the file
147 try (FileInputStream fstream = new FileInputStream(fScenariofile);
148 DataInputStream in = new DataInputStream(fstream);
149 BufferedReader br = new BufferedReader(new InputStreamReader(in));) {
150 String strLine;
151
152 // Read File Line by Line
153
154 // Temporary map for generating instance numbers for lttng list
155 // <session> commands.
156 // The numbers are per scenario.
157 Map<String, Integer> tmpSessionNameMap = new HashMap<>();
158 while ((strLine = br.readLine()) != null) {
159
160 // Ignore comments
161 if (isComment(strLine)) {
162 continue;
163 }
164
165 if (SCENARIO_KEY.equals(strLine)) {
166 // scenario start
167
168 // Ignore comments
169 strLine = br.readLine();
170 while (isComment(strLine)) {
171 strLine = br.readLine();
172 }
173
174 String scenario = strLine;
175 Map<String, ICommandResult> commandMap = new HashMap<>();
176 fScenarioMap.put(scenario, commandMap);
177 List<String> output = null;
178 List<String> errorOutput = null;
179 String input = null;
180 boolean inOutput = false;
181 boolean inErrorOutput = false;
182 int result = 0;
183 tmpSessionNameMap.clear();
184 while ((strLine = br.readLine()) != null) {
185 // Ignore comments
186 if (isComment(strLine)) {
187 continue;
188 }
189
190 if (SCENARIO_END_KEY.equals(strLine)) {
191 // Scenario is finished
192 break;
193 }
194 if (INPUT_KEY.equals(strLine)) {
195 strLine = br.readLine();
196 // Ignore comments
197 while (isComment(strLine)) {
198 strLine = br.readLine();
199 }
200 // Read command
201 input = strLine;
202
203 // Update userhome
204 input = input.replaceAll(LTTNG_USER_HOME_PATTERN, Matcher.quoteReplacement(USER_HOME));
205
206 // Update workspace
207 input = input.replaceAll(LTTNG_WORKSPACE_PATTERN, Matcher.quoteReplacement(WORKSPACE_HOME));
208
209 // Update session variable
210 if (fSessionName != null) {
211 input = input.replaceAll(SESSION_NAME_PATTERN, Matcher.quoteReplacement(fSessionName));
212 }
213
214 // Handle instances of 'lttng list
215 // <session"-command
216 Matcher matcher = LTTNG_LIST_SESSION_PATTERN.matcher(strLine);
217 Matcher miMatcher = LTTNG_LIST_SESSION_MI_PATTERN.matcher(strLine);
218
219 if (matcher.matches() && !input.matches(LTTNG_LIST_PROVIDER_PATTERN)) {
220 String sessionName = matcher.group(1).trim();
221 input += updateSessionMap(tmpSessionNameMap, input, sessionName);
222 } else if (miMatcher.matches() && !input.matches(LTTNG_LIST_PROVIDER_MI_PATTERN)) {
223 String sessionName = miMatcher.group(1).trim();
224 input += updateSessionMap(tmpSessionNameMap, input, sessionName);
225 }
226 } else if (INPUT_END_KEY.equals(strLine)) {
227 // Initialize output array
228 output = new ArrayList<>();
229 errorOutput = new ArrayList<>();
230 } else if (RESULT_KEY.equals(strLine)) {
231 strLine = br.readLine();
232 // Ignore comments
233 while (isComment(strLine)) {
234 strLine = br.readLine();
235 }
236 // Save result value
237 result = Integer.parseInt(strLine);
238 } else if (OUTPUT_END_KEY.equals(strLine)) {
239 // Save output/result in command map
240 if (output != null && errorOutput != null) {
241 commandMap.put(input, createCommandResult(result,
242 output.toArray(new @NonNull String[output.size()]),
243 errorOutput.toArray(new @NonNull String[errorOutput.size()])));
244 }
245 inOutput = false;
246 } else if (OUTPUT_KEY.equals(strLine)) {
247 // first line of output
248 inOutput = true;
249 } else if (ERROR_OUTPUT_KEY.equals(strLine)) {
250 // first line of output
251 inErrorOutput = true;
252 } else if (ERROR_OUTPUT_END_KEY.equals(strLine)) {
253 inErrorOutput = false;
254 } else if (inOutput) {
255 while (isComment(strLine)) {
256 strLine = br.readLine();
257 }
258
259 // Update userhome
260 strLine = strLine.replaceAll(LTTNG_USER_HOME_PATTERN, Matcher.quoteReplacement(USER_HOME));
261
262 // Update workspace
263 strLine = strLine.replaceAll(LTTNG_WORKSPACE_PATTERN, Matcher.quoteReplacement(WORKSPACE_HOME));
264
265 // Update session variable
266 if (fSessionName != null) {
267 strLine = strLine.replaceAll(SESSION_NAME_PATTERN, Matcher.quoteReplacement(fSessionName));
268 }
269
270 // lines of output/error output
271 if (errorOutput != null && inErrorOutput) {
272 errorOutput.add(strLine);
273 } else if (output != null) {
274 output.add(strLine);
275 }
276 }
277 // else {
278 // if (RESULT_END_KEY.equals(strLine)) {
279 // nothing to do
280 // }
281 }
282 }
283 }
284 } catch (IOException e) {
285 e.printStackTrace();
286 }
287 }
288
289 private static String updateSessionMap(Map<String, Integer> tmpSessionNameMap, String input, String sessionName) {
290 Integer i = tmpSessionNameMap.get(sessionName);
291 if (i != null) {
292 i++;
293 } else {
294 i = 0;
295 }
296 tmpSessionNameMap.put(sessionName, i);
297 return String.valueOf(i);
298 }
299
300 // Set the scenario to consider in executeCommand()
301 public synchronized void setScenario(String scenario) {
302 fScenario = scenario;
303 fSessionNameMap.clear();
304 if (!fScenarioMap.containsKey(fScenario)) {
305 throw new IllegalArgumentException();
306 }
307 }
308
309 @Override
310 public synchronized ICommandResult executeCommand(ICommandInput command, IProgressMonitor monitor) throws ExecutionException {
311 return executeCommand(command, monitor, null);
312 }
313
314 @Override
315 public synchronized ICommandResult executeCommand(ICommandInput command, IProgressMonitor monitor, ICommandOutputListener listener) throws ExecutionException {
316 Map<String, ICommandResult> commands = checkNotNull(fScenarioMap.get(fScenario));
317 String commandLine = command.toString();
318 String fullCommand = commandLine;
319
320 Matcher matcher = LTTNG_LIST_SESSION_PATTERN.matcher(commandLine);
321 Matcher miMatcher = LTTNG_LIST_SESSION_MI_PATTERN.matcher(commandLine);
322 if (matcher.matches() && !commandLine.matches(LTTNG_LIST_PROVIDER_PATTERN)) {
323 String sessionName = matcher.group(1).trim();
324 fullCommand += updateSessionMap(fSessionNameMap, fullCommand, sessionName);
325 } else if (miMatcher.matches() && !commandLine.matches(LTTNG_LIST_PROVIDER_MI_PATTERN)) {
326 String sessionName = miMatcher.group(1).trim();
327 fullCommand += updateSessionMap(fSessionNameMap, fullCommand, sessionName);
328 }
329
330 if (commands.containsKey(fullCommand)) {
331 Matcher saveMatcher = LTTNG_SAVE_MI_PATTERN.matcher(fullCommand);
332 if (fProfileName != null && saveMatcher.matches()) {
333 try {
334 createProfileFile();
335 } catch (IOException e) {
336 throw new ExecutionException("Profile file can't be created", e);
337 }
338 }
339 return checkNotNull(commands.get(fullCommand));
340 }
341
342 @NonNull String[] output = new @NonNull String[1];
343 output[0] = String.valueOf("Command not found");
344 ICommandResult result = createCommandResult(1, output, output);
345 return result;
346 }
347
348 // ------------------------------------------------------------------------
349 // Helper methods
350 // ------------------------------------------------------------------------
351
352 private static boolean isComment(String line) {
353 if (line == null) {
354 throw new RuntimeException("line is null");
355 }
356 return line.matches(COMMENT_KEY);
357 }
358
359 private void createProfileFile() throws IOException {
360 if (fProfileName != null) {
361 File path = new File(PROFILE_PATH_STRING);
362 if (!path.exists()) {
363 if (!path.mkdirs()) {
364 throw new RuntimeException();
365 }
366 }
367 File profileFile = new File(PROFILE_PATH_STRING + '/' + fProfileName + ".lttng");
368 if (!profileFile.exists()) {
369 try (PrintWriter writer = new PrintWriter(profileFile)) {
370 writer.println("This file is created by JUnit test using " + LTTngToolsFileShell.class.getCanonicalName());
371 writer.println("Can be deleted!");
372 writer.close();
373 }
374 }
375 fProfileFile = profileFile;
376 }
377 }
378
379 public void setProfileName(String profileName) {
380 fProfileName = profileName;
381 }
382
383 public void deleteProfileFile() {
384 if (fProfileFile != null && fProfileFile.exists()) {
385 fProfileFile.delete();
386 }
387 }
388
389 public void setSessionName(String sessionName) {
390 fSessionName = sessionName;
391 }
392
393
394 }
This page took 0.039929 seconds and 5 git commands to generate.