KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > csdl > stackmvc > control > command > TestPopCommand


1 package csdl.stackmvc.control.command;
2
3 import com.meterware.httpunit.WebConversation;
4 import com.meterware.httpunit.WebForm;
5 import com.meterware.httpunit.WebRequest;
6 import com.meterware.httpunit.WebResponse;
7 import com.meterware.httpunit.WebTable;
8
9 import junit.framework.TestCase;
10 import junit.framework.TestSuite;
11 import junit.textui.TestRunner;
12
13 /**
14  * Tests operation of the StackMVC Pop command.
15  *
16  * @author Jitender Miglani
17  * @author Philip Johnson
18  */

19 public class TestPopCommand extends TestCase {
20
21   /** The stackMVC's single page title. */
22   private String JavaDoc pageTitle = "Stack MVC";
23
24   /** Get the test host. */
25   private String JavaDoc testHost = System.getProperty("test_host");
26
27   /**
28    * Required for JUnit.
29    *
30    * @param name Test case name.
31    */

32   public TestPopCommand(String JavaDoc name) {
33     super(name);
34   }
35
36   /**
37    * Tests the stackMVC pop operation under normal situation.
38    *
39    * @throws Exception If problems occur during test.
40    */

41   public void testLegalPop() throws Exception JavaDoc {
42     WebConversation conversation = new WebConversation();
43
44     // Get the initialized stack page and check that we got it.
45
String JavaDoc initStackUrl = testHost + "stackmvc/controller?CommandName=Clear";
46     WebResponse response = conversation.getResponse(initStackUrl);
47     assertEquals("Checking initialized stack page", pageTitle, response.getTitle());
48
49     // Push a single value onto the stack.
50
WebForm pushForm = response.getFormWithID("PushForm");
51     WebRequest pushRequest = pushForm.getRequest();
52     response = conversation.getResponse(pushRequest);
53
54     // Check that the stack contains a single value
55
WebTable stackTable = response.getTableWithID("StackTable");
56     assertEquals("Checking stack size", 1, stackTable.getRowCount());
57
58     // Pop the stack and make sure that it's empty.
59
WebForm popForm = response.getFormWithID("PopForm");
60     response = conversation.getResponse(popForm.getRequest());
61     stackTable = response.getTableWithID("StackTable");
62     assertEquals("Checking size of popped stack", 0, stackTable.getRowCount());
63   }
64
65   /**
66    * Tests that stackMVC signals an error when an empty stack is popped.
67    *
68    * @throws Exception If problems occur during test.
69    */

70   public void testIllegalPop() throws Exception JavaDoc {
71     WebConversation conversation = new WebConversation();
72
73     // Get the initialized stack page and check that we got it.
74
String JavaDoc initStackUrl = testHost + "stackmvc/controller?CommandName=Clear";
75     WebResponse response = conversation.getResponse(initStackUrl);
76
77     // Push a single value onto the stack.
78
WebForm pushForm = response.getFormWithID("PushForm");
79     WebRequest pushRequest = pushForm.getRequest();
80     response = conversation.getResponse(pushRequest);
81
82     // Pop the stack twice. (Second time is illegal).
83
WebForm popForm = response.getFormWithID("PopForm");
84     response = conversation.getResponse(popForm.getRequest());
85     popForm = response.getFormWithID("PopForm");
86     response = conversation.getResponse(popForm.getRequest());
87
88     // Now check that error message is displayed.
89
WebTable errorMessageTable = response.getTableWithID("ErrorMessageTable");
90     String JavaDoc errorMessage = errorMessageTable.getCellAsText(0,0);
91     assertEquals("Checking error message", "Attempt to pop empty stack.", errorMessage);
92   }
93
94
95   /**
96    * Provide stand-alone execution of this test case during initial development.
97    *
98    * @param args The command line arguments
99    */

100   public static void main(String JavaDoc[] args) {
101     System.out.println("JUnit testing Pop command.");
102     //Runs all no-arg methods starting with "test".
103
TestRunner.run(new TestSuite(TestPopCommand.class));
104   }
105 }
106
Popular Tags