KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > Ostermiller > util > ExecHelperTests


1 /*
2  * Regression tests for ExecHelper
3  * Copyright (C) 2005 Stephen Ostermiller
4  * http://ostermiller.org/contact.pl?regarding=Java+Utilities
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (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 import java.util.*;
22
23 /**
24  * Regression test for ExecHelper. When run, this program
25  * should nothing unless an error occurs.
26  *
27  * More information about this class is available from <a target="_top" HREF=
28  * "http://ostermiller.org/utils/ExecHelper.html">ostermiller.org</a>.
29  *
30  * @author Stephen Ostermiller http://ostermiller.org/contact.pl?regarding=Java+Utilities
31  * @since ostermillerutils 1.06.00
32  */

33 class ExecHelperTests {
34
35     public static void main(String JavaDoc args[]){
36         try {
37             File temp = File.createTempFile("ExecHelperTests", "tmp");
38             temp.deleteOnExit();
39             String JavaDoc s = createLargeString();
40             Writer out = new FileWriter(temp);
41             out.write(s);
42             out.close();
43             ExecHelper eh = ExecHelper.exec(new String JavaDoc[]{"cat", temp.toString()});
44             if (!eh.getOutput().equals(s)){
45                 throw new Exception JavaDoc("Couldn't read file via cat");
46             }
47             // Test the shell, but only on unix
48
File sh = new File("/bin/sh");
49             if (sh.exists()){
50                 eh = ExecHelper.execUsingShell("sleep 3; echo -n stdin && echo -n stderr 1>&2; exit 11");
51                 if (!eh.getOutput().equals("stdin")){
52                     throw new Exception JavaDoc("Couldn't echo to stdin through a shell.");
53                 }
54                 if (!eh.getError().equals("stderr")){
55                     throw new Exception JavaDoc("Couldn't echo to stderr through a shell.");
56                 }
57                 if (eh.getStatus() != 11){
58                     throw new Exception JavaDoc("Expected exit status of 11.");
59                 }
60             }
61         } catch (Exception JavaDoc x){
62             x.printStackTrace();
63             System.exit(1);
64         }
65         System.exit(0);
66     }
67
68     private static String JavaDoc createLargeString(){
69         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(40000*26);
70         for (int i=0; i<40000; i++){
71             sb.append("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
72         }
73         return sb.toString();
74
75     }
76 }
77
Popular Tags