comparison test/com/tmate/hgkit/ExecHelper.java @ 61:fac8e7fcc8b0

Simple test framework - capable of parsing Hg cmdline output to compare with Java result
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Tue, 18 Jan 2011 18:32:49 +0100
parents
children
comparison
equal deleted inserted replaced
60:613c936d74e4 61:fac8e7fcc8b0
1 /*
2 * Copyright (c) 2011 Artem Tikhomirov
3 */
4 package com.tmate.hgkit;
5
6 import java.io.File;
7 import java.io.IOException;
8 import java.io.InputStreamReader;
9 import java.nio.CharBuffer;
10 import java.util.LinkedList;
11
12 /**
13 *
14 * @author artem
15 */
16 public class ExecHelper {
17
18 private final OutputParser parser;
19 private final File dir;
20
21 public ExecHelper(OutputParser outParser, File workingDir) {
22 parser = outParser;
23 dir = workingDir;
24 }
25
26 public void run(String... cmd) throws IOException, InterruptedException {
27 Process p = new ProcessBuilder(cmd).directory(dir).redirectErrorStream(true).start();
28 // Process p = Runtime.getRuntime().exec(cmd, null, dir);
29 InputStreamReader stdOut = new InputStreamReader(p.getInputStream());
30 LinkedList<CharBuffer> l = new LinkedList<CharBuffer>();
31 int r = -1;
32 CharBuffer b = null;
33 do {
34 if (b == null || b.remaining() < b.capacity() / 3) {
35 b = CharBuffer.allocate(512);
36 l.add(b);
37 }
38 r = stdOut.read(b);
39 } while (r != -1);
40 int total = 0;
41 for (CharBuffer cb : l) {
42 total += cb.position();
43 cb.flip();
44 }
45 CharBuffer res = CharBuffer.allocate(total);
46 for (CharBuffer cb : l) {
47 res.put(cb);
48 }
49 res.flip();
50 p.waitFor();
51 parser.parse(res);
52 }
53 }