KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > examples > clients > olstore > F_OlStore


1 /**
2  * Copyright (c) 2004 Red Hat, Inc. All rights reserved.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17  * USA
18  *
19  * Component of: Red Hat Application Server
20  *
21  *
22  * Initial Developer: Vivek Lakshmanan
23  *
24  */

25 package org.objectweb.jonas.examples.clients.olstore;
26
27 import junit.framework.TestSuite;
28
29 import org.objectweb.jonas.examples.util.JExampleTestCase;
30
31 import com.meterware.httpunit.HttpUnitOptions;
32 import com.meterware.httpunit.TableCell;
33 import com.meterware.httpunit.WebForm;
34 import com.meterware.httpunit.WebResponse;
35 import com.meterware.httpunit.WebTable;
36 import com.meterware.httpunit.WebLink;
37 import java.util.Date JavaDoc;
38
39 /**
40  * Define a class to test the olstore example.
41  * Test authentication and basic operations for the online store.
42  * @author Vivek Lakshmanan
43  */

44 public class F_OlStore extends JExampleTestCase {
45
46     private Date JavaDoc date;
47     /**
48      * URL of the login page
49      */

50     private static final String JavaDoc URL_OLSTORE = "/olstore";
51
52     /**
53      * URL of features to test.
54      */

55     private static final String JavaDoc URL_OLSTORE_POPULATE = "/olstore/populate/Populate.jsp";
56     private static final String JavaDoc OLSTORE_ITEM_DETAILS = "Details";
57     private static final String JavaDoc OLSTORE_ITEM_ADDTOCART = "Purchase this Item";
58     private static final String JavaDoc URL_OLSTORE_ITEM_PURCHASE = "/olstore/checkout/Checkout.do";
59     private static final String JavaDoc URL_OLSTORE_LOGOUT = "/olstore/login/Logout.do";
60     private static final String JavaDoc URL_OLSTORE_NEWUSER = "/olstore/general/CreateUser.do";
61     
62     private static final String JavaDoc URL_OLSTORE_ITEMS = "/olstore/views/index.do";
63     /**
64      * Main method
65      * @param args the arguments
66      */

67     public static void main(String JavaDoc[] args) {
68
69         String JavaDoc testtorun = null;
70         // Get args
71
for (int argn = 0; argn < args.length; argn++) {
72             String JavaDoc sArg = args[argn];
73             if (sArg.equals("-n")) {
74                 testtorun = args[++argn];
75             }
76         }
77         if (testtorun == null) {
78             junit.textui.TestRunner.run(suite());
79         } else {
80             junit.textui.TestRunner.run(new F_OlStore(testtorun));
81         }
82     }
83
84     /**
85      * Get a new TestSuite for this class
86      * @return a new TestSuite for this class
87      */

88     public static TestSuite suite() {
89         return new TestSuite(F_OlStore.class);
90     }
91
92     /**
93      * Setup need for these tests
94      * jonasAdmin is required
95      * @throws Exception if it fails
96      */

97     protected void setUp() throws Exception JavaDoc {
98         super.setUp();
99         useEar("olstore");
100     populateDB();
101     }
102
103       
104     /**
105      * Constructor with a specified name
106      * @param s name
107      */

108     public F_OlStore(String JavaDoc s) {
109         super(s, URL_OLSTORE_POPULATE);
110         date = new Date JavaDoc();
111     }
112
113     /**
114      * @throws Exception
115      */

116     private void populateDB() throws Exception JavaDoc{
117         String JavaDoc url = URL_OLSTORE;
118         WebResponse wr = wc.getResponse(getAbsoluteUrl(url));
119     if(wr.getText().indexOf("DATABASE POPULATION")!=-1){
120         wc.getResponse(getAbsoluteUrl(URL_OLSTORE_POPULATE));
121     }
122         /*
123          *TODO Check for success and failure here
124          */

125     }
126
127
128
129    /*
130     The test creates a unique username by appending tonas with the last 9 digits of
131     the number of milliseconds since epoch till the instantiation of date.
132         This should be a fairly unique number but it can potentially be duplicated.
133         If an error occurs and you suspect a duplicate entry, try dropping and recreating
134     the DB and then re-running the test.
135     */

136     
137     public void testValidUserCreate() throws Exception JavaDoc{
138         WebResponse wr = wc.getResponse(getAbsoluteUrl(URL_OLSTORE_NEWUSER));
139         WebForm[] forms = wr.getForms();
140          WebForm form = forms[0];
141             form.setParameter( "username", "tonas" + getDigitsOfTime()); // (2) check team names
142
form.setParameter( "passwd1" , "tonas" );
143             form.setParameter( "passwd2" , "tonas" );
144             form.setParameter( "fname" , "tonas" );
145             form.setParameter( "lname" , "tonas" );
146             form.setParameter( "phoneNum" , "123-456-7890" );
147             form.setParameter( "emailAdd" , "tonas@test.com" );
148             form.setParameter( "street1" , "1 tonas st." );
149             form.setParameter( "city" , "tonas" );
150             form.setParameter( "province" , "TO" );
151             form.setParameter( "postalCode" , "12345" );
152             form.setParameter( "country" , "Tonas" );
153             wr = form.submit();
154             if(wr.getText().indexOf("Your Profile has been created")==-1){
155         System.out.println(wr.getText());
156                 fail("Valid User registration did not pass.");
157             }
158             
159     }
160     
161    private String JavaDoc getDigitsOfTime(){
162     String JavaDoc time = Long.toString(date.getTime());
163     return time.substring(time.length()-9);
164
165    }
166     public void testInValidUserCreate() throws Exception JavaDoc{
167         WebResponse wr = wc.getResponse(getAbsoluteUrl(URL_OLSTORE_NEWUSER));
168         WebForm[] forms = wr.getForms();
169          WebForm form = forms[0];
170             form.setParameter( "username", "ton" );
171             form.setParameter( "passwd1" , "tonas" );
172             form.setParameter( "passwd2" , "ton" );
173             form.setParameter( "fname" , "" );
174             form.setParameter( "lname" , "" );
175             form.setParameter( "phoneNum" , "123-AHJ-7890" );
176             form.setParameter( "emailAdd" , "tonastest.com" );
177             form.setParameter( "street1" , "1 tonas st." );
178             form.setParameter( "city" , "tonas" );
179             form.setParameter( "province" , "TO" );
180             form.setParameter( "postalCode" , "12345" );
181             form.setParameter( "country" , "Tonas" );
182             wr = form.submit();
183             
184             if(wr.getText().indexOf("Your Profile has been created please click here to login.")!=-1)
185                 fail("Invalid User registration passed.");
186             if(wr.getText().indexOf("Confirm Password must match Password")==-1)
187                 fail("Invalid Username should not have passed registration.");
188             if(wr.getText().indexOf("Username can not be less than 5 characters.")==-1)
189                 fail("Invalid Username should not have passed registration.");
190             if(wr.getText().indexOf("Last Name is required.")==-1)
191                 fail("Invalid last name should not have passed registration.");
192             if(wr.getText().indexOf("First Name is required.")==-1)
193                 fail("Invalid first name should not have passed registration.");
194             if(wr.getText().indexOf("Email Address is an invalid e-mail address.")==-1)
195                 fail("Invalid e-mail address should not have passed registration.");
196             if(wr.getText().indexOf("Phone Number Format Invalid.")==-1)
197                 fail("Invalid phone should not have passed registration.");
198                 
199     }
200     
201       
202     /**
203      * Try to authenticate with a bad login/password
204      * @throws Exception if an error occurs
205      */

206     private void tryBadAuth() throws Exception JavaDoc {
207         WebResponse wRes = doAuth("bad", "bad");
208         String JavaDoc txt = wRes.getText();
209         
210         if (txt.indexOf("Invalid username and/or password") == -1) {
211             fail("The login/password bad/bad can't be a valid l/p.");
212         }
213     }
214
215     /**
216      * Try to authenticate with a right login/password
217      * @throws Exception if an error occurs
218      */

219     private void tryWithGoodAuth() throws Exception JavaDoc {
220         WebResponse wRes = doValidAuth();
221         String JavaDoc txt = wRes.getText();
222         
223         if (txt.indexOf("Invalid username and/or password") != -1) {
224             fail("The login/password jonas/jonas must be valid.");
225         }
226     }
227     
228     public void testAuthorization() throws Exception JavaDoc{
229         tryBadAuth();
230         tryWithGoodAuth();
231         logout();
232     }
233     
234     private void logout() throws Exception JavaDoc{
235         WebResponse wr = wc.getResponse(getAbsoluteUrl(URL_OLSTORE_LOGOUT));
236         
237         if(wr.getText().indexOf("Welcome to the RHAPS online store")==-1){
238             fail("Could not logout.");
239         }
240         
241     }
242     
243     
244     public void testShoppingCartActivities() throws Exception JavaDoc{
245         doValidAuth();
246         if(!(addItemToShoppingCart() && purchaseItemInShoppingCart())){
247             fail("Activities related to session bean shopping cart failed.");
248         }
249         logout();
250     }
251     
252     private boolean addItemToShoppingCart() throws Exception JavaDoc{
253         WebResponse wr = wc.getResponse(getAbsoluteUrl(URL_OLSTORE_ITEMS));
254         String JavaDoc str = OLSTORE_ITEM_DETAILS;
255     WebLink wl = wr.getLinkWith(str);
256         if(wl==null){
257         fail("Link for product details was not found.");
258             return false;
259         }else{
260         WebResponse webR = wl.click();
261         if(webR==null || webR.getText().indexOf(OLSTORE_ITEM_ADDTOCART)==-1){
262                 fail("Link to shopping cart not found.");
263                 return false;
264         }else{
265             wl = webR.getLinkWith(OLSTORE_ITEM_ADDTOCART);
266             if(wl == null || wl.click().getText().indexOf("was successfully added")==-1){
267                 fail("Shopping Cart addition failed.");
268                     return false;
269             }else{
270                 return true;
271
272             }
273
274         }
275
276     }
277     }
278     
279     private boolean purchaseItemInShoppingCart() throws Exception JavaDoc{
280         String JavaDoc url= URL_OLSTORE_ITEM_PURCHASE;
281         WebResponse wr = wc.getResponse(getAbsoluteUrl(url));
282         
283         WebForm[] webForms = wr.getForms();
284         WebForm webForm = webForms[0];
285     WebResponse submitPurchase = webForm.submit(webForm.getSubmitButton("", "order"));
286         if(submitPurchase.getText().indexOf("The order was successfully placed!")==-1){
287             fail("Order couldn't be placed.");
288             return false;
289         }
290         return true;
291         
292     }
293     
294
295     /**
296      * Authenticate with a valid login/password
297      * @return the response
298      * @throws Exception if an error occurs
299      */

300     private WebResponse doValidAuth() throws Exception JavaDoc {
301         return doAuth("jonas", "jonas");
302     }
303
304     /**
305      * Authenticate with the specified login/password
306      * @param login login to use
307      * @param password password to use
308      * @return the response after authentication
309      * @throws Exception if an error occurs
310      */

311     private WebResponse doAuth(String JavaDoc login, String JavaDoc password) throws Exception JavaDoc {
312         WebResponse wr = wc.getResponse(getAbsoluteUrl(URL_OLSTORE));
313         WebForm[] webForms = wr.getForms();
314         WebForm webForm = webForms[0];
315
316         webForm.setParameter("username", login);
317         webForm.setParameter("password", password);
318         return webForm.submit();
319         
320     }
321
322
323 }
324
Popular Tags