comparison hg4j/src/test/java/org/tmatesoft/hg/test/ExecHelper.java @ 213:6ec4af642ba8 gradle

Project uses Gradle for build - actual changes
author Alexander Kitaev <kitaev@gmail.com>
date Tue, 10 May 2011 10:52:53 +0200
parents
children
comparison
equal deleted inserted replaced
212:edb2e2829352 213:6ec4af642ba8
1 /*
2 * Copyright (c) 2011 TMate Software Ltd
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * For information on how to redistribute this software under
14 * the terms of a license other than GNU General Public License
15 * contact TMate Software at support@hg4j.com
16 */
17 package org.tmatesoft.hg.test;
18
19 import java.io.File;
20 import java.io.IOException;
21 import java.io.InputStreamReader;
22 import java.nio.CharBuffer;
23 import java.util.ArrayList;
24 import java.util.Arrays;
25 import java.util.LinkedList;
26 import java.util.StringTokenizer;
27
28 /**
29 *
30 * @author Artem Tikhomirov
31 * @author TMate Software Ltd.
32 */
33 public class ExecHelper {
34
35 private final OutputParser parser;
36 private File dir;
37 private int exitValue;
38
39 public ExecHelper(OutputParser outParser, File workingDir) {
40 parser = outParser;
41 dir = workingDir;
42 }
43
44 public void run(String... cmd) throws IOException, InterruptedException {
45 ProcessBuilder pb = null;
46 if (System.getProperty("os.name").startsWith("Windows")) {
47 StringTokenizer st = new StringTokenizer(System.getenv("PATH"), ";");
48 while (st.hasMoreTokens()) {
49 File pe = new File(st.nextToken());
50 if (new File(pe, cmd[0] + ".exe").exists()) {
51 break;
52 }
53 // PATHEXT controls precedence of .exe, .bat and .cmd files, ususlly .exe wins
54 if (new File(pe, cmd[0] + ".bat").exists() || new File(pe, cmd[0] + ".cmd").exists()) {
55 ArrayList<String> command = new ArrayList<String>();
56 command.add("cmd.exe");
57 command.add("/C");
58 command.addAll(Arrays.asList(cmd));
59 pb = new ProcessBuilder(command);
60 break;
61 }
62 }
63 }
64 if (pb == null) {
65 pb = new ProcessBuilder(cmd);
66 }
67 Process p = pb.directory(dir).redirectErrorStream(true).start();
68 InputStreamReader stdOut = new InputStreamReader(p.getInputStream());
69 LinkedList<CharBuffer> l = new LinkedList<CharBuffer>();
70 int r = -1;
71 CharBuffer b = null;
72 do {
73 if (b == null || b.remaining() < b.capacity() / 3) {
74 b = CharBuffer.allocate(512);
75 l.add(b);
76 }
77 r = stdOut.read(b);
78 } while (r != -1);
79 int total = 0;
80 for (CharBuffer cb : l) {
81 total += cb.position();
82 cb.flip();
83 }
84 CharBuffer res = CharBuffer.allocate(total);
85 for (CharBuffer cb : l) {
86 res.put(cb);
87 }
88 res.flip();
89 p.waitFor();
90 exitValue = p.exitValue();
91 parser.parse(res);
92 }
93
94 public int getExitValue() {
95 return exitValue;
96 }
97
98 public void cwd(File wd) {
99 dir = wd;
100 }
101 }