1 17 18 package org.apache.jmeter.functions; 19 import java.io.FileInputStream ; 20 import java.io.FileNotFoundException ; 21 import java.io.IOException ; 22 import java.io.InputStream ; 23 24 import javax.xml.parsers.DocumentBuilder ; 25 import javax.xml.parsers.DocumentBuilderFactory ; 26 import javax.xml.parsers.ParserConfigurationException ; 27 import javax.xml.transform.TransformerException ; 28 29 import org.apache.jmeter.junit.JMeterTestCase; 30 import org.apache.jorphan.logging.LoggingManager; 31 import org.apache.log.Logger; 32 import org.apache.xpath.XPathAPI; 33 import org.w3c.dom.NodeList ; 34 import org.xml.sax.SAXException ; 35 36 41 public class XPathFileContainer 42 { 43 44 transient private static Logger log = LoggingManager.getLoggerForClass(); 45 46 47 private NodeList nodeList; 48 49 private String fileName; private String xpath; 51 52 53 private int nextRow; 54 55 private XPathFileContainer() { 57 } 58 59 public XPathFileContainer(String file,String xpath) 60 throws FileNotFoundException , IOException , ParserConfigurationException , SAXException , TransformerException 61 { 62 log.debug("XPath("+file+") xpath "+xpath+""); 63 fileName = file; 64 this.xpath = xpath; 65 nextRow = 0; 66 load(); 67 } 68 69 private void load() 70 throws IOException ,FileNotFoundException , ParserConfigurationException , SAXException , TransformerException 71 { 72 InputStream fis=null; 73 try 74 { 75 DocumentBuilder 76 builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 77 78 fis = new FileInputStream (fileName); 79 nodeList = XPathAPI.selectNodeList(builder.parse(fis), xpath); 80 log.debug("found "+nodeList.getLength()); 81 82 } 83 catch (FileNotFoundException e) 84 { 85 nodeList = null; 86 log.warn(e.toString()); 87 throw e; 88 } 89 catch (IOException e) 90 { 91 nodeList = null; 92 log.warn(e.toString()); 93 throw e; 94 } catch (ParserConfigurationException e) { 95 nodeList = null; 96 log.warn(e.toString()); 97 throw e; 98 } catch (SAXException e) { 99 nodeList = null; 100 log.warn(e.toString()); 101 throw e; 102 } catch (TransformerException e) { 103 nodeList = null; 104 log.warn(e.toString()); 105 throw e; 106 } 107 finally 108 { 109 if (fis != null) fis.close(); 110 } 111 } 112 113 public String getXPathString(int num) { 114 return nodeList.item(num).getNodeValue(); 115 } 116 117 124 public int nextRow() 125 { 126 int row = nextRow; 127 nextRow++; 128 if (nextRow >= size()) { 130 nextRow = 0; 131 } 132 log.debug (new StringBuffer ("Row: ").append(row).toString()); 133 return row; 134 } 135 136 public int size() { 137 return (nodeList == null ) ? -1 : nodeList.getLength(); 138 } 139 140 public static class Test extends JMeterTestCase 141 { 142 143 static{ 144 } 147 148 149 public Test(String a) 150 { 151 super(a); 152 } 153 154 public void testNull() throws Exception 155 { 156 try 157 { 158 new XPathFileContainer("nosuch.xml","/"); 159 fail("Should not find the file"); 160 } 161 catch (FileNotFoundException e) 162 { 163 } 164 } 165 166 public void testrowNum() throws Exception 167 { 168 XPathFileContainer f = new XPathFileContainer("../build.xml", "/project/target/@name"); 169 assertNotNull(f); 170 172 int myRow=f.nextRow(); 173 assertEquals(0,myRow); 174 assertEquals(1,f.nextRow); 175 176 myRow = f.nextRow(); 177 assertEquals(1,myRow); 178 assertEquals(2,f.nextRow); 179 180 myRow = f.nextRow(); 181 assertEquals(2,myRow); 182 assertEquals(3,f.nextRow); 183 184 185 189 193 } 194 195 public void testColumns() throws Exception 196 { 197 XPathFileContainer f = new XPathFileContainer("../build.xml","/project/target/@name"); 198 assertNotNull(f); 199 assertTrue("Not empty",f.size() > 0); 200 int last = 0; 201 for (int i=0; i< f.size(); i++) { 202 last = f.nextRow(); 203 log.debug("found ["+i+"]"+f.getXPathString(last)); 204 } 205 assertEquals(last+1, f.size()); 206 207 } 208 public void testDefault() throws Exception 209 { 210 XPathFileContainer f = new XPathFileContainer("../build.xml","/project/@default"); 211 assertNotNull(f); 212 assertTrue("Not empty",f.size() > 0); 213 assertEquals("all", f.getXPathString(0)); 214 215 } 216 217 } 218 221 public String getFileName() 222 { 223 return fileName; 224 } 225 226 } | Popular Tags |