1 16 17 package org.apache.commons.latka.junit; 18 19 import java.io.File ; 21 import java.io.IOException ; 22 import java.net.MalformedURLException ; 23 import java.net.URL ; 24 import javax.xml.parsers.FactoryConfigurationError ; 26 import javax.xml.parsers.ParserConfigurationException ; 27 import javax.xml.parsers.SAXParser ; 28 import javax.xml.parsers.SAXParserFactory ; 29 import org.apache.commons.latka.DefaultLatkaEventInfo; 31 import org.apache.commons.latka.Latka; 32 import org.apache.commons.latka.LatkaException; 33 import org.apache.commons.latka.Suite; 34 import org.apache.log4j.Category; 36 import org.xml.sax.Attributes ; 38 import org.xml.sax.InputSource ; 39 import org.xml.sax.SAXException ; 40 import org.xml.sax.XMLReader ; 41 import org.xml.sax.helpers.DefaultHandler ; 42 import junit.framework.Test; 44 import junit.framework.TestResult; 45 46 54 public class JUnitTestAdapter implements Test { 55 56 private static final Category _log = Category.getInstance( 57 JUnitTestAdapter.class); 58 59 60 private Suite _latkaSuite = null; 61 62 private int _testCount = 0; 63 64 71 protected JUnitTestAdapter(Suite suite, int testCount) { 72 _latkaSuite = suite; 73 _testCount = testCount; 74 } 75 76 81 public static Test createTestFromFile(String fileName) { 82 Test result = null; 83 File file = new File (fileName); 84 if (file.exists()) { 85 result = createTestFromFile(file); 86 } else { 87 _log.debug("Input file " + file.getAbsolutePath() 88 + " does not exist"); 89 } 90 return result; 91 } 92 93 98 public static Test createTestFromFile(File file) { 99 Test result = null; 100 try { 101 result = createTestFromURL(file.toURL()); 102 } catch (MalformedURLException e) { 103 _log.debug("Could not access input file", e); 104 } 105 return result; 106 } 107 108 116 public static Test createTestFromResource(String resourceName) { 117 Test result = null; 118 ClassLoader loader = JUnitTestAdapter.class.getClassLoader(); 119 URL resource = loader.getResource(resourceName); 120 if (resource != null) { 121 result = createTestFromURL(resource); 122 } 123 return result; 124 } 125 126 132 public static Test createTestFromURL(String url) { 133 Test result = null; 134 try { 135 result = createTestFromURL(new URL (url)); 136 } catch (MalformedURLException e) { 137 _log.debug("Unable to create URL " + url, e); 138 } 139 return result; 140 } 141 142 147 public static Test createTestFromURL(URL url) { 148 Test result = null; 149 try { 150 InputSource source = new InputSource (url.toString()); 151 Suite suite = new Suite(url); 152 result = new JUnitTestAdapter(suite, parse(source)); 153 } catch (IOException ioe) { 154 _log.debug("IOException obtaining xml from URL " + url, ioe); 155 } catch (SAXException se) { 156 _log.debug("Problem parsing URL " + url, se); 157 } catch (ParserConfigurationException pce) { 158 _log.debug("Problem determining parser", pce); 159 } 160 return result; 161 } 162 163 174 protected static int parse(InputSource xml) throws IOException , SAXException 175 , FactoryConfigurationError , ParserConfigurationException { 176 int result = 0; 177 XMLReader reader = null; 178 SAXParserFactory factory = SAXParserFactory.newInstance(); 179 factory.setValidating(false); 180 SAXParser parser = factory.newSAXParser(); 181 reader = parser.getXMLReader(); 182 TestCounter handler = new TestCounter(); 183 reader.setContentHandler(handler); 184 reader.parse(xml); 185 result = handler.getCount(); 186 return result; 187 } 188 189 197 private static class TestCounter extends DefaultHandler { 198 199 private int _count = 0; 200 201 204 public TestCounter() { 205 _count = 0; 206 } 207 208 215 public void startElement(String uri, String localName, String qName, 216 Attributes atts) { 217 if (qName.equals("request")) { 218 _count++; 219 } 220 } 221 222 226 public int getCount() { 227 return _count; 228 } 229 } 230 231 236 public int countTestCases() { 237 return _testCount; 238 } 239 240 246 public void run(TestResult r) { 247 _log.debug("Attempting to perform latka tests"); 248 Latka latka = new Latka(); 249 try { 250 latka.runTests(_latkaSuite, new DefaultLatkaEventInfo(new JUnitEventReporter(r))); 251 } catch (LatkaException e) { 252 _log.error("Unable to execute latka tests", e); 253 } 254 } 255 } | Popular Tags |