001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.jmeter.assertions.gui;
020:
021: import java.awt.LayoutManager;
022: import java.awt.event.ActionEvent;
023: import java.awt.event.ActionListener;
024:
025: import javax.swing.Box;
026: import javax.swing.JButton;
027: import javax.swing.JCheckBox;
028: import javax.swing.JOptionPane;
029: import javax.swing.JPanel;
030: import javax.swing.JTextField;
031: import javax.xml.parsers.ParserConfigurationException;
032: import javax.xml.transform.TransformerException;
033:
034: import org.apache.jmeter.util.JMeterUtils;
035: import org.apache.jmeter.util.XPathUtil;
036: import org.apache.jorphan.logging.LoggingManager;
037: import org.apache.log.Logger;
038: import org.apache.xpath.XPathAPI;
039: import org.w3c.dom.Document;
040: import org.w3c.dom.Element;
041:
042: /**
043: * author jspears
044: *
045: */
046: public class XPathPanel extends JPanel {
047: private static Document testDoc = null;
048:
049: private final static Logger log = LoggingManager
050: .getLoggerForClass();
051:
052: private JCheckBox negated;
053:
054: private JTextField xpath;
055:
056: private JButton checkXPath;
057:
058: /**
059: *
060: */
061: public XPathPanel() {
062: super ();
063: init();
064: }
065:
066: /**
067: * @param isDoubleBuffered
068: */
069: public XPathPanel(boolean isDoubleBuffered) {
070: super (isDoubleBuffered);
071: init();
072: }
073:
074: /**
075: * @param layout
076: */
077: public XPathPanel(LayoutManager layout) {
078: super (layout);
079: init();
080: }
081:
082: /**
083: * @param layout
084: * @param isDoubleBuffered
085: */
086: public XPathPanel(LayoutManager layout, boolean isDoubleBuffered) {
087: super (layout, isDoubleBuffered);
088: init();
089: }
090:
091: private void init() {
092: Box hbox = Box.createHorizontalBox();
093: hbox.add(Box.createHorizontalGlue());
094: hbox.add(getXPathTextField());
095: hbox.add(Box.createHorizontalGlue());
096: hbox.add(getCheckXPathButton());
097:
098: Box vbox = Box.createVerticalBox();
099: vbox.add(hbox);
100: vbox.add(Box.createVerticalGlue());
101: vbox.add(getNegatedCheckBox());
102:
103: add(vbox);
104:
105: setDefaultValues();
106: }
107:
108: public void setDefaultValues() {
109: setXPath("/"); //$NON-NLS-1$
110: setNegated(false);
111: }
112:
113: /**
114: * Get the XPath String
115: *
116: * @return String
117: */
118: public String getXPath() {
119: return this .xpath.getText();
120: }
121:
122: /**
123: * Set the string that will be used in the xpath evaluation
124: *
125: * @param xpath
126: */
127: public void setXPath(String xpath) {
128: this .xpath.setText(xpath);
129: }
130:
131: /**
132: * Does this negate the xpath results
133: *
134: * @return boolean
135: */
136: public boolean isNegated() {
137: return this .negated.isSelected();
138: }
139:
140: /**
141: * Set this to true, if you want success when the xpath does not match.
142: *
143: * @param negated
144: */
145: public void setNegated(boolean negated) {
146: this .negated.setSelected(negated);
147: }
148:
149: /**
150: * Negated chechbox
151: *
152: * @return JCheckBox
153: */
154: public JCheckBox getNegatedCheckBox() {
155: if (negated == null) {
156: negated = new JCheckBox(JMeterUtils
157: .getResString("xpath_assertion_negate"), false); //$NON-NLS-1$
158: }
159:
160: return negated;
161: }
162:
163: /**
164: * Check XPath button
165: *
166: * @return JButton
167: */
168: public JButton getCheckXPathButton() {
169: if (checkXPath == null) {
170: checkXPath = new JButton(JMeterUtils
171: .getResString("xpath_assertion_button")); //$NON-NLS-1$
172: checkXPath.addActionListener(new ActionListener() {
173: public void actionPerformed(ActionEvent e) {
174: validXPath(xpath.getText(), true);
175: }
176: });
177: }
178: return checkXPath;
179: }
180:
181: public JTextField getXPathTextField() {
182: if (xpath == null) {
183: xpath = new JTextField(50);
184: }
185: return xpath;
186: }
187:
188: /**
189: * @return Returns the showNegate.
190: */
191: public boolean isShowNegated() {
192: return this .getNegatedCheckBox().isVisible();
193: }
194:
195: /**
196: * @param showNegate
197: * The showNegate to set.
198: */
199: public void setShowNegated(boolean showNegate) {
200: getNegatedCheckBox().setVisible(showNegate);
201: }
202:
203: /**
204: * Test weather an XPath is valid. It seems the Xalan has no easy way to
205: * check, so this creates a test document, then tries to evaluate the xpath.
206: *
207: * @param xpathString
208: * XPath String to validate
209: * @param showDialog
210: * weather to show a dialog
211: * @return returns true if valid, valse otherwise.
212: */
213: public static boolean validXPath(String xpathString,
214: boolean showDialog) {
215: String ret = null;
216: boolean success = true;
217: try {
218: if (testDoc == null) {
219: testDoc = XPathUtil.makeDocumentBuilder(false, false,
220: false).newDocument();
221: Element el = testDoc.createElement("root"); //$NON-NLS-1$
222: testDoc.appendChild(el);
223:
224: }
225: if (XPathAPI.eval(testDoc, xpathString) == null) {
226: // We really should never get here
227: // because eval will throw an exception
228: // if xpath is invalid, but whatever, better
229: // safe
230: log.warn("xpath eval was null ");
231: ret = "xpath eval was null";
232: success = false;
233: }
234:
235: } catch (ParserConfigurationException e) {
236: success = false;
237: ret = e.getLocalizedMessage();
238: } catch (TransformerException e) {
239: success = false;
240: ret = e.getLocalizedMessage();
241: }
242:
243: if (showDialog) {
244: JOptionPane
245: .showMessageDialog(
246: null,
247: (success) ? JMeterUtils
248: .getResString("xpath_assertion_valid") : ret, //$NON-NLS-1$
249: (success) ? JMeterUtils
250: .getResString("xpath_assertion_valid") : JMeterUtils //$NON-NLS-1$
251: .getResString("xpath_assertion_failed"), (success) ? JOptionPane.INFORMATION_MESSAGE //$NON-NLS-1$
252: : JOptionPane.ERROR_MESSAGE);
253: }
254: return success;
255:
256: }
257: }
|