author | herrick |
Wed, 16 Oct 2019 10:32:08 -0400 | |
branch | JDK-8200758-branch |
changeset 58648 | 3bf53ffa9ae7 |
parent 58463 | 4e71249f291c |
permissions | -rw-r--r-- |
58036
f7f10023f7c0
8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff
changeset
|
1 |
/* |
f7f10023f7c0
8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff
changeset
|
2 |
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. |
f7f10023f7c0
8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
f7f10023f7c0
8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff
changeset
|
4 |
* |
f7f10023f7c0
8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
f7f10023f7c0
8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
f7f10023f7c0
8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. |
f7f10023f7c0
8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff
changeset
|
8 |
* |
f7f10023f7c0
8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
f7f10023f7c0
8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
f7f10023f7c0
8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
f7f10023f7c0
8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
f7f10023f7c0
8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff
changeset
|
13 |
* accompanied this code). |
f7f10023f7c0
8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff
changeset
|
14 |
* |
f7f10023f7c0
8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
f7f10023f7c0
8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
f7f10023f7c0
8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
f7f10023f7c0
8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff
changeset
|
18 |
* |
f7f10023f7c0
8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
f7f10023f7c0
8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
f7f10023f7c0
8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff
changeset
|
21 |
* questions. |
f7f10023f7c0
8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff
changeset
|
22 |
*/ |
f7f10023f7c0
8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff
changeset
|
23 |
package jdk.jpackage.test; |
f7f10023f7c0
8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff
changeset
|
24 |
|
58416 | 25 |
import java.io.BufferedReader; |
26 |
import java.io.ByteArrayOutputStream; |
|
58113 | 27 |
import java.io.IOException; |
58416 | 28 |
import java.io.InputStreamReader; |
29 |
import java.io.OutputStream; |
|
30 |
import java.io.PrintStream; |
|
31 |
import java.io.StringReader; |
|
58036
f7f10023f7c0
8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff
changeset
|
32 |
import java.nio.file.Path; |
58463 | 33 |
import java.util.*; |
58301
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58113
diff
changeset
|
34 |
import java.util.regex.Pattern; |
58113 | 35 |
import java.util.spi.ToolProvider; |
58301
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58113
diff
changeset
|
36 |
import java.util.stream.Collectors; |
58113 | 37 |
import java.util.stream.Stream; |
58416 | 38 |
import jdk.jpackage.test.Functional.ThrowingSupplier; |
58036
f7f10023f7c0
8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff
changeset
|
39 |
|
f7f10023f7c0
8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff
changeset
|
40 |
public final class Executor extends CommandArguments<Executor> { |
f7f10023f7c0
8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff
changeset
|
41 |
|
f7f10023f7c0
8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff
changeset
|
42 |
public Executor() { |
58416 | 43 |
saveOutputType = new HashSet<>(Set.of(SaveOutputType.NONE)); |
58036
f7f10023f7c0
8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff
changeset
|
44 |
} |
f7f10023f7c0
8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff
changeset
|
45 |
|
f7f10023f7c0
8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff
changeset
|
46 |
public Executor setExecutable(String v) { |
58416 | 47 |
return setExecutable(Path.of(v)); |
48 |
} |
|
49 |
||
50 |
public Executor setExecutable(Path v) { |
|
58463 | 51 |
executable = Objects.requireNonNull(v); |
52 |
toolProvider = null; |
|
58113 | 53 |
return this; |
54 |
} |
|
55 |
||
56 |
public Executor setToolProvider(ToolProvider v) { |
|
58463 | 57 |
toolProvider = Objects.requireNonNull(v); |
58 |
executable = null; |
|
58036
f7f10023f7c0
8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff
changeset
|
59 |
return this; |
f7f10023f7c0
8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff
changeset
|
60 |
} |
f7f10023f7c0
8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff
changeset
|
61 |
|
58648 | 62 |
public Executor setToolProvider(JavaTool v) { |
63 |
return setToolProvider(v.asToolProvider()); |
|
64 |
} |
|
65 |
||
58036
f7f10023f7c0
8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff
changeset
|
66 |
public Executor setDirectory(Path v) { |
f7f10023f7c0
8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff
changeset
|
67 |
directory = v; |
f7f10023f7c0
8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff
changeset
|
68 |
return this; |
f7f10023f7c0
8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff
changeset
|
69 |
} |
f7f10023f7c0
8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff
changeset
|
70 |
|
f7f10023f7c0
8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff
changeset
|
71 |
public Executor setExecutable(JavaTool v) { |
58416 | 72 |
return setExecutable(v.getPath()); |
58036
f7f10023f7c0
8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff
changeset
|
73 |
} |
f7f10023f7c0
8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff
changeset
|
74 |
|
58416 | 75 |
/** |
76 |
* Configures this instance to save full output that command will produce. |
|
77 |
* This function is mutual exclusive with |
|
78 |
* saveFirstLineOfOutput() function. |
|
79 |
* |
|
80 |
* @return this |
|
81 |
*/ |
|
58036
f7f10023f7c0
8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff
changeset
|
82 |
public Executor saveOutput() { |
58416 | 83 |
saveOutputType.remove(SaveOutputType.FIRST_LINE); |
84 |
saveOutputType.add(SaveOutputType.FULL); |
|
58036
f7f10023f7c0
8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff
changeset
|
85 |
return this; |
f7f10023f7c0
8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff
changeset
|
86 |
} |
f7f10023f7c0
8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff
changeset
|
87 |
|
58416 | 88 |
/** |
89 |
* Configures how to save output that command will produce. If |
|
90 |
* <code>v</code> is <code>true</code>, the function call is equivalent to |
|
91 |
* <code>saveOutput()</code> call. If <code>v</code> is <code>false</code>, |
|
92 |
* the function will result in not preserving command output. |
|
93 |
* |
|
94 |
* @return this |
|
95 |
*/ |
|
96 |
public Executor saveOutput(boolean v) { |
|
97 |
if (v) { |
|
98 |
saveOutput(); |
|
99 |
} else { |
|
100 |
saveOutputType.remove(SaveOutputType.FIRST_LINE); |
|
101 |
saveOutputType.remove(SaveOutputType.FULL); |
|
102 |
} |
|
58036
f7f10023f7c0
8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff
changeset
|
103 |
return this; |
f7f10023f7c0
8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff
changeset
|
104 |
} |
f7f10023f7c0
8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff
changeset
|
105 |
|
58416 | 106 |
/** |
107 |
* Configures this instance to save only the first line out output that |
|
108 |
* command will produce. This function is mutual exclusive with |
|
109 |
* saveOutput() function. |
|
110 |
* |
|
111 |
* @return this |
|
112 |
*/ |
|
113 |
public Executor saveFirstLineOfOutput() { |
|
114 |
saveOutputType.add(SaveOutputType.FIRST_LINE); |
|
115 |
saveOutputType.remove(SaveOutputType.FULL); |
|
116 |
return this; |
|
117 |
} |
|
118 |
||
119 |
/** |
|
120 |
* Configures this instance to dump all output that command will produce to |
|
121 |
* System.out and System.err. Can be used together with saveOutput() and |
|
122 |
* saveFirstLineOfOutput() to save command output and also copy it in the |
|
123 |
* default output streams. |
|
124 |
* |
|
125 |
* @return this |
|
126 |
*/ |
|
127 |
public Executor dumpOutput() { |
|
128 |
return dumpOutput(true); |
|
129 |
} |
|
130 |
||
131 |
public Executor dumpOutput(boolean v) { |
|
132 |
if (v) { |
|
133 |
saveOutputType.add(SaveOutputType.DUMP); |
|
134 |
} else { |
|
135 |
saveOutputType.remove(SaveOutputType.DUMP); |
|
136 |
} |
|
58301
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58113
diff
changeset
|
137 |
return this; |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58113
diff
changeset
|
138 |
} |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58113
diff
changeset
|
139 |
|
58036
f7f10023f7c0
8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff
changeset
|
140 |
public class Result { |
f7f10023f7c0
8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff
changeset
|
141 |
|
f7f10023f7c0
8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff
changeset
|
142 |
Result(int exitCode) { |
f7f10023f7c0
8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff
changeset
|
143 |
this.exitCode = exitCode; |
f7f10023f7c0
8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff
changeset
|
144 |
} |
f7f10023f7c0
8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff
changeset
|
145 |
|
f7f10023f7c0
8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff
changeset
|
146 |
public String getFirstLineOfOutput() { |
58301
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58113
diff
changeset
|
147 |
return output.get(0); |
58036
f7f10023f7c0
8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff
changeset
|
148 |
} |
f7f10023f7c0
8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff
changeset
|
149 |
|
f7f10023f7c0
8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff
changeset
|
150 |
public List<String> getOutput() { |
f7f10023f7c0
8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff
changeset
|
151 |
return output; |
f7f10023f7c0
8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff
changeset
|
152 |
} |
f7f10023f7c0
8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff
changeset
|
153 |
|
f7f10023f7c0
8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff
changeset
|
154 |
public String getPrintableCommandLine() { |
f7f10023f7c0
8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff
changeset
|
155 |
return Executor.this.getPrintableCommandLine(); |
f7f10023f7c0
8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff
changeset
|
156 |
} |
f7f10023f7c0
8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff
changeset
|
157 |
|
f7f10023f7c0
8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff
changeset
|
158 |
public Result assertExitCodeIs(int expectedExitCode) { |
58416 | 159 |
TKit.assertEquals(expectedExitCode, exitCode, String.format( |
58036
f7f10023f7c0
8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff
changeset
|
160 |
"Check command %s exited with %d code", |
f7f10023f7c0
8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff
changeset
|
161 |
getPrintableCommandLine(), expectedExitCode)); |
f7f10023f7c0
8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff
changeset
|
162 |
return this; |
f7f10023f7c0
8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff
changeset
|
163 |
} |
f7f10023f7c0
8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff
changeset
|
164 |
|
f7f10023f7c0
8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff
changeset
|
165 |
public Result assertExitCodeIsZero() { |
f7f10023f7c0
8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff
changeset
|
166 |
return assertExitCodeIs(0); |
f7f10023f7c0
8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff
changeset
|
167 |
} |
f7f10023f7c0
8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff
changeset
|
168 |
|
58113 | 169 |
final int exitCode; |
170 |
private List<String> output; |
|
58036
f7f10023f7c0
8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff
changeset
|
171 |
} |
f7f10023f7c0
8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff
changeset
|
172 |
|
f7f10023f7c0
8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff
changeset
|
173 |
public Result execute() { |
58648 | 174 |
if (toolProvider != null && directory != null) { |
175 |
throw new IllegalArgumentException( |
|
176 |
"Can't change directory when using tool provider"); |
|
177 |
} |
|
178 |
||
58416 | 179 |
return ThrowingSupplier.toSupplier(() -> { |
180 |
if (toolProvider != null) { |
|
181 |
return runToolProvider(); |
|
182 |
} |
|
58113 | 183 |
|
184 |
if (executable != null) { |
|
185 |
return runExecutable(); |
|
186 |
} |
|
187 |
||
58416 | 188 |
throw new IllegalStateException("No command to execute"); |
189 |
}).get(); |
|
58036
f7f10023f7c0
8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff
changeset
|
190 |
} |
f7f10023f7c0
8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff
changeset
|
191 |
|
58301
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58113
diff
changeset
|
192 |
public String executeAndGetFirstLineOfOutput() { |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58113
diff
changeset
|
193 |
return saveFirstLineOfOutput().execute().assertExitCodeIsZero().getFirstLineOfOutput(); |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58113
diff
changeset
|
194 |
} |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58113
diff
changeset
|
195 |
|
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58113
diff
changeset
|
196 |
public List<String> executeAndGetOutput() { |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58113
diff
changeset
|
197 |
return saveOutput().execute().assertExitCodeIsZero().getOutput(); |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58113
diff
changeset
|
198 |
} |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58113
diff
changeset
|
199 |
|
58416 | 200 |
private boolean withSavedOutput() { |
201 |
return saveOutputType.contains(SaveOutputType.FULL) || saveOutputType.contains( |
|
202 |
SaveOutputType.FIRST_LINE); |
|
203 |
} |
|
204 |
||
58648 | 205 |
private Path executablePath() { |
206 |
if (directory == null || executable.isAbsolute()) { |
|
207 |
return executable; |
|
208 |
} |
|
209 |
||
210 |
// If relative path to executable is used it seems to be broken when |
|
211 |
// ProcessBuilder changes the directory. On Windows it changes the |
|
212 |
// directory first and on Linux it looks up for executable before |
|
213 |
// changing the directory. So to stay of safe side, use absolute path |
|
214 |
// to executable. |
|
215 |
return executable.toAbsolutePath(); |
|
216 |
} |
|
217 |
||
58113 | 218 |
private Result runExecutable() throws IOException, InterruptedException { |
58036
f7f10023f7c0
8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff
changeset
|
219 |
List<String> command = new ArrayList<>(); |
58648 | 220 |
command.add(executablePath().toString()); |
58036
f7f10023f7c0
8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff
changeset
|
221 |
command.addAll(args); |
f7f10023f7c0
8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff
changeset
|
222 |
ProcessBuilder builder = new ProcessBuilder(command); |
f7f10023f7c0
8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff
changeset
|
223 |
StringBuilder sb = new StringBuilder(getPrintableCommandLine()); |
58416 | 224 |
if (withSavedOutput()) { |
58036
f7f10023f7c0
8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff
changeset
|
225 |
builder.redirectErrorStream(true); |
58416 | 226 |
sb.append("; save output"); |
227 |
} else if (saveOutputType.contains(SaveOutputType.DUMP)) { |
|
228 |
builder.inheritIO(); |
|
229 |
sb.append("; inherit I/O"); |
|
58417
67ffaf3a2b75
8231280: Linux packages produced by jpackage should have correct dependencies
herrick
parents:
58416
diff
changeset
|
230 |
} else { |
67ffaf3a2b75
8231280: Linux packages produced by jpackage should have correct dependencies
herrick
parents:
58416
diff
changeset
|
231 |
builder.redirectError(ProcessBuilder.Redirect.DISCARD); |
67ffaf3a2b75
8231280: Linux packages produced by jpackage should have correct dependencies
herrick
parents:
58416
diff
changeset
|
232 |
builder.redirectOutput(ProcessBuilder.Redirect.DISCARD); |
67ffaf3a2b75
8231280: Linux packages produced by jpackage should have correct dependencies
herrick
parents:
58416
diff
changeset
|
233 |
sb.append("; discard I/O"); |
58036
f7f10023f7c0
8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff
changeset
|
234 |
} |
f7f10023f7c0
8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff
changeset
|
235 |
if (directory != null) { |
f7f10023f7c0
8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff
changeset
|
236 |
builder.directory(directory.toFile()); |
f7f10023f7c0
8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff
changeset
|
237 |
sb.append(String.format("; in directory [%s]", directory)); |
f7f10023f7c0
8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff
changeset
|
238 |
} |
f7f10023f7c0
8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff
changeset
|
239 |
|
58416 | 240 |
TKit.trace("Execute " + sb.toString() + "..."); |
241 |
Process process = builder.start(); |
|
242 |
||
243 |
List<String> outputLines = null; |
|
244 |
if (withSavedOutput()) { |
|
245 |
try (BufferedReader outReader = new BufferedReader( |
|
246 |
new InputStreamReader(process.getInputStream()))) { |
|
58463 | 247 |
if (saveOutputType.contains(SaveOutputType.DUMP) |
248 |
|| saveOutputType.contains(SaveOutputType.FULL)) { |
|
58416 | 249 |
outputLines = outReader.lines().collect(Collectors.toList()); |
250 |
} else { |
|
251 |
outputLines = Arrays.asList( |
|
252 |
outReader.lines().findFirst().orElse(null)); |
|
253 |
} |
|
254 |
} finally { |
|
255 |
if (saveOutputType.contains(SaveOutputType.DUMP) && outputLines != null) { |
|
256 |
outputLines.stream().forEach(System.out::println); |
|
257 |
if (saveOutputType.contains(SaveOutputType.FIRST_LINE)) { |
|
258 |
// Pick the first line of saved output if there is one |
|
259 |
for (String line: outputLines) { |
|
260 |
outputLines = List.of(line); |
|
261 |
break; |
|
262 |
} |
|
263 |
} |
|
264 |
} |
|
58036
f7f10023f7c0
8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff
changeset
|
265 |
} |
f7f10023f7c0
8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff
changeset
|
266 |
} |
58416 | 267 |
|
268 |
Result reply = new Result(process.waitFor()); |
|
269 |
TKit.trace("Done. Exit code: " + reply.exitCode); |
|
270 |
||
271 |
if (outputLines != null) { |
|
272 |
reply.output = Collections.unmodifiableList(outputLines); |
|
273 |
} |
|
274 |
return reply; |
|
275 |
} |
|
276 |
||
277 |
private Result runToolProvider(PrintStream out, PrintStream err) { |
|
278 |
TKit.trace("Execute " + getPrintableCommandLine() + "..."); |
|
279 |
Result reply = new Result(toolProvider.run(out, err, args.toArray( |
|
280 |
String[]::new))); |
|
281 |
TKit.trace("Done. Exit code: " + reply.exitCode); |
|
282 |
return reply; |
|
58036
f7f10023f7c0
8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff
changeset
|
283 |
} |
f7f10023f7c0
8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff
changeset
|
284 |
|
58416 | 285 |
|
286 |
private Result runToolProvider() throws IOException { |
|
287 |
if (!withSavedOutput()) { |
|
288 |
if (saveOutputType.contains(SaveOutputType.DUMP)) { |
|
289 |
return runToolProvider(System.out, System.err); |
|
290 |
} |
|
58113 | 291 |
|
58416 | 292 |
PrintStream nullPrintStream = new PrintStream(new OutputStream() { |
293 |
@Override |
|
294 |
public void write(int b) { |
|
295 |
// Nop |
|
296 |
} |
|
297 |
}); |
|
298 |
return runToolProvider(nullPrintStream, nullPrintStream); |
|
299 |
} |
|
58113 | 300 |
|
58416 | 301 |
try (ByteArrayOutputStream buf = new ByteArrayOutputStream(); |
302 |
PrintStream ps = new PrintStream(buf)) { |
|
303 |
Result reply = runToolProvider(ps, ps); |
|
304 |
ps.flush(); |
|
305 |
try (BufferedReader bufReader = new BufferedReader(new StringReader( |
|
306 |
buf.toString()))) { |
|
307 |
if (saveOutputType.contains(SaveOutputType.FIRST_LINE)) { |
|
58463 | 308 |
String firstLine = bufReader.lines().findFirst().orElse(null); |
58416 | 309 |
if (firstLine != null) { |
310 |
reply.output = List.of(firstLine); |
|
311 |
} |
|
312 |
} else if (saveOutputType.contains(SaveOutputType.FULL)) { |
|
58463 | 313 |
reply.output = bufReader.lines().collect( |
58416 | 314 |
Collectors.toUnmodifiableList()); |
315 |
} |
|
58113 | 316 |
|
58416 | 317 |
if (saveOutputType.contains(SaveOutputType.DUMP)) { |
318 |
Stream<String> lines; |
|
319 |
if (saveOutputType.contains(SaveOutputType.FULL)) { |
|
320 |
lines = reply.output.stream(); |
|
321 |
} else { |
|
322 |
lines = bufReader.lines(); |
|
323 |
} |
|
324 |
lines.forEach(System.out::println); |
|
325 |
} |
|
326 |
} |
|
327 |
return reply; |
|
58113 | 328 |
} |
58036
f7f10023f7c0
8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff
changeset
|
329 |
} |
f7f10023f7c0
8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff
changeset
|
330 |
|
58113 | 331 |
public String getPrintableCommandLine() { |
58301
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58113
diff
changeset
|
332 |
final String exec; |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58113
diff
changeset
|
333 |
String format = "[%s](%d)"; |
58113 | 334 |
if (toolProvider == null && executable == null) { |
58301
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58113
diff
changeset
|
335 |
exec = "<null>"; |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58113
diff
changeset
|
336 |
} else if (toolProvider != null) { |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58113
diff
changeset
|
337 |
format = "tool provider " + format; |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58113
diff
changeset
|
338 |
exec = toolProvider.name(); |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58113
diff
changeset
|
339 |
} else { |
58648 | 340 |
exec = executablePath().toString(); |
58113 | 341 |
} |
342 |
||
58301
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58113
diff
changeset
|
343 |
return String.format(format, printCommandLine(exec, args), |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58113
diff
changeset
|
344 |
args.size() + 1); |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58113
diff
changeset
|
345 |
} |
58113 | 346 |
|
58301
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58113
diff
changeset
|
347 |
private static String printCommandLine(String executable, List<String> args) { |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58113
diff
changeset
|
348 |
// Want command line printed in a way it can be easily copy/pasted |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58113
diff
changeset
|
349 |
// to be executed manally |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58113
diff
changeset
|
350 |
Pattern regex = Pattern.compile("\\s"); |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58113
diff
changeset
|
351 |
return Stream.concat(Stream.of(executable), args.stream()).map( |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58113
diff
changeset
|
352 |
v -> (v.isEmpty() || regex.matcher(v).find()) ? "\"" + v + "\"" : v).collect( |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58113
diff
changeset
|
353 |
Collectors.joining(" ")); |
58113 | 354 |
} |
355 |
||
356 |
private ToolProvider toolProvider; |
|
58416 | 357 |
private Path executable; |
358 |
private Set<SaveOutputType> saveOutputType; |
|
58036
f7f10023f7c0
8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff
changeset
|
359 |
private Path directory; |
f7f10023f7c0
8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff
changeset
|
360 |
|
f7f10023f7c0
8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff
changeset
|
361 |
private static enum SaveOutputType { |
58301
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58113
diff
changeset
|
362 |
NONE, FULL, FIRST_LINE, DUMP |
58036
f7f10023f7c0
8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff
changeset
|
363 |
}; |
f7f10023f7c0
8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff
changeset
|
364 |
} |