1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18 /*
19 * $Id$
20 */
21 package org.apache.qetest.dtm;
22
23 import java.io.File;
24 import java.io.FileOutputStream;
25 import java.io.StringReader;
26 import java.util.Properties;
27
28 import javax.xml.transform.Source;
29 import javax.xml.transform.stream.StreamSource;
30
31 import org.apache.qetest.FileBasedTest;
32 import org.apache.qetest.OutputNameManager;
33 import org.apache.qetest.xsl.XSLTestfileInfo;
34 import org.apache.xml.dtm.Axis;
35 import org.apache.xml.dtm.DTM;
36 import org.apache.xml.dtm.DTMAxisTraverser;
37 import org.apache.xml.dtm.DTMManager;
38 import org.apache.xml.dtm.ref.DTMManagerDefault;
39 import org.apache.xpath.objects.XMLStringFactoryImpl;
40
41 //-------------------------------------------------------------------------
42
43 /**
44 * This test creates a DTM and then walks it with AxisTraversers
45 * for each axis within XPATH
46 * - execute 'build package.trax', 'traxapitest TestDTMTrav.java'
47 * - a bunch of convenience variables/initializers are included,
48 * use or delete as is useful
49 * @author Paul Dick
50 * @version $Id$
51 */
52 public class TestDTMTrav extends FileBasedTest
53 {
54 /**
55 * Provides nextName(), currentName() functionality for tests
56 * that may produce any number of output files.
57 */
58 protected OutputNameManager outNames;
59
60 /**
61 * Information about an xsl/xml file pair for transforming.
62 * Public members include inputName (for xsl); xmlName; goldName; etc.
63 * If you don't use an .xml file on disk, you don't actually need this.
64 */
65 protected XSLTestfileInfo testFileInfo = new XSLTestfileInfo();
66
67 /** Subdirectory under test\tests\api for our xsl/xml files. */
68 public static final String DTM_SUBDIR = "dtm";
69 public static final String TRAV_Prefix = "Trav_";
70
71 private static final String defaultSource=
72 "<?xml version=\"1.0\"?>\n"+
73 "<Document xmlns:d=\"www.d.com\" a1=\"hello\" a2=\"goodbye\">"+
74 "<!-- Default test document -->"+
75 "<?api a1=\"yes\" a2=\"no\"?>"+
76 "<A><!-- A Subtree --><B><C><D><E><F xmlns:f=\"www.f.com\" a1=\"down\" a2=\"up\"/></E></D></C></B></A>"+
77 "<Aa/><Ab/><Ac><Ac1/></Ac>"+
78 "<Ad xmlns:Ad=\"www.Ad.com\" xmlns:y=\"www.y.com\" xmlns:z=\"www.z.com\">"+
79 "<Ad1/></Ad>"+
80 "</Document>";
81
82 static final String[] TYPENAME=
83 { "NULL",
84 "ELEMENT",
85 "ATTRIBUTE",
86 "TEXT",
87 "CDATA_SECTION",
88 "ENTITY_REFERENCE",
89 "ENTITY",
90 "PROCESSING_INSTRUCTION",
91 "COMMENT",
92 "DOCUMENT",
93 "DOCUMENT_TYPE",
94 "DOCUMENT_FRAGMENT",
95 "NOTATION",
96 "NAMESPACE"
97 };
98
99 private int lastNode = 0; // Set by first axis, used by subsequent axis.
100 private String lastName;
101
102 private int lastNode2 = 0; // Set by DESCENDANTORSELF, used in 11 & 12
103 private String lastName2;
104
105 private int ANode = 0; // Used in testcase 7 - 10
106 private String ANodeName;
107
108 private int CNode = 0;
109 private String CNodeName;
110
111 private int DNode = 0;
112 private String DNodeName;
113
114 private int PINode = 0;
115 private String PINodeName;
116
117 private static dtmWSStripper stripper = new dtmWSStripper();
118
119 /** Just initialize test name, comment, numTestCases. */
TestDTMTrav()120 public TestDTMTrav()
121 {
122 numTestCases = 20;
123 testName = "TestDTMTrav";
124 testComment = "Function test of DTM Traversers";
125 }
126
127 /**
128 * Initialize this test - Set names of xml/xsl test files,
129 * REPLACE_other_test_file_init.
130 *
131 * @param p Properties to initialize from (if needed)
132 * @return false if we should abort the test; true otherwise
133 */
doTestFileInit(Properties p)134 public boolean doTestFileInit(Properties p)
135 {
136 // Used for all tests; just dump files in dtm subdir
137 File outSubDir = new File(outputDir + File.separator + DTM_SUBDIR);
138 if (!outSubDir.mkdirs())
139 reporter.logWarningMsg("Could not create output dir: " + outSubDir);
140
141 // Initialize an output name manager to that dir with .out extension
142 outNames = new OutputNameManager(outputDir + File.separator + DTM_SUBDIR
143 + File.separator + testName, ".out");
144
145 String testBasePath = inputDir
146 + File.separator
147 + DTM_SUBDIR
148 + File.separator;
149 String goldBasePath = goldDir
150 + File.separator
151 + DTM_SUBDIR
152 + File.separator
153 + TRAV_Prefix;
154
155 //testFileInfo.inputName = testBasePath + "REPLACE_xslxml_filename.xsl";
156 //testFileInfo.xmlName = testBasePath + "REPLACE_xslxml_filename.xml";
157 testFileInfo.goldName = goldBasePath;
158
159 return true;
160 }
161
162 /**
163 * Cleanup this test - REPLACE_other_test_file_cleanup.
164 *
165 * @param p Properties to initialize from (if needed)
166 * @return false if we should abort the test; true otherwise
167 */
doTestFileClose(Properties p)168 public boolean doTestFileClose(Properties p)
169 {
170 // Often will be a no-op
171 return true;
172 }
173
174 /**
175 * Create AxisTraverser and walk CHILD axis.
176 * @return false if we should abort the test; true otherwise
177 */
testCase1()178 public boolean testCase1()
179 {
180 reporter.testCaseInit("Walk CHILD AxisTraverser");
181 StringBuffer buf = new StringBuffer();
182 FileOutputStream fos = openFileStream(outNames.nextName());
183 String gold = testFileInfo.goldName + "testcase1.out";
184
185 // Create dtm and generate initial context
186 DTM dtm = generateDTM();
187
188 // Get various nodes to use as context nodes.
189 int dtmRoot = dtm.getDocument(); // #document
190 String dtmRootName = dtm.getNodeName(dtmRoot); // Used for output
191 DNode = dtm.getFirstChild(dtmRoot); // <Document>
192 DNodeName = dtm.getNodeName(DNode);
193 CNode = dtm.getFirstChild(DNode); // <Comment>
194 CNodeName = dtm.getNodeName(CNode);
195 PINode = dtm.getNextSibling(CNode); // <PI>
196 PINodeName = dtm.getNodeName(PINode);
197 ANode = dtm.getNextSibling(PINode); // <A>
198 ANodeName = dtm.getNodeName(ANode);
199
200 // Get a Traverser for CHILD:: axis and query it's direction.
201 buf.append("#### CHILD from " + DNodeName + "\n");
202 DTMAxisTraverser at = dtm.getAxisTraverser(Axis.CHILD);
203
204 // Traverse the axis and write node info to output file
205 for (int atNode = at.first(DNode); DTM.NULL != atNode;
206 atNode = at.next(DNode,atNode))
207 {
208 buf.append(getNodeInfo(dtm, atNode, " "));
209 lastNode = atNode; // Setting this GLOBAL IS BAD, but easy. Investigate!!
210 }
211 lastName = dtm.getNodeName(lastNode);
212
213 // Write results and close output file.
214 writeClose(fos, buf);
215
216 // Verify results
217 fileChecker.check(reporter, new File(outNames.currentName()),
218 new File(gold),
219 "Testcase1");
220 reporter.testCaseClose();
221 return true;
222 }
223
testCase2()224 public boolean testCase2()
225 {
226 reporter.testCaseInit("Walk PARENT AxisTraverser");
227 StringBuffer buf = new StringBuffer();
228 FileOutputStream fos = openFileStream(outNames.nextName());
229 String gold = testFileInfo.goldName + "testcase2.out";
230
231 // Create dtm and generate initial context
232 DTM dtm = generateDTM();
233
234 // Get a Traverser for PARENT:: axis.
235 buf.append("#### PARENT from " + lastName + "\n");
236 DTMAxisTraverser at = dtm.getAxisTraverser(Axis.PARENT);
237
238 // Traverse the axis and write node info to output file
239 for (int atNode = at.first(lastNode); DTM.NULL != atNode;
240 atNode = at.next(lastNode, atNode))
241 buf.append(getNodeInfo(dtm, atNode, " "));
242
243 // Write results and close output file.
244 writeClose(fos, buf);
245
246 // Verify results
247 fileChecker.check(reporter, new File(outNames.currentName()),
248 new File(gold),
249 "Testcase2");
250
251 reporter.testCaseClose();
252 return true;
253 }
254
255 /**
256 * Create AxisTraverser and walk SELF axis.
257 * @return false if we should abort the test; true otherwise
258 */
testCase3()259 public boolean testCase3()
260 {
261 reporter.testCaseInit("Walk SELF AxisTraverser");
262 StringBuffer buf = new StringBuffer();
263 FileOutputStream fos = openFileStream(outNames.nextName());
264 String gold = testFileInfo.goldName + "testcase3.out";
265
266 // Create dtm and generate initial context
267 DTM dtm = generateDTM();
268
269 // Get a Traverser for CHILD:: axis.
270 buf.append("#### SELF from " + lastName + "\n");
271 DTMAxisTraverser at = dtm.getAxisTraverser(Axis.SELF);
272
273 // Traverse the axis and write node info to output file
274 for (int atNode = at.first(lastNode); DTM.NULL != atNode;
275 atNode = at.next(lastNode, atNode))
276 buf.append(getNodeInfo(dtm, atNode, " "));
277
278 // Write results and close output file.
279 writeClose(fos, buf);
280
281 // Verify results
282 fileChecker.check(reporter, new File(outNames.currentName()),
283 new File(gold),
284 "Testcase3");
285
286 reporter.testCaseClose();
287 return true;
288 }
289
290 /**
291 * Create AxisTraverser and walk NAMESPACE axis.
292 * @return false if we should abort the test; true otherwise
293 */
testCase4()294 public boolean testCase4()
295 {
296 reporter.testCaseInit("Walk NAMESPACE AxisTraverser");
297 StringBuffer buf = new StringBuffer();
298 FileOutputStream fos = openFileStream(outNames.nextName());
299 String gold = testFileInfo.goldName + "testcase4.out";
300
301 // Create dtm and generate initial context
302 DTM dtm = generateDTM();
303
304 // Get a Traverser for NAMESPACE:: axis.
305 buf.append("#### NAMESPACE from " + lastName + "\n");
306 DTMAxisTraverser at = dtm.getAxisTraverser(Axis.NAMESPACE);
307
308 // Traverse the axis and write node info to output file
309 for (int atNode = at.first(lastNode); DTM.NULL != atNode;
310 atNode = at.next(lastNode, atNode)) {
311 buf.append(getNodeInfo(dtm, atNode, " "));
312 }
313 // Write results and close output file.
314 writeClose(fos, buf);
315
316 // Verify results
317 fileChecker.check(reporter, new File(outNames.currentName()),
318 new File(gold),
319 "Testcase4");
320
321 reporter.testCaseClose();
322 return true;
323 }
324 /**
325 * Create AxisTraverser and walk NAMESPACEDECLS axis.
326 * @return false if we should abort the test; true otherwise
327 */
testCase5()328 public boolean testCase5()
329 {
330 reporter.testCaseInit("Walk NAMESPACEDECLS AxisTraverser");
331 StringBuffer buf = new StringBuffer();
332 FileOutputStream fos = openFileStream(outNames.nextName());
333 String gold = testFileInfo.goldName + "testcase5.out";
334
335 // Create dtm and generate initial context
336 DTM dtm = generateDTM();
337
338 // Get a Traverser for NAMESPACEDECLS:: axis.
339 buf.append("#### NAMESPACEDECLS from "+ lastName + "\n");
340 DTMAxisTraverser at = dtm.getAxisTraverser(Axis.NAMESPACEDECLS);
341
342 // Traverse the axis and write node info to output file
343 for (int atNode = at.first(lastNode); DTM.NULL != atNode;
344 atNode = at.next(lastNode, atNode)) {
345 buf.append(getNodeInfo(dtm, atNode, " "));
346 }
347 // Write results and close output file.
348 writeClose(fos, buf);
349
350 // Verify results
351 fileChecker.check(reporter, new File(outNames.currentName()),
352 new File(gold),
353 "Testcase5");
354
355 reporter.testCaseClose();
356 return true;
357 }
358
359 /**
360 * Create AxisTraverser and walk PRECEDING axis.
361 * @return false if we should abort the test; true otherwise
362 */
testCase6()363 public boolean testCase6()
364 {
365 reporter.testCaseInit("Walk PRECEDING AxisTraverser");
366 StringBuffer buf = new StringBuffer();
367 FileOutputStream fos = openFileStream(outNames.nextName());
368 String gold = testFileInfo.goldName + "testcase6.out";
369
370 // Create dtm and generate initial context
371 DTM dtm = generateDTM();
372
373 // Get a Traverser for PRECEDING:: axis.
374 buf.append("#### PRECEDING from "+ lastName + "\n");
375 DTMAxisTraverser at = dtm.getAxisTraverser(Axis.PRECEDING);
376
377 // Traverse the axis and write node info to output file
378 for (int atNode = at.first(lastNode); DTM.NULL != atNode;
379 atNode = at.next(lastNode, atNode)) {
380 buf.append(getNodeInfo(dtm, atNode, " "));
381 }
382 // Write results and close output file.
383 writeClose(fos, buf);
384
385 // Verify results
386 fileChecker.check(reporter, new File(outNames.currentName()),
387 new File(gold),
388 "Testcase5");
389
390 reporter.testCaseClose();
391 return true;
392 }
393
394 /**
395 * Create AxisTraverser and walk PRECEDINGSIBLING axis.
396 * @return false if we should abort the test; true otherwise
397 */
testCase7()398 public boolean testCase7()
399 {
400 reporter.testCaseInit("Walk PRECEDINGSIBLING AxisTraverser");
401 StringBuffer buf = new StringBuffer();
402 FileOutputStream fos = openFileStream(outNames.nextName());
403 String gold = testFileInfo.goldName + "testcase7.out";
404
405 // Create dtm and generate initial context
406 DTM dtm = generateDTM();
407
408 // Get a Traverser for PRECEDINGSIBLING:: axis.
409 buf.append("#### PRECEDINGSIBLING from "+ lastName + "\n");
410 DTMAxisTraverser at = dtm.getAxisTraverser(Axis.PRECEDINGSIBLING);
411
412 // Traverse the axis and write node info to output file
413 for (int atNode = at.first(lastNode); DTM.NULL != atNode;
414 atNode = at.next(lastNode, atNode)) {
415 buf.append(getNodeInfo(dtm, atNode, " "));
416 }
417
418 // Write results and close output file.
419 writeClose(fos, buf);
420
421 // Verify results
422 fileChecker.check(reporter, new File(outNames.currentName()),
423 new File(gold),
424 "Testcase7");
425 reporter.testCaseClose();
426 return true;
427 }
428
429
430 /**
431 * Create AxisTraverser and walk FOLLOWING axis.
432 * @return false if we should abort the test; true otherwise
433 */
testCase8()434 public boolean testCase8()
435 {
436 reporter.testCaseInit("Walk FOLLOWING AxisTraverser");
437 StringBuffer buf = new StringBuffer();
438 FileOutputStream fos = openFileStream(outNames.nextName());
439 String gold = testFileInfo.goldName + "testcase8.out";
440
441 // Create dtm and generate initial context
442 DTM dtm = generateDTM();
443
444 // Get a Traverser for FOLLOWING:: axis.
445 buf.append("#### FOLLOWING from "+ ANodeName + "\n");
446 DTMAxisTraverser at = dtm.getAxisTraverser(Axis.FOLLOWING);
447
448 // Traverse the axis and write node info to output file
449 for (int atNode = at.first(ANode); DTM.NULL != atNode;
450 atNode = at.next(ANode, atNode)) {
451 buf.append(getNodeInfo(dtm, atNode, " "));
452 }
453 // Write results and close output file.
454 writeClose(fos, buf);
455
456 // Verify results
457 fileChecker.check(reporter, new File(outNames.currentName()),
458 new File(gold),
459 "Testcase8");
460 reporter.testCaseClose();
461 return true;
462 }
463
464 /**
465 * Create AxisTraverser and walk FOLLOWINGSIBLING axis.
466 * @return false if we should abort the test; true otherwise
467 */
testCase9()468 public boolean testCase9()
469 {
470 reporter.testCaseInit("Walk FOLLOWINGSIBLING AxisTraverser");
471 StringBuffer buf = new StringBuffer();
472 FileOutputStream fos = openFileStream(outNames.nextName());
473 String gold = testFileInfo.goldName + "testcase9.out";
474
475 // Create dtm and generate initial context
476 DTM dtm = generateDTM();
477
478 // Get a Traverser for FOLLOWINGSIBLING:: axis.
479 buf.append("#### FOLLOWINGSIBLING from "+ ANodeName + "\n");
480 DTMAxisTraverser at = dtm.getAxisTraverser(Axis.FOLLOWINGSIBLING);
481
482 // Traverse the axis and write node info to output file
483 for (int atNode = at.first(ANode); DTM.NULL != atNode;
484 atNode = at.next(ANode, atNode))
485 buf.append(getNodeInfo(dtm, atNode, " "));
486
487 // Write results and close output file.
488 writeClose(fos, buf);
489
490 // Verify results
491 fileChecker.check(reporter, new File(outNames.currentName()),
492 new File(gold),
493 "Testcase9");
494 reporter.testCaseClose();
495 return true;
496 }
497
498
499 /**
500 * Create AxisTraverser and walk DESCENDANT axis.
501 * @return false if we should abort the test; true otherwise
502 */
testCase10()503 public boolean testCase10()
504 {
505 reporter.testCaseInit("Walk DESCENDANT AxisTraverser");
506 StringBuffer buf = new StringBuffer();
507 FileOutputStream fos = openFileStream(outNames.nextName());
508 String gold = testFileInfo.goldName + "testcase10.out";
509
510 // Create dtm and generate initial context
511 DTM dtm = generateDTM();
512
513 // Get a Traverser for DESCENDANT:: axis.
514 buf.append("#### DESCENDANT from "+ ANodeName + "\n");
515 DTMAxisTraverser at = dtm.getAxisTraverser(Axis.DESCENDANT);
516
517 // Traverse the axis and write node info to output file
518 for (int atNode = at.first(ANode); DTM.NULL != atNode;
519 atNode = at.next(ANode, atNode)) {
520 buf.append(getNodeInfo(dtm, atNode, " "));
521 }
522 // Write results and close output file.
523 writeClose(fos, buf);
524
525 // Verify results
526 fileChecker.check(reporter, new File(outNames.currentName()),
527 new File(gold),
528 "Testcase10");
529 reporter.testCaseClose();
530 return true;
531 }
532
533 /**
534 * Create AxisTraverser and walk DESCENDANTORSELF axis.
535 * @return false if we should abort the test; true otherwise
536 */
testCase11()537 public boolean testCase11()
538 {
539 reporter.testCaseInit("Walk DESCENDANTORSELF AxisTraverser");
540 StringBuffer buf = new StringBuffer();
541 FileOutputStream fos = openFileStream(outNames.nextName());
542 String gold = testFileInfo.goldName + "testcase11.out";
543
544 // Create dtm and generate initial context
545 DTM dtm = generateDTM();
546
547 // Get a Traverser for DESCENDANTORSELF:: axis.
548 buf.append("#### DESCENDANTORSELF from "+ ANodeName + "\n");
549 DTMAxisTraverser at = dtm.getAxisTraverser(Axis.DESCENDANTORSELF);
550
551 // Traverse the axis and write node info to output file
552 for (int atNode = at.first(ANode); DTM.NULL != atNode;
553 atNode = at.next(ANode, atNode))
554 {
555 buf.append(getNodeInfo(dtm, atNode, " "));
556 lastNode2 = atNode;
557 }
558
559 lastName2 = dtm.getNodeName(lastNode2);
560
561 // Write results and close output file.
562 writeClose(fos, buf);
563
564 // Verify results
565 fileChecker.check(reporter, new File(outNames.currentName()),
566 new File(gold),
567 "Testcase11");
568 reporter.testCaseClose();
569 return true;
570 }
571
572
573 /**
574 * Create AxisTraverser and walk ANCESTOR axis.
575 * @return false if we should abort the test; true otherwise
576 */
testCase12()577 public boolean testCase12()
578 {
579 reporter.testCaseInit("Walk ANCESTOR AxisTraverser");
580 StringBuffer buf = new StringBuffer();
581 FileOutputStream fos = openFileStream(outNames.nextName());
582 String gold = testFileInfo.goldName + "testcase12.out";
583
584 // Create dtm and generate initial context
585 DTM dtm = generateDTM();
586
587 // Get a Traverser for ANCESTOR:: axis.
588 buf.append("#### ANCESTOR from "+ lastName2 + "\n");
589 DTMAxisTraverser at = dtm.getAxisTraverser(Axis.ANCESTOR);
590
591 // Traverse the axis and write node info to output file
592 for (int atNode = at.first(lastNode2); DTM.NULL != atNode;
593 atNode = at.next(lastNode2, atNode))
594 buf.append(getNodeInfo(dtm, atNode, " "));
595
596 // Write results and close output file.
597 writeClose(fos, buf);
598
599 // Verify results
600 fileChecker.check(reporter, new File(outNames.currentName()),
601 new File(gold),
602 "Testcase12");
603 reporter.testCaseClose();
604 return true;
605 }
606
607
608 /**
609 * Create AxisTraverser and walk ANCESTORORSELF axis.
610 * @return false if we should abort the test; true otherwise
611 */
testCase13()612 public boolean testCase13()
613 {
614 reporter.testCaseInit("Walk ANCESTORORSELF AxisTraverser");
615 StringBuffer buf = new StringBuffer();
616 FileOutputStream fos = openFileStream(outNames.nextName());
617 String gold = testFileInfo.goldName + "testcase13.out";
618
619 // Create dtm and generate initial context
620 DTM dtm = generateDTM();
621
622 // Get a Traverser for ANCESTORORSELF:: axis.
623 buf.append("#### ANCESTORORSELF from "+ lastName2 + "\n");
624 DTMAxisTraverser at = dtm.getAxisTraverser(Axis.ANCESTORORSELF);
625
626 // Traverse the axis and write node info to output file
627 for (int atNode = at.first(lastNode2); DTM.NULL != atNode;
628 atNode = at.next(lastNode2, atNode))
629 buf.append(getNodeInfo(dtm, atNode, " "));
630
631 // Write results and close output file.
632 writeClose(fos, buf);
633
634 // Verify results
635 fileChecker.check(reporter, new File(outNames.currentName()),
636 new File(gold),
637 "Testcase13");
638 reporter.testCaseClose();
639 return true;
640 }
641
642 /**
643 * Create AxisTraverser and walk ALLFROMNODE axis.
644 * @return false if we should abort the test; true otherwise
645 */
testCase14()646 public boolean testCase14()
647 {
648 reporter.testCaseInit("Walk ALLFROMNODE AxisTraverser");
649 StringBuffer buf = new StringBuffer();
650 FileOutputStream fos = openFileStream(outNames.nextName());
651 String gold = testFileInfo.goldName + "testcase14.out";
652
653 // Create dtm and generate initial context
654 DTM dtm = generateDTM();
655
656 // Get a Traverser for ALLFROMNODE:: axis.
657 buf.append("#### ALL-FROM-NODE from "+ lastName2 + "\n");
658 DTMAxisTraverser at = dtm.getAxisTraverser(Axis.ALLFROMNODE);
659
660 // Traverse the axis and write node info to output file
661 for (int atNode = at.first(lastNode2); DTM.NULL != atNode;
662 atNode = at.next(lastNode2, atNode))
663 buf.append(getNodeInfo(dtm, atNode, " "));
664
665 // Write results and close output file.
666 writeClose(fos, buf);
667
668 // Verify results
669 fileChecker.check(reporter, new File(outNames.currentName()),
670 new File(gold),
671 "Testcase14");
672 reporter.testCaseClose();
673 return true;
674 }
675
676 /**
677 * Create AxisTraverser and walk ATTRIBUTE axis.
678 * @return false if we should abort the test; true otherwise
679 */
testCase15()680 public boolean testCase15()
681 {
682 reporter.testCaseInit("Walk ATTRIBUTE AxisTraverser");
683 StringBuffer buf = new StringBuffer();
684 FileOutputStream fos = openFileStream(outNames.nextName());
685 String gold = testFileInfo.goldName + "testcase15.out";
686
687 // Create dtm and generate initial context
688 DTM dtm = generateDTM();
689
690 // Get a Traverser for ATTRIBUTE:: axis.
691 buf.append("#### ATTRIBUTE from "+ DNodeName + "\n");
692 DTMAxisTraverser at = dtm.getAxisTraverser(Axis.ATTRIBUTE);
693
694 // Traverse the axis and write node info to output file
695 for (int atNode = at.first(DNode); DTM.NULL != atNode;
696 atNode = at.next(DNode, atNode))
697 buf.append(getNodeInfo(dtm, atNode, " "));
698
699 // Write results and close output file.
700 writeClose(fos, buf);
701
702 // Verify results
703 fileChecker.check(reporter, new File(outNames.currentName()),
704 new File(gold),
705 "Testcase15");
706 reporter.testCaseClose();
707 return true;
708 }
709
710 // ABSOLUTE AXIS TESTS
711 // These next axis are all Absolute. They all default to the root of the dtm
712 // tree, regardless of what we call first() with.
713
714
715 /**
716 * Create AxisTraverser and walk ALLFROMNODE axis.
717 * @return false if we should abort the test; true otherwise
718 */
testCase16()719 public boolean testCase16()
720 {
721 reporter.testCaseInit("Walk ALL(absolute) AxisTraverser");
722 StringBuffer buf = new StringBuffer();
723 FileOutputStream fos = openFileStream(outNames.nextName());
724 String gold = testFileInfo.goldName + "testcase16.out";
725
726 // Create dtm and generate initial context
727 DTM dtm = generateDTM();
728
729 // Get a Traverser for ALLFROMNODE:: axis.
730 buf.append("#### ALL(absolute) from "+ lastName2 + "(root)\n");
731 DTMAxisTraverser at = dtm.getAxisTraverser(Axis.ALL);
732
733 // Traverse the axis and write node info to output file
734 for (int atNode = at.first(lastNode2); DTM.NULL != atNode;
735 atNode = at.next(lastNode2, atNode))
736 buf.append(getNodeInfo(dtm, atNode, " "));
737
738 // Write results and close output file.
739 writeClose(fos, buf);
740
741 // Verify results
742 fileChecker.check(reporter, new File(outNames.currentName()),
743 new File(gold),
744 "Testcase16");
745 reporter.testCaseClose();
746 return true;
747 }
748
749 /**
750 * Create AxisTraverser and walk ALLFROMNODE axis.
751 * @return false if we should abort the test; true otherwise
752 */
testCase17()753 public boolean testCase17()
754 {
755 reporter.testCaseInit("Walk DESCENDANTSFROMROOT(abs) AxisTraverser");
756 StringBuffer buf = new StringBuffer();
757 FileOutputStream fos = openFileStream(outNames.nextName());
758 String gold = testFileInfo.goldName + "testcase17.out";
759
760 // Create dtm and generate initial context
761 DTM dtm = generateDTM();
762
763 // Get a Traverser for DESCENDANTSFROMROOT:: axis.
764 buf.append("#### DESCENDANTSFROMROOT(abs) from "+ lastName2 + "(root)\n");
765 DTMAxisTraverser at = dtm.getAxisTraverser(Axis.DESCENDANTSFROMROOT);
766
767 // Traverse the axis and write node info to output file
768 for (int atNode = at.first(lastNode2); DTM.NULL != atNode;
769 atNode = at.next(lastNode2, atNode))
770 buf.append(getNodeInfo(dtm, atNode, " "));
771
772 // Write results and close output file.
773 writeClose(fos, buf);
774
775 // Verify results
776 fileChecker.check(reporter, new File(outNames.currentName()),
777 new File(gold),
778 "Testcase17");
779 reporter.testCaseClose();
780 return true;
781 }
782
783 /**
784 * Create AxisTraverser and walk DESCENDANTSORSELFFROMROOT axis.
785 * @return false if we should abort the test; true otherwise
786 */
testCase18()787 public boolean testCase18()
788 {
789 reporter.testCaseInit("Walk DESCENDANTSORSELFFROMROOT(abs) AxisTraverser");
790 StringBuffer buf = new StringBuffer();
791 FileOutputStream fos = openFileStream(outNames.nextName());
792 String gold = testFileInfo.goldName + "testcase18.out";
793
794 // Create dtm and generate initial context
795 DTM dtm = generateDTM();
796
797 // Get a Traverser for ALLFROMNODE:: axis.
798 buf.append("#### DESCENDANTSORSELFFROMROOT(abs) from "+ lastName2 + "(root)\n");
799 DTMAxisTraverser at = dtm.getAxisTraverser(Axis.DESCENDANTSORSELFFROMROOT);
800
801 // Traverse the axis and write node info to output file
802 for (int atNode = at.first(lastNode2); DTM.NULL != atNode;
803 atNode = at.next(lastNode2, atNode))
804 buf.append(getNodeInfo(dtm, atNode, " "));
805
806 // Write results and close output file.
807 writeClose(fos, buf);
808
809 // Verify results
810 fileChecker.check(reporter, new File(outNames.currentName()),
811 new File(gold),
812 "Testcase18");
813 reporter.testCaseClose();
814 return true;
815 }
816
817 /**
818 * Create AxisTraverser and walk ALLFROMNODE axis.
819 * @return false if we should abort the test; true otherwise
820 */
testCase19()821 public boolean testCase19()
822 {
823 reporter.testCaseInit("Walk ALLFROMNODE AxisTraverser");
824 StringBuffer buf = new StringBuffer();
825 FileOutputStream fos = openFileStream(outNames.nextName());
826 String gold = testFileInfo.goldName + "testcase19.out";
827
828 // Create dtm and generate initial context
829 DTM dtm = generateDTM();
830
831 // Get a Traverser for ALLFROMNODE:: axis.
832 buf.append("#### ALL-FROM-NODE from "+ ANodeName + "\n");
833 DTMAxisTraverser at = dtm.getAxisTraverser(Axis.ALLFROMNODE);
834
835 // Traverse the axis and write node info to output file
836 for (int atNode = at.first(ANode); DTM.NULL != atNode;
837 atNode = at.next(ANode, atNode))
838 buf.append(getNodeInfo(dtm, atNode, " "));
839
840 // Write results and close output file.
841 writeClose(fos, buf);
842
843 // Verify results
844 fileChecker.check(reporter, new File(outNames.currentName()),
845 new File(gold),
846 "Testcase19");
847 reporter.testCaseClose();
848 return true;
849 }
850
851 /**
852 * Create AxisTraverser and walk ALLFROMNODE axis.
853 * @return false if we should abort the test; true otherwise
854 */
testCase20()855 public boolean testCase20()
856 {
857 reporter.testCaseInit("Walk ALLFROMNODE AxisTraverser");
858 StringBuffer buf = new StringBuffer();
859 FileOutputStream fos = openFileStream(outNames.nextName());
860 String gold = testFileInfo.goldName + "testcase20.out";
861
862 // Create dtm and generate initial context
863 DTM dtm = generateDTM();
864
865 // Get a Traverser for ALLFROMNODE:: axis.
866 buf.append("#### ALL-FROM-NODE from "+ CNodeName + "\n");
867 DTMAxisTraverser at = dtm.getAxisTraverser(Axis.ALLFROMNODE);
868
869 // Traverse the axis and write node info to output file
870 for (int atNode = at.first(CNode); DTM.NULL != atNode;
871 atNode = at.next(CNode, atNode))
872 buf.append(getNodeInfo(dtm, atNode, " "));
873
874 // Write results and close output file.
875 writeClose(fos, buf);
876
877 // Verify results
878 fileChecker.check(reporter, new File(outNames.currentName()),
879 new File(gold),
880 "Testcase20");
881 reporter.testCaseClose();
882 return true;
883 }
884
885
usage()886 public String usage()
887 {
888 return ("Common [optional] options supported by TestDTMTrav:\n"
889 + "(Note: assumes inputDir=.\\tests\\api)\n");
890 }
891
892 // This routine generates the output file stream.
openFileStream(String name)893 FileOutputStream openFileStream(String name)
894 {
895 FileOutputStream fos = null;
896
897 try
898 { fos = new FileOutputStream(name); }
899
900 catch (Exception e)
901 { reporter.checkFail("Failure opening output file."); }
902
903 return fos;
904 }
905
906 // This routine generates a new DTM for each testcase
generateDTM()907 DTM generateDTM()
908 {
909 // Create DTM and generate initial context
910 Source source = new StreamSource(new StringReader(defaultSource));
911 DTMManager manager= new DTMManagerDefault().newInstance(new XMLStringFactoryImpl());
912 DTM dtm=manager.getDTM(source, true, stripper, false, true);
913
914 return dtm;
915 }
916
917 // This routine writes the results to the output file.
writeClose(FileOutputStream fos, StringBuffer buf)918 void writeClose(FileOutputStream fos, StringBuffer buf)
919 {
920 // Write results and close output file.
921 try
922 {
923 fos.write(buf.toString().getBytes("UTF-8"));
924 fos.close();
925 }
926
927 catch (Exception e)
928 { reporter.checkFail("Failure writing output."); }
929 }
930
931 // This routine gathers up all the important info about a node, concatenates
932 // in all together into a single string and returns it.
getNodeInfo(DTM dtm, int nodeHandle, String indent)933 String getNodeInfo(DTM dtm, int nodeHandle, String indent)
934 {
935 // Formatting hack -- suppress quotes when value is null, to distinguish
936 // it from "null" (JK).
937 String buf = new String("null");
938 String value=dtm.getNodeValue(nodeHandle);
939 String vq = (value==null) ? "" : "\"";
940
941 // Skip outputing of text nodes. In most cases they clutter the output,
942 // besides I'm only interested in the elemental structure of the dtm.
943 if( TYPENAME[dtm.getNodeType(nodeHandle)] != "TEXT" )
944 {
945 buf = new String(indent+
946 nodeHandle+": "+
947 TYPENAME[dtm.getNodeType(nodeHandle)]+" "+
948 dtm.getNodeName(nodeHandle)+" "+
949 " Level=" + dtm.getLevel(nodeHandle)+" "+
950 "\tValue=" + vq + value + vq + "\n"
951 );
952 }
953 return buf;
954 }
955
956 /**
957 * Main method to run test from the command line - can be left alone.
958 * @param args command line argument array
959 */
main(String[] args)960 public static void main(String[] args)
961 {
962 TestDTMTrav app = new TestDTMTrav();
963 app.doMain(args);
964 }
965
966 }
967