KickJava   Java API By Example, From Geeks To Geeks.

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


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.WebLink;
8 import com.meterware.httpunit.WebTable;
9
10 import junit.framework.TestCase;
11 import junit.framework.TestSuite;
12 import junit.textui.TestRunner;
13
14 /**
15  * Tests operation of the StackMVC Clear command.
16  *
17  * @author Jitender Miglani
18  * @author Philip Johnson
19  */

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

33   public TestClearCommand(String JavaDoc name) {
34     super(name);
35   }
36
37   /**
38    * Tests the stackMVC clear operation.
39    *
40    * @throws Exception If problems occur
41    */

42   public void testClear() throws Exception JavaDoc {
43     WebConversation conversation = new WebConversation();
44
45     // Get welcome.jsp page and check for successful retrieval
46
String JavaDoc url = testHost + "stackmvc/welcome.jsp";
47     WebResponse response = conversation.getResponse(url);
48     assertEquals("Checking welcome.jsp retrieval", pageTitle, response.getTitle());
49
50     // Go to the stack page and check that we got it OK.
51
WebLink startLink = response.getLinkWithID("InitStack");
52     response = conversation.getResponse(startLink.getRequest());
53     assertEquals("Checking index.jsp retrieval", pageTitle, response.getTitle());
54
55     // Now try pushing "2" onto the stack.
56
WebForm pushForm = response.getFormWithID("PushForm");
57     WebRequest pushRequest = pushForm.getRequest();
58     pushRequest.setParameter("number", "2");
59     response = conversation.getResponse(pushRequest);
60
61     // Check that the stack contains a single "2"
62
WebTable stackTable = response.getTableWithID("StackTable");
63     assertEquals("Checking stack size", 1, stackTable.getRowCount());
64
65     // Now try clearing the stack
66
WebForm clearForm = response.getFormWithID("ClearForm");
67     response = conversation.getResponse(clearForm.getRequest());
68     stackTable = response.getTableWithID("StackTable");
69     assertEquals("Checking size of cleared stack", 0, stackTable.getRowCount());
70   }
71
72
73   /**
74    * Provide stand-alone execution of this test case during initial development.
75    *
76    * @param args The command line arguments
77    */

78   public static void main(String JavaDoc[] args) {
79     System.out.println("JUnit testing Clear command.");
80     //Runs all no-arg methods starting with "test".
81
TestRunner.run(new TestSuite(TestClearCommand.class));
82   }
83 }
84
Popular Tags