Commit | Line | Data |
---|---|---|
eb1bab5b | 1 | /********************************************************************** |
ed902a2b | 2 | * Copyright (c) 2012, 2015 Ericsson |
cfdb727a | 3 | * |
eb1bab5b 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: | |
eb1bab5b | 10 | * Bernd Hufmann - Initial API and implementation |
b732adaa | 11 | * Markus Schorn - Bug 448058: Use org.eclipse.remote in favor of RSE |
eb1bab5b | 12 | **********************************************************************/ |
9bc60be7 | 13 | package org.eclipse.tracecompass.internal.lttng2.control.stubs.shells; |
eb1bab5b | 14 | |
13729cbc BH |
15 | import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull; |
16 | import static org.eclipse.tracecompass.common.core.NonNullUtils.nullToEmptyString; | |
17 | ||
364dcfaf | 18 | import java.util.ArrayList; |
13729cbc | 19 | import java.util.Arrays; |
774a7993 BH |
20 | import java.util.List; |
21 | ||
eb1bab5b BH |
22 | import org.eclipse.core.commands.ExecutionException; |
23 | import org.eclipse.core.runtime.IProgressMonitor; | |
364dcfaf | 24 | import org.eclipse.jdt.annotation.NonNull; |
cbc46cc9 | 25 | import org.eclipse.jdt.annotation.NonNullByDefault; |
364dcfaf BH |
26 | import org.eclipse.jdt.annotation.Nullable; |
27 | import org.eclipse.tracecompass.tmf.remote.core.shell.ICommandInput; | |
ec619615 BH |
28 | import org.eclipse.tracecompass.tmf.remote.core.shell.ICommandResult; |
29 | import org.eclipse.tracecompass.tmf.remote.core.shell.ICommandShell; | |
eb1bab5b | 30 | |
cfdb727a AM |
31 | /** |
32 | * Command shell stub | |
33 | */ | |
eb1bab5b BH |
34 | public class TestCommandShell implements ICommandShell { |
35 | ||
6f4e8ec0 | 36 | /** If the shell is connected */ |
eb1bab5b | 37 | protected boolean fIsConnected = false; |
cfdb727a | 38 | |
eb1bab5b | 39 | @Override |
13729cbc | 40 | public void dispose() { |
eb1bab5b BH |
41 | fIsConnected = false; |
42 | } | |
43 | ||
44 | @Override | |
364dcfaf | 45 | public ICommandResult executeCommand(ICommandInput command, IProgressMonitor monitor) throws ExecutionException { |
eb1bab5b | 46 | if (fIsConnected) { |
13729cbc BH |
47 | return new CommandResultStub(0, new String[0], new String[0]); |
48 | } | |
49 | return new CommandResultStub(1, new String[0], new String[0]); | |
50 | } | |
51 | ||
364dcfaf BH |
52 | @Override |
53 | public ICommandInput createCommand() { | |
54 | return new CommandInputStub(); | |
55 | } | |
56 | ||
13729cbc BH |
57 | /** |
58 | * Command Result Stub | |
59 | */ | |
cbc46cc9 | 60 | @NonNullByDefault |
13729cbc BH |
61 | protected class CommandResultStub implements ICommandResult { |
62 | ||
63 | private final int fResult; | |
64 | private final List<String> fOutput; | |
65 | private final List<String> fErrorOutput; | |
66 | /** | |
67 | * Constructor | |
68 | * | |
69 | * @param result | |
70 | * The result of the command | |
71 | * @param output | |
72 | * The output, as an array of strings | |
73 | * @param errorOutput | |
74 | * THe error output as an array of strings | |
75 | */ | |
76 | public CommandResultStub(int result, String[] output, String[] errorOutput) { | |
77 | fResult = result; | |
cbc46cc9 BH |
78 | fOutput = checkNotNull(Arrays.asList(output)); |
79 | fErrorOutput = checkNotNull(Arrays.asList(errorOutput)); | |
13729cbc BH |
80 | } |
81 | ||
82 | @Override | |
83 | public int getResult() { | |
84 | return fResult; | |
85 | } | |
86 | ||
87 | @Override | |
cbc46cc9 BH |
88 | public List<String> getOutput() { |
89 | return fOutput; | |
13729cbc BH |
90 | } |
91 | ||
92 | @Override | |
cbc46cc9 BH |
93 | public List<String> getErrorOutput() { |
94 | return fErrorOutput; | |
13729cbc BH |
95 | } |
96 | ||
97 | @Override | |
98 | public String toString() { | |
99 | StringBuffer ret = new StringBuffer(); | |
100 | ret.append("Error Output:\n"); //$NON-NLS-1$ | |
101 | for (String string : fErrorOutput) { | |
102 | ret.append(string).append("\n"); //$NON-NLS-1$ | |
103 | } | |
104 | ret.append("Return Value: "); //$NON-NLS-1$ | |
105 | ret.append(fResult); | |
106 | ret.append("\n"); //$NON-NLS-1$ | |
107 | for (String string : fOutput) { | |
108 | ret.append(string).append("\n"); //$NON-NLS-1$ | |
109 | } | |
110 | return nullToEmptyString(ret.toString()); | |
eb1bab5b | 111 | } |
eb1bab5b | 112 | } |
364dcfaf BH |
113 | |
114 | /** | |
115 | * Command Input Stub | |
116 | */ | |
117 | public class CommandInputStub implements ICommandInput { | |
118 | private final List<String> fInput = new ArrayList<>(); | |
119 | ||
120 | @Override | |
121 | @NonNull public List<String> getInput() { | |
122 | return checkNotNull(fInput); | |
123 | } | |
124 | ||
125 | @Override | |
126 | public void add(@Nullable String segment) { | |
127 | if (segment != null) { | |
128 | fInput.add(segment); | |
129 | } | |
130 | } | |
131 | ||
132 | @Override | |
133 | public void addAll(@Nullable List<String> segments) { | |
134 | if (segments != null) { | |
135 | fInput.addAll(segments); | |
136 | } | |
137 | } | |
138 | ||
139 | @Override | |
140 | public String toString() { | |
141 | StringBuilder builder = new StringBuilder(); | |
142 | for (String segment : fInput) { | |
143 | builder.append(segment).append(' '); | |
144 | } | |
145 | return nullToEmptyString(builder.toString().trim()); | |
146 | } | |
147 | } | |
eb1bab5b | 148 | } |