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