KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > examples > clients > earsample > F_EarSample


1 /*
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or 1any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * Initial developer: Florent BENOIT
22  * --------------------------------------------------------------------------
23  * $Id: F_EarSample.java,v 1.2 2004/03/19 11:57:20 benoitf Exp $
24  * --------------------------------------------------------------------------
25  */

26
27 package org.objectweb.jonas.examples.clients.earsample;
28
29 import junit.framework.TestSuite;
30
31 import org.objectweb.jonas.examples.util.JExampleTestCase;
32
33 import com.meterware.httpunit.AuthorizationRequiredException;
34 import com.meterware.httpunit.WebResponse;
35
36 /**
37  * Define a class to test the Alarm example
38  * Test authentication, and check the sample is ok
39  * @author Florent Benoit
40  */

41 public class F_EarSample extends JExampleTestCase {
42
43     /**
44      * URL of the earsample page
45      */

46     private static final String JavaDoc URL_EARSAMPLE = "/earsample/secured/Op";
47
48     /**
49      * Main method
50      * @param args the arguments
51      */

52     public static void main(String JavaDoc[] args) {
53
54         String JavaDoc testtorun = null;
55         // Get args
56
for (int argn = 0; argn < args.length; argn++) {
57             String JavaDoc sArg = args[argn];
58             if (sArg.equals("-n")) {
59                 testtorun = args[++argn];
60             }
61         }
62
63         if (testtorun == null) {
64             junit.textui.TestRunner.run(suite());
65         } else {
66             junit.textui.TestRunner.run(new F_EarSample(testtorun));
67         }
68     }
69
70     /**
71      * Get a new TestSuite for this class
72      * @return a new TestSuite for this class
73      */

74     public static TestSuite suite() {
75         return new TestSuite(F_EarSample.class);
76     }
77
78     /**
79      * Setup need for these tests
80      * earsample is required
81      * @throws Exception if it fails
82      */

83     protected void setUp() throws Exception JavaDoc {
84         super.setUp();
85         useEar("earsample");
86     }
87
88     /**
89      * Constructor with a specified name
90      * @param s name
91      */

92     public F_EarSample(String JavaDoc s) {
93         super(s, URL_EARSAMPLE);
94     }
95
96     /**
97      * Try to log in without authentication
98      * @throws Exception if an error occurs
99      */

100     public void testTryWithoutAuth() throws Exception JavaDoc {
101         try {
102             WebResponse wr = wc.getResponse(url);
103             fail("EarSample is not protected");
104         } catch (AuthorizationRequiredException e) {
105             ;
106         }
107     }
108
109     /**
110      * Try to authenticate with a bad login/password
111      * @throws Exception if an error occurs
112      */

113     public void testTryWithBadAuth() throws Exception JavaDoc {
114         try {
115             wc.setAuthorization("bad", "bad");
116             WebResponse wr = wc.getResponse(url);
117             fail("EarSample is not protected");
118         } catch (AuthorizationRequiredException e) {
119             ;
120         }
121     }
122
123      /**
124      * Try to authenticate with a right login/password (jetty)
125      * @throws Exception if an error occurs
126      */

127    public void testTryWithJettyAuth() throws Exception JavaDoc {
128         try {
129             wc.setAuthorization("jetty", "jetty");
130             WebResponse wr = wc.getResponse(url);
131         } catch (AuthorizationRequiredException e) {
132             fail("EarSample don't accept l/p jetty/jetty");
133         }
134     }
135
136     /**
137      * Try to authenticate with a right login/password (tomcat)
138      * @throws Exception if an error occurs
139      */

140     public void testTryWithTomcatAuth() throws Exception JavaDoc {
141         try {
142             wc.setAuthorization("tomcat", "tomcat");
143             WebResponse wr = wc.getResponse(url);
144         } catch (AuthorizationRequiredException e) {
145             fail("EarSample don't accept l/p tomcat/tomcat");
146         }
147     }
148
149     /**
150      * Check if the sample is OK. Analyze response
151      * @throws Exception if an error occurs
152      */

153     public void testIsSampleOk() throws Exception JavaDoc {
154         try {
155             wc.setAuthorization("tomcat", "tomcat");
156             WebResponse wr = wc.getResponse(url);
157             String JavaDoc txt = wr.getText();
158
159             if (txt.indexOf("<strong>Sample is OK.</strong>") == -1) {
160                 fail("The example was not ok : " + txt);
161             }
162
163         } catch (AuthorizationRequiredException e) {
164             fail("Authentication failed");
165         }
166     }
167
168
169 }
170
Popular Tags