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 18 public class TestDoubleCommand extends TestCase { 19 20 21 private String pageTitle = "Stack MVC"; 22 23 24 private String testHost = System.getProperty("test_host"); 25 26 31 public TestDoubleCommand(String name) { 32 super(name); 33 } 34 35 40 public void testLegalDouble() throws Exception { 41 WebConversation conversation = new WebConversation(); 42 43 String initStackUrl = testHost + "stackmvc/controller?CommandName=Clear"; 45 WebResponse response = conversation.getResponse(initStackUrl); 46 assertEquals("Checking initialized stack page", pageTitle, response.getTitle()); 47 48 WebForm pushForm = response.getFormWithID("PushForm"); 50 WebRequest pushRequest = pushForm.getRequest(); 51 response = conversation.getResponse(pushRequest); 52 53 WebTable stackTable = response.getTableWithID("StackTable"); 55 assertEquals("Checking stack size", 1, stackTable.getRowCount()); 56 57 WebForm doubleForm = response.getFormWithID("DoubleForm"); 59 WebRequest doubleRequest = doubleForm.getRequest(); 60 response = conversation.getResponse(doubleRequest); 61 62 stackTable = response.getTableWithID("StackTable"); 64 assertEquals("Checking stack size", 2, stackTable.getRowCount()); 65 66 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 82 public void testIllegalDouble() throws Exception { 83 WebConversation conversation = new WebConversation(); 84 85 String initStackUrl = testHost + "stackmvc/controller?CommandName=Clear"; 87 WebResponse response = conversation.getResponse(initStackUrl); 88 89 WebForm pushForm = response.getFormWithID("PushForm"); 91 WebRequest pushRequest = pushForm.getRequest(); 92 response = conversation.getResponse(pushRequest); 93 94 WebForm popForm = response.getFormWithID("PopForm"); 96 response = conversation.getResponse(popForm.getRequest()); 97 popForm = response.getFormWithID("PopForm"); 98 response = conversation.getResponse(popForm.getRequest()); 99 100 WebForm doubleForm = response.getFormWithID("DoubleForm"); 102 response = conversation.getResponse(doubleForm.getRequest()); 103 104 WebTable stackTopTable = response.getTableWithID("stackTop"); 106 String stackTop = stackTopTable.getCellAsText(0,0); 107 assertEquals("Checking stack top message", "Top of stack is: The stack is empty.", stackTop); 108 109 WebTable stackTable = response.getTableWithID("StackTable"); 111 assertEquals("Checking stack size", 0, stackTable.getRowCount()); 112 113 WebTable errorMessageTable = response.getTableWithID("ErrorMessageTable"); 114 String errorMessage = errorMessageTable.getCellAsText(0,0); 115 assertEquals("Checking error message", "Attempt to double empty stack.", errorMessage); 116 } 117 118 123 public static void main(String [] args) { 124 System.out.println("JUnit testing Double command."); 125 TestRunner.run(new TestSuite(TestDoubleCommand.class)); 127 } 128 } 129 | Popular Tags |