KickJava   Java API By Example, From Geeks To Geeks.

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


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 Double command.
15  *
16  * @author Joy Agustin
17  */

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

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

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

82   public void testIllegalDouble() throws Exception JavaDoc {
83     WebConversation conversation = new WebConversation();
84
85     // Get the initialized stack page and check that we got it.
86
String JavaDoc initStackUrl = testHost + "stackmvc/controller?CommandName=Clear";
87     WebResponse response = conversation.getResponse(initStackUrl);
88
89     // Push a single value onto the stack.
90
WebForm pushForm = response.getFormWithID("PushForm");
91     WebRequest pushRequest = pushForm.getRequest();
92     response = conversation.getResponse(pushRequest);
93
94     // Pop the stack once to clear stack.
95
WebForm popForm = response.getFormWithID("PopForm");
96     response = conversation.getResponse(popForm.getRequest());
97     popForm = response.getFormWithID("PopForm");
98     response = conversation.getResponse(popForm.getRequest());
99
100     // Double the stack once. (First time is illegal).
101
WebForm doubleForm = response.getFormWithID("DoubleForm");
102     response = conversation.getResponse(doubleForm.getRequest());
103
104     // Now check that "Stack is empty" and error message is displayed.
105
WebTable stackTopTable = response.getTableWithID("stackTop");
106     String JavaDoc stackTop = stackTopTable.getCellAsText(0,0);
107     assertEquals("Checking stack top message", "Top of stack is: The stack is empty.", stackTop);
108
109     // Check that the stack is empty.
110
WebTable stackTable = response.getTableWithID("StackTable");
111     assertEquals("Checking stack size", 0, stackTable.getRowCount());
112
113     WebTable errorMessageTable = response.getTableWithID("ErrorMessageTable");
114     String JavaDoc errorMessage = errorMessageTable.getCellAsText(0,0);
115     assertEquals("Checking error message", "Attempt to double empty stack.", errorMessage);
116   }
117
118   /**
119    * Provide stand-alone execution of this test case during initial development.
120    *
121    * @param args The command line arguments
122    */

123   public static void main(String JavaDoc[] args) {
124     System.out.println("JUnit testing Double command.");
125     //Runs all no-arg methods starting with "test".
126
TestRunner.run(new TestSuite(TestDoubleCommand.class));
127   }
128 }
129
Popular Tags