KickJava   Java API By Example, From Geeks To Geeks.

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


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 Push command.
15  *
16  * @author Jitender Miglani
17  * @author Philip Johnson
18  */

19 public class TestPushCommand 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 TestPushCommand(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
40    */

41   public void testPush() 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 the default value (1) 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 "2"
55
WebTable stackTable = response.getTableWithID("StackTable");
56     assertEquals("Checking stack size (1)", 1, stackTable.getRowCount());
57     assertEquals("Checking stack contents", "1", stackTable.getTableCell(0,0).asText());
58
59     // Now push a second, explicit value (2) onto stack.
60
pushForm = response.getFormWithID("PushForm");
61     pushRequest = pushForm.getRequest();
62     pushRequest.setParameter("number", "2");
63     response = conversation.getResponse(pushRequest);
64
65     // Check that the stack contains two elements and that top of stack is "2".
66
stackTable = response.getTableWithID("StackTable");
67     assertEquals("Checking stack size (2)", 2, stackTable.getRowCount());
68     assertEquals("Checking stack contents (2)", "2", stackTable.getTableCell(1,0).asText());
69   }
70
71
72   /**
73    * Provide stand-alone execution of this test case during initial development.
74    *
75    * @param args The command line arguments
76    */

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