comparison test/org/tmatesoft/hg/test/ExecHelper.java @ 66:52dc3f4cfc76

Primitive test suite in org.tmatesoft
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Fri, 21 Jan 2011 18:20:05 +0100
parents test/com/tmate/hgkit/ExecHelper.java@fac8e7fcc8b0
children a3a2e5deb320
comparison
equal deleted inserted replaced
65:e21df6259f83 66:52dc3f4cfc76
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@svnkit.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.LinkedList;
24
25 /**
26 *
27 * @author Artem Tikhomirov
28 * @author TMate Software Ltd.
29 */
30 public class ExecHelper {
31
32 private final OutputParser parser;
33 private final File dir;
34
35 public ExecHelper(OutputParser outParser, File workingDir) {
36 parser = outParser;
37 dir = workingDir;
38 }
39
40 public void run(String... cmd) throws IOException, InterruptedException {
41 Process p = new ProcessBuilder(cmd).directory(dir).redirectErrorStream(true).start();
42 // Process p = Runtime.getRuntime().exec(cmd, null, dir);
43 InputStreamReader stdOut = new InputStreamReader(p.getInputStream());
44 LinkedList<CharBuffer> l = new LinkedList<CharBuffer>();
45 int r = -1;
46 CharBuffer b = null;
47 do {
48 if (b == null || b.remaining() < b.capacity() / 3) {
49 b = CharBuffer.allocate(512);
50 l.add(b);
51 }
52 r = stdOut.read(b);
53 } while (r != -1);
54 int total = 0;
55 for (CharBuffer cb : l) {
56 total += cb.position();
57 cb.flip();
58 }
59 CharBuffer res = CharBuffer.allocate(total);
60 for (CharBuffer cb : l) {
61 res.put(cb);
62 }
63 res.flip();
64 p.waitFor();
65 parser.parse(res);
66 }
67 }