File: ExampleTest.java

package info (click to toggle)
httpunit 1.7%2Bdfsg-9
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 3,412 kB
  • sloc: java: 33,504; xml: 483; sh: 68; makefile: 11
file content (107 lines) | stat: -rw-r--r-- 3,353 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import com.meterware.httpunit.*;

import junit.framework.*;


/**
 * An example of testing servlets using httpunit and JUnit.
 **/
public class ExampleTest extends TestCase {


	/**
	 * run this testcase as a suite from the command line
	 * @param args - ignored
	 */
	public static void main(String args[]) {
        junit.textui.TestRunner.run( suite() );
	}
	
	
	/**
	 * supply this test cases as part of a suite
	 * @return
	 */
	public static Test suite() {
		return new TestSuite( ExampleTest.class );
	}


	/**
	 * constructor with name parameter needed for suite creation
	 * @param name
	 */
	public ExampleTest( String name ) {
	    super( name );
	}

	/**
	 * try getting a response for the given Conversation and Request
	 * show an error message if a 404 error appears
	 * @param conversation - the conversation to use
	 * @param request
	 * @return the response
	 * @throws an Exception if getting the response fails
	 */
	public WebResponse tryGetResponse(WebConversation conversation,WebRequest request) throws Exception {
		WebResponse response=null;
		try {
			response = conversation.getResponse( request );
		} catch (HttpNotFoundException nfe) {
			System.err.println("The URL '"+request.getURL()+"' is not active any more");
			throw nfe;
		}
		return response;
	}

  /**
   * Verifies that the welcome page has exactly one form, with the single parameter, "name"
   **/
  public void testWelcomePage() throws Exception {
      WebConversation     conversation = new WebConversation();
      WebRequest request = new GetMethodWebRequest( "http://www.meterware.com/servlet/TopSecret" );
     	WebResponse response = tryGetResponse(conversation, request );
      
      WebForm forms[] = response.getForms();
      assertEquals( 1, forms.length );
      assertEquals( 1, forms[0].getParameterNames().length );
      assertEquals( "name", forms[0].getParameterNames()[0] );
  }


  /**
   * Verifies that submitting the login form without entering a name results in a page
   * containing the text "Login failed"
   **/
  public void testBadLogin() throws Exception {
      WebConversation     conversation = new WebConversation();
      WebRequest  request = new GetMethodWebRequest( "http://www.meterware.com/servlet/TopSecret" );

     	WebResponse response = tryGetResponse(conversation, request );
      WebForm loginForm = response.getForms()[0];
      request = loginForm.getRequest();
      response = conversation.getResponse( request );
      assertTrue( "Login not rejected", response.getText().indexOf( "Login failed" ) != -1 );
  }


  /**
   * Verifies that submitting the login form with the name "master" results
   * in a page containing the text "Top Secret"
   **/
  public void testGoodLogin() throws Exception {
      WebConversation     conversation = new WebConversation();
      WebRequest  request = new GetMethodWebRequest( "http://www.meterware.com/servlet/TopSecret" );

     	WebResponse response = tryGetResponse(conversation, request );
      WebForm loginForm = response.getForms()[0];
      request = loginForm.getRequest();
      request.setParameter( "name", "master" );
      response = conversation.getResponse( request );
      assertTrue( "Login not accepted", response.getText().indexOf( "You made it!" ) != -1 );

      assertEquals( "Page title", "Top Secret", response.getTitle() );
  }
}