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 22 /* 23 * 24 * DTMDumpTest.java 25 * 26 */ 27 package org.apache.qetest.xalanj2; 28 29 import java.io.File; 30 import java.util.Properties; 31 32 import javax.xml.transform.Templates; 33 import javax.xml.transform.Transformer; 34 import javax.xml.transform.TransformerFactory; 35 import javax.xml.transform.stream.StreamResult; 36 37 import org.apache.qetest.FileBasedTest; 38 import org.apache.qetest.Logger; 39 import org.apache.qetest.OutputNameManager; 40 import org.apache.qetest.xsl.TraxDatalet; 41 import org.apache.xalan.extensions.ExpressionContext; 42 import org.apache.xml.dtm.ref.DTMNodeProxy; 43 import org.apache.xpath.NodeSet; 44 import org.w3c.dom.Node; 45 import org.w3c.dom.traversal.NodeIterator; 46 47 //------------------------------------------------------------------------- 48 49 /** 50 * Simple unit test of various DTM and related apis. 51 * This class acts as it's own Xalan extension. 52 * @author shane_curcuru@lotus.com 53 * @version $Id$ 54 */ 55 public class DTMDumpTest extends FileBasedTest 56 { 57 58 /** Provides nextName(), currentName() functionality. */ 59 protected OutputNameManager outNames; 60 61 /** Simple test with dumpDTM extension calls in. */ 62 protected TraxDatalet testFileInfo = new TraxDatalet(); 63 64 /** Just initialize test name, comment, numTestCases. */ DTMDumpTest()65 public DTMDumpTest() 66 { 67 numTestCases = 2; // REPLACE_num 68 testName = "DTMDumpTest"; 69 testComment = "Simple unit test of various DTM and related apis"; 70 } 71 72 73 /** 74 * Initialize this test - create output dir, outNames. 75 * 76 * @param p Properties to initialize from (if needed) 77 * @return false if we should abort the test; true otherwise 78 */ doTestFileInit(Properties p)79 public boolean doTestFileInit(Properties p) 80 { 81 final String XALANJ2 = "xalanj2"; 82 File outSubDir = new File(outputDir + File.separator + XALANJ2); 83 if (!outSubDir.mkdirs()) 84 reporter.logWarningMsg("Could not create output dir: " + outSubDir); 85 // Initialize an output name manager to that dir with .out extension 86 outNames = new OutputNameManager(outputDir + File.separator + XALANJ2 87 + File.separator + testName, ".out"); 88 89 testFileInfo.setDescription("Simple transform with dumpDTM extension call"); 90 testFileInfo.setNames(inputDir + File.separator + XALANJ2, "DTMDumpTest"); 91 testFileInfo.goldName = goldDir + File.separator + XALANJ2 + File.separator + "DTMDumpTest.out"; 92 return true; 93 } 94 95 96 /** 97 * Simple dumping of DTM info from nodes. 98 * 99 * @return false if we should abort the test; true otherwise 100 */ testCase1()101 public boolean testCase1() 102 { 103 reporter.testCaseInit("Simple dumping of DTM info from nodes"); 104 105 TransformerFactory factory = null; 106 Templates templates = null; 107 Transformer transformer = null; 108 try 109 { 110 factory = TransformerFactory.newInstance(); 111 // Transform a file that calls us as an extension 112 templates = factory.newTemplates(testFileInfo.getXSLSource()); 113 transformer = templates.newTransformer(); 114 reporter.logInfoMsg("Before dtmBuf: " + dtmBuf.toString()); 115 reporter.logInfoMsg("About to create output: " + outNames.nextName()); 116 transformer.transform(testFileInfo.getXMLSource(), 117 new StreamResult(outNames.currentName())); 118 reporter.checkPass("Crash test only: returned from transform() call"); 119 reporter.logInfoMsg("After dtmBuf: " + dtmBuf.toString()); 120 121 } 122 catch (Throwable t) 123 { 124 reporter.logThrowable(Logger.ERRORMSG, t, "Simple DTM test threw:"); 125 reporter.checkFail("Simple DTM test threw:"); 126 } 127 128 reporter.testCaseClose(); 129 return true; 130 } 131 132 133 /** 134 * Validate transforms with FEATURE_INCREMENTAL on/off. 135 * 136 * @return false if we should abort the test; true otherwise 137 */ testCase2()138 public boolean testCase2() 139 { 140 reporter.testCaseInit("Unused"); 141 reporter.checkPass("Unused"); 142 reporter.testCaseClose(); 143 return true; 144 } 145 146 147 /** Cheap way to pass info from extension methods to test. */ 148 protected static StringBuffer dtmBuf = new StringBuffer(); 149 150 /** Cheap way to pass info from extension methods to test. */ 151 protected static final String DTMBUFSEP = ";"; 152 153 /** 154 * Implements a simple Xalan extension method. 155 * 156 * Just a way to implement an extension and the test that calls 157 * it together in the same class. Watch out for thread safety. 158 * @param ExpressionContext from the transformer 159 * @return String describing actions 160 */ dumpDTM(ExpressionContext context)161 public static String dumpDTM(ExpressionContext context) 162 { 163 Node contextNode = context.getContextNode(); 164 DTMNodeProxy proxy = (DTMNodeProxy)contextNode; 165 dtmBuf.append(XalanDumper.dump(proxy, XalanDumper.DUMP_DEFAULT) + DTMBUFSEP); 166 return XalanDumper.dump(proxy, XalanDumper.DUMP_NOIDS); 167 } 168 169 /** 170 * Implements a simple Xalan extension method. 171 * 172 * Just a way to implement an extension and the test that calls 173 * it together in the same class. Watch out for thread safety. 174 * @param context from the transformer 175 * @param obj object to test; presumably an RTF 176 * @return String describing actions 177 */ dumpDTM(ExpressionContext context, Object rtf)178 public static String dumpDTM(ExpressionContext context, Object rtf) 179 { 180 if (rtf instanceof NodeIterator) 181 { 182 NodeSet ns = new NodeSet((NodeIterator) rtf); 183 Node first = ns.nextNode(); 184 DTMNodeProxy proxy = (DTMNodeProxy)first; 185 dtmBuf.append("NI:" + XalanDumper.dump(proxy, XalanDumper.DUMP_DEFAULT) + DTMBUFSEP); 186 return XalanDumper.dump(proxy, XalanDumper.DUMP_NOIDS); 187 } 188 else if (rtf instanceof NodeSet) 189 { 190 NodeSet ns = (NodeSet)rtf; 191 Node first = ns.nextNode(); 192 DTMNodeProxy proxy = (DTMNodeProxy)first; 193 dtmBuf.append("NS:" + XalanDumper.dump(proxy, XalanDumper.DUMP_DEFAULT) + DTMBUFSEP); 194 return XalanDumper.dump(proxy, XalanDumper.DUMP_NOIDS); 195 } 196 else 197 { 198 dtmBuf.append("UK:" + rtf.toString() + DTMBUFSEP); 199 return "UK:" + rtf.toString(); 200 } 201 } 202 203 /** 204 * Convenience method to print out usage information - update if needed. 205 * @return String denoting usage of this test class 206 */ usage()207 public String usage() 208 { 209 return ("Common [optional] options supported by DTMDumpTest:\n" 210 + "(Note: assumes inputDir=.\\tests\\api)\n" 211 + super.usage()); // Grab our parent classes usage as well 212 } 213 214 215 /** 216 * Main method to run test from the command line - can be left alone. 217 * @param args command line argument array 218 */ main(String[] args)219 public static void main(String[] args) 220 { 221 DTMDumpTest app = new DTMDumpTest(); 222 app.doMain(args); 223 } 224 } 225