1 23 24 package org.enhydra.xml.xmlc.html; 25 26 import java.lang.reflect.Method ; 27 import java.util.Enumeration ; 28 import java.util.Hashtable ; 29 import java.util.StringTokenizer ; 30 import java.util.Vector ; 31 32 import junit.framework.Test; 33 34 import org.enhydra.xml.xmlc.XMLCUtil; 35 import org.enhydra.xml.xmlc.driver.TestDOMSource; 36 import org.w3c.dom.Node ; 37 import org.w3c.dom.Text ; 38 import org.w3c.dom.html.HTMLAnchorElement; 39 import org.w3c.dom.html.HTMLImageElement; 40 import org.w3c.dom.html.HTMLParagraphElement; 41 42 46 public class ShelfTests extends HtmlTestCaseBase { 47 50 private static boolean DEBUG = false; 51 52 56 private TestDOMSource fShelfSource; 57 58 61 private static Inventory fInventoryRoot; 62 63 66 private static int fRequestCnt; 67 68 71 private static final String SHELF_URL = "Shelf.po"; 72 73 76 private static final String ITEM_URL = "Item.po"; 77 78 81 private static final String OPEN_CAT_IMAGE = "../media/true.gif"; 82 private static final String CLOSED_CAT_IMAGE = "../media/false.gif"; 83 84 87 private class RequestTest { 88 92 private Long fButtonCatId; 93 94 98 private Hashtable fOpenCatIds = new Hashtable (); 99 100 104 private String fOpenCatCgiArg; 105 106 109 private ShelfHTML fShelfHtml; 110 111 114 private HTMLParagraphElement fSampleCatBlock; 115 private HTMLAnchorElement fSampleCatAnchor; 116 private HTMLImageElement fSampleCatImg; 117 private Text fSampleCatText; 118 119 122 private HTMLParagraphElement fSampleItemBlock; 123 private HTMLAnchorElement fSampleItemAnchor; 124 private Text fSampleItemText; 125 126 127 public RequestTest() { 128 } 129 130 135 private void prepareHtml() { 136 fShelfHtml = (ShelfHTML)fShelfSource.create(); 137 138 fSampleCatBlock = fShelfHtml.getElementSampleCatBlock(); 140 fSampleCatAnchor = fShelfHtml.getElementSampleCatAnchor(); 141 fSampleCatImg = fShelfHtml.getElementSampleCatImg(); 142 fSampleCatText = XMLCUtil.getFirstText(fShelfHtml.getElementSampleCatText()); 143 144 fSampleItemBlock = fShelfHtml.getElementSampleItemBlock(); 145 fSampleItemAnchor = fShelfHtml.getElementSampleItemAnchor(); 146 fSampleItemText = XMLCUtil.getFirstText(fShelfHtml.getElementSampleItemText()); 147 148 Node listNode = fShelfHtml.getElementList(); 150 Node child = listNode.getFirstChild(); 151 while (child != null) { 152 Node nextChild = child.getNextSibling(); 153 listNode.removeChild(child); 154 child = nextChild; 155 } 156 157 } 158 159 162 private void makeCategoryHtml(Inventory category, Node parent, 163 boolean isOpen, int level) { 164 if (DEBUG) { 165 msgPrintln("makeCategotyHtml enter: " + level); 166 } 167 long id = category.getObjectId(); 168 169 String url = SHELF_URL + "?button=" + id; 170 if (fOpenCatCgiArg != null) { 171 url += "&" + fOpenCatCgiArg; 172 } 173 fSampleCatAnchor.setHref(url); 174 if (isOpen) { 175 fSampleCatImg.setSrc(OPEN_CAT_IMAGE); 176 } else { 177 fSampleCatImg.setSrc(CLOSED_CAT_IMAGE); 178 } 179 fSampleCatText.setData(category.getDesc()); 180 fSampleCatBlock.setClassName("level" + level); 181 parent.appendChild(fSampleCatBlock.cloneNode(true)); 182 if (DEBUG) { 183 msgPrintln("makeCategotyHtml exit: " + level); 184 } 185 } 186 187 190 private void makeItemHtml(Inventory item, Node parent, int level) { 191 if (DEBUG) { 192 msgPrintln("makeItemHtml enter: " + level); 193 } 194 String url = ITEM_URL + "?ref=../contents/" + item.getRef() 195 + "&add=" + item.getObjectId(); 196 fSampleItemAnchor.setHref(url); 197 fSampleItemAnchor.setTarget("item"); 198 fSampleItemText.setData(item.getDesc()); 199 fSampleItemBlock.setClassName("level" + level); 200 parent.appendChild(fSampleItemBlock.cloneNode(true)); 201 if (DEBUG) { 202 msgPrintln("makeItemHtml exit: " + level); 203 } 204 } 205 206 210 private void processInventoryEntry(Inventory entry, Node parent, int level) { 211 if (entry.isCategory()) { 212 boolean isOpen = fOpenCatIds.contains(new Long (entry.getObjectId())); 213 makeCategoryHtml(entry, parent, isOpen, level); 214 if (isOpen) { 215 Vector entries = entry.getChildren(); 216 for (int idx = 0; idx < entries.size(); idx++) { 217 processInventoryEntry((Inventory)entries.elementAt(idx), parent, level+1); 218 } 219 } 220 } else { 221 makeItemHtml(entry, parent, level); 222 } 223 } 224 225 228 private void processInventory() { 229 processInventoryEntry(fInventoryRoot, 230 fShelfHtml.getElementList(), 1); 231 } 232 233 236 private void parseArgs(String button, 237 String open) { 238 239 if (button != null) { 241 fButtonCatId = new Long (button); 242 } 243 244 if (open != null) { 245 StringTokenizer tokens = new StringTokenizer (open, ","); 246 while (tokens.hasMoreTokens()) { 247 String token = tokens.nextToken(); 248 try { 249 Long id = new Long (token); 250 fOpenCatIds.put(id, id); 251 } catch (NumberFormatException except) { 252 } 254 } 255 } 256 } 257 258 262 private void adjustOpenCategories() { 263 if (fButtonCatId != null) { 264 if (fOpenCatIds.containsKey(fButtonCatId)) { 266 fOpenCatIds.remove(fButtonCatId); 267 } else { 268 fOpenCatIds.put(fButtonCatId, fButtonCatId); 269 } 270 } 271 272 StringBuffer buf = new StringBuffer (); 274 Enumeration openIds = fOpenCatIds.keys(); 275 276 while (openIds.hasMoreElements()) { 277 if (buf.length() > 0) { 278 buf.append(","); 279 } 280 buf.append(((Long )openIds.nextElement()).toString()); 281 } 282 if (buf.length() > 0) { 283 fOpenCatCgiArg = "open=" + buf.toString(); 284 } 285 } 286 287 290 public void runRequest(String button, 291 String open) { 292 parseArgs(button, open); 293 prepareHtml(); 294 adjustOpenCategories(); 295 processInventory(); 296 } 297 298 } 299 300 301 public static Test suite() { 302 return createSuite(ShelfTests.class, null); 303 } 304 305 306 public ShelfTests(Method method) { 307 super(method); 308 fShelfSource = new TestDOMSource(this, getParams(), 309 getInputFile("Shelf.html"), 310 "ShelfHTMLImpl", 311 ShelfHTML.class); 312 } 313 314 317 private void request(String button, 318 String open) { 319 RequestTest test = new RequestTest(); 320 test.runRequest(button, open); 321 fRequestCnt++; 322 } 323 324 327 private void playback() { 328 request(null, null); 329 request("0", null); 330 request("2", "0"); 331 request("2", "2,0"); 332 request("7", "0"); 333 request("8", "7,0"); 334 request("9", "8,7,0"); 335 request("7", "9,8,7,0"); 336 request("7", "9,8,0"); 337 request("8", "9,8,7,0"); 338 request("7", "9,7,0"); 339 request("3", "9,0"); 340 request("4", "9,3,0"); 341 request("1", "9,4,3,0"); 342 request("2", "9,4,3,1,0"); 343 request("1", "9,4,3,2,1,0"); 344 request("0", "9,4,3,2,0"); 345 request("0", "9,4,3,2"); 346 request("2", "9,4,3,2,0"); 347 request("4", "9,4,3,0"); 348 request("3", "9,3,0"); 349 request("1", "9,0"); 350 request("1", "9,1,0"); 351 request("0", "9,0"); 352 request("0", "9"); 353 request("7", "9,0"); 354 request("3", "9,7,0"); 355 request("2", "9,7,3,0"); 356 request("1", "9,7,3,2,0"); 357 request("0", "9,7,3,2,1,0"); 358 request("0", "9,7,3,2,1"); 359 request("1", "9,7,3,2,1,0"); 360 request("2", "9,7,3,2,0"); 361 request("5", "9,7,3,0"); 362 request("2", "9,7,5,3,0"); 363 request("7", "9,7,5,3,2,0"); 364 request("2", "9,5,3,2,0"); 365 } 366 367 370 private Inventory buildInventory() { 371 Inventory entry0, entry1, entry2, entry3; 372 entry0 = new Inventory(true, 0, "Store Directory", "Main Menu"); 373 entry1 = new Inventory(true, 1, "Accessories", "Accessories"); 374 entry0.addChild(entry1); 375 entry2 = new Inventory(false, 5, "Assorted Balls", "pages/accessories_balls.html"); 376 entry1.addChild(entry2); 377 entry2 = new Inventory(false, 6, "Gloves", "pages/accessories_gloves.html"); 378 entry1.addChild(entry2); 379 entry2 = new Inventory(false, 7, "Practice Balls", "pages/accessories_practiceballs.html"); 380 entry1.addChild(entry2); 381 entry1 = new Inventory(true, 2, "Clubs", "Clubs"); 382 entry0.addChild(entry1); 383 entry2 = new Inventory(false, 8, "Graphite Irons", "pages/clubs_irons_graphite.html"); 384 entry1.addChild(entry2); 385 entry2 = new Inventory(false, 9, "Black Hawk Putter", "pages/clubs_putter_1.html"); 386 entry1.addChild(entry2); 387 entry2 = new Inventory(false, 10, "Steel Irons", "pages/clubs_irons_steel.html"); 388 entry1.addChild(entry2); 389 entry2 = new Inventory(false, 11, "Basic Putter", "pages/clubs_putter_2.html"); 390 entry1.addChild(entry2); 391 entry2 = new Inventory(false, 12, "Grey Hawk Putter", "pages/clubs_putter_3.html"); 392 entry1.addChild(entry2); 393 entry2 = new Inventory(false, 13, "Side Waggle Putter", "pages/clubs_putter_4.html"); 394 entry1.addChild(entry2); 395 entry2 = new Inventory(false, 14, "2 Tone Putter", "pages/clubs_putter_5.html"); 396 entry1.addChild(entry2); 397 entry2 = new Inventory(false, 15, "Graphite Wood", "pages/clubs_woods_graphite.html"); 398 entry1.addChild(entry2); 399 entry2 = new Inventory(false, 16, "Steel Wood Driver", "pages/clubs_woods_steel.html"); 400 entry1.addChild(entry2); 401 entry2 = new Inventory(false, 17, "Wood Woods", "pages/clubs_woods_woods.html"); 402 entry1.addChild(entry2); 403 entry1 = new Inventory(true, 3, "Men's Apparel", "MensApparel"); 404 entry0.addChild(entry1); 405 entry2 = new Inventory(true, 4, "Shirts", "Shirts"); 406 entry1.addChild(entry2); 407 entry3 = new Inventory(false, 22, "Classic Golf Shirt", "pages/mens_shirt_1.html"); 408 entry2.addChild(entry3); 409 entry3 = new Inventory(false, 23, "Pocketed Golf Shirt", "pages/mens_shirt_2.html"); 410 entry2.addChild(entry3); 411 entry3 = new Inventory(false, 24, "No-Pocket Polo", "pages/mens_shirt_3.html"); 412 entry2.addChild(entry3); 413 entry3 = new Inventory(false, 25, "Striped Polo", "pages/mens_shirt_4.html"); 414 entry2.addChild(entry3); 415 entry3 = new Inventory(false, 26, "Long Sleeve ", "pages/mens_shirt_5.html"); 416 entry2.addChild(entry3); 417 entry2 = new Inventory(true, 5, "Shoes", "Shoes"); 418 entry1.addChild(entry2); 419 entry3 = new Inventory(false, 27, "Golf-Classic Series", "pages/mens_shoes_1.html"); 420 entry2.addChild(entry3); 421 entry3 = new Inventory(false, 28, "Talon Golf Shoe", "pages/mens_shoes_2.html"); 422 entry2.addChild(entry3); 423 entry3 = new Inventory(false, 29, "Sport Golf Shoe", "pages/mens_shoes_3.html"); 424 entry2.addChild(entry3); 425 entry2 = new Inventory(true, 6, "Sweaters", "Sweaters"); 426 entry1.addChild(entry2); 427 entry3 = new Inventory(false, 30, "Pretty-N-Pink Sweater", "pages/mens_sweater_1.html"); 428 entry2.addChild(entry3); 429 entry3 = new Inventory(false, 31, "Stylish Sweater", "pages/mens_sweater_2.html"); 430 entry2.addChild(entry3); 431 entry3 = new Inventory(false, 32, "Supplex Wind Shirt", "pages/mens_sweater_3.html"); 432 entry2.addChild(entry3); 433 entry1 = new Inventory(true, 7, "Women's Apparel", "WomensApparel"); 434 entry0.addChild(entry1); 435 entry2 = new Inventory(true, 8, "Shirts", "Shirts"); 436 entry1.addChild(entry2); 437 entry3 = new Inventory(false, 33, "Short Sleeve Polo", "pages/womens_shirt_1.html"); 438 entry2.addChild(entry3); 439 entry3 = new Inventory(false, 34, "Striped Polo Jersey", "pages/womens_shirt_2.html"); 440 entry2.addChild(entry3); 441 entry3 = new Inventory(false, 35, "Fancy Collar Jersey", "pages/womens_shirt_3.html"); 442 entry2.addChild(entry3); 443 entry3 = new Inventory(false, 36, "Polo Jersey", "pages/womens_shirt_4.html"); 444 entry2.addChild(entry3); 445 entry2 = new Inventory(true, 9, "Shorts", "Shorts"); 446 entry1.addChild(entry2); 447 entry3 = new Inventory(false, 39, "Microfiber Twill", "pages/womens_shorts_1.html"); 448 entry2.addChild(entry3); 449 entry3 = new Inventory(false, 40, "Twill II", "pages/womens_shorts_2.html"); 450 entry2.addChild(entry3); 451 entry3 = new Inventory(false, 41, "Plaid Twill", "pages/womens_shorts_3.html"); 452 entry2.addChild(entry3); 453 entry3 = new Inventory(false, 42, "Khaki Shorts", "pages/womens_shorts_4.html"); 454 entry2.addChild(entry3); 455 return entry0; 456 } 457 458 461 public void test1() { 462 fInventoryRoot = buildInventory(); 463 playback(); 464 } 465 } 466 467 470 class Inventory { 471 private boolean fIsCategory; 472 private long fObjectId; 473 private String fDesc; 474 private String fRef; 475 private Vector children; 476 477 480 public Inventory(boolean isCategory, 481 long objectId, 482 String desc, 483 String ref) { 484 fIsCategory = isCategory; 485 fObjectId = objectId; 486 fDesc = desc; 487 fRef = ref; 488 } 489 490 public void addChild(Inventory child) { 491 if (children == null) { 492 children = new Vector (); 493 } 494 children.add(child); 495 } 496 497 public Vector getChildren() { 498 return children; 499 } 500 501 public boolean isCategory() { 502 return fIsCategory; 503 } 504 505 public long getObjectId() { 506 return fObjectId; 507 } 508 509 public String getDesc() { 510 return fDesc; 511 } 512 513 public String getRef() { 514 return fRef; 515 } 516 } 517 | Popular Tags |