1 16 17 package org.apache.commons.latka; 18 19 import java.io.File ; 20 import java.io.FileInputStream ; 21 import java.io.FileWriter ; 22 import java.io.IOException ; 23 import java.io.StringReader ; 24 import java.io.StringWriter ; 25 import java.net.URL ; 26 import java.text.SimpleDateFormat ; 27 import java.util.Date ; 28 import java.util.Iterator ; 29 import java.util.Properties ; 30 31 import javax.xml.transform.Source ; 32 import javax.xml.transform.Transformer ; 33 import javax.xml.transform.TransformerException ; 34 import javax.xml.transform.TransformerFactory ; 35 import javax.xml.transform.stream.StreamResult ; 36 import javax.xml.transform.stream.StreamSource ; 37 38 import org.apache.commons.jelly.Jelly; 39 import org.apache.commons.jelly.JellyContext; 40 import org.apache.commons.jelly.XMLOutput; 41 42 import org.apache.commons.latka.event.LatkaEventInfo; 43 import org.apache.commons.latka.event.LatkaEventListener; 44 import org.apache.commons.latka.jelly.JellyUtils; 45 46 import org.apache.log4j.Category; 47 48 63 public class Latka { 64 65 66 protected boolean _isValidating = true; 67 68 protected URL _reportStylesheetUrl = null; 69 70 71 protected static final Category _log = 72 Category.getInstance(Latka.class.getName()); 73 74 75 protected Properties _props = LatkaProperties.getProperties(); 76 77 78 private static final String LATKA_USAGE = 79 "\n######################\n" 80 + "Latka usage:\n" 81 + " java org.apache.commons.latka.Latka \"XML_suite_URL\" " 82 + "[\"propfile:file_name\"] [\"prop:prop_name=prop_value\"]\n\n" 83 + "The quotes around properties are REQUIRED.\n" 84 + "######################\n"; 85 86 99 public void runTests(Suite suite, LatkaEventListener listener) 100 throws LatkaException { 101 102 try { 103 104 Jelly jelly = new Jelly(); 105 URL url = suite.getURL(); 106 jelly.setUrl(url); 107 jelly.setValidateXML(_isValidating); 108 jelly.setDefaultNamespaceURI("jelly:org.apache.commons.latka.jelly.LatkaTagLibrary"); 109 110 JellyContext context = buildJellyContext(); 111 JellyUtils.getInstance().setLatkaEventListener(context,listener); 112 113 String exturl = url.toExternalForm(); 114 int lastSlash = exturl.lastIndexOf("/"); 115 String extBase = exturl.substring(0,lastSlash+1); 116 URL baseurl = new URL (extBase); 117 context.setCurrentURL(baseurl); 118 119 jelly.compileScript().run(context,XMLOutput.createDummyXMLOutput()); 121 122 } catch (Exception e) { 123 throw new LatkaException(e); 124 } 125 } 126 127 protected JellyContext buildJellyContext() { 128 JellyContext context = new JellyContext(); 129 Iterator keys = _props.keySet().iterator(); 130 while (keys.hasNext()) { 131 String key = (String ) keys.next(); 132 String value = _props.getProperty(key); 133 context.setVariable(key,value); 134 } 135 136 return context; 137 } 138 139 145 public void setValidating(boolean isValidating) { 146 _isValidating = isValidating; 147 } 148 149 153 public void setReportStylesheet(URL url) { 154 _reportStylesheetUrl = url; 155 } 156 157 164 protected void logXML(String xml) throws IOException { 165 166 String logProp = _props.getProperty("latka.writeLog"); 167 if ((logProp != null) && (logProp.equals("false"))) { 168 return; 169 } 170 171 File dir = new File ("logs"); 172 if (!dir.mkdirs()) { 175 if (!dir.exists ()) { 176 throw new IOException ("Unable to create logs directory"); 177 } else if (!dir.canWrite ()) { 178 throw new IOException ("Unable to write to logs directory"); 179 } 180 } 181 SimpleDateFormat formatter = new SimpleDateFormat ("yyyyMMdd'-'HHmm"); 182 String dateString = formatter.format(new Date ()); 183 File file = new File (dir, dateString + ".xml"); 184 FileWriter fileWriter = new FileWriter (file); 185 fileWriter.write(xml); 186 fileWriter.close(); 187 } 188 189 197 public String transformXML(String xml) throws LatkaException { 198 199 try { 200 StringWriter output = new StringWriter (); 201 202 Source xslSource = null; 203 204 if (_reportStylesheetUrl == null) { 205 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 206 if (loader == null) { 207 loader = getClass().getClassLoader(); 209 } 210 211 xslSource = new StreamSource ( 212 loader.getResourceAsStream( 213 "org.apache.commons.latka.report.xsl") 214 ); 215 } else { 216 xslSource = new StreamSource (_reportStylesheetUrl.toString()); 217 } 218 219 220 Transformer transformer = 221 TransformerFactory.newInstance().newTransformer(xslSource); 222 StreamSource xmlSource = new StreamSource (new StringReader (xml)); 223 StreamResult result = new StreamResult (output); 224 transformer.transform(xmlSource, result); 225 return output.toString(); 226 } catch (TransformerException e) { 227 throw new LatkaException(e); 228 } 229 } 230 231 241 protected void runCommandLine(String args[]) throws LatkaException { 242 243 if (args.length < 1) { 244 System.out.println(LATKA_USAGE); 245 } 246 247 String urlString = args[0]; 248 249 if (args.length > 1) { 250 251 for (int i = 1; i < args.length; ++i) { 252 String arg = args[i]; 253 if (arg.startsWith("prop:")) { 254 String propName = arg.substring(5, arg.indexOf("=")); 255 String propValue = arg.substring(arg.indexOf("=") + 1); 256 _props.setProperty(propName, propValue); 257 } else if (arg.startsWith("propfile:")) { 258 try { 259 _props.load(new FileInputStream (arg.substring(9))); 260 } catch (IOException e) { 261 System.out.println("Could not load user prop file, uri=" 262 + arg.substring(9)); 263 } 264 } else { 265 throw new IllegalArgumentException (LATKA_USAGE); 266 } 267 } } 270 String xml = null; 271 XMLReporter listener = new XMLReporter(); 272 273 LatkaEventInfo info = new DefaultLatkaEventInfo(listener); 275 276 try { 277 278 URL url = new URL (urlString); 279 Suite suite = new Suite(url); 280 281 282 runTests(suite, info); 283 284 xml = listener.getDocumentAsString(); 285 logXML(xml); 286 } catch (IOException e) { 287 throw new LatkaException(e); 288 } 289 290 System.out.println(transformXML(xml)); 291 292 if (info.didSuiteSucceed() == false) { 293 throw new LatkaException("SUITE FAILED"); 296 } 297 } 298 299 310 public static void main (String args[]) { 311 312 Latka latka = new Latka(); 313 try { 314 latka.runCommandLine(args); 315 } catch (LatkaException e) { 316 e.printStackTrace(); 317 System.exit(1); 318 } 319 320 } 321 } 322 | Popular Tags |