001: // Copyright (C) 2003,2004,2005 by Object Mentor, Inc. All rights reserved.
002: // Released under the terms of the GNU General Public License version 2 or later.
003: package fitnesse.responders;
004:
005: import fitnesse.Responder;
006: import fitnesse.http.MockResponseSender;
007: import fitnesse.http.Response;
008: import fitnesse.wiki.PathParser;
009:
010: public class ExecuteProcessResponderTest extends AbstractResponderRegex {
011: protected Responder responderInstance() {
012: return new ExecuteProcessResponder();
013: }
014:
015: private String arguments;
016: private String delim = ";";
017:
018: public void SetUp() throws Exception {
019: crawler.addPage(root, PathParser.parse("SimplePage"),
020: "simple content");
021: request.setResource("SimplePage");
022: }
023:
024: public void testMissingArgs() throws Exception {
025: arguments = "";
026: String result = getResponse(arguments);
027: assertMatches("Need 1 or more arguments", result);
028: }
029:
030: public void testCommandArrayWindows95() throws Exception {
031: ExecuteProcessResponder execProcResponder = new ExecuteProcessResponder();
032: execProcResponder.OsName = execProcResponder.OS_NAME_WINDOWS_95;
033: arguments = "c:\test.bat";
034: String[] cmd = execProcResponder.getCommandArray(arguments);
035: assertEquals(execProcResponder.COMMAND_NAME_WINDOWS_95, cmd[0]);
036: assertEquals(execProcResponder.COMMAND_FLAG_WINDOWS, cmd[1]);
037: assertEquals(arguments, cmd[2]);
038: }
039:
040: public void testSimpleCommandArrayWindows() throws Exception {
041: ExecuteProcessResponder execProcResponder = new ExecuteProcessResponder();
042: execProcResponder.OsName = "Windows";
043: arguments = "c:\\test.bat";
044: String[] cmd = execProcResponder.getCommandArray(arguments);
045: assertEquals(execProcResponder.COMMAND_NAME_WINDOWS, cmd[0]);
046: assertEquals(execProcResponder.COMMAND_FLAG_WINDOWS, cmd[1]);
047: assertEquals(arguments, cmd[2]);
048: }
049:
050: public void testComplexCommandArrayWindows() throws Exception {
051: ExecuteProcessResponder execProcResponder = new ExecuteProcessResponder();
052: execProcResponder.OsName = "Windows";
053: String arg1 = "c:\\test.bat";
054: String arg2 = "this";
055: String arg3 = "that";
056: String arg4 = "another";
057: arguments = arg1 + delim + arg2 + delim + arg3 + delim + arg4;
058:
059: String[] cmd = execProcResponder.getCommandArray(arguments);
060: assertEquals(execProcResponder.COMMAND_NAME_WINDOWS, cmd[0]);
061: assertEquals(execProcResponder.COMMAND_FLAG_WINDOWS, cmd[1]);
062: assertEquals(arg1, cmd[2]);
063: assertEquals(arg2, cmd[3]);
064: assertEquals(arg3, cmd[4]);
065: assertEquals(arg4, cmd[5]);
066: }
067:
068: // TODO: What's the real OS Name?
069: public void testCommandArrayNOTWindows() throws Exception {
070: ExecuteProcessResponder execProcResponder = new ExecuteProcessResponder();
071: execProcResponder.OsName = "MacOS X";
072: String arg1 = "/bin/sh";
073: String arg2 = "-c";
074: String arg3 = "ls > hello";
075: arguments = arg1 + delim + arg2 + delim + arg3;
076:
077: String[] cmd = execProcResponder.getCommandArray(arguments);
078: assertEquals(arg1, cmd[0]);
079: assertEquals(arg2, cmd[1]);
080: assertEquals(arg3, cmd[2]);
081: }
082:
083: public void testSubstituteWorkingDirNoArgs() throws Exception {
084: ExecuteProcessResponder execProcResponder = new ExecuteProcessResponder();
085: execProcResponder.OsName = "Windows";
086: arguments = "\"c:\\test.bat\"";
087: String[] cmd = execProcResponder.getCommandArray(arguments);
088: assertEquals(execProcResponder.COMMAND_NAME_WINDOWS, cmd[0]);
089: assertEquals(execProcResponder.COMMAND_FLAG_WINDOWS, cmd[1]);
090: assertEquals(arguments, cmd[2]);
091: }
092:
093: public void testSubstituteWorkingDirWithOtherArgs()
094: throws Exception {
095: ExecuteProcessResponder execProcResponder = new ExecuteProcessResponder();
096: execProcResponder.OsName = "Windows";
097: String arg1 = "\"c:\\test.bat\"";
098: String arg2 = "this";
099: String arg3 = "that";
100: String arg4 = "another";
101: arguments = arg1 + delim + arg2 + delim + arg3 + delim + arg4;
102: String[] cmd = execProcResponder.getCommandArray(arguments);
103: assertEquals(execProcResponder.COMMAND_NAME_WINDOWS, cmd[0]);
104: assertEquals(execProcResponder.COMMAND_FLAG_WINDOWS, cmd[1]);
105: assertEquals(arg1, cmd[2]);
106: assertEquals(arg2, cmd[3]);
107: assertEquals(arg3, cmd[4]);
108: assertEquals(arg4, cmd[5]);
109: }
110:
111: private String getResponse(String args) throws Exception {
112: request.addInput("execArgs", args);
113: Response response = responder.makeResponse(context, request);
114: MockResponseSender sender = new MockResponseSender(response);
115: return sender.sentData();
116: }
117: }
|