001: /**
002: * Copyright (c) 2004 Red Hat, Inc. All rights reserved.
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
017: * USA
018: *
019: * Component of: Red Hat Application Server
020: *
021: *
022: * Initial Developer: Vivek Lakshmanan
023: *
024: */package org.objectweb.jonas.examples.clients.olstore;
025:
026: import junit.framework.TestSuite;
027:
028: import org.objectweb.jonas.examples.util.JExampleTestCase;
029:
030: import com.meterware.httpunit.HttpUnitOptions;
031: import com.meterware.httpunit.TableCell;
032: import com.meterware.httpunit.WebForm;
033: import com.meterware.httpunit.WebResponse;
034: import com.meterware.httpunit.WebTable;
035: import com.meterware.httpunit.WebLink;
036: import java.util.Date;
037:
038: /**
039: * Define a class to test the olstore example.
040: * Test authentication and basic operations for the online store.
041: * @author Vivek Lakshmanan
042: */
043: public class C_olstore extends JExampleTestCase {
044:
045: private Date date;
046: /**
047: * URL of the login page
048: */
049: private static final String URL_OLSTORE = "/olstore";
050:
051: /**
052: * URL of features to test.
053: */
054: private static final String URL_OLSTORE_POPULATE = "/olstore/populate/Populate.jsp";
055: private static final String OLSTORE_ITEM_DETAILS = "Details";
056: private static final String OLSTORE_ITEM_ADDTOCART = "Purchase this Item";
057: private static final String URL_OLSTORE_ITEM_PURCHASE = "/olstore/checkout/Checkout.do";
058: private static final String URL_OLSTORE_LOGOUT = "/olstore/login/Logout.do";
059: private static final String URL_OLSTORE_NEWUSER = "/olstore/general/CreateUser.do";
060:
061: private static final String URL_OLSTORE_ITEMS = "/olstore/views/index.do";
062:
063: /**
064: * Main method
065: * @param args the arguments
066: */
067: public static void main(String[] args) {
068:
069: String testtorun = null;
070: // Get args
071: for (int argn = 0; argn < args.length; argn++) {
072: String sArg = args[argn];
073: if (sArg.equals("-n")) {
074: testtorun = args[++argn];
075: }
076: }
077: if (testtorun == null) {
078: junit.textui.TestRunner.run(suite());
079: } else {
080: junit.textui.TestRunner.run(new C_olstore(testtorun));
081: }
082: }
083:
084: /**
085: * Get a new TestSuite for this class
086: * @return a new TestSuite for this class
087: */
088: public static TestSuite suite() {
089: return new TestSuite(C_olstore.class);
090: }
091:
092: /**
093: * Setup need for these tests
094: * jonasAdmin is required
095: * @throws Exception if it fails
096: */
097: protected void setUp() throws Exception {
098: super .setUp();
099: useEar("olstore");
100: populateDB();
101: }
102:
103: /**
104: * Constructor with a specified name
105: * @param s name
106: */
107: public C_olstore(String s) {
108: super (s, URL_OLSTORE_POPULATE);
109: date = new Date();
110: }
111:
112: /**
113: * @throws Exception
114: */
115: private void populateDB() throws Exception {
116: String url = URL_OLSTORE;
117: WebResponse wr = wc.getResponse(getAbsoluteUrl(url));
118: if (wr.getText().indexOf("DATABASE POPULATION") != -1) {
119: wc.getResponse(getAbsoluteUrl(URL_OLSTORE_POPULATE));
120: }
121: /*
122: *TODO Check for success and failure here
123: */
124: }
125:
126: /*
127: The test creates a unique username by appending tonas with the last 9 digits of
128: the number of milliseconds since epoch till the instantiation of date.
129: This should be a fairly unique number but it can potentially be duplicated.
130: If an error occurs and you suspect a duplicate entry, try dropping and recreating
131: the DB and then re-running the test.
132: */
133:
134: public void testValidUserCreate() throws Exception {
135: WebResponse wr = wc
136: .getResponse(getAbsoluteUrl(URL_OLSTORE_NEWUSER));
137: WebForm[] forms = wr.getForms();
138: WebForm form = forms[0];
139: form.setParameter("username", "tonas" + getDigitsOfTime()); // (2) check team names
140: form.setParameter("passwd1", "tonas");
141: form.setParameter("passwd2", "tonas");
142: form.setParameter("fname", "tonas");
143: form.setParameter("lname", "tonas");
144: form.setParameter("phoneNum", "123-456-7890");
145: form.setParameter("emailAdd", "tonas@test.com");
146: form.setParameter("street1", "1 tonas st.");
147: form.setParameter("city", "tonas");
148: form.setParameter("province", "TO");
149: form.setParameter("postalCode", "12345");
150: form.setParameter("country", "Tonas");
151: wr = form.submit();
152: if (wr.getText().indexOf("Your Profile has been created") == -1) {
153: System.out.println(wr.getText());
154: fail("Valid User registration did not pass.");
155: }
156:
157: }
158:
159: private String getDigitsOfTime() {
160: String time = Long.toString(date.getTime());
161: return time.substring(time.length() - 9);
162:
163: }
164:
165: public void testInValidUserCreate() throws Exception {
166: WebResponse wr = wc
167: .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
185: .getText()
186: .indexOf(
187: "Your Profile has been created please click here to login.") != -1)
188: fail("Invalid User registration passed.");
189: if (wr.getText()
190: .indexOf("Confirm Password must match Password") == -1)
191: fail("Invalid Username should not have passed registration.");
192: if (wr.getText().indexOf(
193: "Username can not be less than 5 characters.") == -1)
194: fail("Invalid Username should not have passed registration.");
195: if (wr.getText().indexOf("Last Name is required.") == -1)
196: fail("Invalid last name should not have passed registration.");
197: if (wr.getText().indexOf("First Name is required.") == -1)
198: fail("Invalid first name should not have passed registration.");
199: if (wr.getText().indexOf(
200: "Email Address is an invalid e-mail address.") == -1)
201: fail("Invalid e-mail address should not have passed registration.");
202: if (wr.getText().indexOf("Phone Number Format Invalid.") == -1)
203: fail("Invalid phone should not have passed registration.");
204:
205: }
206:
207: /**
208: * Try to authenticate with a bad login/password
209: * @throws Exception if an error occurs
210: */
211: private void tryBadAuth() throws Exception {
212: WebResponse wRes = doAuth("bad", "bad");
213: String txt = wRes.getText();
214:
215: if (txt.indexOf("Invalid username and/or password") == -1) {
216: fail("The login/password bad/bad can't be a valid l/p.");
217: }
218: }
219:
220: /**
221: * Try to authenticate with a right login/password
222: * @throws Exception if an error occurs
223: */
224: private void tryWithGoodAuth() throws Exception {
225: WebResponse wRes = doValidAuth();
226: String txt = wRes.getText();
227:
228: if (txt.indexOf("Invalid username and/or password") != -1) {
229: fail("The login/password jonas/jonas must be valid.");
230: }
231: }
232:
233: public void testAuthorization() throws Exception {
234: tryBadAuth();
235: tryWithGoodAuth();
236: logout();
237: }
238:
239: private void logout() throws Exception {
240: WebResponse wr = wc
241: .getResponse(getAbsoluteUrl(URL_OLSTORE_LOGOUT));
242:
243: if (wr.getText().indexOf("Welcome to the RHAPS online store") == -1) {
244: fail("Could not logout.");
245: }
246:
247: }
248:
249: public void testShoppingCartActivities() throws Exception {
250: doValidAuth();
251: if (!(addItemToShoppingCart() && purchaseItemInShoppingCart())) {
252: fail("Activities related to session bean shopping cart failed.");
253: }
254: logout();
255: }
256:
257: private boolean addItemToShoppingCart() throws Exception {
258: WebResponse wr = wc
259: .getResponse(getAbsoluteUrl(URL_OLSTORE_ITEMS));
260: String str = OLSTORE_ITEM_DETAILS;
261: WebLink wl = wr.getLinkWith(str);
262: if (wl == null) {
263: fail("Link for product details was not found.");
264: return false;
265: } else {
266: WebResponse webR = wl.click();
267: if (webR == null
268: || webR.getText().indexOf(OLSTORE_ITEM_ADDTOCART) == -1) {
269: fail("Link to shopping cart not found.");
270: return false;
271: } else {
272: wl = webR.getLinkWith(OLSTORE_ITEM_ADDTOCART);
273: if (wl == null
274: || wl.click().getText().indexOf(
275: "was successfully added") == -1) {
276: fail("Shopping Cart addition failed.");
277: return false;
278: } else {
279: return true;
280:
281: }
282:
283: }
284:
285: }
286: }
287:
288: private boolean purchaseItemInShoppingCart() throws Exception {
289: String url = URL_OLSTORE_ITEM_PURCHASE;
290: WebResponse wr = wc.getResponse(getAbsoluteUrl(url));
291:
292: WebForm[] webForms = wr.getForms();
293: WebForm webForm = webForms[0];
294: WebResponse submitPurchase = webForm.submit(webForm
295: .getSubmitButton("", "order"));
296: if (submitPurchase.getText().indexOf(
297: "The order was successfully placed!") == -1) {
298: fail("Order couldn't be placed.");
299: return false;
300: }
301: return true;
302:
303: }
304:
305: /**
306: * Authenticate with a valid login/password
307: * @return the response
308: * @throws Exception if an error occurs
309: */
310: private WebResponse doValidAuth() throws Exception {
311: return doAuth("jonas", "jonas");
312: }
313:
314: /**
315: * Authenticate with the specified login/password
316: * @param login login to use
317: * @param password password to use
318: * @return the response after authentication
319: * @throws Exception if an error occurs
320: */
321: private WebResponse doAuth(String login, String password)
322: throws Exception {
323: WebResponse wr = wc.getResponse(getAbsoluteUrl(URL_OLSTORE));
324: WebForm[] webForms = wr.getForms();
325: WebForm webForm = webForms[0];
326:
327: webForm.setParameter("username", login);
328: webForm.setParameter("password", password);
329: return webForm.submit();
330:
331: }
332:
333: }
|