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.util.Properties;
26
27 import org.apache.qetest.FileBasedTest;
28 import org.apache.qetest.OutputNameManager;
29 import org.apache.qetest.xsl.XSLTestfileInfo;
30 import org.apache.xml.dtm.Axis;
31 import org.apache.xml.dtm.DTM;
32
33
34 public class TimeDTMIterDeep extends FileBasedTest
35 {
36 /**
37 * Provides nextName(), currentName() functionality for tests
38 * that may produce any number of output files.
39 */
40 protected OutputNameManager outNames;
41
42 /**
43 * Information about an xsl/xml file pair for transforming.
44 * Public members include inputName (for xsl); xmlName; goldName; etc.
45 * If you don't use an .xml file on disk, you don't actually need this.
46 */
47 protected XSLTestfileInfo testFileInfo = new XSLTestfileInfo();
48
49 /** Subdirectory under test\tests\api for our xsl/xml files. */
50 public static final String DTM_SUBDIR = "dtm";
51 public static final String TIME_Prefix = "TimeID_";
52
53 int lastNode;
54 String lastNodeName;
55
56 /** Just initialize test name, comment, numTestCases. */
TimeDTMIterDeep()57 public TimeDTMIterDeep()
58 {
59 numTestCases = 4;
60 testName = "TimeDTMIterDeep";
61 testComment = "Time the creation and various axis iterations with a deep tree";
62 }
63
64 /**
65 * Initialize this test - Set names of xml/xsl test files,
66 * REPLACE_other_test_file_init.
67 *
68 * @param p Properties to initialize from (if needed)
69 * @return false if we should abort the test; true otherwise
70 */
doTestFileInit(Properties p)71 public boolean doTestFileInit(Properties p)
72 {
73 // Used for all tests; just dump files in dtm subdir
74 File outSubDir = new File(outputDir + File.separator + DTM_SUBDIR);
75 if (!outSubDir.mkdirs())
76 reporter.logWarningMsg("Could not create output dir: " + outSubDir);
77
78 // Initialize an output name manager to that dir with .out extension
79 outNames = new OutputNameManager(outputDir + File.separator + DTM_SUBDIR
80 + File.separator + testName, ".out");
81
82 String testBasePath = inputDir
83 + File.separator
84 + DTM_SUBDIR
85 + File.separator;
86 String goldBasePath = goldDir
87 + File.separator
88 + DTM_SUBDIR
89 + File.separator
90 + TIME_Prefix;
91
92 testFileInfo.goldName = goldBasePath;
93
94 return true;
95 }
96
97 /**
98 * Cleanup this test - REPLACE_other_test_file_cleanup.
99 *
100 * @param p Properties to initialize from (if needed)
101 * @return false if we should abort the test; true otherwise
102 */
doTestFileClose(Properties p)103 public boolean doTestFileClose(Properties p)
104 {
105 // Often will be a no-op
106 return true;
107 }
108
testCase1()109 public boolean testCase1()
110 {
111 reporter.testCaseInit("Time Iteration of Descendant:: axis");
112 StringBuffer buf = new StringBuffer();
113 FileOutputStream fos = QeDtmUtils.openFileStream(outNames.nextName(), reporter);
114 String gold = testFileInfo.goldName + "testcase1.out";
115
116 buf.append("\nAxis is DESCENDANT");
117 DTM dtm = QeDtmUtils.createDTM(0, QeDtmUtils.deepFile, buf);
118
119 // Get various nodes to use as context nodes.
120 int dtmRoot = dtm.getDocument(); // #document
121 String dtmRootName = dtm.getNodeName(dtmRoot); // Used for output
122 int DNode = dtm.getFirstChild(dtmRoot); // <Doc>
123 String DNodeName = dtm.getNodeName(DNode);
124 int[] rtData = {0,0,0}; // returns Iteration time, last node, number of nodes traversed
125
126 // Get a iterator for Descendant:: axis.
127 buf.append("\n\tSTARTING from: "+ DNodeName);
128 QeDtmUtils.timeAxisIterator(dtm, Axis.DESCENDANT, DNode, rtData);
129 buf.append("\n\tTime="+rtData[0] + " : " + "LastNode="+rtData[1]+" nodes="+rtData[2]);
130
131 // Write results and close output file.
132 QeDtmUtils.writeClose(fos, buf, reporter);
133
134 // Verify results
135 fileChecker.check(reporter, new File(outNames.currentName()),
136 new File(gold),
137 "Testcase1");
138 reporter.testCaseClose();
139 return true;
140
141 }
142
testCase2()143 public boolean testCase2()
144 {
145 reporter.testCaseInit("Time Iteration of DESCENDANT-OR-SELF:: axis");
146 StringBuffer buf = new StringBuffer();
147 FileOutputStream fos = QeDtmUtils.openFileStream(outNames.nextName(), reporter);
148 String gold = testFileInfo.goldName + "testcase2.out";
149
150 buf.append("\nAxis is DESCENDANT-OR-SELF");
151 DTM dtm = QeDtmUtils.createDTM(0, QeDtmUtils.deepFile, buf);
152
153 // Get various nodes to use as context nodes.
154 int dtmRoot = dtm.getDocument(); // #document
155 String dtmRootName = dtm.getNodeName(dtmRoot); // Used for output
156 int DNode = dtm.getFirstChild(dtmRoot); // <Doc>
157 String DNodeName = dtm.getNodeName(DNode);
158 int[] rtData = {0,0,0}; // returns Iteration time, last node, number of nodes traversed
159
160 // Get a iterator for Descendant:: axis.
161 buf.append("\n\tSTARTING from: "+ DNodeName);
162 QeDtmUtils.timeAxisIterator(dtm, Axis.DESCENDANTORSELF, DNode, rtData);
163 buf.append("\n\tTime="+rtData[0] + " : " + "LastNode="+rtData[1]+" nodes="+rtData[2]);
164 lastNode = rtData[1];
165 lastNodeName = dtm.getNodeName(lastNode);
166
167 // Write results and close output file.
168 QeDtmUtils.writeClose(fos, buf, reporter);
169
170 // Verify results
171 fileChecker.check(reporter, new File(outNames.currentName()),
172 new File(gold),
173 "Testcase2");
174 reporter.testCaseClose();
175 return true;
176
177 }
178
testCase3()179 public boolean testCase3()
180 {
181 reporter.testCaseInit("Time Iteration of ANCESTOR:: axis");
182 StringBuffer buf = new StringBuffer();
183 FileOutputStream fos = QeDtmUtils.openFileStream(outNames.nextName(), reporter);
184 String gold = testFileInfo.goldName + "testcase3.out";
185
186 buf.append("\nAxis is ANCESTOR");
187 DTM dtm = QeDtmUtils.createDTM(0, QeDtmUtils.deepFile, buf);
188 buf.append("\n\tSTARTING from: "+ lastNodeName);
189 int[] rtData = {0,0,0}; // returns Iteration time, last node, number of nodes traversed
190
191 // Get a iterator for ANCESTOR:: axis.
192 QeDtmUtils.timeAxisIterator(dtm, Axis.ANCESTOR, lastNode, rtData);
193 buf.append("\n\tTime="+rtData[0] + " : " + "LastNode="+rtData[1]+" nodes="+rtData[2]);
194
195 // Write results and close output file.
196 QeDtmUtils.writeClose(fos, buf, reporter);
197
198 // Verify results
199 fileChecker.check(reporter, new File(outNames.currentName()),
200 new File(gold),
201 "Testcase3");
202 reporter.testCaseClose();
203 return true;
204
205 }
206
testCase4()207 public boolean testCase4()
208 {
209 reporter.testCaseInit("Time Iteration of ANCESTOR-or-Self:: axis");
210 StringBuffer buf = new StringBuffer();
211 FileOutputStream fos = QeDtmUtils.openFileStream(outNames.nextName(), reporter);
212 String gold = testFileInfo.goldName + "testcase4.out";
213
214 buf.append("\nAxis is ANCESTOR-or-Self");
215 DTM dtm = QeDtmUtils.createDTM(0, QeDtmUtils.deepFile, buf);
216 int[] rtData = {0,0,0}; // returns Iteration time, last node, number of nodes traversed
217
218 // Get a iterator for ANCESTORORSELF:: axis.
219 buf.append("\n\tSTARTING from: "+ lastNodeName);
220 QeDtmUtils.timeAxisIterator(dtm, Axis.ANCESTORORSELF, lastNode, rtData);
221 buf.append("\n\tTime="+rtData[0] + " : " + "LastNode="+rtData[1]+" nodes="+rtData[2]);
222
223 // Write results and close output file.
224 QeDtmUtils.writeClose(fos, buf, reporter);
225
226 // Verify results
227 fileChecker.check(reporter, new File(outNames.currentName()),
228 new File(gold),
229 "Testcase4");
230 reporter.testCaseClose();
231 return true;
232
233 }
234
235
236 /**
237 * Main method to run test from the command line - can be left alone.
238 * @param args command line argument array
239 */
main(String[] args)240 public static void main(String[] args)
241 {
242 TimeDTMIterDeep app = new TimeDTMIterDeep();
243 app.doMain(args);
244 }
245
246 }
247