~ubuntu-branches/ubuntu/trusty/eclipse-linuxtools/trusty

« back to all changes in this revision

Viewing changes to lttng/org.eclipse.linuxtools.lttng2.ui.tests/stubs/org/eclipse/linuxtools/internal/lttng2/stubs/shells/LTTngToolsFileShell.java

  • Committer: Package Import Robot
  • Author(s): tony mancill
  • Date: 2013-05-13 21:43:22 UTC
  • mfrom: (1.2.1) (2.1.2 experimental)
  • Revision ID: package-import@ubuntu.com-20130513214322-6frgd9du1n0w2uo7
Tags: 1.2.1-1
* Team upload.
* New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/**********************************************************************
2
2
 * Copyright (c) 2012 Ericsson
3
 
 * 
 
3
 *
4
4
 * All rights reserved. This program and the accompanying materials are
5
5
 * made available under the terms of the Eclipse Public License v1.0 which
6
6
 * accompanies this distribution, and is available at
7
7
 * http://www.eclipse.org/legal/epl-v10.html
8
 
 * 
9
 
 * Contributors: 
 
8
 *
 
9
 * Contributors:
10
10
 *   Bernd Hufmann - Initial API and implementation
11
11
 **********************************************************************/
12
12
package org.eclipse.linuxtools.internal.lttng2.stubs.shells;
29
29
import org.eclipse.linuxtools.internal.lttng2.ui.views.control.remote.CommandResult;
30
30
import org.eclipse.linuxtools.internal.lttng2.ui.views.control.remote.ICommandResult;
31
31
 
 
32
@SuppressWarnings("javadoc")
32
33
public class LTTngToolsFileShell extends TestCommandShell {
33
34
 
34
35
    // ------------------------------------------------------------------------
44
45
    private final static String OUTPUT_KEY = "<COMMAND_OUTPUT>"; //$NON-NLS-1$
45
46
    private final static String OUTPUT_END_KEY = "</COMMAND_OUTPUT>"; //$NON-NLS-1$
46
47
    private final static String COMMENT_KEY = "#.*"; //$NON-NLS-1$
47
 
    
 
48
 
48
49
    private final static Pattern LTTNG_LIST_SESSION_PATTERN =  Pattern.compile("lttng\\s+list\\s+(.+)"); //$NON-NLS-1$
49
50
    private final static String LTTNG_LIST_PROVIDER_PATTERN = "lttng\\s+list\\s+(-u|-k)"; //$NON-NLS-1$
50
51
 
54
55
    private String fScenariofile;
55
56
    private String fScenario;
56
57
 
57
 
    private Map<String, Map<String, ICommandResult>> fScenarioMap = new HashMap<String, Map<String, ICommandResult>>();
58
 
