001: package org.objectweb.celtix.common.commands;
002:
003: import java.io.ByteArrayOutputStream;
004: import java.io.File;
005: import java.io.PrintStream;
006: import java.net.URL;
007: import java.util.StringTokenizer;
008:
009: import junit.framework.TestCase;
010:
011: import org.objectweb.celtix.common.i18n.Message;
012:
013: public class ForkedCommandTest extends TestCase {
014:
015: private static final String[] ENV_COMMAND;
016:
017: static {
018: if (System.getProperty("os.name").startsWith("Windows")) {
019: ENV_COMMAND = new String[] { "cmd", "/c", "set" };
020: } else {
021: ENV_COMMAND = new String[] { "env" };
022: }
023: }
024:
025: public void testBasics() {
026: ForkedCommand fc1 = new ForkedCommand();
027: String cmdline1 = fc1.toString();
028: assertNull(cmdline1);
029: try {
030: fc1.execute();
031: } catch (ForkedCommandException ex) {
032: assertEquals("NO_ARGUMENTS_EXC", ex.getCode());
033: }
034: String[] args = new String[] { "a", "b", "c d e" };
035: ForkedCommand fc2 = new ForkedCommand(args);
036: String cmdline2 = fc2.toString();
037: assertTrue(cmdline2.startsWith("a"));
038: assertTrue(cmdline2.endsWith("\""));
039: fc1.setArgs(args);
040: cmdline1 = fc1.toString();
041: assertEquals(cmdline1, cmdline2);
042:
043: new ForkedCommandException(new NullPointerException());
044: Message msg = org.easymock.classextension.EasyMock
045: .createMock(Message.class);
046: new ForkedCommandException(msg, new NullPointerException());
047: }
048:
049: public void testExecuteInDefaultEnvironment() {
050: ByteArrayOutputStream bosOut = new ByteArrayOutputStream();
051: ByteArrayOutputStream bosErr = new ByteArrayOutputStream();
052:
053: executeEnvCommand(null, bosOut, bosErr);
054:
055: String output = bosOut.toString();
056: assertTrue(output.indexOf("AVAR") < 0
057: || output.indexOf("BVAR") < 0);
058: }
059:
060: public void testExecuteInNonDefaultEnvironment() {
061: ByteArrayOutputStream bosOut = new ByteArrayOutputStream();
062: ByteArrayOutputStream bosErr = new ByteArrayOutputStream();
063: String[] env = new String[3];
064: env[0] = "BVAR=strange";
065: if (System.getProperty("os.name").startsWith("Windows")) {
066: env[1] = "AVAR=something %BVAR%";
067: env[2] = "AVAR=something very %BVAR%";
068: } else {
069: env[1] = "AVAR=something $BVAR";
070: env[2] = "AVAR=something very $BVAR";
071: }
072:
073: executeEnvCommand(env, bosOut, bosErr);
074:
075: // test variables are overwritten but not replaced
076:
077: boolean found = false;
078: String output = bosOut.toString();
079: StringTokenizer st = new StringTokenizer(output, System
080: .getProperty("line.separator"));
081: while (st.hasMoreTokens()) {
082: String line = st.nextToken();
083: if (line.length() > 0) {
084: if (System.getProperty("os.name").startsWith("Windows")) {
085: if ("AVAR=something very %BVAR%".equals(line)) {
086: found = true;
087: break;
088: }
089: } else {
090: if ("AVAR=something very $BVAR".equals(line)
091: || "AVAR=something $BVAR".equals(line)) {
092: found = true;
093: break;
094: }
095: }
096: }
097: }
098: assertTrue(found);
099:
100: }
101:
102: public void testTimeout() throws Exception {
103: URL url = TestCommand.class.getResource("TestCommand.class");
104: File file = new File(url.getFile());
105: file = file.getParentFile();
106: file = new File(file, "../../../../..");
107: String[] cmd = new String[] { JavaHelper.getJavaCommand(),
108: "-classpath", file.getCanonicalPath(),
109: "org.objectweb.celtix.common.commands.TestCommand",
110: "-duration", "60000", };
111: ForkedCommand fc = new ForkedCommand(cmd);
112: try {
113: fc.execute(1);
114: fail("Expected ForkedCommandException not thrown.");
115: } catch (ForkedCommandException ex) {
116: assertEquals("TIMEOUT_EXC", ex.getCode());
117: }
118: }
119:
120: private void executeEnvCommand(String[] env,
121: ByteArrayOutputStream bosOut, ByteArrayOutputStream bosErr) {
122:
123: ForkedCommand fc = new ForkedCommand(ENV_COMMAND);
124: if (null != env) {
125: fc.setEnvironment(env);
126: }
127: fc.joinErrOut(true);
128:
129: PrintStream pso = new PrintStream(bosOut);
130: PrintStream pse = new PrintStream(bosErr);
131: fc.setOutputStream(pso);
132: fc.setErrorStream(pse);
133:
134: int result = fc.execute();
135: assertEquals(0, result);
136:
137: }
138:
139: }
|