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 Pop command.
015: *
016: * @author Jitender Miglani
017: * @author Philip Johnson
018: */
019: public class TestPopCommand extends TestCase {
020:
021: /** The stackMVC's single page title. */
022: private String pageTitle = "Stack MVC";
023:
024: /** Get the test host. */
025: private String testHost = System.getProperty("test_host");
026:
027: /**
028: * Required for JUnit.
029: *
030: * @param name Test case name.
031: */
032: public TestPopCommand(String name) {
033: super (name);
034: }
035:
036: /**
037: * Tests the stackMVC pop operation under normal situation.
038: *
039: * @throws Exception If problems occur during test.
040: */
041: public void testLegalPop() throws Exception {
042: WebConversation conversation = new WebConversation();
043:
044: // Get the initialized stack page and check that we got it.
045: String initStackUrl = testHost
046: + "stackmvc/controller?CommandName=Clear";
047: WebResponse response = conversation.getResponse(initStackUrl);
048: assertEquals("Checking initialized stack page", pageTitle,
049: response.getTitle());
050:
051: // Push a single value onto the stack.
052: WebForm pushForm = response.getFormWithID("PushForm");
053: WebRequest pushRequest = pushForm.getRequest();
054: response = conversation.getResponse(pushRequest);
055:
056: // Check that the stack contains a single value
057: WebTable stackTable = response.getTableWithID("StackTable");
058: assertEquals("Checking stack size", 1, stackTable.getRowCount());
059:
060: // Pop the stack and make sure that it's empty.
061: WebForm popForm = response.getFormWithID("PopForm");
062: response = conversation.getResponse(popForm.getRequest());
063: stackTable = response.getTableWithID("StackTable");
064: assertEquals("Checking size of popped stack", 0, stackTable
065: .getRowCount());
066: }
067:
068: /**
069: * Tests that stackMVC signals an error when an empty stack is popped.
070: *
071: * @throws Exception If problems occur during test.
072: */
073: public void testIllegalPop() throws Exception {
074: WebConversation conversation = new WebConversation();
075:
076: // Get the initialized stack page and check that we got it.
077: String initStackUrl = testHost
078: + "stackmvc/controller?CommandName=Clear";
079: WebResponse response = conversation.getResponse(initStackUrl);
080:
081: // Push a single value onto the stack.
082: WebForm pushForm = response.getFormWithID("PushForm");
083: WebRequest pushRequest = pushForm.getRequest();
084: response = conversation.getResponse(pushRequest);
085:
086: // Pop the stack twice. (Second time is illegal).
087: WebForm popForm = response.getFormWithID("PopForm");
088: response = conversation.getResponse(popForm.getRequest());
089: popForm = response.getFormWithID("PopForm");
090: response = conversation.getResponse(popForm.getRequest());
091:
092: // Now check that error message is displayed.
093: WebTable errorMessageTable = response
094: .getTableWithID("ErrorMessageTable");
095: String errorMessage = errorMessageTable.getCellAsText(0, 0);
096: assertEquals("Checking error message",
097: "Attempt to pop empty stack.", errorMessage);
098: }
099:
100: /**
101: * Provide stand-alone execution of this test case during initial development.
102: *
103: * @param args The command line arguments
104: */
105: public static void main(String[] args) {
106: System.out.println("JUnit testing Pop command.");
107: //Runs all no-arg methods starting with "test".
108: TestRunner.run(new TestSuite(TestPopCommand.class));
109: }
110: }
|