    private Map<String, Integer> fSessionNameMap = new HashMap<String, Integer>();
 
58
    private final Map<String, Map<String, ICommandResult>> fScenarioMap = new HashMap<String, Map<String, ICommandResult>>();
 
59
    private final Map<String, Integer> fSessionNameMap = new HashMap<String, Integer>();
59
60
 
60
61
    /**
61
62
     * Parse a scenario file with the format:
62
63
     * <SCENARIO>
63
64
     * ScenarioName
64
 
     * 
 
65
     *
65
66
     * <COMMAND_INPUT>
66
67
     * Command
67
68
     * </COMAND_INPUT>
68
 
     * 
 
69
     *
69
70
     * <COMMAND_RESULT>
70
71
     * CommandResult
71
72
     * </COMMAND_RESULT>
72
 
     * 
 
73
     *
73
74
     * <COMMAND_OUTPUT>
74
75
     * CommandOutput
75
76
     * </COMMAND_OUTPUT>
76
 
     * 
 
77
     *
77
78
     * </SCENARIO>
78
 
     * 
 
79
     *
79
80
     * Where: ScenarioName - is the scenario name
80
81
     *        Command - the command line string
81
82
     *        CommandResult - the result integer of the command (0 for success, 1 for failure)
82
83
     *        ComandOutput - the command output string (multi-line possible)
83
 
     *        
 
84
     *
84
85
     * Note: 1) There can be many scenarios per file
85
86
     *       2) There can be many (Command-CommandResult-CommandOutput) triples per scenario
86
87
     *       3) Lines starting with # will be ignored (comments)
87
 
     *                 
 
88
     *
88
89
     * @param scenariofile - path to scenario file
89
90
     * @throws Exception
90
91
     */
91
92
    public synchronized void loadScenarioFile(String scenariofile) throws Exception {
92
93
        fScenariofile = scenariofile;
93
 
        
 
94
 
94
95
        // clean up map
95
96
        Collection<Map<String, ICommandResult>> values = fScenarioMap.values();
96
97
        for (Iterator<Map<String, ICommandResult>> iterator = values.iterator(); iterator.hasNext();) {
97
 
            Map<String, ICommandResult> map = (Map<String, ICommandResult>) iterator.next();
 
98
            Map<String, ICommandResult> map = iterator.next();
98
99
            map.clear();
99
100
        }
100
101
        fScenarioMap.clear();
101
 
        
 
102
 
102
103
        // load from file
103
 
        
 
104
 
104
105
        // Open the file
105
106
        FileInputStream fstream = new FileInputStream(fScenariofile);
106
 
        
 
107
 
107
108
        // Get the object of DataInputStream
108
109
        DataInputStream in = new DataInputStream(fstream);
109
110
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
110
111
        String strLine;
111
 
        
 
112
 
112
113
        // Read File Line by Line
113
 
        
114
 
        // Temporary map for generating instance numbers for lttng list <session> commands. 
 
114
 
 
115
        // Temporary map for generating instance numbers for lttng list <session> commands.
115
116
        // The numbers are per scenario.
116
117
        Map<String, Integer> tmpSessionNameMap = new HashMap<String, Integer>();
117
118
        while ((strLine = br.readLine()) != null) {
139
140
                int result = 0;
140
141
                tmpSessionNameMap.clear();
141
142
                while ((strLine = br.readLine()) != null) {
142
 
                    // Ignore comments 
 
143
                    // Ignore comments
143
144
                    if(isComment(strLine)) {
144
145
                        continue;
145
146
                    }
147
148
                    if (SCENARIO_END_KEY.equals(strLine)) {
148
149
                        // Scenario is finished
149
150
                        break;
150
 
                    } 
 
151
                    }
151
152
                    if (INPUT_KEY.equals(strLine)) {
152
153
                        strLine = br.readLine();
153
154
                        // Ignore comments
156
157
                        }
157
158
                        // Read command
158
159
                        input = strLine;
159
 
                        
 
160
 
160
161
                        // Handle instances of 'lttng list <session"-comamand
161
162
                        Matcher matcher = LTTNG_LIST_SESSION_PATTERN.matcher(strLine);
162
163
                        if (matcher.matches() && !input.matches(LTTNG_LIST_PROVIDER_PATTERN)) {
183
184
                        result = Integer.parseInt(strLine);
184
185
                    }  else if (OUTPUT_END_KEY.equals(strLine)) {
185
186
                        // Save output/result in command map
186
 
                        commandMap.put(input, new CommandResult(result, output.toArray(new String[output.size()])));
 
187
                        if (output != null) {
 
188
                            commandMap.put(input, new CommandResult(result, output.toArray(new String[output.size()])));
 
189
                        }
187
190
                        inOutput = false;
188
191
                    } else if (OUTPUT_KEY.equals(strLine)) {
189
192
                        // first line of output
194
197
                        while (isComment(strLine)) {
195
198
                            strLine = br.readLine();
196
199
                        }
197
 
                        output.add(strLine);
 
200
                        if (output != null) {
 
201
                            output.add(strLine);
 
202
                        }
198
203
                    } else if (inOutput) {
199
204
                        // subsequent lines of output
200
 
                        output.add(strLine);
201
 
                    } 
202
 
//                    else { 
 
205
                        if (output != null) {
 
206
                            output.add(strLine);
 
207
                        }
 
208
                    }
 
209
//                    else {
203
210
//                        if (RESULT_END_KEY.equals(strLine)) {
204
211
                        // nothing to do
205
212
//                    }
243
250
 
244
251
        if (commands.containsKey(command)) {
245
252
            return commands.get(command);
246
 
        } 
 
253
        }
247
254
 
248
255
        String[] output = new String[1];
249
256
        output[0] = String.valueOf("Command not found");
253
260
        result.setResult(1);
254
261
        return result;
255
262
   }
256
 
    
 
263
 
257
264
    // ------------------------------------------------------------------------
258
265
    // Helper methods
259
266
    // ------------------------------------------------------------------------
260
 
    private boolean isComment(String line) {
 
267
    private static boolean isComment(String line) {
261
268
        if (line == null) {
262
269
            throw new RuntimeException("line is null"); //$NON-NLS-1$
263
270
        }