1 18 19 package org.apache.jmeter.assertions.gui; 20 21 import java.awt.BorderLayout ; 22 import java.awt.Dimension ; 23 import java.awt.event.ActionEvent ; 24 import java.awt.event.ActionListener ; 25 26 import javax.swing.BorderFactory ; 27 import javax.swing.Box ; 28 import javax.swing.ButtonGroup ; 29 import javax.swing.JButton ; 30 import javax.swing.JCheckBox ; 31 import javax.swing.JPanel ; 32 import javax.swing.JRadioButton ; 33 import javax.swing.JScrollPane ; 34 import javax.swing.JTable ; 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 49 public class AssertionGui extends AbstractAssertionGui 50 { 51 52 private static final String COL_NAME = 53 JMeterUtils.getResString("assertion_patterns_to_test"); 54 55 56 private JRadioButton responseStringButton; 57 58 59 private JRadioButton labelButton; 60 61 62 private JRadioButton responseMessageButton; 63 64 65 private JRadioButton responseCodeButton; 66 67 71 private JCheckBox assumeSuccess; 72 73 77 private JRadioButton containsBox; 78 79 83 private JRadioButton matchesBox; 84 85 89 private JCheckBox notBox; 90 91 92 private JTable stringTable; 93 94 95 private JButton addPattern; 96 97 98 private JButton deletePattern; 99 100 101 private PowerTableModel tableModel; 102 103 106 public AssertionGui() 107 { 108 init(); 109 } 110 111 public String getLabelResource() 112 { 113 return "assertion_title"; 114 } 115 116 117 public TestElement createTestElement() 118 { 119 ResponseAssertion el = new ResponseAssertion(); 120 modifyTestElement(el); 121 return el; 122 } 123 124 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 [] 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 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 { 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 [] { 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 252 private void init() 253 { 254 setLayout(new BorderLayout ()); 255 Box 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 271 private JPanel createFieldPanel() 272 { 273 JPanel panel = new JPanel (); 274 panel.setBorder( 275 BorderFactory.createTitledBorder( 276 JMeterUtils.getResString("assertion_resp_field"))); 277 278 responseStringButton = 279 new JRadioButton (JMeterUtils.getResString("assertion_text_resp")); 280 labelButton = 281 new JRadioButton (JMeterUtils.getResString("assertion_url_samp")); 282 responseCodeButton = 283 new JRadioButton (JMeterUtils.getResString("assertion_code_resp")); 284 responseMessageButton = 285 new JRadioButton (JMeterUtils.getResString("assertion_message_resp")); 286 287 ButtonGroup group = new ButtonGroup (); 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 (JMeterUtils.getResString("assertion_assume_success")); 301 panel.add(assumeSuccess); 302 303 return panel; 304 } 305 306 312 private JPanel createTypePanel() 313 { 314 JPanel panel = new JPanel (); 315 panel.setBorder( 316 BorderFactory.createTitledBorder( 317 JMeterUtils.getResString("assertion_pattern_match_rules"))); 318 319 ButtonGroup group = new ButtonGroup (); 320 321 containsBox = 322 new JRadioButton (JMeterUtils.getResString("assertion_contains")); 323 group.add(containsBox); 324 containsBox.setSelected(true); 325 panel.add(containsBox); 326 327 matchesBox = 328 new JRadioButton (JMeterUtils.getResString("assertion_matches")); 329 group.add(matchesBox); 330 panel.add(matchesBox); 331 332 notBox = new JCheckBox (JMeterUtils.getResString("assertion_not")); 333 panel.add(notBox); 334 335 return panel; 336 } 337 338 344 private JPanel createStringPanel() 345 { 346 tableModel = 347 new PowerTableModel( 348 new String [] { COL_NAME }, 349 new Class [] { String .class }); 350 stringTable = new JTable (tableModel); 351 352 TextAreaCellRenderer renderer = new TextAreaCellRenderer(); 353 stringTable.setRowHeight(renderer.getPreferredHeight()); 354 stringTable.setDefaultRenderer(String .class, renderer); 355 stringTable.setDefaultEditor( 356 String .class, 357 new TextAreaTableCellEditor()); 358 stringTable.setPreferredScrollableViewportSize(new Dimension (100, 70)); 359 360 JPanel panel = new JPanel (); 361 panel.setLayout(new BorderLayout ()); 362 panel.setBorder( 363 BorderFactory.createTitledBorder( 364 JMeterUtils.getResString("assertion_patterns_to_test"))); 365 366 panel.add(new JScrollPane (stringTable), BorderLayout.CENTER); 367 panel.add(createButtonPanel(), BorderLayout.SOUTH); 368 369 return panel; 370 } 371 372 377 private JPanel createButtonPanel() 378 { 379 addPattern = new JButton (JMeterUtils.getResString("add")); 380 addPattern.addActionListener(new AddPatternListener()); 381 382 deletePattern = new JButton (JMeterUtils.getResString("delete")); 383 deletePattern.addActionListener(new ClearPatternsListener()); 384 deletePattern.setEnabled(false); 385 386 JPanel buttonPanel = new JPanel (); 387 buttonPanel.add(addPattern); 388 buttonPanel.add(deletePattern); 389 return buttonPanel; 390 } 391 392 398 private class ClearPatternsListener implements ActionListener 399 { 400 public void actionPerformed(ActionEvent 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 423 private class AddPatternListener implements ActionListener 424 { 425 public void actionPerformed(ActionEvent e) 426 { 427 tableModel.addNewRow(); 428 deletePattern.setEnabled(true); 429 tableModel.fireTableDataChanged(); 430 } 431 } 432 } 433 | Popular Tags |