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 // Common Qetest / Xalan testing imports 19 import org.apache.qetest.Datalet; 20 import org.apache.qetest.Logger; 21 import org.apache.qetest.TestletImpl; 22 23 import java.io.BufferedReader; 24 import java.io.File; 25 import java.io.InputStreamReader; 26 import java.io.IOException; 27 import java.util.Date; 28 import java.util.HashSet; 29 import java.util.Properties; 30 31 import javax.xml.transform.*; 32 import javax.xml.transform.stream.*; 33 34 import org.w3c.dom.Node; 35 import org.w3c.dom.NodeList; 36 import org.w3c.dom.DocumentFragment; 37 import org.w3c.dom.traversal.NodeIterator; 38 39 import org.apache.xalan.extensions.XSLProcessorContext; 40 import org.apache.xalan.templates.ElemExtensionCall; 41 42 /** 43 * Testlet for reproducing Bugzilla reported bugs. 44 * @author [email protected] 45 * @author [email protected] 46 */ 47 public class Bugzilla6181 extends TestletImpl 48 { 49 50 // Initialize our classname for TestletImpl's main() method - must be updated! 51 static { thisClassName = "Bugzilla6181"; } 52 53 /** Cheap-o validation for extension call */ 54 static int extCounter = 0; 55 56 /** 57 * Write Minimal code to reproduce your Bugzilla bug report. 58 * Many Bugzilla tests won't bother with a datalet; they'll 59 * just have the data to reproduce the bug encoded by default. 60 * @param d (optional) Datalet to use as data point for the test. 61 */ execute(Datalet d)62 public void execute(Datalet d) 63 { 64 // Use logger.logMsg(...) instead of System.out.println(...) 65 logger.logMsg(Logger.STATUSMSG, "Reproducing Bugzilla#6181"); 66 67 logger.logMsg(logger.STATUSMSG, "extCounter(before) = " + extCounter); 68 try 69 { 70 TransformerFactory factory = TransformerFactory.newInstance(); 71 72 // Simply transform our stylesheet.. 73 Transformer transformer = factory.newTransformer(new StreamSource("Bugzilla6181.xsl")); 74 transformer.transform(new StreamSource("Bugzilla6181.xml"), 75 new StreamResult("Bugzilla6181.output")); 76 logger.checkPass("Crash test: produced unvalidated output into: Bugzilla6181.output"); 77 } 78 catch (Throwable t) 79 { 80 logger.logThrowable(logger.ERRORMSG, t, "Unexpected exception"); 81 logger.checkErr("Unexpected exception: " + t.toString()); 82 } 83 // Then see how many times we've been called 84 logger.logMsg(logger.STATUSMSG, "extCounter(after) = " + extCounter); 85 86 } 87 88 /** 89 * <a href="http://nagoya.apache.org/bugzilla/show_bug.cgi?id=6181"> 90 * Link to Bugzilla report</a> 91 * @return Problems with value-of and extension function. 92 */ getDescription()93 public String getDescription() 94 { 95 return "Problems with value-of and extension function"; 96 } 97 makeString( NodeIterator it )98 private String makeString( NodeIterator it ) { 99 String source = null; 100 StringBuffer sourceBuf = new StringBuffer(); 101 Node n; 102 while((n = it.nextNode()) != null) { 103 sourceBuf.append( n.getNodeValue() ); 104 } 105 return sourceBuf.toString(); 106 } 107 108 // initcap 109 // Handles function nn:initcap(<XPath-Expr>) 110 // initcap( NodeIterator it )111 public String initcap( NodeIterator it ) { 112 String source = makeString( it ); 113 extCounter++; 114 logger.logMsg(logger.INFOMSG, "initcap(ni) called with: " + source); 115 // Now do the initcap thing and return it... 116 if( source.length() > 1 ) { 117 return source.substring(0,1).toUpperCase() + source.substring( 1 ); 118 } 119 return ""; 120 } 121 makeString( DocumentFragment fragment )122 private String makeString( DocumentFragment fragment ) { 123 NodeList nodes = fragment.getChildNodes(); 124 StringBuffer sourceBuf = new StringBuffer(); 125 for( int i = 0; i < nodes.getLength(); ++i ) { 126 String s = nodes.item(i).getNodeValue(); 127 if( s != null ) sourceBuf.append( s ); 128 } 129 return sourceBuf.toString(); 130 } 131 132 // initcap 133 // Handles function nn:initcap(<XPath-Expr>) 134 // initcap( DocumentFragment fragment )135 public String initcap( DocumentFragment fragment ) { 136 try { 137 String source = makeString( fragment ); 138 extCounter++; 139 logger.logMsg(logger.INFOMSG, "initcap(f) called with: " + source); 140 // Now do the initcap thing and return it... 141 if( source.length() > 1 ) { 142 return source.substring(0,1).toUpperCase() + source.substring( 1 ); 143 } 144 } catch( Exception e ) { 145 e.printStackTrace(); 146 } 147 return ""; 148 } 149 150 } 151