KickJava   Java API By Example, From Geeks To Geeks.

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


1 // $Header: /home/cvs/jakarta-jmeter/src/components/org/apache/jmeter/assertions/gui/AssertionGui.java,v 1.17.2.1 2004/06/03 23:01:35 sebb Exp $
2
/*
3  * Copyright 2001-2004 The Apache Software Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17 */

18
19 package org.apache.jmeter.assertions.gui;
20
21 import java.awt.BorderLayout JavaDoc;
22 import java.awt.Dimension JavaDoc;
23 import java.awt.event.ActionEvent JavaDoc;
24 import java.awt.event.ActionListener JavaDoc;
25
26 import javax.swing.BorderFactory JavaDoc;
27 import javax.swing.Box JavaDoc;
28 import javax.swing.ButtonGroup JavaDoc;
29 import javax.swing.JButton JavaDoc;
30 import javax.swing.JCheckBox JavaDoc;
31 import javax.swing.JPanel JavaDoc;
32 import javax.swing.JRadioButton JavaDoc;
33 import javax.swing.JScrollPane JavaDoc;
34 import javax.swing.JTable JavaDoc;
35
36 import org.apache.jmeter.assertions.ResponseAssertion;
37 import org.apache.jmeter.gui.util.PowerTableModel;
38 import org.apache.jmeter.gui.util.TextAreaCellRenderer;
39 import org.apache.jmeter.gui.util.TextAreaTableCellEditor;
40 import org.apache.jmeter.testelement.TestElement;
41 import org.apache.jmeter.testelement.property.PropertyIterator;
42 import org.apache.jmeter.util.JMeterUtils;
43
44 /**
45  * GUI interface for a {@link ResponseAssertion}.
46  *
47  * @version $Revision: 1.17.2.1 $ on $Date: 2004/06/03 23:01:35 $
48  */

