/usr/share/doc/libhttpunit-java-doc/tutorial/task1editor-initial.html is in libhttpunit-java-doc 1.7+dfsg-10.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 108 109 110 111 112 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>HttpUnit Tutorial - create pool editor - step 1</title>
<LINK REL="stylesheet" HREF="tutorial.css" TYPE="text/css">
</head>
<body>
<p class="location"><a href="index.html">Tutorial</a>
<img src="arrow_yellow.gif" width=13 height=9 align=bottom ALT="->"> <a href="task1.html">Task 1</a>
<img src="arrow_yellow.gif" width=13 height=9 align=bottom ALT="->"> Step 1: Invoking the pool editor</p>
<h1>Invoking the Pool Editor</h1>
<p class="goals">In this step, you will learn how to:<br />
• Initialize ServletUnit<br />
• Invoke a servlet<br />
• Specify a username and password for basic authentication</p>
<p>The first step will simply be to verify that we can register
and access the servlet, which we will name <code>PoolEditorServlet</code>. A GET method to this page should return the editor form
itself, while updates will be handled by a POST method to the same address. Since we are working with servlets, we can
bypass the web server and use the <code>servletunit</code> package to run our tests.</p>
<p>Here is the initial test code:</p>
<pre class="test-code">
<b>package</b> tutorial;
<b>import</b> com.meterware.httpunit.*;
<b>import</b> com.meterware.servletunit.*;
<b>import</b> java.util.*;
<b>import</b> junit.framework.*;
<b>import</b> tutorial.persistence.*;
<b>public class</b> PoolEditorTest <b>extends</b> TestCase {
<b>public static void</b> main( String args[] ) {
junit.textui.TestRunner.run( suite() );
}
<b>public static</b> TestSuite suite() {
<b>return new</b> TestSuite( PoolEditorTest.<b>class</b> );
}
<b>public</b> PoolEditorTest( String s ) {
<b>super</b>( s );
}
<b>public void</b> testGetForm() <b>throws</b> Exception {
ServletRunner sr = <b>new</b> ServletRunner( "web.xml" ); // (1) use the web.xml file to define mappings
ServletUnitClient client = sr.newClient(); // (2) create a client to invoke the application
try {
client.getResponse( "http://localhost/PoolEditor" ); // (3) invoke the servlet w/o authorization
fail( "PoolEditor is not protected" );
} catch (AuthorizationRequiredException e) { // (4) verify that access is denied
}
client.setAuthorization( "aUser", "pool-admin" ); // (5) specify authorization and
client.getResponse( "http://localhost/PoolEditor" ); // invoke the servlet again
}
}
</pre>
<p>This code uses <code>JUnit</code> and <code>ServletUnit</code> to verify that a servlet is present at the specified address.
The significant points in the code are:<ol>
<li>Creating the <code>ServletRunner</code> class which represents access to a Servlet application.
The application is defined by an XML file which maps URL information to servlet classes.</li>
<li>Creating a client which can access the application and maintain state across multiple invocations.</li>
<li>Invoking the servlet via its URL. Note that ServletUnit ignores any host and port information. All URL patterns
are treated as being relative to the root ("/").</li>
<li>Catching an exception which indicates that authentication is required.</li>
<li>Specifying the authorization information. ServletUnit does not maintain a database of users, no any username is
accepted, and the password is interpreted as a comma-separated list of role names associated with the user.</li></ol></p>
<p>To run this code, you will also need the <a href="web.xml">web.xml</a> file in your current directory. This file
maps the request URL to the Pool Editor servlet.</p>
<p>This code should fail with a <code>HttpNotFoundException</code>, because we have not yet created the servlet class. We
can now proceed to do so. Here is a simple implementation:</p>
<p></p>
<pre class="servlet-code">
<b>package</b> tutorial;
<b>import</b> java.io.*;
<b>import</b> java.util.*;
<b>import</b> javax.servlet.http.*;
<b>import</b> javax.servlet.ServletException;
<b>import</b> tutorial.persistence.*;
<b>public class</b> PoolEditorServlet <b>extends</b> HttpServlet {
<b>protected void</b> doGet( HttpServletRequest request, HttpServletResponse response )
<b>throws</b> ServletException, IOException {
response.setContentType( "text/html" );
PrintWriter pw = response.getWriter();
pw.println( "<html><head></head><body>" );
printBody( pw );
pw.println( "</body></html>" );
}
<b>private void</b> printBody( PrintWriter pw ) {
pw.println( "A simple page" );
}
}
</pre>
<p>With this code in place, the first test will now pass and we can move to <a href="task1editor-form.html">the next task</a>.</p>
</body>
</html>
|