You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jmeter-dev@jakarta.apache.org by se...@apache.org on 2005/03/06 13:45:27 UTC
cvs commit: jakarta-jmeter/src/components/org/apache/jmeter/assertions XPathAssertion.java
sebb 2005/03/06 04:45:27
Modified: src/components/org/apache/jmeter/assertions/gui Tag: rel-2_0
XPathAssertionGui.java
xdocs/images/screenshots Tag: rel-2_0 xpath_assertion.png
src/core/org/apache/jmeter/resources Tag: rel-2_0
messages.properties
xdocs/usermanual Tag: rel-2_0 component_reference.xml
src/components/org/apache/jmeter/assertions Tag: rel-2_0
XPathAssertion.java
Added: src/components/org/apache/jmeter/assertions/gui Tag: rel-2_0
XPathPanel.java XMLConfPanel.java
src/core/org/apache/jmeter/util Tag: rel-2_0 XPathUtil.java
Log:
Bug 32976 - XPath assertion. Changed to refer to "tolerant" XML processing rather than specifically JTidy.
Revision Changes Path
No revision
No revision
1.1.2.3 +87 -212 jakarta-jmeter/src/components/org/apache/jmeter/assertions/gui/Attic/XPathAssertionGui.java
Index: XPathAssertionGui.java
===================================================================
RCS file: /home/cvs/jakarta-jmeter/src/components/org/apache/jmeter/assertions/gui/Attic/XPathAssertionGui.java,v
retrieving revision 1.1.2.2
retrieving revision 1.1.2.3
diff -u -r1.1.2.2 -r1.1.2.3
--- XPathAssertionGui.java 22 Feb 2005 14:26:32 -0000 1.1.2.2
+++ XPathAssertionGui.java 6 Mar 2005 12:45:27 -0000 1.1.2.3
@@ -18,20 +18,9 @@
package org.apache.jmeter.assertions.gui;
import java.awt.BorderLayout;
-import java.awt.event.ActionEvent;
-import java.awt.event.ActionListener;
-import java.awt.event.FocusEvent;
-import java.awt.event.FocusListener;
import javax.swing.BorderFactory;
-import javax.swing.JButton;
-import javax.swing.JCheckBox;
-import javax.swing.JOptionPane;
import javax.swing.JPanel;
-import javax.swing.JTextField;
-import javax.xml.parsers.DocumentBuilderFactory;
-import javax.xml.parsers.ParserConfigurationException;
-import javax.xml.transform.TransformerException;
import org.apache.jmeter.assertions.XPathAssertion;
import org.apache.jmeter.testelement.TestElement;
@@ -40,9 +29,6 @@
import org.apache.jorphan.logging.LoggingManager;
import org.apache.log.Logger;
-import org.apache.xpath.XPathAPI;
-import org.w3c.dom.Document;
-import org.w3c.dom.Element;
/**
*
@@ -50,213 +36,102 @@
*
*/
-public class XPathAssertionGui extends AbstractAssertionGui implements
- FocusListener, ActionListener {
-
- private static transient Logger log = LoggingManager.getLoggerForClass();
-
- private JTextField xpath;
+public class XPathAssertionGui extends AbstractAssertionGui
+{
- private JCheckBox validation, whitespace, tidy, negated;
-
- private JButton checkXPath;
-
- private static Document testDoc = null; // Used to validate XPath expressions
+ private static transient Logger log = LoggingManager.getLoggerForClass();
+ private static final String OPERATOR_KEY = null;
- /**
- * The constructor.
- */
+ private int execState;
+ private XPathPanel xpath;
+ private XMLConfPanel xml;
+
public XPathAssertionGui() {
- init();
- }
-
- /**
- * Returns the label to be shown within the JTree-Component.
- */
- public String getLabelResource() {
- return "xpath_assertion_title";
- }
-
- /**
- * Create test element
- */
- public TestElement createTestElement() {
-
- XPathAssertion el = new XPathAssertion();
- modifyTestElement(el);
- return el;
- }
-
- public String getXPathAttributesTitle() {
- return JMeterUtils.getResString("xpath_assertion_test");
- }
-
- public void focusGained(FocusEvent e) {
- log.debug("XPathAssertionGui.focusGained() called");
- }
-
- public void focusLost(FocusEvent e) {
-
- }
-
- public void configure(TestElement el) {
- super.configure(el);
- XPathAssertion assertion = (XPathAssertion) el;
- xpath.setText(assertion.getXPathString());
- whitespace.setSelected(assertion.isWhitespace());
- validation.setSelected(assertion.isValidating());
- negated.setSelected(assertion.isNegated());
- tidy.setSelected(assertion.isJTidy());
- tidySelected();
- }
+ init();
+ }
- private void init() {
- setLayout(new VerticalLayout(5, VerticalLayout.LEFT, VerticalLayout.TOP));
- setBorder(makeBorder());
-
- add(makeTitlePanel());
-
- // USER_INPUT
- JPanel sizePanel = new JPanel(new BorderLayout());
- sizePanel.setBorder(BorderFactory.createEmptyBorder(0, 10, 10, 10));
- sizePanel.setBorder(BorderFactory.createTitledBorder(BorderFactory
- .createEtchedBorder(), getXPathAttributesTitle()));
-
- negated = new JCheckBox(JMeterUtils
- .getResString("xpath_assertion_negate"), false);
- xpath = new JTextField(50);
- xpath.setText(XPathAssertion.DEFAULT_XPATH);
- checkXPath = new JButton(JMeterUtils
- .getResString("xpath_assertion_button"));
- checkXPath.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- validXPath(xpath.getText());
- }
- });
-
- xpath.addFocusListener(this);
- sizePanel.add(xpath, BorderLayout.WEST);
- sizePanel.add(checkXPath, BorderLayout.EAST);
- sizePanel.add(negated, BorderLayout.SOUTH);// JDK1.4 PAGE_END
-
- JPanel optionPanel = new JPanel();
- optionPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory
- .createEtchedBorder(), JMeterUtils
- .getResString("xpath_assertion_option")));
-
- validation = new JCheckBox(JMeterUtils
- .getResString("xpath_assertion_validation"), false);
- whitespace = new JCheckBox(JMeterUtils
- .getResString("xpath_assertion_whitespace"), false);
- tidy = new JCheckBox(JMeterUtils.getResString("xpath_assertion_tidy"),
- false);
- tidy.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- tidySelected();
- }
- });
-
- optionPanel.add(validation);
- optionPanel.add(whitespace);
- optionPanel.add(tidy);
-
- add(sizePanel);
- add(optionPanel);
- }
-
- public void actionPerformed(ActionEvent e) {
- // Actions are handled by individual listeners
+ /**
+ * Returns the label to be shown within the JTree-Component.
+ */
+ public String getLabelResource()
+ {
+ return "xpath_assertion_title";
+ }
+
+ /**
+ * Create test element
+ */
+ public TestElement createTestElement()
+ {
+ XPathAssertion el = new XPathAssertion();
+ modifyTestElement(el);
+ return el;
+ }
+
+ public String getXPathAttributesTitle()
+ {
+ return JMeterUtils.getResString("xpath_assertion_test");
+ }
+
+ public void configure(TestElement el)
+ {
+ super.configure(el);
+ XPathAssertion assertion = (XPathAssertion) el;
+ xpath.setXPath(assertion.getXPathString());
+ xpath.setNegated(assertion.isNegated());
+
+ xml.setWhitespace(assertion.isWhitespace());
+ xml.setValidate(assertion.isValidating());
+ xml.setTolerant(assertion.isTolerant());
+ xml.setNamespace(assertion.isNamespace());
+
+ }
+
+ private void init()
+ {
+ setLayout(
+ new VerticalLayout(5, VerticalLayout.LEFT, VerticalLayout.TOP));
+ setBorder(makeBorder());
+
+ add(makeTitlePanel());
+
+ // USER_INPUT
+ JPanel sizePanel = new JPanel(new BorderLayout());
+ sizePanel.setBorder(BorderFactory.createEmptyBorder(0, 10, 10, 10));
+ sizePanel.setBorder(
+ BorderFactory.createTitledBorder(
+ BorderFactory.createEtchedBorder(),
+ getXPathAttributesTitle()));
+ xpath = new XPathPanel();
+ sizePanel.add(xpath);
+
+ xml = new XMLConfPanel();
+ xpath = new XPathPanel();
+
+ xml.setBorder(
+ BorderFactory.createTitledBorder(
+ BorderFactory.createEtchedBorder(),
+ JMeterUtils.getResString("xpath_assertion_option")));
+ add(xml);
+
+ add(sizePanel);
}
/**
* Modifies a given TestElement to mirror the data in the gui components.
- *
* @see org.apache.jmeter.gui.JMeterGUIComponent#modifyTestElement(TestElement)
*/
- public void modifyTestElement(TestElement el) {
- super.configureTestElement(el);
- String xpathString = xpath.getText();
-
- XPathAssertion assertion = (XPathAssertion) el;
- assertion.setValidating(validation.isSelected());
- assertion.setWhitespace(whitespace.isSelected());
- assertion.setJTidy(tidy.isSelected());
- assertion.setNegated(negated.isSelected());
- /*
- * Should I really check this? If someone really wants an invalid xpath,
- * why should i stop them.... I am commenting it out. I offer them a
- * button to do the validation, so if they don't use it, it ain't my
- * problem. if (validXPath(xpathString, false))
- *
- * An XPath might need to be feed through a function, in which case it is
- * invalid now, but perhaps valid when executed.
- */
- if (xpathString == null || xpathString.length() == 0)
- xpathString = XPathAssertion.DEFAULT_XPATH;
- assertion.setXPathString(xpathString);
- }
-
- /*
- * Set the options according to validness, let the user know that tidy is
- * not compatible with validation.
- */
- private void tidySelected() {
- if (tidy.isSelected()) {
- validation.setEnabled(false);
- whitespace.setEnabled(false);
- } else {
- validation.setEnabled(true);
- whitespace.setEnabled(true);
- }
+ public void modifyTestElement(TestElement el)
+ {
+ super.configureTestElement(el);
+ if (el instanceof XPathAssertion ) {
+ XPathAssertion assertion = (XPathAssertion) el;
+ assertion.setValidating(xml.isValidate());
+ assertion.setWhitespace(xml.isWhitespace());
+ assertion.setTolerant(xml.isTolerant());
+ assertion.setNamespace(xml.isNamespace());
+ assertion.setNegated(xpath.isNegated());
+ assertion.setXPathString(xpath.getXPath());
+ }
}
-
- /**
- * Test weather an XPath is valid. It seems the Xalan has no easy way to tes
- * this. so it creates a test document, then tries to evaluate the xpath.
- *
- * @param xpathString
- * XPath String to validate
- * @param showDialog
- * weather to show a dialog
- * @return returns true if valid, valse otherwise.
- */
- private boolean validXPath(String xpathString) {
- String ret = null;
- boolean success = true;
- try {
- if (testDoc == null) {
-
- testDoc = DocumentBuilderFactory.newInstance()
- .newDocumentBuilder().newDocument();
- Element el = testDoc.createElement("root");
- testDoc.appendChild(el);
- }
- if (XPathAPI.eval(testDoc, xpathString) == null) {
- // We really should never get here
- // because eval will throw an exception
- // if xpath is invalid, but whatever, better
- // safe
- log.warn("xpath eval was null ");
- ret = "xpath eval was null";
- success = false;
- }
-
- } catch (ParserConfigurationException e) {
- success = false;
- ret = e.getLocalizedMessage();
- } catch (TransformerException e) {
- success = false;
- ret = e.getLocalizedMessage();
- }
- JOptionPane.showMessageDialog(null, (success) ? JMeterUtils
- .getResString("xpath_assertion_valid") : ret,
- (success) ? JMeterUtils.getResString("xpath_assertion_valid")
- : JMeterUtils.getResString("xpath_assertion_failed"),
- (success) ? JOptionPane.INFORMATION_MESSAGE
- : JOptionPane.ERROR_MESSAGE);
-
- return success;
-
- }
-
}
No revision
Index: XPathAssertionGui.java
===================================================================
RCS file: /home/cvs/jakarta-jmeter/src/components/org/apache/jmeter/assertions/gui/Attic/XPathAssertionGui.java,v
retrieving revision 1.1.2.2
retrieving revision 1.1.2.3
diff -u -r1.1.2.2 -r1.1.2.3
--- XPathAssertionGui.java 22 Feb 2005 14:26:32 -0000 1.1.2.2
+++ XPathAssertionGui.java 6 Mar 2005 12:45:27 -0000 1.1.2.3
@@ -18,20 +18,9 @@
package org.apache.jmeter.assertions.gui;
import java.awt.BorderLayout;
-import java.awt.event.ActionEvent;
-import java.awt.event.ActionListener;
-import java.awt.event.FocusEvent;
-import java.awt.event.FocusListener;
import javax.swing.BorderFactory;
-import javax.swing.JButton;
-import javax.swing.JCheckBox;
-import javax.swing.JOptionPane;
import javax.swing.JPanel;
-import javax.swing.JTextField;
-import javax.xml.parsers.DocumentBuilderFactory;
-import javax.xml.parsers.ParserConfigurationException;
-import javax.xml.transform.TransformerException;
import org.apache.jmeter.assertions.XPathAssertion;
import org.apache.jmeter.testelement.TestElement;
@@ -40,9 +29,6 @@
import org.apache.jorphan.logging.LoggingManager;
import org.apache.log.Logger;
-import org.apache.xpath.XPathAPI;
-import org.w3c.dom.Document;
-import org.w3c.dom.Element;
/**
*
@@ -50,213 +36,102 @@
*
*/
-public class XPathAssertionGui extends AbstractAssertionGui implements
- FocusListener, ActionListener {
-
- private static transient Logger log = LoggingManager.getLoggerForClass();
-
- private JTextField xpath;
+public class XPathAssertionGui extends AbstractAssertionGui
+{
- private JCheckBox validation, whitespace, tidy, negated;
-
- private JButton checkXPath;
-
- private static Document testDoc = null; // Used to validate XPath expressions
+ private static transient Logger log = LoggingManager.getLoggerForClass();
+ private static final String OPERATOR_KEY = null;
- /**
- * The constructor.
- */
+ private int execState;
+ private XPathPanel xpath;
+ private XMLConfPanel xml;
+
public XPathAssertionGui() {
- init();
- }
-
- /**
- * Returns the label to be shown within the JTree-Component.
- */
- public String getLabelResource() {
- return "xpath_assertion_title";
- }
-
- /**
- * Create test element
- */
- public TestElement createTestElement() {
-
- XPathAssertion el = new XPathAssertion();
- modifyTestElement(el);
- return el;
- }
-
- public String getXPathAttributesTitle() {
- return JMeterUtils.getResString("xpath_assertion_test");
- }
-
- public void focusGained(FocusEvent e) {
- log.debug("XPathAssertionGui.focusGained() called");
- }
-
- public void focusLost(FocusEvent e) {
-
- }
-
- public void configure(TestElement el) {
- super.configure(el);
- XPathAssertion assertion = (XPathAssertion) el;
- xpath.setText(assertion.getXPathString());
- whitespace.setSelected(assertion.isWhitespace());
- validation.setSelected(assertion.isValidating());
- negated.setSelected(assertion.isNegated());
- tidy.setSelected(assertion.isJTidy());
- tidySelected();
- }
+ init();
+ }
- private void init() {
- setLayout(new VerticalLayout(5, VerticalLayout.LEFT, VerticalLayout.TOP));
- setBorder(makeBorder());
-
- add(makeTitlePanel());
-
- // USER_INPUT
- JPanel sizePanel = new JPanel(new BorderLayout());
- sizePanel.setBorder(BorderFactory.createEmptyBorder(0, 10, 10, 10));
- sizePanel.setBorder(BorderFactory.createTitledBorder(BorderFactory
- .createEtchedBorder(), getXPathAttributesTitle()));
-
- negated = new JCheckBox(JMeterUtils
- .getResString("xpath_assertion_negate"), false);
- xpath = new JTextField(50);
- xpath.setText(XPathAssertion.DEFAULT_XPATH);
- checkXPath = new JButton(JMeterUtils
- .getResString("xpath_assertion_button"));
- checkXPath.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- validXPath(xpath.getText());
- }
- });
-
- xpath.addFocusListener(this);
- sizePanel.add(xpath, BorderLayout.WEST);
- sizePanel.add(checkXPath, BorderLayout.EAST);
- sizePanel.add(negated, BorderLayout.SOUTH);// JDK1.4 PAGE_END
-
- JPanel optionPanel = new JPanel();
- optionPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory
- .createEtchedBorder(), JMeterUtils
- .getResString("xpath_assertion_option")));
-
- validation = new JCheckBox(JMeterUtils
- .getResString("xpath_assertion_validation"), false);
- whitespace = new JCheckBox(JMeterUtils
- .getResString("xpath_assertion_whitespace"), false);
- tidy = new JCheckBox(JMeterUtils.getResString("xpath_assertion_tidy"),
- false);
- tidy.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- tidySelected();
- }
- });
-
- optionPanel.add(validation);
- optionPanel.add(whitespace);
- optionPanel.add(tidy);
-
- add(sizePanel);
- add(optionPanel);
- }
-
- public void actionPerformed(ActionEvent e) {
- // Actions are handled by individual listeners
+ /**
+ * Returns the label to be shown within the JTree-Component.
+ */
+ public String getLabelResource()
+ {
+ return "xpath_assertion_title";
+ }
+
+ /**
+ * Create test element
+ */
+ public TestElement createTestElement()
+ {
+ XPathAssertion el = new XPathAssertion();
+ modifyTestElement(el);
+ return el;
+ }
+
+ public String getXPathAttributesTitle()
+ {
+ return JMeterUtils.getResString("xpath_assertion_test");
+ }
+
+ public void configure(TestElement el)
+ {
+ super.configure(el);
+ XPathAssertion assertion = (XPathAssertion) el;
+ xpath.setXPath(assertion.getXPathString());
+ xpath.setNegated(assertion.isNegated());
+
+ xml.setWhitespace(assertion.isWhitespace());
+ xml.setValidate(assertion.isValidating());
+ xml.setTolerant(assertion.isTolerant());
+ xml.setNamespace(assertion.isNamespace());
+
+ }
+
+ private void init()
+ {
+ setLayout(
+ new VerticalLayout(5, VerticalLayout.LEFT, VerticalLayout.TOP));
+ setBorder(makeBorder());
+
+ add(makeTitlePanel());
+
+ // USER_INPUT
+ JPanel sizePanel = new JPanel(new BorderLayout());
+ sizePanel.setBorder(BorderFactory.createEmptyBorder(0, 10, 10, 10));
+ sizePanel.setBorder(
+ BorderFactory.createTitledBorder(
+ BorderFactory.createEtchedBorder(),
+ getXPathAttributesTitle()));
+ xpath = new XPathPanel();
+ sizePanel.add(xpath);
+
+ xml = new XMLConfPanel();
+ xpath = new XPathPanel();
+
+ xml.setBorder(
+ BorderFactory.createTitledBorder(
+ BorderFactory.createEtchedBorder(),
+ JMeterUtils.getResString("xpath_assertion_option")));
+ add(xml);
+
+ add(sizePanel);
}
/**
* Modifies a given TestElement to mirror the data in the gui components.
- *
* @see org.apache.jmeter.gui.JMeterGUIComponent#modifyTestElement(TestElement)
*/
- public void modifyTestElement(TestElement el) {
- super.configureTestElement(el);
- String xpathString = xpath.getText();
-
- XPathAssertion assertion = (XPathAssertion) el;
- assertion.setValidating(validation.isSelected());
- assertion.setWhitespace(whitespace.isSelected());
- assertion.setJTidy(tidy.isSelected());
- assertion.setNegated(negated.isSelected());
- /*
- * Should I really check this? If someone really wants an invalid xpath,
- * why should i stop them.... I am commenting it out. I offer them a
- * button to do the validation, so if they don't use it, it ain't my
- * problem. if (validXPath(xpathString, false))
- *
- * An XPath might need to be feed through a function, in which case it is
- * invalid now, but perhaps valid when executed.
- */
- if (xpathString == null || xpathString.length() == 0)
- xpathString = XPathAssertion.DEFAULT_XPATH;
- assertion.setXPathString(xpathString);
- }
-
- /*
- * Set the options according to validness, let the user know that tidy is
- * not compatible with validation.
- */
- private void tidySelected() {
- if (tidy.isSelected()) {
- validation.setEnabled(false);
- whitespace.setEnabled(false);
- } else {
- validation.setEnabled(true);
- whitespace.setEnabled(true);
- }
+ public void modifyTestElement(TestElement el)
+ {
+ super.configureTestElement(el);
+ if (el instanceof XPathAssertion ) {
+ XPathAssertion assertion = (XPathAssertion) el;
+ assertion.setValidating(xml.isValidate());
+ assertion.setWhitespace(xml.isWhitespace());
+ assertion.setTolerant(xml.isTolerant());
+ assertion.setNamespace(xml.isNamespace());
+ assertion.setNegated(xpath.isNegated());
+ assertion.setXPathString(xpath.getXPath());
+ }
}
-
- /**
- * Test weather an XPath is valid. It seems the Xalan has no easy way to tes
- * this. so it creates a test document, then tries to evaluate the xpath.
- *
- * @param xpathString
- * XPath String to validate
- * @param showDialog
- * weather to show a dialog
- * @return returns true if valid, valse otherwise.
- */
- private boolean validXPath(String xpathString) {
- String ret = null;
- boolean success = true;
- try {
- if (testDoc == null) {
-
- testDoc = DocumentBuilderFactory.newInstance()
- .newDocumentBuilder().newDocument();
- Element el = testDoc.createElement("root");
- testDoc.appendChild(el);
- }
- if (XPathAPI.eval(testDoc, xpathString) == null) {
- // We really should never get here
- // because eval will throw an exception
- // if xpath is invalid, but whatever, better
- // safe
- log.warn("xpath eval was null ");
- ret = "xpath eval was null";
- success = false;
- }
-
- } catch (ParserConfigurationException e) {
- success = false;
- ret = e.getLocalizedMessage();
- } catch (TransformerException e) {
- success = false;
- ret = e.getLocalizedMessage();
- }
- JOptionPane.showMessageDialog(null, (success) ? JMeterUtils
- .getResString("xpath_assertion_valid") : ret,
- (success) ? JMeterUtils.getResString("xpath_assertion_valid")
- : JMeterUtils.getResString("xpath_assertion_failed"),
- (success) ? JOptionPane.INFORMATION_MESSAGE
- : JOptionPane.ERROR_MESSAGE);
-
- return success;
-
- }
-
}
No revision
Index: XPathAssertionGui.java
===================================================================
RCS file: /home/cvs/jakarta-jmeter/src/components/org/apache/jmeter/assertions/gui/Attic/XPathAssertionGui.java,v
retrieving revision 1.1.2.2
retrieving revision 1.1.2.3
diff -u -r1.1.2.2 -r1.1.2.3
--- XPathAssertionGui.java 22 Feb 2005 14:26:32 -0000 1.1.2.2
+++ XPathAssertionGui.java 6 Mar 2005 12:45:27 -0000 1.1.2.3
@@ -18,20 +18,9 @@
package org.apache.jmeter.assertions.gui;
import java.awt.BorderLayout;
-import java.awt.event.ActionEvent;
-import java.awt.event.ActionListener;
-import java.awt.event.FocusEvent;
-import java.awt.event.FocusListener;
import javax.swing.BorderFactory;
-import javax.swing.JButton;
-import javax.swing.JCheckBox;
-import javax.swing.JOptionPane;
import javax.swing.JPanel;
-import javax.swing.JTextField;
-import javax.xml.parsers.DocumentBuilderFactory;
-import javax.xml.parsers.ParserConfigurationException;
-import javax.xml.transform.TransformerException;
import org.apache.jmeter.assertions.XPathAssertion;
import org.apache.jmeter.testelement.TestElement;
@@ -40,9 +29,6 @@
import org.apache.jorphan.logging.LoggingManager;
import org.apache.log.Logger;
-import org.apache.xpath.XPathAPI;
-import org.w3c.dom.Document;
-import org.w3c.dom.Element;
/**
*
@@ -50,213 +36,102 @@
*
*/
-public class XPathAssertionGui extends AbstractAssertionGui implements
- FocusListener, ActionListener {
-
- private static transient Logger log = LoggingManager.getLoggerForClass();
-
- private JTextField xpath;
+public class XPathAssertionGui extends AbstractAssertionGui
+{
- private JCheckBox validation, whitespace, tidy, negated;
-
- private JButton checkXPath;
-
- private static Document testDoc = null; // Used to validate XPath expressions
+ private static transient Logger log = LoggingManager.getLoggerForClass();
+ private static final String OPERATOR_KEY = null;
- /**
- * The constructor.
- */
+ private int execState;
+ private XPathPanel xpath;
+ private XMLConfPanel xml;
+
public XPathAssertionGui() {
- init();
- }
-
- /**
- * Returns the label to be shown within the JTree-Component.
- */
- public String getLabelResource() {
- return "xpath_assertion_title";
- }
-
- /**
- * Create test element
- */
- public TestElement createTestElement() {
-
- XPathAssertion el = new XPathAssertion();
- modifyTestElement(el);
- return el;
- }
-
- public String getXPathAttributesTitle() {
- return JMeterUtils.getResString("xpath_assertion_test");
- }
-
- public void focusGained(FocusEvent e) {
- log.debug("XPathAssertionGui.focusGained() called");
- }
-
- public void focusLost(FocusEvent e) {
-
- }
-
- public void configure(TestElement el) {
- super.configure(el);
- XPathAssertion assertion = (XPathAssertion) el;
- xpath.setText(assertion.getXPathString());
- whitespace.setSelected(assertion.isWhitespace());
- validation.setSelected(assertion.isValidating());
- negated.setSelected(assertion.isNegated());
- tidy.setSelected(assertion.isJTidy());
- tidySelected();
- }
+ init();
+ }
- private void init() {
- setLayout(new VerticalLayout(5, VerticalLayout.LEFT, VerticalLayout.TOP));
- setBorder(makeBorder());
-
- add(makeTitlePanel());
-
- // USER_INPUT
- JPanel sizePanel = new JPanel(new BorderLayout());
- sizePanel.setBorder(BorderFactory.createEmptyBorder(0, 10, 10, 10));
- sizePanel.setBorder(BorderFactory.createTitledBorder(BorderFactory
- .createEtchedBorder(), getXPathAttributesTitle()));
-
- negated = new JCheckBox(JMeterUtils
- .getResString("xpath_assertion_negate"), false);
- xpath = new JTextField(50);
- xpath.setText(XPathAssertion.DEFAULT_XPATH);
- checkXPath = new JButton(JMeterUtils
- .getResString("xpath_assertion_button"));
- checkXPath.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- validXPath(xpath.getText());
- }
- });
-
- xpath.addFocusListener(this);
- sizePanel.add(xpath, BorderLayout.WEST);
- sizePanel.add(checkXPath, BorderLayout.EAST);
- sizePanel.add(negated, BorderLayout.SOUTH);// JDK1.4 PAGE_END
-
- JPanel optionPanel = new JPanel();
- optionPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory
- .createEtchedBorder(), JMeterUtils
- .getResString("xpath_assertion_option")));
-
- validation = new JCheckBox(JMeterUtils
- .getResString("xpath_assertion_validation"), false);
- whitespace = new JCheckBox(JMeterUtils
- .getResString("xpath_assertion_whitespace"), false);
- tidy = new JCheckBox(JMeterUtils.getResString("xpath_assertion_tidy"),
- false);
- tidy.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- tidySelected();
- }
- });
-
- optionPanel.add(validation);
- optionPanel.add(whitespace);
- optionPanel.add(tidy);
-
- add(sizePanel);
- add(optionPanel);
- }
-
- public void actionPerformed(ActionEvent e) {
- // Actions are handled by individual listeners
+ /**
+ * Returns the label to be shown within the JTree-Component.
+ */
+ public String getLabelResource()
+ {
+ return "xpath_assertion_title";
+ }
+
+ /**
+ * Create test element
+ */
+ public TestElement createTestElement()
+ {
+ XPathAssertion el = new XPathAssertion();
+ modifyTestElement(el);
+ return el;
+ }
+
+ public String getXPathAttributesTitle()
+ {
+ return JMeterUtils.getResString("xpath_assertion_test");
+ }
+
+ public void configure(TestElement el)
+ {
+ super.configure(el);
+ XPathAssertion assertion = (XPathAssertion) el;
+ xpath.setXPath(assertion.getXPathString());
+ xpath.setNegated(assertion.isNegated());
+
+ xml.setWhitespace(assertion.isWhitespace());
+ xml.setValidate(assertion.isValidating());
+ xml.setTolerant(assertion.isTolerant());
+ xml.setNamespace(assertion.isNamespace());
+
+ }
+
+ private void init()
+ {
+ setLayout(
+ new VerticalLayout(5, VerticalLayout.LEFT, VerticalLayout.TOP));
+ setBorder(makeBorder());
+
+ add(makeTitlePanel());
+
+ // USER_INPUT
+ JPanel sizePanel = new JPanel(new BorderLayout());
+ sizePanel.setBorder(BorderFactory.createEmptyBorder(0, 10, 10, 10));
+ sizePanel.setBorder(
+ BorderFactory.createTitledBorder(
+ BorderFactory.createEtchedBorder(),
+ getXPathAttributesTitle()));
+ xpath = new XPathPanel();
+ sizePanel.add(xpath);
+
+ xml = new XMLConfPanel();
+ xpath = new XPathPanel();
+
+ xml.setBorder(
+ BorderFactory.createTitledBorder(
+ BorderFactory.createEtchedBorder(),
+ JMeterUtils.getResString("xpath_assertion_option")));
+ add(xml);
+
+ add(sizePanel);
}
/**
* Modifies a given TestElement to mirror the data in the gui components.
- *
* @see org.apache.jmeter.gui.JMeterGUIComponent#modifyTestElement(TestElement)
*/
- public void modifyTestElement(TestElement el) {
- super.configureTestElement(el);
- String xpathString = xpath.getText();
-
- XPathAssertion assertion = (XPathAssertion) el;
- assertion.setValidating(validation.isSelected());
- assertion.setWhitespace(whitespace.isSelected());
- assertion.setJTidy(tidy.isSelected());
- assertion.setNegated(negated.isSelected());
- /*
- * Should I really check this? If someone really wants an invalid xpath,
- * why should i stop them.... I am commenting it out. I offer them a
- * button to do the validation, so if they don't use it, it ain't my
- * problem. if (validXPath(xpathString, false))
- *
- * An XPath might need to be feed through a function, in which case it is
- * invalid now, but perhaps valid when executed.
- */
- if (xpathString == null || xpathString.length() == 0)
- xpathString = XPathAssertion.DEFAULT_XPATH;
- assertion.setXPathString(xpathString);
- }
-
- /*
- * Set the options according to validness, let the user know that tidy is
- * not compatible with validation.
- */
- private void tidySelected() {
- if (tidy.isSelected()) {
- validation.setEnabled(false);
- whitespace.setEnabled(false);
- } else {
- validation.setEnabled(true);
- whitespace.setEnabled(true);
- }
+ public void modifyTestElement(TestElement el)
+ {
+ super.configureTestElement(el);
+ if (el instanceof XPathAssertion ) {
+ XPathAssertion assertion = (XPathAssertion) el;
+ assertion.setValidating(xml.isValidate());
+ assertion.setWhitespace(xml.isWhitespace());
+ assertion.setTolerant(xml.isTolerant());
+ assertion.setNamespace(xml.isNamespace());
+ assertion.setNegated(xpath.isNegated());
+ assertion.setXPathString(xpath.getXPath());
+ }
}
-
- /**
- * Test weather an XPath is valid. It seems the Xalan has no easy way to tes
- * this. so it creates a test document, then tries to evaluate the xpath.
- *
- * @param xpathString
- * XPath String to validate
- * @param showDialog
- * weather to show a dialog
- * @return returns true if valid, valse otherwise.
- */
- private boolean validXPath(String xpathString) {
- String ret = null;
- boolean success = true;
- try {
- if (testDoc == null) {
-
- testDoc = DocumentBuilderFactory.newInstance()
- .newDocumentBuilder().newDocument();
- Element el = testDoc.createElement("root");
- testDoc.appendChild(el);
- }
- if (XPathAPI.eval(testDoc, xpathString) == null) {
- // We really should never get here
- // because eval will throw an exception
- // if xpath is invalid, but whatever, better
- // safe
- log.warn("xpath eval was null ");
- ret = "xpath eval was null";
- success = false;
- }
-
- } catch (ParserConfigurationException e) {
- success = false;
- ret = e.getLocalizedMessage();
- } catch (TransformerException e) {
- success = false;
- ret = e.getLocalizedMessage();
- }
- JOptionPane.showMessageDialog(null, (success) ? JMeterUtils
- .getResString("xpath_assertion_valid") : ret,
- (success) ? JMeterUtils.getResString("xpath_assertion_valid")
- : JMeterUtils.getResString("xpath_assertion_failed"),
- (success) ? JOptionPane.INFORMATION_MESSAGE
- : JOptionPane.ERROR_MESSAGE);
-
- return success;
-
- }
-
}
1.1.2.1 +238 -0 jakarta-jmeter/src/components/org/apache/jmeter/assertions/gui/Attic/XPathPanel.java
1.1.2.1 +143 -0 jakarta-jmeter/src/components/org/apache/jmeter/assertions/gui/Attic/XMLConfPanel.java
No revision
No revision
1.1.2.2 +10 -8 jakarta-jmeter/xdocs/images/screenshots/Attic/xpath_assertion.png
<<Binary file>>
No revision
No revision
1.1.2.1 +180 -0 jakarta-jmeter/src/core/org/apache/jmeter/util/Attic/XPathUtil.java
No revision
No revision
1.106.2.16 +4 -0 jakarta-jmeter/src/core/org/apache/jmeter/resources/messages.properties
Index: messages.properties
===================================================================
RCS file: /home/cvs/jakarta-jmeter/src/core/org/apache/jmeter/resources/messages.properties,v
retrieving revision 1.106.2.15
retrieving revision 1.106.2.16
diff -u -r1.106.2.15 -r1.106.2.16
--- messages.properties 5 Mar 2005 02:13:14 -0000 1.106.2.15
+++ messages.properties 6 Mar 2005 12:45:27 -0000 1.106.2.16
@@ -525,6 +525,10 @@
wsdl_url=WSDL URL
wsdl_url_error=The WSDL was emtpy.
xml_assertion_title=XML Assertion
+xml_namespace_button=Use Namespaces
+xml_tolerant_button=Tolerant XML/HTML Parser
+xml_validate_button=Validate XML
+xml_whitespace_button=Ignore Whitespace
xpath_assertion_label=XPath
xpath_assertion_option=XML Parsing Options
xpath_assertion_validation=Validate the XML against the DTD
No revision
No revision
1.87.2.23 +9 -8 jakarta-jmeter/xdocs/usermanual/component_reference.xml
Index: component_reference.xml
===================================================================
RCS file: /home/cvs/jakarta-jmeter/xdocs/usermanual/component_reference.xml,v
retrieving revision 1.87.2.22
retrieving revision 1.87.2.23
diff -u -r1.87.2.22 -r1.87.2.23
--- component_reference.xml 5 Mar 2005 11:12:59 -0000 1.87.2.22
+++ component_reference.xml 6 Mar 2005 12:45:27 -0000 1.87.2.23
@@ -1565,11 +1565,12 @@
<properties>
<property name="Name" required="No">Descriptive name for this element that is shown in the tree.</property>
-<property name="Tidy" required="No">Send the returning document through JTidy.</property>
-<property name="Validation" required="No">Check the document against its schema.</property>
-<property name="XPath" required="Yes">XPath to match in the document.</property>
-<property name="Whitespace" required="No">Ignore Element Whitespace.</property>
-<property name="Negation" required="No">True if a XPath expression is not matched</property>
+<property name="Tolerant Parser" required="No">Be tolerant of XML/HTML errors</property>
+<property name="Use Namespaces" required="No">Should namespaces be honoured?</property>
+<property name="Validate XML" required="No">Check the document against its schema.</property>
+<property name="XPath Assertion" required="Yes">XPath to match in the document.</property>
+<property name="Ignore Whitespace" required="No">Ignore Element Whitespace.</property>
+<property name="True if nothing matches" required="No">True if a XPath expression is not matched</property>
</properties>
</component>
No revision
No revision
1.1.2.2 +286 -184 jakarta-jmeter/src/components/org/apache/jmeter/assertions/Attic/XPathAssertion.java
Index: XPathAssertion.java
===================================================================
RCS file: /home/cvs/jakarta-jmeter/src/components/org/apache/jmeter/assertions/Attic/XPathAssertion.java,v
retrieving revision 1.1.2.1
retrieving revision 1.1.2.2
diff -u -r1.1.2.1 -r1.1.2.2
--- XPathAssertion.java 8 Jan 2005 15:51:33 -0000 1.1.2.1
+++ XPathAssertion.java 6 Mar 2005 12:45:27 -0000 1.1.2.2
@@ -19,31 +19,33 @@
import java.io.ByteArrayInputStream;
import java.io.IOException;
-import java.io.InputStream;
import java.io.Serializable;
-import javax.xml.parsers.DocumentBuilder;
-import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;
+import junit.framework.TestCase;
+import junit.textui.TestRunner;
+
import org.apache.jmeter.samplers.SampleResult;
import org.apache.jmeter.testelement.AbstractTestElement;
import org.apache.jmeter.testelement.property.BooleanProperty;
import org.apache.jmeter.testelement.property.StringProperty;
+import org.apache.jmeter.threads.JMeterContext;
+import org.apache.jmeter.threads.JMeterContextService;
+import org.apache.jmeter.threads.JMeterVariables;
+import org.apache.jmeter.util.XPathUtil;
import org.apache.jorphan.logging.LoggingManager;
import org.apache.log.Logger;
import org.apache.xpath.XPathAPI;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
-import org.w3c.tidy.Tidy;
-import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
-import org.xml.sax.SAXParseException;
+
+
/**
- * Checks if the result is a well-formed XML content. and whether it matches an
- * XPath
+ * Checks if the result is a well-formed XML content and whether it matches an XPath
*
* author <a href="mailto:jspears@astrology.com">Justin Spears </a>
*/
@@ -51,25 +53,19 @@
Serializable, Assertion {
private static final Logger log = LoggingManager.getLoggerForClass();
- public static final String DEFAULT_XPATH = "/";
private static XPathAPI xpath = null;
- // one builder for all requests
- private static DocumentBuilder builder = null;
-
- // one factory for all requests
- private static DocumentBuilderFactory factory = null;
+ private static final String XPATH_KEY = "XPath.xpath";
+ private static final String WHITESPACE_KEY = "XPath.whitespace";
+ private static final String VALIDATE_KEY = "XPath.validate";
+ private static final String TOLERANT_KEY = "XPath.tolerant";
+ private static final String NEGATE_KEY = "XPath.negate";
+ private static final String NAMESPACE_KEY = "XPath.namespace";
- private static final String XPATH_KEY = "XPath.xpath";
-
- private static final String WHITESPACE_KEY = "XPath.whitespace";
-
- private static final String VALIDATE_KEY = "XPath.validate";
-
- private static final String JTIDY_KEY = "XPath.jtidy";
-
- private static final String NEGATE_KEY = "XPath.negate";
+ public static final String DEFAULT_XPATH = "/";
+
+
/**
* Returns the result of the Assertion. Checks if the result is well-formed
@@ -83,59 +79,38 @@
return setResultForNull(result);
}
result.setFailure(false);
+ result.setFailureMessage("");
- /*
- * create a new builder if something changes and/or the builder has not
- * been set
- */
-
- if (log.isDebugEnabled()) {
- log.debug(new StringBuffer("Validation is set to ").append(
- isValidating()).toString());
- log.debug(new StringBuffer("Whitespace is set to ").append(
- isWhitespace()).toString());
- log.debug(new StringBuffer("Jtidy is set to ").append(isJTidy())
- .toString());
+ if (log.isDebugEnabled()){
+ log.debug(new StringBuffer("Validation is set to ").append(isValidating()).toString());
+ log.debug(new StringBuffer("Whitespace is set to ").append(isWhitespace()).toString());
+ log.debug(new StringBuffer("Tolerant is set to ").append(isTolerant()).toString());
}
+
Document doc = null;
try {
- if (isJTidy()) {
- doc = makeTidyParser().parseDOM(
- new ByteArrayInputStream(response.getResponseData()),
- null);
- if (log.isDebugEnabled()) {
- log.debug("node : " + doc);
- }
- doc.normalize();
- // remove the document declaration cause I think it causes
- // issues this is only needed for JDOM, since I am not
- // using it... But in case we change.
- // Node name = doc.getDoctype();
- // doc.removeChild(name);
-
- } else {
- doc = parse(isValidating(), isWhitespace(), true,
- new ByteArrayInputStream(response.getResponseData()));
- }
- } catch (SAXException e) {
- log.warn("Cannot parse result content", e);
- result.setFailure(true);
- result.setFailureMessage(e.getMessage());
+ doc = XPathUtil.makeDocument(
+ new ByteArrayInputStream(response.getResponseData()),
+ isValidating(), isWhitespace(), isNamespace(), isTolerant());
+ }catch (SAXException e) {
+ log.debug("Caught sax exception: "+e);
+ result.setError(true);
+ result.setFailureMessage(new StringBuffer("SAXException: ").append(e.getMessage()).toString());
return result;
} catch (IOException e) {
log.warn("Cannot parse result content", e);
result.setError(true);
- result.setFailureMessage(e.getMessage());
+ result.setFailureMessage(new StringBuffer("IOException: ").append(e.getMessage()).toString());
return result;
} catch (ParserConfigurationException e) {
log.warn("Cannot parse result content", e);
result.setError(true);
- result.setFailureMessage(e.getMessage());
+ result.setFailureMessage(new StringBuffer("ParserConfigurationException: ").append(e.getMessage()).toString());
return result;
}
-
- if (doc == null) {
+
+ if ( doc == null || doc.getDocumentElement() == null){
result.setError(true);
result.setFailureMessage("Document is null, probably not parsable");
return result;
@@ -146,38 +121,25 @@
try {
nodeList = XPathAPI.selectNodeList(doc, getXPathString());
} catch (TransformerException e) {
- log.warn("Cannot extract XPath", e);
result.setError(true);
- result.setFailureMessage(e.getLocalizedMessage());
+ result.setFailureMessage(new StringBuffer("TransformerException: ").append(e.getMessage()).toString());
return result;
}
-
- if (nodeList == null || nodeList.getLength() == 0) {
- if (isNegated()) {
+
+ if ( nodeList == null || nodeList.getLength() == 0 ) {
+ log.debug(new StringBuffer("nodeList null no match ").append(getXPathString()).toString());
+ result.setFailure(!isNegated());
+ result.setFailureMessage("No Nodes Matched " + getXPathString());
return result;
- } else {
- result.setFailure(true);
- result
- .setFailureMessage("No Nodes Matched "
- + getXPathString());
- return result;
- }
- }
- // At this point, we have matched one or more nodes
- if (isNegated()) {// should we have found a match?
- result.setFailure(true);
- result.setFailureMessage("One or more Nodes Matched "
- + getXPathString());
}
-
- if (log.isDebugEnabled()) {
- // if (!isNegated()) {
- for (int i = 0; i < nodeList.getLength(); i++) {
- log.debug(new StringBuffer("nodeList[").append(i).append("] ")
- .append(nodeList.item(i)).toString());
- }
- // }
+ log.debug("nodeList length "+nodeList.getLength());
+ if (log.isDebugEnabled() &! isNegated()){
+ for (int i=0; i< nodeList.getLength(); i++)
+ log.debug(new StringBuffer("nodeList[").append(i).append("] ").append(nodeList.item(i)).toString());
}
+ result.setFailure(isNegated());
+ if (isNegated())
+ result.setFailureMessage("Specified XPath was found... Turn off negate if this is not desired");
return result;
}
@@ -194,28 +156,23 @@
* @return String xpath String
*/
public String getXPathString() {
- return getPropertyAsString(XPATH_KEY, DEFAULT_XPATH);
+ return getPropertyAsString(XPATH_KEY, DEFAULT_XPATH);
}
/**
- * Get a Property or return the defualt string
- *
- * @param key
- * Property Key
- * @param defaultValue
- * Default Value
+ * Get a Property or return the default string
+ * @param key Property Key
+ * @param defaultValue Default Value
* @return String property
*/
- private String getPropertyAsString(String key, String defaultValue) {
+ private String getPropertyAsString(String key, String defaultValue){
String str = getPropertyAsString(key);
return (str == null || str.length() == 0) ? defaultValue : str;
}
/**
* Set the XPath String this will be used as an xpath
- *
- * @param String
- * xpath
+ * @param String xpath
*/
public void setXPathString(String xpath) {
setProperty(new StringProperty(XPATH_KEY, xpath));
@@ -223,40 +180,42 @@
/**
* Set whether to ignore element whitespace
- *
- * @param boolean
- * whitespace
+ * @param boolean whitespace
*/
public void setWhitespace(boolean whitespace) {
setProperty(new BooleanProperty(WHITESPACE_KEY, whitespace));
}
/**
- * Set use validation
- *
- * @param boolean
- * validate
+ * Set use validation
+ * @param boolean validate
*/
public void setValidating(boolean validate) {
setProperty(new BooleanProperty(VALIDATE_KEY, validate));
}
/**
- * Feed Document through JTidy. In order to use xpath against XML.
- *
- * @param jtidy
+ * Set whether this is namespace aware
+ * @param boolean validate
+ */
+ public void setNamespace(boolean namespace) {
+ setProperty(new BooleanProperty(NAMESPACE_KEY, namespace));
+ }
+
+ /**
+ * Set tolerant mode if required
+ * @param tolerant true/false
*/
- public void setJTidy(boolean jtidy) {
- setProperty(new BooleanProperty(JTIDY_KEY, jtidy));
+ public void setTolerant(boolean tolerant) {
+ setProperty(new BooleanProperty(TOLERANT_KEY, tolerant));
}
- public void setNegated(boolean negate) {
+ public void setNegated(boolean negate){
setProperty(new BooleanProperty(NEGATE_KEY, negate));
}
/**
* Is this whitepsace ignored.
- *
* @return boolean
*/
public boolean isWhitespace() {
@@ -264,94 +223,237 @@
}
/**
- * Is this validating
- *
+ * Is this validating
* @return boolean
*/
public boolean isValidating() {
return getPropertyAsBoolean(VALIDATE_KEY, false);
}
+ /**
+ * Is this namespace aware?
+ * @return boolean
+ */
+ public boolean isNamespace() {
+ return getPropertyAsBoolean( NAMESPACE_KEY, false);
+ }
/**
- * Is this using JTidy
- *
+ * Is this using tolerant mode?
* @return boolean
*/
- public boolean isJTidy() {
- return getPropertyAsBoolean(JTIDY_KEY, false);
+ public boolean isTolerant() {
+ return getPropertyAsBoolean(TOLERANT_KEY, false);
}
/**
- * Negate the XPath test, that is return true if something is not found.
- *
+ * Negate the XPath test, that is return true if something
+ * is not found.
* @return boolean negated
*/
public boolean isNegated() {
return getPropertyAsBoolean(NEGATE_KEY, false);
}
-
- private Tidy makeTidyParser() {
- log.debug("Start : getParser");
- Tidy tidy = new Tidy();
- tidy.setCharEncoding(org.w3c.tidy.Configuration.UTF8);
- tidy.setQuiet(true);
- tidy.setShowWarnings(false);
- tidy.setMakeClean(true);
- tidy.setXmlTags(true);
- return tidy;
- }
-
- private static synchronized Document parse(boolean validating,
- boolean whitespace, boolean namespace, InputStream is)
- throws SAXException, IOException, ParserConfigurationException {
- return makeDocumentBuilder(validating, whitespace, namespace).parse(is);
- }
-
- private static synchronized DocumentBuilderFactory makeDocumentFactory(
- boolean validating, boolean whitespace, boolean namespace) {
- // (Re)create the factory only if it has changed
- if (factory == null || factory.isValidating() != validating
- || factory.isIgnoringElementContentWhitespace() != whitespace
- || factory.isNamespaceAware() != namespace) {
- // configure the document builder factory
- factory = DocumentBuilderFactory.newInstance();
- factory.setValidating(validating);
- factory.setNamespaceAware(namespace);
- factory.setIgnoringElementContentWhitespace(whitespace);
- builder = null;
- }
- return factory;
- }
-
- private static synchronized DocumentBuilder makeDocumentBuilder(
- boolean validating, boolean whitespace, boolean namespace)
- throws ParserConfigurationException {
- factory = makeDocumentFactory(validating, whitespace, namespace);
-
- if (builder == null) {
- builder = factory.newDocumentBuilder();
- if (validating) {
- builder.setErrorHandler(new ErrorHandler() {
-
- public void warning(SAXParseException exception)
- throws SAXException {
- // TODO - should this be enabled?
- // throw new SAXException(exception);
- }
-
- public void error(SAXParseException exception)
- throws SAXException {
- throw new SAXException(exception);
- }
-
- public void fatalError(SAXParseException exception)
- throws SAXException {
- // TODO - should this be enabled?
- // throw new SAXException(exception);
- }
- });
- }
- }
- return builder;
+
+ ////////////////////////////////// TEST CASES //////////////////////////////
+
+ public static class XPathAssertionTest extends TestCase {
+
+ XPathAssertion assertion;
+ SampleResult result;
+ JMeterVariables vars;
+ JMeterContext jmctx;
+ public XPathAssertionTest() {super();}
+ public XPathAssertionTest(String name)
+ {
+ super(name);
+ }
+ public void setUp() {
+ jmctx = JMeterContextService.getContext();
+ assertion = new XPathAssertion();
+ assertion.setThreadContext(jmctx);// This would be done by the run command
+// assertion.setRefName("regVal");
+
+ result = new SampleResult();
+ String data =
+ "<company-xmlext-query-ret>" +
+ "<row>" +
+ "<value field=\"RetCode\">LIS_OK</value>" +
+ "<value field=\"RetCodeExtension\"></value>" +
+ "<value field=\"alias\"></value>" +
+ "<value field=\"positioncount\"></value>" +
+ "<value field=\"invalidpincount\">0</value>" +
+ "<value field=\"pinposition1\">1</value>" +
+ "<value field=\"pinpositionvalue1\"></value>" +
+ "<value field=\"pinposition2\">5</value>" +
+ "<value field=\"pinpositionvalue2\"></value>" +
+ "<value field=\"pinposition3\">6</value>" +
+ "<value field=\"pinpositionvalue3\"></value>" +
+ "</row>" +
+ "</company-xmlext-query-ret>";
+ result.setResponseData(data.getBytes());
+ vars = new JMeterVariables();
+ jmctx.setVariables(vars);
+ jmctx.setPreviousResult(result);
+ }
+
+ public void testAssertion() throws Exception {
+ assertion.setXPathString("//row/value[@field = 'alias']");
+ AssertionResult res = assertion.getResult(jmctx.getPreviousResult());
+ log.debug(" res "+res.isError());
+ log.debug(" failure "+res.getFailureMessage());
+ assertFalse(res.isError());
+ assertFalse(res.isFailure());
+ }
+ public void testNegateAssertion() throws Exception {
+ assertion.setXPathString("//row/value[@field = 'noalias']");
+ assertion.setNegated(true);
+
+ AssertionResult res = assertion.getResult(jmctx.getPreviousResult());
+ log.debug(" res "+res.isError());
+ log.debug(" failure "+res.getFailureMessage());
+ assertFalse(res.isError());
+ assertFalse(res.isFailure());
+ }
+ public void testValidationFailure() throws Exception {
+ assertion.setXPathString("//row/value[@field = 'alias']");
+ assertion.setNegated(false);
+ assertion.setValidating(true);
+ AssertionResult res = assertion.getResult(jmctx.getPreviousResult());
+ log.debug(res.getFailureMessage()+" error: "+res.isError()+" failure: "+res.isFailure());
+ assertTrue(res.isError());
+ assertFalse(res.isFailure());
+
+ }
+ public void testValidationSuccess() throws Exception {
+ String data ="<?xml version=\"1.0\"?>"
+ +"<!DOCTYPE BOOK ["
+ +"<!ELEMENT p (#PCDATA)>"
+ +"<!ELEMENT BOOK (OPENER,SUBTITLE?,INTRODUCTION?,(SECTION | PART)+)>"
+ +"<!ELEMENT OPENER (TITLE_TEXT)*>"
+ +"<!ELEMENT TITLE_TEXT (#PCDATA)>"
+ +"<!ELEMENT SUBTITLE (#PCDATA)>"
+ +"<!ELEMENT INTRODUCTION (HEADER, p+)+>"
+ +"<!ELEMENT PART (HEADER, CHAPTER+)>"
+ +"<!ELEMENT SECTION (HEADER, p+)>"
+ +"<!ELEMENT HEADER (#PCDATA)>"
+ +"<!ELEMENT CHAPTER (CHAPTER_NUMBER, CHAPTER_TEXT)>"
+ +"<!ELEMENT CHAPTER_NUMBER (#PCDATA)>"
+ +"<!ELEMENT CHAPTER_TEXT (p)+>"
+ +"]>"
+ +"<BOOK>"
+ +"<OPENER>"
+ +"<TITLE_TEXT>All About Me</TITLE_TEXT>"
+ +"</OPENER>"
+ +"<PART>"
+ +"<HEADER>Welcome To My Book</HEADER>"
+ +"<CHAPTER>"
+ +"<CHAPTER_NUMBER>CHAPTER 1</CHAPTER_NUMBER>"
+ +"<CHAPTER_TEXT>"
+ +"<p>Glad you want to hear about me.</p>"
+ +"<p>There's so much to say!</p>"
+ +"<p>Where should we start?</p>"
+ +"<p>How about more about me?</p>"
+ +"</CHAPTER_TEXT>"
+ +"</CHAPTER>"
+ +"</PART>"
+ +"</BOOK>";
+
+ result.setResponseData(data.getBytes());
+ vars = new JMeterVariables();
+ jmctx.setVariables(vars);
+ jmctx.setPreviousResult(result);
+ assertion.setXPathString("/");
+ assertion.setValidating(true);
+ AssertionResult res = assertion.getResult(result);
+ assertFalse(res.isError());
+ assertFalse(res.isFailure());
+ }
+ public void testValidationFailureWithDTD() throws Exception {
+ String data ="<?xml version=\"1.0\"?>"
+ +"<!DOCTYPE BOOK ["
+ +"<!ELEMENT p (#PCDATA)>"
+ +"<!ELEMENT BOOK (OPENER,SUBTITLE?,INTRODUCTION?,(SECTION | PART)+)>"
+ +"<!ELEMENT OPENER (TITLE_TEXT)*>"
+ +"<!ELEMENT TITLE_TEXT (#PCDATA)>"
+ +"<!ELEMENT SUBTITLE (#PCDATA)>"
+ +"<!ELEMENT INTRODUCTION (HEADER, p+)+>"
+ +"<!ELEMENT PART (HEADER, CHAPTER+)>"
+ +"<!ELEMENT SECTION (HEADER, p+)>"
+ +"<!ELEMENT HEADER (#PCDATA)>"
+ +"<!ELEMENT CHAPTER (CHAPTER_NUMBER, CHAPTER_TEXT)>"
+ +"<!ELEMENT CHAPTER_NUMBER (#PCDATA)>"
+ +"<!ELEMENT CHAPTER_TEXT (p)+>"
+ +"]>"
+ +"<BOOK>"
+ +"<OPENER>"
+ +"<TITLE_TEXT>All About Me</TITLE_TEXT>"
+ +"</OPENER>"
+ +"<PART>"
+ +"<HEADER>Welcome To My Book</HEADER>"
+ +"<CHAPTER>"
+ +"<CHAPTER_NUMBER>CHAPTER 1</CHAPTER_NUMBER>"
+ +"<CHAPTER_TEXT>"
+ +"<p>Glad you want to hear about me.</p>"
+ +"<p>There's so much to say!</p>"
+ +"<p>Where should we start?</p>"
+ +"<p>How about more about me?</p>"
+ +"</CHAPTER_TEXT>"
+ +"</CHAPTER>"
+ +"<illegal>not defined in dtd</illegal>"
+ +"</PART>"
+ +"</BOOK>";
+
+ result.setResponseData(data.getBytes());
+ vars = new JMeterVariables();
+ jmctx.setVariables(vars);
+ jmctx.setPreviousResult(result);
+ assertion.setXPathString("/");
+ assertion.setValidating(true);
+ AssertionResult res = assertion.getResult(result);
+ log.debug("failureMessage: "+res.getFailureMessage());
+ assertTrue(res.isError());
+ assertFalse(res.isFailure());
+ }
+ public void testTolerance() throws Exception {
+ String data ="<html><head><title>testtitle</title></head>"
+ +"<body>"
+ +"<p><i><b>invalid tag nesting</i></b><hr>"
+ +"</body></html>";
+
+ result.setResponseData(data.getBytes());
+ vars = new JMeterVariables();
+ jmctx.setVariables(vars);
+ jmctx.setPreviousResult(result);
+ assertion.setXPathString("/html/head/title");
+ assertion.setValidating(true);
+ assertion.setTolerant(true);
+ AssertionResult res = assertion.getResult(result);
+ log.debug("failureMessage: "+res.getFailureMessage());
+ assertFalse(res.isFailure());
+ assertFalse(res.isError());
+ }
+
+ public void testNoTolerance() throws Exception {
+ String data ="<html><head><title>testtitle</title></head>"
+ +"<body>"
+ +"<p><i><b>invalid tag nesting</i></b><hr>"
+ +"</body></html>";
+
+ result.setResponseData(data.getBytes());
+ vars = new JMeterVariables();
+ jmctx.setVariables(vars);
+ jmctx.setPreviousResult(result);
+ assertion.setXPathString("/html/head/title");
+ assertion.setValidating(false);
+ assertion.setTolerant(false);
+ AssertionResult res = assertion.getResult(result);
+ log.debug("failureMessage: "+res.getFailureMessage());
+ assertTrue(res.isError());
+ assertFalse(res.isFailure());
+ }
+
+ public static void main(String[] args) {
+ TestRunner.run(XPathAssertionTest.class);
+ }
}
}
\ No newline at end of file
---------------------------------------------------------------------
To unsubscribe, e-mail: jmeter-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: jmeter-dev-help@jakarta.apache.org