KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > xml > xmlc > html > ShelfTests


1 /*
2  * Enhydra Java Application Server Project
3  *
4  * The contents of this file are subject to the Enhydra Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License on
7  * the Enhydra web site ( http://www.enhydra.org/ ).
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11  * the License for the specific terms governing rights and limitations
12  * under the License.
13  *
14  * The Initial Developer of the Enhydra Application Server is Lutris
15  * Technologies, Inc. The Enhydra Application Server and portions created
16  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17  * All Rights Reserved.
18  *
19  * Contributor(s):
20  *
21  * $Id: ShelfTests.java,v 1.2 2005/01/26 08:29:25 jkjome Exp $
22  */

23
24 package org.enhydra.xml.xmlc.html;
25
26 import java.lang.reflect.Method JavaDoc;
27 import java.util.Enumeration JavaDoc;
28 import java.util.Hashtable JavaDoc;
29 import java.util.StringTokenizer JavaDoc;
30 import java.util.Vector JavaDoc;
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 JavaDoc;
37 import org.w3c.dom.Text JavaDoc;
38 import org.w3c.dom.html.HTMLAnchorElement;
39 import org.w3c.dom.html.HTMLImageElement;
40 import org.w3c.dom.html.HTMLParagraphElement;
41
42 /**
43  * Test case, based on the shopping demo, that does some manipulation of the
44  * DOM, including cloning and inserting.
45  */

