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 import org.apache.qetest.CheckService; 23 import org.apache.qetest.xsl.XHTFileCheckService; 24 25 import org.apache.xalan.processor.TransformerFactoryImpl; 26 import org.apache.xalan.transformer.TransformerImpl; 27 import org.apache.xalan.transformer.XalanProperties; 28 import javax.xml.transform.*; 29 import javax.xml.transform.stream.*; 30 31 import java.io.File; 32 import java.io.FileReader; 33 import java.io.BufferedReader; 34 import java.io.IOException; 35 36 /** 37 * Custom Testlet for testing NodeInfo extension. 38 * 39 * @author [email protected] 40 */ 41 public class BugzillaNodeInfo extends TestletImpl 42 { 43 // Initialize our classname for TestletImpl's main() method - must be updated! 44 static { thisClassName = "BugzillaNodeInfo"; } 45 46 /** 47 * Write Minimal code to reproduce your Bugzilla bug report. 48 * Many Bugzilla tests won't bother with a datalet; they'll 49 * just have the data to reproduce the bug encoded by default. 50 * @param d (optional) Datalet to use as data point for the test. 51 */ execute(Datalet d)52 public void execute(Datalet d) 53 { 54 // Use logger.logMsg(...) instead of System.out.println(...) 55 logger.logMsg(Logger.STATUSMSG, getDescription()); 56 57 String xslName = "BugzillaNodeInfo.xsl"; 58 String xmlName = "BugzillaNodeInfo.xml"; 59 String outputName = "BugzillaNodeInfo.output"; 60 String goldName = "BugzillaNodeInfo.gold"; 61 try 62 { 63 TransformerFactory factory = TransformerFactory.newInstance(); 64 // Must set on both the factory impl.. 65 TransformerFactoryImpl factoryImpl = (TransformerFactoryImpl)factory; 66 factoryImpl.setAttribute(XalanProperties.SOURCE_LOCATION, Boolean.TRUE); 67 68 Transformer transformer = factory.newTransformer(new StreamSource(xslName)); 69 // ..and the transformer impl 70 TransformerImpl impl = ((TransformerImpl) transformer); 71 impl.setProperty(XalanProperties.SOURCE_LOCATION, Boolean.TRUE); 72 transformer.transform(new StreamSource(xmlName), 73 new StreamResult(outputName)); 74 } 75 catch (Throwable t) 76 { 77 logger.logThrowable(logger.ERRORMSG, t, "Unexpectedly threw"); 78 logger.checkErr("Unexpectedly threw: " + t.toString()); 79 return; 80 } 81 82 // CheckService fileChecker = new XHTFileCheckService(); 83 // fileChecker.check(logger, 84 // new File(outputName), 85 // new File(goldName), 86 // getDescription()); 87 // Since the gold data isn't nailed down yet, do a simple validation 88 // Just ensure the systemId of the xml file was included somewhere in the file 89 checkFileContains(outputName, xmlName, "NodeInfo got partial systemID correct"); 90 } 91 92 93 /** 94 * Checks and reports if a file contains a certain string 95 * (all within one line). 96 * 97 * @param fName local path/name of file to check 98 * @param checkStr String to look for in the file 99 * @param comment to log with the check() call 100 * @return true if pass, false otherwise 101 */ checkFileContains(String fName, String checkStr, String comment)102 protected boolean checkFileContains(String fName, String checkStr, 103 String comment) 104 { 105 boolean passFail = false; 106 File f = new File(fName); 107 108 if (!f.exists()) 109 { 110 logger.checkFail("checkFileContains(" + fName 111 + ") does not exist: " + comment); 112 return false; 113 } 114 115 try 116 { 117 FileReader fr = new FileReader(f); 118 BufferedReader br = new BufferedReader(fr); 119 120 for (;;) 121 { 122 String inbuf = br.readLine(); 123 if (inbuf == null) 124 break; 125 126 if (inbuf.indexOf(checkStr) >= 0) 127 { 128 passFail = true; 129 logger.logMsg(logger.TRACEMSG, 130 "checkFileContains passes with line: " + inbuf); 131 break; 132 } 133 } 134 } 135 catch (IOException ioe) 136 { 137 logger.checkFail("checkFileContains(" + fName + ") threw: " 138 + ioe.toString() + " for: " + comment); 139 140 return false; 141 } 142 143 if (passFail) 144 { 145 logger.checkPass(comment); 146 } 147 else 148 { 149 logger.logMsg(logger.ERRORMSG, "checkFileContains failed to find: " + checkStr); 150 logger.checkFail(comment); 151 } 152 return passFail; 153 } 154 155 156 /** 157 * @return Xalan custom extension NodeInfo. 158 */ getDescription()159 public String getDescription() 160 { 161 return "Xalan custom extension NodeInfo"; 162 } 163 164 } // end of class BugzillaNodeInfo 165 166