KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jmeter > assertions > gui > XPathPanel


1 /*
2  * Copyright 2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */

17
18 package org.apache.jmeter.assertions.gui;
19
20 import java.awt.LayoutManager JavaDoc;
21 import java.awt.event.ActionEvent JavaDoc;
22 import java.awt.event.ActionListener JavaDoc;
23
24 import javax.swing.Box JavaDoc;
25 import javax.swing.JButton JavaDoc;
26 import javax.swing.JCheckBox JavaDoc;
27 import javax.swing.JFrame JavaDoc;
28 import javax.swing.JOptionPane JavaDoc;
29 import javax.swing.JPanel JavaDoc;
30 import javax.swing.JTextField JavaDoc;
31 import javax.xml.parsers.ParserConfigurationException JavaDoc;
32 import javax.xml.transform.TransformerException JavaDoc;
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 JavaDoc;
40 import org.w3c.dom.Element JavaDoc;
41 import org.xml.sax.SAXException JavaDoc;
42
43 /**
44  * author jspears
45  *
46  */

47 public class XPathPanel extends JPanel JavaDoc {
48     private static Document JavaDoc testDoc = null;
49     private final static Log log = LogFactory.getLog(XPathPanel.class);
50     private JCheckBox JavaDoc negated;
51     private JTextField JavaDoc xpath;
52     private JButton JavaDoc checkXPath;
53     
54     /**
55      *
56      */

57     public XPathPanel() {
58         super();
59         init();
60     }
61     
62     /**
63      * @param isDoubleBuffered
64      */

65     public XPathPanel(boolean isDoubleBuffered) {
66         super( isDoubleBuffered);
67     }
68     
69     /**
70      * @param layout
71      */

72     public XPathPanel(LayoutManager JavaDoc layout) {
73         super(layout);
74     }
75     /**
76      * @param layout
77      * @param isDoubleBuffered
78      */

79     public XPathPanel(LayoutManager JavaDoc layout, boolean isDoubleBuffered) {
80         super(layout, isDoubleBuffered);
81     
82     }
83     private void init() {
84         Box JavaDoc hbox = Box.createHorizontalBox();
85         hbox.add(Box.createHorizontalGlue());
86         hbox.add(getXPathTextField());
87         hbox.add(Box.createHorizontalGlue());
88         hbox.add(getCheckXPathButton());
89         
90         Box JavaDoc vbox = Box.createVerticalBox();
91         vbox.add(hbox);
92         vbox.add(Box.createVerticalGlue());
93         vbox.add(getNegatedCheckBox());
94         
95         add(vbox);
96     }
97     /**
98      * Get the XPath String
99      * @return String
100      */

101     public String JavaDoc getXPath() {
102         return this.xpath.getText();
103     }
104     /**
105      * Set the string that will be used in the xpath evaluation
106      * @param xpath
107      */

108     public void setXPath(String JavaDoc xpath) {
109         this.xpath.setText(xpath);
110     }
111     /**
112      * Does this negate the xpath results
113      * @return boolean
114      */

115     public boolean isNegated() {
116         return this.negated.isSelected();
117     }
118     /**
119      * Set this to true, if you want success when the xpath
120      * does not match.
121      * @param negated
122      */

123     public void setNegated(boolean negated) {
124         this.negated.setSelected(negated);
125     }
126     /**
127      * Negated chechbox
128      * @return JCheckBox
129      */

130     public JCheckBox JavaDoc getNegatedCheckBox() {
131         if (negated == null ) {
132             negated = new JCheckBox JavaDoc(JMeterUtils.getResString("xpath_assertion_negate"), false);
133         }
134
135         return negated;
136     }
137     /**
138      * Check XPath button
139      * @return JButton
140      */

141     public JButton JavaDoc getCheckXPathButton() {
142         if (checkXPath == null) {
143             checkXPath = new JButton JavaDoc(JMeterUtils.getResString("xpath_assertion_button"));
144             checkXPath.addActionListener(new ActionListener JavaDoc() {
145                 public void actionPerformed(ActionEvent JavaDoc e) {
146                     validXPath(xpath.getText(), true);
147                 }});
148         }
149         return checkXPath;
150     }
151     public JTextField JavaDoc getXPathTextField() {
152         if (xpath == null ) {
153             xpath = new JTextField JavaDoc(50);
154             xpath.setText("/");
155         }
156         return xpath;
157     }
158     /**
159      * @return Returns the showNegate.
160      */

161     public boolean isShowNegated() {
162         return this.getNegatedCheckBox().isVisible();
163     }
164     /**
165      * @param showNegate The showNegate to set.
166      */

167     public void setShowNegated(boolean showNegate) {
168         getNegatedCheckBox().setVisible(showNegate);
169     }
170     
171     /**
172      * Test weather an XPath is valid. It seems the Xalan has no
173      * easy way to check, so this creates a test document, then
174      * tries to evaluate the xpath.
175      *
176      * @param xpathString XPath String to validate
177      * @param showDialog weather to show a dialog
178      * @return returns true if valid, valse otherwise.
179      */

180     public static boolean validXPath(String JavaDoc xpathString, boolean showDialog) {
181         String JavaDoc ret = null;
182         boolean success= true;
183         try {
184             if (testDoc == null){
185                 testDoc = XPathUtil.makeDocumentBuilder(false,false,false).newDocument();
186                 Element JavaDoc el = testDoc.createElement("root");
187                 testDoc.appendChild(el);
188                     
189             }
190             if(XPathAPI.eval(testDoc, xpathString) == null){
191                 //We really should never get here
192
// because eval will throw an exception
193
// if xpath is invalid, but whatever, better
194
// safe
195
log.warn("xpath eval was null ");
196                 ret ="xpath eval was null";
197                 success = false;
198             }
199     
200         } catch (ParserConfigurationException JavaDoc e) {
201             success = false;
202             ret = e.getLocalizedMessage();
203         } catch (TransformerException JavaDoc e) {
204             success = false;
205             ret =e.getLocalizedMessage();
206     
207         } catch (SAXException JavaDoc 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     /**
224      * Just a simple main to run this panel, It would be nice
225      * if it loaded up all the stuff for JMeterUtil but for
226      * a little test it is overkill.
227      * @param args
228      */

229     public static void main (String JavaDoc[] args) {
230         JMeterUtils.setJMeterHome("/eclipse/workspace/jakarta-jmeter/bin");
231         JFrame JavaDoc frame = new JFrame JavaDoc();
232         frame.add(new XPathPanel());
233         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
234         frame.pack();
235         frame.setVisible(true);
236     }
237 }
Popular Tags