46 public class ShelfTests extends HtmlTestCaseBase {
47     /**
48      * Compile-time debug flag.
49      */

50     private static boolean DEBUG = false;
51
52     /**
53      * Wrapper around XMLC factory object, includes compilation on first
54      * use.
55      */

56     private TestDOMSource fShelfSource;
57     
58     /**
59      * Root of similated Inventory.
60      */

61     private static Inventory fInventoryRoot;
62
63     /**
64      * Current request number for DOM dumps.
65      */

66     private static int fRequestCnt;
67
68     /**
69      * URL to reference ourselve.
70      */

71     private static final String JavaDoc SHELF_URL = "Shelf.po";
72
73     /**
74      * URL to reference the item presentation object.
75      */

76     private static final String JavaDoc ITEM_URL = "Item.po";
77
78     /**
79      * Image to use for open categories.
80      */

81     private static final String JavaDoc OPEN_CAT_IMAGE = "../media/true.gif";
82     private static final String JavaDoc CLOSED_CAT_IMAGE = "../media/false.gif";
83
84     /**
85      * Class for generating a simulating a request.
86      */

87     private class RequestTest {
88         /**
89          * The catagory id of the button that was pressed or NULL if
90          * one was not pressed.
91          */

92         private Long JavaDoc fButtonCatId;
93
94         /**
95          * Table of category ids that are open. Each entry contains a Long
96          * object.
97          */

98         private Hashtable JavaDoc fOpenCatIds = new Hashtable JavaDoc();
99
100         /**
101          * CGI argument containing all open category ids. If null,
102          * then none are open.
103          */

104         private String JavaDoc fOpenCatCgiArg;
105
106         /**
107          * Shelf HTML object, addressed as an interface.
108          */

109         private ShelfHTML fShelfHtml;
110
111         /**
112          * Pointers to category prototype entries.
113          */

114         private HTMLParagraphElement fSampleCatBlock;
115         private HTMLAnchorElement fSampleCatAnchor;
116         private HTMLImageElement fSampleCatImg;
117         private Text JavaDoc fSampleCatText;
118
119         /**
120          * Pointers to item prototype entries.
121          */

122         private HTMLParagraphElement fSampleItemBlock;
123         private HTMLAnchorElement fSampleItemAnchor;
124         private Text JavaDoc fSampleItemText;
125
126         /** Constructor */
127         public RequestTest() {
128         }
129
130         /**
131          * Remove prototype table and save pointers to the prototype
132          * entries. For easy of use, prototypes are edited and then
133          * cloned.
134          */

135         private void prepareHtml() {
136             fShelfHtml = (ShelfHTML)fShelfSource.create();
137
138             // Save pointers to prototypes, remove ids as they shouldn't be duplicated.
139
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             // Remove children of prototype list.
149
Node JavaDoc listNode = fShelfHtml.getElementList();
150             Node JavaDoc child = listNode.getFirstChild();
151             while (child != null) {
152                 Node JavaDoc nextChild = child.getNextSibling();
153                 listNode.removeChild(child);
154                 child = nextChild;
155             }
156
157         }
158
159         /**
160          * Create a new HTML entry for a category.
161          */

162         private void makeCategoryHtml(Inventory category, Node JavaDoc parent,
163                                       boolean isOpen, int level) {
164             if (DEBUG) {
165                 msgPrintln("makeCategotyHtml enter: " + level);
166             }
167             long id = category.getObjectId();
168
169             String JavaDoc 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         /**
188          * Create a new HTML entry for an item.
189          */

190         private void makeItemHtml(Inventory item, Node JavaDoc parent, int level) {
191             if (DEBUG) {
192                 msgPrintln("makeItemHtml enter: " + level);
193             }
194             String JavaDoc 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         /**
207          * Recursively process a category or item entry, adding it to the DOM.
208          * Sets node class using level for CSS.
209          */

210         private void processInventoryEntry(Inventory entry, Node JavaDoc parent, int level) {
211             if (entry.isCategory()) {
212                 boolean isOpen = fOpenCatIds.contains(new Long JavaDoc(entry.getObjectId()));
213                 makeCategoryHtml(entry, parent, isOpen, level);
214                 if (isOpen) {
215                     Vector JavaDoc 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         /**
226          * Generate the DOM entries for the current state of the inventory.
227          */

228         private void processInventory() {
229             processInventoryEntry(fInventoryRoot,
230                                   fShelfHtml.getElementList(), 1);
231         }
232
233         /**
234          * Parse CGI arguments into fields.
235          */

236         private void parseArgs(String JavaDoc button,
237                                String JavaDoc open) {
238
239             // Parse button id.
240
if (button != null) {
241                 fButtonCatId = new Long JavaDoc(button);
242             }
243
244             if (open != null) {
245                 StringTokenizer JavaDoc tokens = new StringTokenizer JavaDoc(open, ",");
246                 while (tokens.hasMoreTokens()) {
247                     String JavaDoc token = tokens.nextToken();
248                     try {
249                         Long JavaDoc id = new Long JavaDoc(token);
250                         fOpenCatIds.put(id, id);
251                     } catch (NumberFormatException JavaDoc except) {
252                         // Ignore, probably a bogus URL.
253
}
254                 }
255             }
256         }
257
258         /**
259          * Modify the open category list based on the currenmt button.
260          * Also generate CGI argument for the open categories.
261          */

262         private void adjustOpenCategories() {
263             if (fButtonCatId != null) {
264                 // Adjust the open entry for the current button.
265
if (fOpenCatIds.containsKey(fButtonCatId)) {
266                     fOpenCatIds.remove(fButtonCatId);
267                 } else {
268                     fOpenCatIds.put(fButtonCatId, fButtonCatId);
269                 }
270             }
271
272             // Generate CGI arguments.
273
StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
274             Enumeration JavaDoc openIds = fOpenCatIds.keys();
275
276             while (openIds.hasMoreElements()) {
277                 if (buf.length() > 0) {
278                     buf.append(",");
279                 }
280                 buf.append(((Long JavaDoc)openIds.nextElement()).toString());
281             }
282             if (buf.length() > 0) {
283                 fOpenCatCgiArg = "open=" + buf.toString();
284             }
285         }
286
287         /**
288          * Run a simulated request.
289          */

290         public void runRequest(String JavaDoc button,
291                                String JavaDoc open) {
292             parseArgs(button, open);
293             prepareHtml();
294             adjustOpenCategories();
295             processInventory();
296         }
297
298     }
299
300     /** Factory method to create suite of these tests */
301     public static Test suite() {
302         return createSuite(ShelfTests.class, null);
303     }
304     
305     /** Constructor */
306     public ShelfTests(Method JavaDoc method) {
307         super(method);
308         fShelfSource = new TestDOMSource(this, getParams(),
309                                          getInputFile("Shelf.html"),
310                                          "ShelfHTMLImpl",
311                                          ShelfHTML.class);
312     }
313
314     /**
315      * Create an object and simulate a request.
316      */

317     private void request(String JavaDoc button,
318                          String JavaDoc open) {
319         RequestTest test = new RequestTest();
320         test.runRequest(button, open);
321         fRequestCnt++;
322     }
323
324     /**
325      * Playback recorded http requests.
326      */

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     /**
368      * Build test inventory (build from tracing demo program).
369      */

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     /**
459      * Run the test.
460      */

461     public void test1() {
462         fInventoryRoot = buildInventory();
463         playback();
464     }
465 }
466
467 /**
468  * Entry in the inventory tree.
469  */

470 class Inventory {
471     private boolean fIsCategory;
472     private long fObjectId;
473     private String JavaDoc fDesc;
474     private String JavaDoc fRef;
475     private Vector JavaDoc children;
476
477     /*
478      * Constructor
479      */

480     public Inventory(boolean isCategory,
481                      long objectId,
482                      String JavaDoc desc,
483                      String JavaDoc 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 JavaDoc();
493         }
494         children.add(child);
495     }
496
497     public Vector JavaDoc 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 JavaDoc getDesc() {
510         return fDesc;
511     }
512
513     public String JavaDoc getRef() {
514         return fRef;
515     }
516 }
517
Popular Tags