1 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 ; 38 39 44 public class F_OlStore extends JExampleTestCase { 45 46 private Date date; 47 50 private static final String URL_OLSTORE = "/olstore"; 51 52 55 private static final String URL_OLSTORE_POPULATE = "/olstore/populate/Populate.jsp"; 56 private static final String OLSTORE_ITEM_DETAILS = "Details"; 57 private static final String OLSTORE_ITEM_ADDTOCART = "Purchase this Item"; 58 private static final String URL_OLSTORE_ITEM_PURCHASE = "/olstore/checkout/Checkout.do"; 59 private static final String URL_OLSTORE_LOGOUT = "/olstore/login/Logout.do"; 60 private static final String URL_OLSTORE_NEWUSER = "/olstore/general/CreateUser.do"; 61 62 private static final String URL_OLSTORE_ITEMS = "/olstore/views/index.do"; 63 67 public static void main(String [] args) { 68 69 String testtorun = null; 70 for (int argn = 0; argn < args.length; argn++) { 72 String 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 88 public static TestSuite suite() { 89 return new TestSuite(F_OlStore.class); 90 } 91 92 97 protected void setUp() throws Exception { 98 super.setUp(); 99 useEar("olstore"); 100 populateDB(); 101 } 102 103 104 108 public F_OlStore(String s) { 109 super(s, URL_OLSTORE_POPULATE); 110 date = new Date (); 111 } 112 113 116 private void populateDB() throws Exception { 117 String 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 125 } 126 127 128 129 136 137 public void testValidUserCreate() throws Exception { 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()); 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 getDigitsOfTime(){ 162 String time = Long.toString(date.getTime()); 163 return time.substring(time.length()-9); 164 165 } 166 public void testInValidUserCreate() throws Exception { 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 206 private void tryBadAuth() throws Exception { 207 WebResponse wRes = doAuth("bad", "bad"); 208 String 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 219 private void tryWithGoodAuth() throws Exception { 220 WebResponse wRes = doValidAuth(); 221 String 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 { 229 tryBadAuth(); 230 tryWithGoodAuth(); 231 logout(); 232 } 233 234 private void logout() throws Exception { 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 { 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 { 253 WebResponse wr = wc.getResponse(getAbsoluteUrl(URL_OLSTORE_ITEMS)); 254 String 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 { 280 String 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 300 private WebResponse doValidAuth() throws Exception { 301 return doAuth("jonas", "jonas"); 302 } 303 304 311 private WebResponse doAuth(String login, String password) throws Exception { 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 |