1 17 18 package org.apache.jmeter.assertions.gui; 19 20 import java.awt.LayoutManager ; 21 import java.awt.event.ActionEvent ; 22 import java.awt.event.ActionListener ; 23 24 import javax.swing.Box ; 25 import javax.swing.JButton ; 26 import javax.swing.JCheckBox ; 27 import javax.swing.JFrame ; 28 import javax.swing.JOptionPane ; 29 import javax.swing.JPanel ; 30 import javax.swing.JTextField ; 31 import javax.xml.parsers.ParserConfigurationException ; 32 import javax.xml.transform.TransformerException ; 33 34 import org.apache.commons.logging.Log; 35 import org.apache.commons.logging.LogFactory; 36 import org.apache.jmeter.util.JMeterUtils; 37 import org.apache.jmeter.util.XPathUtil; 38 import org.apache.xpath.XPathAPI; 39 import org.w3c.dom.Document ; 40 import org.w3c.dom.Element ; 41 import org.xml.sax.SAXException ; 42 43 47 public class XPathPanel extends JPanel { 48 private static Document testDoc = null; 49 private final static Log log = LogFactory.getLog(XPathPanel.class); 50 private JCheckBox negated; 51 private JTextField xpath; 52 private JButton checkXPath; 53 54 57 public XPathPanel() { 58 super(); 59 init(); 60 } 61 62 65 public XPathPanel(boolean isDoubleBuffered) { 66 super( isDoubleBuffered); 67 } 68 69 72 public XPathPanel(LayoutManager layout) { 73 super(layout); 74 } 75 79 public XPathPanel(LayoutManager layout, boolean isDoubleBuffered) { 80 super(layout, isDoubleBuffered); 81 82 } 83 private void init() { 84 Box hbox = Box.createHorizontalBox(); 85 hbox.add(Box.createHorizontalGlue()); 86 hbox.add(getXPathTextField()); 87 hbox.add(Box.createHorizontalGlue()); 88 hbox.add(getCheckXPathButton()); 89 90 Box vbox = Box.createVerticalBox(); 91 vbox.add(hbox); 92 vbox.add(Box.createVerticalGlue()); 93 vbox.add(getNegatedCheckBox()); 94 95 add(vbox); 96 } 97 101 public String getXPath() { 102 return this.xpath.getText(); 103 } 104 108 public void setXPath(String xpath) { 109 this.xpath.setText(xpath); 110 } 111 115 public boolean isNegated() { 116 return this.negated.isSelected(); 117 } 118 123 public void setNegated(boolean negated) { 124 this.negated.setSelected(negated); 125 } 126 130 public JCheckBox getNegatedCheckBox() { 131 if (negated == null ) { 132 negated = new JCheckBox (JMeterUtils.getResString("xpath_assertion_negate"), false); 133 } 134 135 return negated; 136 } 137 141 public JButton getCheckXPathButton() { 142 if (checkXPath == null) { 143 checkXPath = new JButton (JMeterUtils.getResString("xpath_assertion_button")); 144 checkXPath.addActionListener(new ActionListener () { 145 public void actionPerformed(ActionEvent e) { 146 validXPath(xpath.getText(), true); 147 }}); 148 } 149 return checkXPath; 150 } 151 public JTextField getXPathTextField() { 152 if (xpath == null ) { 153 xpath = new JTextField (50); 154 xpath.setText("/"); 155 } 156 return xpath; 157 } 158 161 public boolean isShowNegated() { 162 return this.getNegatedCheckBox().isVisible(); 163 } 164 167 public void setShowNegated(boolean showNegate) { 168 getNegatedCheckBox().setVisible(showNegate); 169 } 170 171 180 public static boolean validXPath(String xpathString, boolean showDialog) { 181 String ret = null; 182 boolean success= true; 183 try { 184 if (testDoc == null){ 185 testDoc = XPathUtil.makeDocumentBuilder(false,false,false).newDocument(); 186 Element el = testDoc.createElement("root"); 187 testDoc.appendChild(el); 188 189 } 190 if(XPathAPI.eval(testDoc, xpathString) == null){ 191 log.warn("xpath eval was null "); 196 ret ="xpath eval was null"; 197 success = false; 198 } 199 200 } catch (ParserConfigurationException e) { 201 success = false; 202 ret = e.getLocalizedMessage(); 203 } catch (TransformerException e) { 204 success = false; 205 ret =e.getLocalizedMessage(); 206 207 } catch (SAXException e) { 208 success = false; 209 ret =e.getLocalizedMessage(); 210 } 211 212 if(showDialog){ 213 JOptionPane.showMessageDialog( 214 null, 215 (success) ? JMeterUtils.getResString("xpath_assertion_valid") : ret, 216 (success) ? JMeterUtils.getResString("xpath_assertion_valid") 217 : JMeterUtils.getResString("xpath_assertion_failed"), 218 (success) ? JOptionPane.INFORMATION_MESSAGE : JOptionPane.ERROR_MESSAGE ); 219 } 220 return success; 221 222 } 223 229 public static void main (String [] args) { 230 JMeterUtils.setJMeterHome("/eclipse/workspace/jakarta-jmeter/bin"); 231 JFrame frame = new JFrame (); 232 frame.add(new XPathPanel()); 233 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 234 frame.pack(); 235 frame.setVisible(true); 236 } 237 } | Popular Tags |