KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > celtix > common > commands > ForkedCommandTest


1 package org.objectweb.celtix.common.commands;
2
3 import java.io.ByteArrayOutputStream JavaDoc;
4 import java.io.File JavaDoc;
5 import java.io.PrintStream JavaDoc;
6 import java.net.URL JavaDoc;
7 import java.util.StringTokenizer JavaDoc;
8
9 import junit.framework.TestCase;
10
11 import org.objectweb.celtix.common.i18n.Message;
12
13 public class ForkedCommandTest extends TestCase {
14
15     private static final String JavaDoc[] ENV_COMMAND;
16
17     static {
18         if (System.getProperty("os.name").startsWith("Windows")) {
19             ENV_COMMAND = new String JavaDoc[] {"cmd", "/c", "set"};
20         } else {
21             ENV_COMMAND = new String JavaDoc[] {"env"};
22         }
23     }
24
25     public void testBasics() {
26         ForkedCommand fc1 = new ForkedCommand();
27         String JavaDoc cmdline1 = fc1.toString();
28         assertNull(cmdline1);
29         try {
30             fc1.execute();
31         } catch (ForkedCommandException ex) {
32             assertEquals("NO_ARGUMENTS_EXC", ex.getCode());
33         }
34         String JavaDoc[] args = new String JavaDoc[] {"a", "b", "c d e"};
35         ForkedCommand fc2 = new ForkedCommand(args);
36         String JavaDoc cmdline2 = fc2.toString();
37         assertTrue(cmdline2.startsWith("a"));
38         assertTrue(cmdline2.endsWith("\""));
39         fc1.setArgs(args);
40         cmdline1 = fc1.toString();
41         assertEquals(cmdline1, cmdline2);
42         
43         new ForkedCommandException(new NullPointerException JavaDoc());
44         Message msg = org.easymock.classextension.EasyMock.createMock(Message.class);
45         new ForkedCommandException(msg, new NullPointerException JavaDoc());
46     }
47
48     public void testExecuteInDefaultEnvironment() {
49         ByteArrayOutputStream JavaDoc bosOut = new ByteArrayOutputStream JavaDoc();
50         ByteArrayOutputStream JavaDoc bosErr = new ByteArrayOutputStream JavaDoc();
51         
52         executeEnvCommand(null, bosOut, bosErr);
53         
54         String JavaDoc output = bosOut.toString();
55         assertTrue(output.indexOf("AVAR") < 0 || output.indexOf("BVAR") < 0);
56     }
57     
58     public void testExecuteInNonDefaultEnvironment() {
59         ByteArrayOutputStream JavaDoc bosOut = new ByteArrayOutputStream JavaDoc();
60         ByteArrayOutputStream JavaDoc bosErr = new ByteArrayOutputStream JavaDoc();
61         String JavaDoc[] env = new String JavaDoc[3];
62         env[0] = "BVAR=strange";
63         if (System.getProperty("os.name").startsWith("Windows")) {
64             env[1] = "AVAR=something %BVAR%";
65             env[2] = "AVAR=something very %BVAR%";
66         } else {
67             env[1] = "AVAR=something $BVAR";
68             env[2] = "AVAR=something very $BVAR";
69         }
70         
71         
72         executeEnvCommand(env, bosOut, bosErr);
73         
74         // test variables are overwritten but not replaced
75

76         boolean found = false;
77         String JavaDoc output = bosOut.toString();
78         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(output, System.getProperty("line.separator"));
79         while (st.hasMoreTokens()) {
80             String JavaDoc line = st.nextToken();
81             if (line.length() > 0) {
82                 if (System.getProperty("os.name").startsWith("Windows")) {
83                     if ("AVAR=something very %BVAR%".equals(line)) {
84                         found = true;
85                         break;
86                     }
87                 } else {
88                     if ("AVAR=something very $BVAR".equals(line)
89                         || "AVAR=something $BVAR".equals(line)) {
90                         found = true;
91                         break;
92                     }
93                 }
94             }
95         }
96         assertTrue(found);
97         
98     }
99     
100     public void testTimeout() throws Exception JavaDoc {
101         URL JavaDoc url = TestCommand.class.getResource("TestCommand.class");
102         File JavaDoc file = new File JavaDoc(url.getFile());
103         file = file.getParentFile();
104         file = new File JavaDoc(file, "../../../../..");
105         String JavaDoc[] cmd = new String JavaDoc[] {
106             JavaHelper.getJavaCommand(),
107             "-classpath",
108             file.getCanonicalPath(),
109             "org.objectweb.celtix.common.commands.TestCommand",
110             "-duration",
111             "60000",
112         };
113         ForkedCommand fc = new ForkedCommand(cmd);
114         try {
115             fc.execute(1);
116             fail("Expected ForkedCommandException not thrown.");
117         } catch (ForkedCommandException ex) {
118             assertEquals("TIMEOUT_EXC", ex.getCode());
119         }
120     }
121
122     private void executeEnvCommand(String JavaDoc[] env, ByteArrayOutputStream JavaDoc bosOut, ByteArrayOutputStream JavaDoc bosErr) {
123
124         ForkedCommand fc = new ForkedCommand(ENV_COMMAND);
125         if (null != env) {
126             fc.setEnvironment(env);
127         }
128         fc.joinErrOut(true);
129
130         PrintStream JavaDoc pso = new PrintStream JavaDoc(bosOut);
131         PrintStream JavaDoc pse = new PrintStream JavaDoc(bosErr);
132         fc.setOutputStream(pso);
133         fc.setErrorStream(pse);
134
135         int result = fc.execute();
136         assertEquals(0, result);
137         
138     }
139     
140     
141     
142     
143
144 }
145
Popular Tags