01: package org.objectweb.celtix.common.commands;
02:
03: import java.io.BufferedReader;
04: import java.io.File;
05: import java.io.IOException;
06: import java.io.InputStream;
07: import java.net.URL;
08:
09: import junit.framework.TestCase;
10:
11: public class ResultBufferedCommandTest extends TestCase {
12:
13: private static final String OUT = "Hello World!";
14: private static final String ERR = "Please contact your administrator.";
15:
16: public void testStreamsEmpty() throws IOException {
17: URL url = TestCommand.class.getResource("TestCommand.class");
18: File file = new File(url.getFile());
19: file = file.getParentFile();
20: file = new File(file, "../../../../..");
21: String[] cmd = new String[] { JavaHelper.getJavaCommand(),
22: "-classpath", file.getCanonicalPath(),
23: "org.objectweb.celtix.common.commands.TestCommand", };
24: ResultBufferedCommand rbc = new ResultBufferedCommand(cmd);
25: assertEquals(0, rbc.execute());
26: BufferedReader br = rbc.getBufferedOutputReader();
27: assertNotNull(br);
28: assertNull(br.readLine());
29: br.close();
30: br = rbc.getBufferedErrorReader();
31: assertNotNull(br);
32: assertNull(br.readLine());
33: br.close();
34: InputStream is = rbc.getOutput();
35: assertEquals(0, is.available());
36: is.close();
37: is = rbc.getError();
38: assertEquals(0, is.available());
39: is.close();
40: }
41:
42: public void testStreamsNotEmpty() throws IOException {
43: URL url = TestCommand.class.getResource("TestCommand.class");
44: File file = new File(url.getFile());
45: file = file.getParentFile();
46: file = new File(file, "../../../../..");
47: String[] cmd = new String[] { JavaHelper.getJavaCommand(),
48: "-classpath", file.getCanonicalPath(),
49: "org.objectweb.celtix.common.commands.TestCommand",
50: "-out", OUT, "-err", ERR, "-result", "2", };
51: ResultBufferedCommand rbc = new ResultBufferedCommand();
52: rbc.setArgs(cmd);
53: assertEquals(2, rbc.execute());
54: BufferedReader br = rbc.getBufferedOutputReader();
55: assertNotNull(br);
56: String line = br.readLine();
57: assertEquals(OUT, line);
58: assertNull(br.readLine());
59: br.close();
60: br = rbc.getBufferedErrorReader();
61: assertNotNull(br);
62: line = br.readLine();
63: assertEquals(ERR, line);
64: assertNull(br.readLine());
65: br.close();
66: InputStream is = rbc.getOutput();
67: assertTrue(is.available() > 0);
68: is.close();
69: is = rbc.getError();
70: assertTrue(is.available() > 0);
71: is.close();
72: }
73:
74: }
|