KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > examples > clients > petstore > F_Petstore


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  * --------------------------------------------------------------------------
22  * $Id: F_Petstore.java,v 1.4 2004/03/19 11:57:20 benoitf Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas.examples.clients.petstore;
27
28 import junit.framework.TestSuite;
29
30 import org.objectweb.jonas.examples.util.JExampleTestCase;
31
32 import com.meterware.httpunit.TableCell;
33 import com.meterware.httpunit.WebForm;
34 import com.meterware.httpunit.WebLink;
35 import com.meterware.httpunit.WebResponse;
36 import com.meterware.httpunit.WebTable;
37
38 /**
39  * Define a class to test the petstore example
40  * @author Jerome Camilleri
41  */

42 public class F_Petstore extends JExampleTestCase {
43
44     /**
45      * URL of the petstore page
46      */

47     private static final String JavaDoc URL_BASEPETSTORE = "/petstore/populating.jsp?forcefully=true";
48     private static final String JavaDoc URL_MAINPAGE = "/petstore/main.screen";
49     private static final String JavaDoc URL_CART = "/petstore/cart.do";
50     private static final String JavaDoc URL_SIGNON = "/petstore/signon_welcome.screen";
51
52     /**
53      * Main method
54      * @param args the arguments
55      */

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

78     public static TestSuite suite() {
79         return new TestSuite(F_Petstore.class);
80     }
81
82     /**
83      * Setup need for these tests
84      * opc.ear, petstoreadmin.ear, petstore.ear, supplierPetstore is required
85      * @throws Exception if it fails
86      */

87     protected void setUp() throws Exception JavaDoc {
88         super.setUp();
89         useEar("opc");
90         useEar("petstoreadmin");
91         useEar("petstore");
92         useEar("supplier");
93     }
94
95     /**
96      * Constructor with a specified name
97      * @param s name
98      */

99     public F_Petstore(String JavaDoc s) {
100         super(s, URL_BASEPETSTORE);
101     }
102
103     /**
104      * Test that the first page of Petstore contains links to 5 pets store into database
105      * @throws Exception if an error occurs
106      */

107     public void testHomePage() throws Exception JavaDoc {
108         WebResponse wr = wc.getResponse(getAbsoluteUrl(URL_MAINPAGE));
109         WebTable[] webTables = wr.getTables();
110
111         WebTable wTable = webTables[0];
112         assertNotNull("There must be a table in the html page", wTable);
113
114         int rows = wTable.getRowCount();
115         int cols = wTable.getColumnCount();
116
117         TableCell tableCell = wTable.getTableCell(1, 0);
118         assertNotNull("There must be a cell in this row", tableCell);
119         WebLink[] links = tableCell.getLinks();
120         assertNotNull("There must have links in this cell", links);
121         if (links.length == 5) {
122             String JavaDoc lk1 = links[0].asText();
123             String JavaDoc lk2 = links[1].asText();
124             String JavaDoc lk3 = links[2].asText();
125             String JavaDoc lk4 = links[3].asText();
126             String JavaDoc lk5 = links[4].asText();
127             if (!lk1.equalsIgnoreCase("Birds")) {
128                 fail("The first link must be egal to Birds and the value is " + lk1 + ".");
129             } else if (!lk2.equalsIgnoreCase("Cats")) {
130                 fail("The first link must be egal to Cats and the value is " + lk2 + ".");
131             } else if (!lk3.equalsIgnoreCase("Dogs")) {
132                 fail("The first link must be egal to Dogs and the value is " + lk3 + ".");
133             } else if (!lk4.equalsIgnoreCase("Fish")) {
134                 fail("The first link must be egal to Fish and the value is " + lk4 + ".");
135             } else if (!lk5.equalsIgnoreCase("Reptiles")) {
136                 fail("The first link must be egal to Reptiles and the value is " + lk5 + ".");
137             }
138         } else {
139             fail("There must have 5 links into this cell but there are " + links.length + " links !");
140         }
141     }
142
143     /**
144      * Test that the first navigation for buy a reptile
145      * @throws Exception if an error occurs
146      */

147     public void testFirstNavigation() throws Exception JavaDoc {
148         WebResponse wr = wc.getResponse(getAbsoluteUrl(URL_MAINPAGE));
149         WebTable[] webTables = wr.getTables();
150
151         WebTable wTable = webTables[0];
152         assertNotNull("There must be a table in the html page", wTable);
153
154         int rows = wTable.getRowCount();
155         int cols = wTable.getColumnCount();
156
157         TableCell tableCell = wTable.getTableCell(1, 0);
158         assertNotNull("There must be a cell in this row", tableCell);
159         WebLink[] links = tableCell.getLinks();
160         assertNotNull("There must have links in this cell", links);
161         if (links.length == 5) {
162             wr = links[4].click();
163             webTables = wr.getTables();
164
165             wTable = webTables[0];
166             assertNotNull("There must be a table in the html page", wTable);
167
168             rows = wTable.getRowCount();
169             cols = wTable.getColumnCount();
170
171             tableCell = wTable.getTableCell(1, 1);
172             assertNotNull("There must be a cell in this row", tableCell);
173             links = tableCell.getLinks();
174             assertNotNull("There must have links in this cell", links);
175             String JavaDoc lk1 = links[0].asText();
176             String JavaDoc lk2 = links[1].asText();
177             if (!lk1.equalsIgnoreCase("Iguana")) {
178                 fail("The first link must be egal to Iguana and the value is " + lk1 + ".");
179             } else if (!lk2.equalsIgnoreCase("Rattlesnake")) {
180                 fail("The first link must be egal to Rattlesnake and the value is " + lk2 + ".");
181             }
182
183         } else {
184             fail("There must have 5 links into this cell but there are " + links.length + " links !");
185         }
186     }
187
188     /**
189      * Test add cart one Rattleless Rattlesnake and Venomless
190      * @throws Exception if an error occurs
191      */

192     public void testAddCartRattlelessVenomless() throws Exception JavaDoc {
193         WebResponse wr = wc.getResponse(getAbsoluteUrl(URL_MAINPAGE));
194         WebTable[] webTables = wr.getTables();
195
196         WebTable wTable = webTables[0];
197         assertNotNull("There must be a table in the html page", wTable);
198
199         int rows = wTable.getRowCount();
200         int cols = wTable.getColumnCount();
201
202         TableCell tableCell = wTable.getTableCell(1, 0);
203         assertNotNull("There must be a cell in this row", tableCell);
204         WebLink[] links = tableCell.getLinks();
205         assertNotNull("There must have links in this cell", links);
206         if (links.length == 5) {
207             wr = links[4].click(); // click into reptile
208
webTables = wr.getTables();
209
210             wTable = webTables[0];
211             assertNotNull("There must be a table in the html page", wTable);
212
213             rows = wTable.getRowCount();
214             cols = wTable.getColumnCount();
215
216             tableCell = wTable.getTableCell(1, 1);
217             assertNotNull("There must be a cell in this row", tableCell);
218             links = tableCell.getLinks();
219             assertNotNull("There must have links in this cell", links);
220                
221             WebLink rattlesnake = links[1];
222             wr = rattlesnake.click(); // click into Rattlesnake
223

224             webTables = wr.getTables();
225
226             wTable = webTables[0];
227             assertNotNull("There must be a table in the html page", wTable);
228
229             rows = wTable.getRowCount();
230             cols = wTable.getColumnCount();
231
232             tableCell = wTable.getTableCell(1, 1);
233             assertNotNull("There must be a cell in this row", tableCell);
234             links = tableCell.getLinks();
235             assertNotNull("There must have links in this cell", links);
236             if (links.length != 4) {
237                 fail("There must have 4 links into this cell but there are " + links.length + " links !");
238             }
239             wr = links[3].click(); // click into Rattleless Rattlesnake Add cart
240
webTables = wr.getTables();
241             WebForm[] webForms = wr.getForms();
242             WebForm webForm = webForms[4];
243             assertNotNull("There must be a form in the html page", webForm);
244             WebResponse wFormRes = webForm.submit();
245
246             wr = rattlesnake.click(); // click into Rattlesnake
247

248             webTables = wr.getTables();
249
250             wTable = webTables[0];
251             assertNotNull("There must be a table in the html page", wTable);
252
253             rows = wTable.getRowCount();
254             cols = wTable.getColumnCount();
255
256             tableCell = wTable.getTableCell(1, 1);
257             assertNotNull("There must be a cell in this row", tableCell);
258             links = tableCell.getLinks();
259             assertNotNull("There must have links in this cell", links);
260             if (links.length != 4) {
261                 fail("There must have 4 links into this cell but there are " + links.length + " links !");
262             }
263             wr = links[1].click(); // click into Venomless Rattlesnake Add cart
264
webTables = wr.getTables();
265             webForms = wr.getForms();
266             webForm = webForms[4];
267             assertNotNull("There must be a form in the html page", webForm);
268             wFormRes = webForm.submit();
269
270             // Verify card contain 2 snakes
271
wr = wc.getResponse(getAbsoluteUrl(URL_CART));
272             webTables = wr.getTables();
273
274             wTable = webTables[0];
275             assertNotNull("There must be a table in the html page", wTable);
276
277             rows = wTable.getRowCount();
278             cols = wTable.getColumnCount();
279
280             tableCell = wTable.getTableCell(1, 1);
281             assertNotNull("There must be a cell in this row", tableCell);
282             webTables = tableCell.getTables();
283
284             wTable = webTables[0];
285             assertNotNull("There must be a table in the html page", wTable);
286
287             rows = wTable.getRowCount();
288             cols = wTable.getColumnCount();
289
290             tableCell = wTable.getTableCell(0, 0);
291
292             webTables = tableCell.getTables();
293
294             wTable = webTables[0];
295             assertNotNull("There must be a table in the html page", wTable);
296
297             rows = wTable.getRowCount();
298             cols = wTable.getColumnCount();
299             tableCell = wTable.getTableCell(2, 3);
300             String JavaDoc total = tableCell.asText().trim();
301             if (!total.equalsIgnoreCase("$37.00")) {
302                 fail("Total of the card must be $37.00 and it is " + total);
303             }
304         } else {
305             fail("There must have 5 links into this cell but there are " + links.length + " links !");
306         }
307     }
308
309 }
310
Popular Tags