annotate test/org/tmatesoft/hg/test/ExecHelper.java @ 102:a3a2e5deb320

Updated contact address to support@hg4j.com
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Fri, 28 Jan 2011 03:50:52 +0100
parents 52dc3f4cfc76
children 0170f95ca915
rev   line source
61
fac8e7fcc8b0 Simple test framework - capable of parsing Hg cmdline output to compare with Java result
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
1 /*
66
52dc3f4cfc76 Primitive test suite in org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 61
diff changeset
2 * Copyright (c) 2011 TMate Software Ltd
52dc3f4cfc76 Primitive test suite in org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 61
diff changeset
3 *
52dc3f4cfc76 Primitive test suite in org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 61
diff changeset
4 * This program is free software; you can redistribute it and/or modify
52dc3f4cfc76 Primitive test suite in org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 61
diff changeset
5 * it under the terms of the GNU General Public License as published by
52dc3f4cfc76 Primitive test suite in org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 61
diff changeset
6 * the Free Software Foundation; version 2 of the License.
52dc3f4cfc76 Primitive test suite in org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 61
diff changeset
7 *
52dc3f4cfc76 Primitive test suite in org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 61
diff changeset
8 * This program is distributed in the hope that it will be useful,
52dc3f4cfc76 Primitive test suite in org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 61
diff changeset
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
52dc3f4cfc76 Primitive test suite in org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 61
diff changeset
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
52dc3f4cfc76 Primitive test suite in org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 61
diff changeset
11 * GNU General Public License for more details.
52dc3f4cfc76 Primitive test suite in org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 61
diff changeset
12 *
52dc3f4cfc76 Primitive test suite in org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 61
diff changeset
13 * For information on how to redistribute this software under
52dc3f4cfc76 Primitive test suite in org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 61
diff changeset
14 * the terms of a license other than GNU General Public License
102
a3a2e5deb320 Updated contact address to support@hg4j.com
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 66
diff changeset
15 * contact TMate Software at support@hg4j.com
61
fac8e7fcc8b0 Simple test framework - capable of parsing Hg cmdline output to compare with Java result
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
16 */
66
52dc3f4cfc76 Primitive test suite in org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 61
diff changeset
17 package org.tmatesoft.hg.test;
61
fac8e7fcc8b0 Simple test framework - capable of parsing Hg cmdline output to compare with Java result
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
18
fac8e7fcc8b0 Simple test framework - capable of parsing Hg cmdline output to compare with Java result
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
19 import java.io.File;
fac8e7fcc8b0 Simple test framework - capable of parsing Hg cmdline output to compare with Java result
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
20 import java.io.IOException;
fac8e7fcc8b0 Simple test framework - capable of parsing Hg cmdline output to compare with Java result
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
21 import java.io.InputStreamReader;
fac8e7fcc8b0 Simple test framework - capable of parsing Hg cmdline output to compare with Java result
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
22 import java.nio.CharBuffer;
fac8e7fcc8b0 Simple test framework - capable of parsing Hg cmdline output to compare with Java result
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
23 import java.util.LinkedList;
fac8e7fcc8b0 Simple test framework - capable of parsing Hg cmdline output to compare with Java result
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
24
fac8e7fcc8b0 Simple test framework - capable of parsing Hg cmdline output to compare with Java result
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
25 /**
fac8e7fcc8b0 Simple test framework - capable of parsing Hg cmdline output to compare with Java result
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
26 *
66
52dc3f4cfc76 Primitive test suite in org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 61
diff changeset
27 * @author Artem Tikhomirov
52dc3f4cfc76 Primitive test suite in org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 61
diff changeset
28 * @author TMate Software Ltd.
61
fac8e7fcc8b0 Simple test framework - capable of parsing Hg cmdline output to compare with Java result
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
29 */
fac8e7fcc8b0 Simple test framework - capable of parsing Hg cmdline output to compare with Java result
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
30 public class ExecHelper {
fac8e7fcc8b0 Simple test framework - capable of parsing Hg cmdline output to compare with Java result
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
31
fac8e7fcc8b0 Simple test framework - capable of parsing Hg cmdline output to compare with Java result
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
32 private final OutputParser parser;
fac8e7fcc8b0 Simple test framework - capable of parsing Hg cmdline output to compare with Java result
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
33 private final File dir;
fac8e7fcc8b0 Simple test framework - capable of parsing Hg cmdline output to compare with Java result
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
34
fac8e7fcc8b0 Simple test framework - capable of parsing Hg cmdline output to compare with Java result
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
35 public ExecHelper(OutputParser outParser, File workingDir) {
fac8e7fcc8b0 Simple test framework - capable of parsing Hg cmdline output to compare with Java result
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
36 parser = outParser;
fac8e7fcc8b0 Simple test framework - capable of parsing Hg cmdline output to compare with Java result
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
37 dir = workingDir;
fac8e7fcc8b0 Simple test framework - capable of parsing Hg cmdline output to compare with Java result
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
38 }
fac8e7fcc8b0 Simple test framework - capable of parsing Hg cmdline output to compare with Java result
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
39
fac8e7fcc8b0 Simple test framework - capable of parsing Hg cmdline output to compare with Java result
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
40 public void run(String... cmd) throws IOException, InterruptedException {
fac8e7fcc8b0 Simple test framework - capable of parsing Hg cmdline output to compare with Java result
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
41 Process p = new ProcessBuilder(cmd).directory(dir).redirectErrorStream(true).start();
fac8e7fcc8b0 Simple test framework - capable of parsing Hg cmdline output to compare with Java result
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
42 // Process p = Runtime.getRuntime().exec(cmd, null, dir);
fac8e7fcc8b0 Simple test framework - capable of parsing Hg cmdline output to compare with Java result
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
43 InputStreamReader stdOut = new InputStreamReader(p.getInputStream());
fac8e7fcc8b0 Simple test framework - capable of parsing Hg cmdline output to compare with Java result
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
44 LinkedList<CharBuffer> l = new LinkedList<CharBuffer>();
fac8e7fcc8b0 Simple test framework - capable of parsing Hg cmdline output to compare with Java result
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
45 int r = -1;
fac8e7fcc8b0 Simple test framework - capable of parsing Hg cmdline output to compare with Java result
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
46 CharBuffer b = null;
fac8e7fcc8b0 Simple test framework - capable of parsing Hg cmdline output to compare with Java result
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
47 do {
fac8e7fcc8b0 Simple test framework - capable of parsing Hg cmdline output to compare with Java result
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
48 if (b == null || b.remaining() < b.capacity() / 3) {
fac8e7fcc8b0 Simple test framework - capable of parsing Hg cmdline output to compare with Java result
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
49 b = CharBuffer.allocate(512);
fac8e7fcc8b0 Simple test framework - capable of parsing Hg cmdline output to compare with Java result
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
50 l.add(b);
fac8e7fcc8b0 Simple test framework - capable of parsing Hg cmdline output to compare with Java result
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
51 }
fac8e7fcc8b0 Simple test framework - capable of parsing Hg cmdline output to compare with Java result
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
52 r = stdOut.read(b);
fac8e7fcc8b0 Simple test framework - capable of parsing Hg cmdline output to compare with Java result
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
53 } while (r != -1);
fac8e7fcc8b0 Simple test framework - capable of parsing Hg cmdline output to compare with Java result
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
54 int total = 0;
fac8e7fcc8b0 Simple test framework - capable of parsing Hg cmdline output to compare with Java result
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
55 for (CharBuffer cb : l) {
fac8e7fcc8b0 Simple test framework - capable of parsing Hg cmdline output to compare with Java result
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
56 total += cb.position();
fac8e7fcc8b0 Simple test framework - capable of parsing Hg cmdline output to compare with Java result
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
57 cb.flip();
fac8e7fcc8b0 Simple test framework - capable of parsing Hg cmdline output to compare with Java result
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
58 }
fac8e7fcc8b0 Simple test framework - capable of parsing Hg cmdline output to compare with Java result
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
59 CharBuffer res = CharBuffer.allocate(total);
fac8e7fcc8b0 Simple test framework - capable of parsing Hg cmdline output to compare with Java result
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
60 for (CharBuffer cb : l) {
fac8e7fcc8b0 Simple test framework - capable of parsing Hg cmdline output to compare with Java result
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
61 res.put(cb);
fac8e7fcc8b0 Simple test framework - capable of parsing Hg cmdline output to compare with Java result
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
62 }
fac8e7fcc8b0 Simple test framework - capable of parsing Hg cmdline output to compare with Java result
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
63 res.flip();
fac8e7fcc8b0 Simple test framework - capable of parsing Hg cmdline output to compare with Java result
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
64 p.waitFor();
fac8e7fcc8b0 Simple test framework - capable of parsing Hg cmdline output to compare with Java result
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
65 parser.parse(res);
fac8e7fcc8b0 Simple test framework - capable of parsing Hg cmdline output to compare with Java result
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
66 }
fac8e7fcc8b0 Simple test framework - capable of parsing Hg cmdline output to compare with Java result
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
67 }