49 public class AssertionGui extends AbstractAssertionGui
50 {
51     /** The name of the table column in the list of patterns. */
52     private static final String JavaDoc COL_NAME =
53         JMeterUtils.getResString("assertion_patterns_to_test");
54
55     /** Radio button indicating that the text response should be tested. */
56     private JRadioButton JavaDoc responseStringButton;
57
58     /** Radio button indicating that the URL should be tested. */
59     private JRadioButton JavaDoc labelButton;
60
61     /** Radio button indicating that the responseMessage should be tested. */
62     private JRadioButton JavaDoc responseMessageButton;
63
64     /** Radio button indicating that the responseCode should be tested. */
65     private JRadioButton JavaDoc responseCodeButton;
66
67     /**
68      * Checkbox to indicate whether the response should be forced successful before testing.
69      * This is intended for use when checking the status code or status message.
70      */

71     private JCheckBox JavaDoc assumeSuccess;
72     
73     /**
74      * Radio button indicating to test if the field contains one of the
75      * patterns.
76      */

77     private JRadioButton JavaDoc containsBox;
78
79     /**
80      * Radio button indicating to test if the field matches one of the
81      * patterns.
82      */

83     private JRadioButton JavaDoc matchesBox;
84
85     /**
86      * Checkbox indicating to test that the field does NOT contain/match
87      * the patterns.
88      */

89     private JCheckBox JavaDoc notBox;
90
91     /** A table of patterns to test against. */
92     private JTable JavaDoc stringTable;
93
94     /** Button to add a new pattern. */
95     private JButton JavaDoc addPattern;
96
97     /** Button to delete a pattern. */
98     private JButton JavaDoc deletePattern;
99
100     /** Table model for the pattern table. */
101     private PowerTableModel tableModel;
102
103     /**
104      * Create a new AssertionGui panel.
105      */

106     public AssertionGui()
107     {
108         init();
109     }
110
111     public String JavaDoc getLabelResource()
112     {
113         return "assertion_title";
114     }
115
116     /* Implements JMeterGUIComponent.createTestElement() */
117     public TestElement createTestElement()
118     {
119         ResponseAssertion el = new ResponseAssertion();
120         modifyTestElement(el);
121         return el;
122     }
123
124     /* Implements JMeterGUIComponent.modifyTestElement(TestElement) */
125     public void modifyTestElement(TestElement el)
126     {
127         configureTestElement(el);
128         if (el instanceof ResponseAssertion)
129         {
130             ResponseAssertion ra = (ResponseAssertion) el;
131
132             ra.clearTestStrings();
133             String JavaDoc[] testStrings = tableModel.getData().getColumn(COL_NAME);
134             for (int i = 0; i < testStrings.length; i++)
135             {
136                 ra.addTestString(testStrings[i]);
137             }
138
139             if (labelButton.isSelected())
140             {
141                 ra.setTestField(ResponseAssertion.SAMPLE_LABEL);
142             }
143             else if (responseCodeButton.isSelected())
144             {
145                 ra.setTestField(ResponseAssertion.RESPONSE_CODE);
146             }
147             else if (responseMessageButton.isSelected())
148             {
149                 ra.setTestField(ResponseAssertion.RESPONSE_MESSAGE);
150             }
151             else
152             {
153                 ra.setTestField(ResponseAssertion.RESPONSE_DATA);
154             }
155
156             ra.setAssumeSuccess(assumeSuccess.isSelected());
157
158             if (containsBox.isSelected())
159             {
160                 ra.setToContainsType();
161             }
162             else
163             {
164                 ra.setToMatchType();
165             }
166
167             if (notBox.isSelected())
168             {
169                 ra.setToNotType();
170             }
171             else
172             {
173                 ra.unsetNotType();
174             }
175         }
176     }
177
178     /**
179      * A newly created component can be initialized with the contents of
180      * a Test Element object by calling this method. The component is
181      * responsible for querying the Test Element object for the
182      * relevant information to display in its GUI.
183      *
184      * @param el the TestElement to configure
185      */

186     public void configure(TestElement el)
187     {
188         super.configure(el);
189         ResponseAssertion model = (ResponseAssertion) el;
190
191         if (model.isContainsType())
192         {
193             containsBox.setSelected(true);
194             matchesBox.setSelected(false);
195         }
196         else
197         {
198             containsBox.setSelected(false);
199             matchesBox.setSelected(true);
200         }
201
202         if (model.isNotType())
203         {
204             notBox.setSelected(true);
205         }
206         else
207         {
208             notBox.setSelected(false);
209         }
210
211         if (ResponseAssertion.RESPONSE_DATA.equals(model.getTestField()))
212         {
213             responseStringButton.setSelected(true);
214         }
215         else if (ResponseAssertion.RESPONSE_CODE.equals(model.getTestField()))
216         {
217             responseCodeButton.setSelected(true);
218         }
219         else if (ResponseAssertion.RESPONSE_MESSAGE.equals(model.getTestField()))
220         {
221             responseMessageButton.setSelected(true);
222         }
223         else // Assume it is the URL
224
{
225             labelButton.setSelected(true);
226         }
227
228         assumeSuccess.setSelected(model.getAssumeSuccess());
229         
230         tableModel.clearData();
231         PropertyIterator tests = model.getTestStrings().iterator();
232         while (tests.hasNext())
233         {
234             tableModel.addRow(new Object JavaDoc[] { tests.next().getStringValue()});
235         }
236
237         if(model.getTestStrings().size() == 0)
238         {
239             deletePattern.setEnabled(false);
240         }
241         else
242         {
243             deletePattern.setEnabled(true);
244         }
245
246         tableModel.fireTableDataChanged();
247     }
248
249     /**
250      * Initialize the GUI components and layout.
251      */

252     private void init()
253     {
254         setLayout(new BorderLayout JavaDoc());
255         Box JavaDoc box = Box.createVerticalBox();
256         setBorder(makeBorder());
257
258         box.add(makeTitlePanel());
259         box.add(createFieldPanel());
260         box.add(createTypePanel());
261         add(box,BorderLayout.NORTH);
262         add(createStringPanel(),BorderLayout.CENTER);
263     }
264
265     /**
266      * Create a panel allowing the user to choose which response field should
267      * be tested.
268      *
269      * @return a new panel for selecting the response field
270      */

271     private JPanel JavaDoc createFieldPanel()
272     {
273         JPanel JavaDoc panel = new JPanel JavaDoc();
274         panel.setBorder(
275             BorderFactory.createTitledBorder(
276                 JMeterUtils.getResString("assertion_resp_field")));
277
278         responseStringButton =
279             new JRadioButton JavaDoc(JMeterUtils.getResString("assertion_text_resp"));
280         labelButton =
281             new JRadioButton JavaDoc(JMeterUtils.getResString("assertion_url_samp"));
282         responseCodeButton =
283             new JRadioButton JavaDoc(JMeterUtils.getResString("assertion_code_resp"));
284         responseMessageButton =
285             new JRadioButton JavaDoc(JMeterUtils.getResString("assertion_message_resp"));
286
287         ButtonGroup JavaDoc group = new ButtonGroup JavaDoc();
288         group.add(responseStringButton);
289         group.add(labelButton);
290         group.add(responseCodeButton);
291         group.add(responseMessageButton);
292         
293         panel.add(responseStringButton);
294         panel.add(labelButton);
295         panel.add(responseCodeButton);
296         panel.add(responseMessageButton);
297         
298         responseStringButton.setSelected(true);
299         
300         assumeSuccess = new JCheckBox JavaDoc(JMeterUtils.getResString("assertion_assume_success"));
301         panel.add(assumeSuccess);
302
303         return panel;
304     }
305
306     /**
307      * Create a panel allowing the user to choose what type of test should be
308      * performed.
309      *
310      * @return a new panel for selecting the type of assertion test
311      */

312     private JPanel JavaDoc createTypePanel()
313     {
314         JPanel JavaDoc panel = new JPanel JavaDoc();
315         panel.setBorder(
316             BorderFactory.createTitledBorder(
317                 JMeterUtils.getResString("assertion_pattern_match_rules")));
318
319         ButtonGroup JavaDoc group = new ButtonGroup JavaDoc();
320
321         containsBox =
322             new JRadioButton JavaDoc(JMeterUtils.getResString("assertion_contains"));
323         group.add(containsBox);
324         containsBox.setSelected(true);
325         panel.add(containsBox);
326
327         matchesBox =
328             new JRadioButton JavaDoc(JMeterUtils.getResString("assertion_matches"));
329         group.add(matchesBox);
330         panel.add(matchesBox);
331
332         notBox = new JCheckBox JavaDoc(JMeterUtils.getResString("assertion_not"));
333         panel.add(notBox);
334
335         return panel;
336     }
337
338     /**
339      * Create a panel allowing the user to supply a list of string patterns to
340      * test against.
341      *
342      * @return a new panel for adding string patterns
343      */

344     private JPanel JavaDoc createStringPanel()
345     {
346         tableModel =
347             new PowerTableModel(
348                 new String JavaDoc[] { COL_NAME },
349                 new Class JavaDoc[] { String JavaDoc.class });
350         stringTable = new JTable JavaDoc(tableModel);
351
352         TextAreaCellRenderer renderer = new TextAreaCellRenderer();
353         stringTable.setRowHeight(renderer.getPreferredHeight());
354         stringTable.setDefaultRenderer(String JavaDoc.class, renderer);
355         stringTable.setDefaultEditor(
356             String JavaDoc.class,
357             new TextAreaTableCellEditor());
358         stringTable.setPreferredScrollableViewportSize(new Dimension JavaDoc(100, 70));
359
360         JPanel JavaDoc panel = new JPanel JavaDoc();
361         panel.setLayout(new BorderLayout JavaDoc());
362         panel.setBorder(
363             BorderFactory.createTitledBorder(
364                 JMeterUtils.getResString("assertion_patterns_to_test")));
365
366         panel.add(new JScrollPane JavaDoc(stringTable), BorderLayout.CENTER);
367         panel.add(createButtonPanel(), BorderLayout.SOUTH);
368
369         return panel;
370     }
371
372     /**
373      * Create a panel with buttons to add and delete string patterns.
374      *
375      * @return the new panel with add and delete buttons
376      */

377     private JPanel JavaDoc createButtonPanel()
378     {
379         addPattern = new JButton JavaDoc(JMeterUtils.getResString("add"));
380         addPattern.addActionListener(new AddPatternListener());
381
382         deletePattern = new JButton JavaDoc(JMeterUtils.getResString("delete"));
383         deletePattern.addActionListener(new ClearPatternsListener());
384         deletePattern.setEnabled(false);
385
386         JPanel JavaDoc buttonPanel = new JPanel JavaDoc();
387         buttonPanel.add(addPattern);
388         buttonPanel.add(deletePattern);
389         return buttonPanel;
390     }
391
392     /**
393      * An ActionListener for deleting a pattern.
394      *
395      * @author
396      * @version $Revision: 1.17.2.1 $ Last updated: $Date: 2004/06/03 23:01:35 $
397      */

398     private class ClearPatternsListener implements ActionListener JavaDoc
399     {
400         public void actionPerformed(ActionEvent JavaDoc e)
401         {
402             int index = stringTable.getSelectedRow();
403             if (index > -1)
404             {
405                 stringTable
406                     .getCellEditor(index, stringTable.getSelectedColumn())
407                     .cancelCellEditing();
408                 tableModel.removeRow(index);
409                 tableModel.fireTableDataChanged();
410             }
411             if (stringTable.getModel().getRowCount() == 0)
412             {
413                 deletePattern.setEnabled(false);
414             }
415         }
416     }
417
418     /**
419      * An ActionListener for adding a pattern.
420      *
421      * @version $Revision: 1.17.2.1 $ Last updated: $Date: 2004/06/03 23:01:35 $
422      */

423     private class AddPatternListener implements ActionListener JavaDoc
424     {
425         public void actionPerformed(ActionEvent JavaDoc e)
426         {
427             tableModel.addNewRow();
428             deletePattern.setEnabled(true);
429             tableModel.fireTableDataChanged();
430         }
431     }
432 }
433
Popular Tags