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