swtbot: move column in trace editor test
[deliverable/tracecompass.git] / org.eclipse.tracecompass.lttng2.control.ui.tests / stubs / org / eclipse / tracecompass / internal / lttng2 / control / stubs / shells / LTTngToolsFileShell.java
CommitLineData
d132bcc7 1/**********************************************************************
ed902a2b 2 * Copyright (c) 2012, 2015 Ericsson
cfdb727a 3 *
d132bcc7
BH
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
cfdb727a
AM
8 *
9 * Contributors:
d132bcc7 10 * Bernd Hufmann - Initial API and implementation
b732adaa 11 * Markus Schorn - Bug 448058: Use org.eclipse.remote in favor of RSE
d132bcc7 12 **********************************************************************/
9bc60be7 13package org.eclipse.tracecompass.internal.lttng2.control.stubs.shells;
d132bcc7 14
13729cbc
BH
15import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
16
d132bcc7
BH
17import java.io.BufferedReader;
18import java.io.DataInputStream;
19import java.io.FileInputStream;
13729cbc 20import java.io.IOException;
d132bcc7
BH
21import java.io.InputStreamReader;
22import java.util.ArrayList;
23import java.util.Collection;
24import java.util.HashMap;
25import java.util.Iterator;
26import java.util.List;
27import java.util.Map;
28import java.util.regex.Matcher;
29import java.util.regex.Pattern;
30
31import org.eclipse.core.commands.ExecutionException;
32import org.eclipse.core.runtime.IProgressMonitor;
364dcfaf 33import org.eclipse.tracecompass.tmf.remote.core.shell.ICommandInput;
ec619615 34import org.eclipse.tracecompass.tmf.remote.core.shell.ICommandResult;
d132bcc7 35
cfdb727a 36@SuppressWarnings("javadoc")
d132bcc7
BH
37public class LTTngToolsFileShell extends TestCommandShell {
38
39 // ------------------------------------------------------------------------
40 // CONSTANTS
41 // ------------------------------------------------------------------------
4e0b52e0
AM
42 private final static String SCENARIO_KEY = "<SCENARIO>";
43 private final static String SCENARIO_END_KEY = "</SCENARIO>";
44 private final static String INPUT_KEY = "<COMMAND_INPUT>";
45 private final static String INPUT_END_KEY = "</COMMAND_INPUT>";
46 private final static String RESULT_KEY = "<COMMAND_RESULT>";
4e0b52e0
AM
47 private final static String OUTPUT_KEY = "<COMMAND_OUTPUT>";
48 private final static String OUTPUT_END_KEY = "</COMMAND_OUTPUT>";
6418ef54
JRJ
49 private final static String ERROR_OUTPUT_KEY = "<COMMAND_ERROR_OUTPUT>";
50 private final static String ERROR_OUTPUT_END_KEY = "</COMMAND_ERROR_OUTPUT>";
4e0b52e0 51 private final static String COMMENT_KEY = "#.*";
cfdb727a 52
774a7993 53 private final static Pattern LTTNG_LIST_SESSION_PATTERN = Pattern.compile("lttng\\s+list\\s+(.+)");
4e0b52e0 54 private final static String LTTNG_LIST_PROVIDER_PATTERN = "lttng\\s+list\\s+(-u|-k)";
d132bcc7
BH
55
56 // ------------------------------------------------------------------------
57 // Attributes
58 // ------------------------------------------------------------------------
59 private String fScenariofile;
60 private String fScenario;
61
e0838ca1
AM
62 private final Map<String, Map<String, ICommandResult>> fScenarioMap = new HashMap<>();
63 private final Map<String, Integer> fSessionNameMap = new HashMap<>();
d132bcc7
BH
64
65 /**
66 * Parse a scenario file with the format:
774a7993 67 *
6418ef54
JRJ
68 * <pre>
69 * &lt;SCENARIO&gt;
d132bcc7 70 * ScenarioName
cfdb727a 71 *
6418ef54 72 * &lt;COMMAND_INPUT&gt;
d132bcc7 73 * Command
4888aef9 74 * &lt;/COMMAND_INPUT&gt;
cfdb727a 75 *
6418ef54 76 * &lt;COMMAND_RESULT&gt;
d132bcc7 77 * CommandResult
6418ef54 78 * &lt;/COMMAND_RESULT&gt;
cfdb727a 79 *
6418ef54 80 * &lt;COMMAND_OUTPUT&gt;
d132bcc7 81 * CommandOutput
6418ef54
JRJ
82 * &lt;COMMAND_ERROR_OUTPUT&gt;
83 * CommandErrorOutput
84 * &lt;/COMMAND_ERROR_OUTPUT&gt;
85 * &lt;/COMMAND_OUTPUT&gt;
cfdb727a 86 *
6418ef54 87 * &lt;/SCENARIO&gt;
cfdb727a 88 *
d132bcc7
BH
89 * Where: ScenarioName - is the scenario name
90 * Command - the command line string
91 * CommandResult - the result integer of the command (0 for success, 1 for failure)
4888aef9
MAL
92 * CommandOutput - the command output string (multi-line possible)
93 * CommandErrorOutput - the command error output string (multi-line possible)
cfdb727a 94 *
d132bcc7
BH
95 * Note: 1) There can be many scenarios per file
96 * 2) There can be many (Command-CommandResult-CommandOutput) triples per scenario
774a7993
BH
97 * 3) Lines starting with # will be ignored (comments)
98 *
6418ef54 99 * <pre>
d132bcc7 100 * @param scenariofile - path to scenario file
d132bcc7 101 */
13729cbc 102 public synchronized void loadScenarioFile(String scenariofile) {
d132bcc7 103 fScenariofile = scenariofile;
cfdb727a 104
d132bcc7
BH
105 // clean up map
106 Collection<Map<String, ICommandResult>> values = fScenarioMap.values();
107 for (Iterator<Map<String, ICommandResult>> iterator = values.iterator(); iterator.hasNext();) {
cfdb727a 108 Map<String, ICommandResult> map = iterator.next();
d132bcc7
BH
109 map.clear();
110 }
111 fScenarioMap.clear();
cfdb727a 112
d132bcc7 113 // load from file
cfdb727a 114
d132bcc7 115 // Open the file
e0838ca1
AM
116 try (FileInputStream fstream = new FileInputStream(fScenariofile);
117 DataInputStream in = new DataInputStream(fstream);
118 BufferedReader br = new BufferedReader(new InputStreamReader(in));) {
119 String strLine;
cfdb727a 120
e0838ca1 121 // Read File Line by Line
cfdb727a 122
e0838ca1
AM
123 // Temporary map for generating instance numbers for lttng list
124 // <session> commands.
125 // The numbers are per scenario.
126 Map<String, Integer> tmpSessionNameMap = new HashMap<>();
127 while ((strLine = br.readLine()) != null) {
d132bcc7
BH
128
129 // Ignore comments
e0838ca1
AM
130 if (isComment(strLine)) {
131 continue;
d132bcc7
BH
132 }
133
e0838ca1
AM
134 if (SCENARIO_KEY.equals(strLine)) {
135 // scenario start
136
cfdb727a 137 // Ignore comments
e0838ca1
AM
138 strLine = br.readLine();
139 while (isComment(strLine)) {
140 strLine = br.readLine();
d132bcc7
BH
141 }
142
e0838ca1
AM
143 String scenario = strLine;
144 Map<String, ICommandResult> commandMap = new HashMap<>();
145 fScenarioMap.put(scenario, commandMap);
146 List<String> output = null;
6418ef54 147 List<String> errorOutput = null;
e0838ca1
AM
148 String input = null;
149 boolean inOutput = false;
6418ef54 150 boolean inErrorOutput = false;
e0838ca1
AM
151 int result = 0;
152 tmpSessionNameMap.clear();
153 while ((strLine = br.readLine()) != null) {
d132bcc7 154 // Ignore comments
e0838ca1
AM
155 if (isComment(strLine)) {
156 continue;
d132bcc7 157 }
e0838ca1
AM
158
159 if (SCENARIO_END_KEY.equals(strLine)) {
160 // Scenario is finished
161 break;
d132bcc7 162 }
e0838ca1 163 if (INPUT_KEY.equals(strLine)) {
d132bcc7 164 strLine = br.readLine();
e0838ca1
AM
165 // Ignore comments
166 while (isComment(strLine)) {
167 strLine = br.readLine();
168 }
169 // Read command
170 input = strLine;
171
172 // Handle instances of 'lttng list
4888aef9 173 // <session"-command
e0838ca1
AM
174 Matcher matcher = LTTNG_LIST_SESSION_PATTERN.matcher(strLine);
175 if (matcher.matches() && !input.matches(LTTNG_LIST_PROVIDER_PATTERN)) {
176 String sessionName = matcher.group(1).trim();
177 Integer i = tmpSessionNameMap.get(sessionName);
178 if (i != null) {
179 i++;
180 } else {
181 i = 0;
182 }
183 tmpSessionNameMap.put(sessionName, i);
184 input += String.valueOf(i);
185 }
186 } else if (INPUT_END_KEY.equals(strLine)) {
187 // Initialize output array
188 output = new ArrayList<>();
6418ef54 189 errorOutput = new ArrayList<>();
e0838ca1 190 } else if (RESULT_KEY.equals(strLine)) {
d132bcc7 191 strLine = br.readLine();
e0838ca1
AM
192 // Ignore comments
193 while (isComment(strLine)) {
194 strLine = br.readLine();
195 }
196 // Save result value
197 result = Integer.parseInt(strLine);
198 } else if (OUTPUT_END_KEY.equals(strLine)) {
199 // Save output/result in command map
6418ef54 200 if (output != null && errorOutput != null) {
13729cbc
BH
201 commandMap.put(input, new CommandResultStub(result,
202 checkNotNull(output.toArray(new String[output.size()])),
203 checkNotNull(errorOutput.toArray(new String[errorOutput.size()]))));
e0838ca1
AM
204 }
205 inOutput = false;
206 } else if (OUTPUT_KEY.equals(strLine)) {
207 // first line of output
208 inOutput = true;
6418ef54
JRJ
209 } else if (ERROR_OUTPUT_KEY.equals(strLine)) {
210 // first line of output
211 inErrorOutput = true;
212 } else if (ERROR_OUTPUT_END_KEY.equals(strLine)) {
213 inErrorOutput = false;
214 } else if (inOutput) {
e0838ca1
AM
215 while (isComment(strLine)) {
216 strLine = br.readLine();
217 }
6418ef54
JRJ
218 // lines of output/error output
219 if (errorOutput != null && inErrorOutput) {
220 errorOutput.add(strLine);
221 } else if (output != null) {
e0838ca1
AM
222 output.add(strLine);
223 }
d132bcc7 224 }
e0838ca1
AM
225 // else {
226 // if (RESULT_END_KEY.equals(strLine)) {
d132bcc7 227 // nothing to do
e0838ca1
AM
228 // }
229 }
d132bcc7
BH
230 }
231 }
13729cbc
BH
232 } catch (IOException e) {
233 e.printStackTrace();
d132bcc7 234 }
d132bcc7
BH
235 }
236
237 // Set the scenario to consider in executeCommand()
238 public synchronized void setScenario(String scenario) {
239 fScenario = scenario;
240 fSessionNameMap.clear();
241 if (!fScenarioMap.containsKey(fScenario)) {
242 throw new IllegalArgumentException();
243 }
244 }
245
d132bcc7 246 @Override
364dcfaf 247 public synchronized ICommandResult executeCommand(ICommandInput command, IProgressMonitor monitor) throws ExecutionException {
d132bcc7 248 Map<String, ICommandResult> commands = fScenarioMap.get(fScenario);
364dcfaf 249 String commandLine = command.toString();
774a7993 250 String fullCommand = commandLine;
d132bcc7 251
774a7993
BH
252 Matcher matcher = LTTNG_LIST_SESSION_PATTERN.matcher(commandLine);
253 if (matcher.matches() && !commandLine.matches(LTTNG_LIST_PROVIDER_PATTERN)) {
d132bcc7
BH
254 String sessionName = matcher.group(1).trim();
255 Integer i = fSessionNameMap.get(sessionName);
256 if (i != null) {
257 i++;
258 } else {
259 i = 0;
260 }
261 fSessionNameMap.put(sessionName, i);
41b5c37f 262 fullCommand += String.valueOf(i);
d132bcc7
BH
263 }
264
41b5c37f 265 if (commands.containsKey(fullCommand)) {
13729cbc 266 return checkNotNull(commands.get(fullCommand));
cfdb727a 267 }
d132bcc7
BH
268
269 String[] output = new String[1];
4e0b52e0 270 output[0] = String.valueOf("Command not found");
13729cbc 271 CommandResultStub result = new CommandResultStub(1, output, output);
4ea599a5 272 return result;
774a7993 273 }
cfdb727a 274
d132bcc7
BH
275 // ------------------------------------------------------------------------
276 // Helper methods
277 // ------------------------------------------------------------------------
11252342 278
0a78d11a 279 private static boolean isComment(String line) {
1f2f091b 280 if (line == null) {
4e0b52e0 281 throw new RuntimeException("line is null");
1f2f091b 282 }
d132bcc7
BH
283 return line.matches(COMMENT_KEY);
284 }
285}
This page took 0.07548 seconds and 5 git commands to generate.