001: package csdl.stackmvc.control.command;
002:
003: import com.meterware.httpunit.WebConversation;
004: import com.meterware.httpunit.WebForm;
005: import com.meterware.httpunit.WebRequest;
006: import com.meterware.httpunit.WebResponse;
007: import com.meterware.httpunit.WebTable;
008:
009: import junit.framework.TestCase;
010: import junit.framework.TestSuite;
011: import junit.textui.TestRunner;
012:
013: /**
014: * Tests operation of the StackMVC Double command.
015: *
016: * @author Joy Agustin
017: */
018: public class TestDoubleCommand extends TestCase {
019:
020: /** The stackMVC's single page title. */
021: private String pageTitle = "Stack MVC";
022:
023: /** Get the test host. */
024: private String testHost = System.getProperty("test_host");
025:
026: /**
027: * Required for JUnit.
028: *
029: * @param name Test case name.
030: */
031: public TestDoubleCommand(String name) {
032: super (name);
033: }
034:
035: /**
036: * Tests the stackMVC double operation under normal situation.
037: *
038: * @throws Exception If problems occur during test.
039: */
040: public void testLegalDouble() throws Exception {
041: WebConversation conversation = new WebConversation();
042:
043: // Get the initialized stack page and check that we got it.
044: String initStackUrl = testHost
045: + "stackmvc/controller?CommandName=Clear";
046: WebResponse response = conversation.getResponse(initStackUrl);
047: assertEquals("Checking initialized stack page", pageTitle,
048: response.getTitle());
049:
050: // Push a single value onto the stack.
051: WebForm pushForm = response.getFormWithID("PushForm");
052: WebRequest pushRequest = pushForm.getRequest();
053: response = conversation.getResponse(pushRequest);
054:
055: // Check that the stack contains a single value
056: WebTable stackTable = response.getTableWithID("StackTable");
057: assertEquals("Checking stack size", 1, stackTable.getRowCount());
058:
059: // Double a single value on the stack.
060: WebForm doubleForm = response.getFormWithID("DoubleForm");
061: WebRequest doubleRequest = doubleForm.getRequest();
062: response = conversation.getResponse(doubleRequest);
063:
064: // Check that the stack contains two values
065: stackTable = response.getTableWithID("StackTable");
066: assertEquals("Checking stack size", 2, stackTable.getRowCount());
067:
068: // Pop the stack twice and make sure that it's empty.
069: WebForm popForm = response.getFormWithID("PopForm");
070: response = conversation.getResponse(popForm.getRequest());
071:
072: popForm = response.getFormWithID("PopForm");
073: response = conversation.getResponse(popForm.getRequest());
074:
075: stackTable = response.getTableWithID("StackTable");
076: assertEquals("Checking size of popped stack", 0, stackTable
077: .getRowCount());
078: }
079:
080: /**
081: * Tests that stackMVC signals an error when an empty stack is doubled.
082: *
083: * @throws Exception If problems occur during test.
084: */
085: public void testIllegalDouble() throws Exception {
086: WebConversation conversation = new WebConversation();
087:
088: // Get the initialized stack page and check that we got it.
089: String initStackUrl = testHost
090: + "stackmvc/controller?CommandName=Clear";
091: WebResponse response = conversation.getResponse(initStackUrl);
092:
093: // Push a single value onto the stack.
094: WebForm pushForm = response.getFormWithID("PushForm");
095: WebRequest pushRequest = pushForm.getRequest();
096: response = conversation.getResponse(pushRequest);
097:
098: // Pop the stack once to clear stack.
099: WebForm popForm = response.getFormWithID("PopForm");
100: response = conversation.getResponse(popForm.getRequest());
101: popForm = response.getFormWithID("PopForm");
102: response = conversation.getResponse(popForm.getRequest());
103:
104: // Double the stack once. (First time is illegal).
105: WebForm doubleForm = response.getFormWithID("DoubleForm");
106: response = conversation.getResponse(doubleForm.getRequest());
107:
108: // Now check that "Stack is empty" and error message is displayed.
109: WebTable stackTopTable = response.getTableWithID("stackTop");
110: String stackTop = stackTopTable.getCellAsText(0, 0);
111: assertEquals("Checking stack top message",
112: "Top of stack is: The stack is empty.", stackTop);
113:
114: // Check that the stack is empty.
115: WebTable stackTable = response.getTableWithID("StackTable");
116: assertEquals("Checking stack size", 0, stackTable.getRowCount());
117:
118: WebTable errorMessageTable = response
119: .getTableWithID("ErrorMessageTable");
120: String errorMessage = errorMessageTable.getCellAsText(0, 0);
121: assertEquals("Checking error message",
122: "Attempt to double empty stack.", errorMessage);
123: }
124:
125: /**
126: * Provide stand-alone execution of this test case during initial development.
127: *
128: * @param args The command line arguments
129: */
130: public static void main(String[] args) {
131: System.out.println("JUnit testing Double command.");
132: //Runs all no-arg methods starting with "test".
133: TestRunner.run(new TestSuite(TestDoubleCommand.class));
134: }
135: }
|