01: /*
02: * Regression tests for ExecHelper
03: * Copyright (C) 2005 Stephen Ostermiller
04: * http://ostermiller.org/contact.pl?regarding=Java+Utilities
05: *
06: * This program is free software; you can redistribute it and/or modify
07: * it under the terms of the GNU General Public License as published by
08: * the Free Software Foundation; either version 2 of the License, or
09: * (at your option) any later version.
10: *
11: * This program is distributed in the hope that it will be useful,
12: * but WITHOUT ANY WARRANTY; without even the implied warranty of
13: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14: * GNU General Public License for more details.
15: *
16: * See COPYING.TXT for details.
17: */
18: package com.Ostermiller.util;
19:
20: import java.io.*;
21:
22: /**
23: * Regression test for ExecHelper. When run, this program
24: * should nothing unless an error occurs.
25: *
26: * More information about this class is available from <a target="_top" href=
27: * "http://ostermiller.org/utils/ExecHelper.html">ostermiller.org</a>.
28: *
29: * @author Stephen Ostermiller http://ostermiller.org/contact.pl?regarding=Java+Utilities
30: * @since ostermillerutils 1.06.00
31: */
32: class ExecHelperTests {
33:
34: /**
35: * Main method to run test
36: * @param args Command line arguments (ignored
37: */
38: public static void main(String args[]) {
39: try {
40: File temp = File.createTempFile("ExecHelperTests", "tmp");
41: temp.deleteOnExit();
42: String s = createLargeString();
43: Writer out = new FileWriter(temp);
44: out.write(s);
45: out.close();
46: ExecHelper eh = ExecHelper.exec(new String[] { "cat",
47: temp.toString() });
48: if (!eh.getOutput().equals(s)) {
49: throw new Exception("Couldn't read file via cat");
50: }
51: // Test the shell, but only on Unix
52: File sh = new File("/bin/sh");
53: if (sh.exists()) {
54: eh = ExecHelper
55: .execUsingShell("sleep 3; echo -n stdin && echo -n stderr 1>&2; exit 11");
56: if (!eh.getOutput().equals("stdin")) {
57: throw new Exception(
58: "Couldn't echo to stdin through a shell.");
59: }
60: if (!eh.getError().equals("stderr")) {
61: throw new Exception(
62: "Couldn't echo to stderr through a shell.");
63: }
64: if (eh.getStatus() != 11) {
65: throw new Exception("Expected exit status of 11.");
66: }
67: }
68: } catch (Exception x) {
69: x.printStackTrace();
70: System.exit(1);
71: }
72: System.exit(0);
73: }
74:
75: private static String createLargeString() {
76: StringBuffer sb = new StringBuffer(40000 * 26);
77: for (int i = 0; i < 40000; i++) {
78: sb.append("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
79: }
80: return sb.toString();
81:
82: }
83: }
|