1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. 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; 22 import java.awt.event.ActionEvent; 23 import java.awt.event.ActionListener; 24 import java.awt.event.FocusEvent; 25 import java.awt.event.KeyEvent; 26 import java.awt.event.KeyListener; 27 28 import javax.swing.BorderFactory; 29 import javax.swing.ButtonGroup; 30 import javax.swing.JCheckBox; 31 import javax.swing.JComboBox; 32 import javax.swing.JLabel; 33 import javax.swing.JOptionPane; 34 import javax.swing.JPanel; 35 import javax.swing.JRadioButton; 36 import javax.swing.JTextField; 37 38 import org.apache.jmeter.assertions.HTMLAssertion; 39 import org.apache.jmeter.gui.util.FilePanel; 40 import org.apache.jmeter.gui.util.HorizontalPanel; 41 import org.apache.jmeter.gui.util.VerticalPanel; 42 import org.apache.jmeter.testelement.TestElement; 43 import org.apache.jmeter.util.JMeterUtils; 44 import org.apache.jorphan.logging.LoggingManager; 45 import org.apache.log.Logger; 46 47 /** 48 * GUI for HTMLAssertion 49 */ 50 public class HTMLAssertionGui extends AbstractAssertionGui implements KeyListener, ActionListener { 51 52 private static final Logger log = LoggingManager.getLoggerForClass(); 53 54 private static final long serialVersionUID = 1L; 55 56 // Names for the fields 57 private static final String WARNING_THRESHOLD_FIELD = "warningThresholdField"; // $NON-NLS-1$ 58 59 private static final String ERROR_THRESHOLD_FIELD = "errorThresholdField"; // $NON-NLS-1$ 60 61 // instance attributes 62 private JTextField errorThresholdField = null; 63 64 private JTextField warningThresholdField = null; 65 66 private JCheckBox errorsOnly = null; 67 68 private JComboBox docTypeBox = null; 69 70 private JRadioButton htmlRadioButton = null; 71 72 private JRadioButton xhtmlRadioButton = null; 73 74 private JRadioButton xmlRadioButton = null; 75 76 private FilePanel filePanel = null; 77 78 /** 79 * The constructor. 80 */ 81 public HTMLAssertionGui() { 82 init(); 83 } 84 85 /** 86 * Returns the label to be shown within the JTree-Component. 87 */ 88 public String getLabelResource() { 89 return "html_assertion_title"; // $NON-NLS-1$ 90 } 91 92 /** 93 * @see org.apache.jmeter.gui.JMeterGUIComponent#createTestElement() 94 */ 95 public TestElement createTestElement() { 96 HTMLAssertion el = new HTMLAssertion(); 97 modifyTestElement(el); 98 return el; 99 } 100 101 /** 102 * Modifies a given TestElement to mirror the data in the gui components. 103 * 104 * @see org.apache.jmeter.gui.JMeterGUIComponent#modifyTestElement(TestElement) 105 */ 106 public void modifyTestElement(TestElement inElement) { 107 108 log.debug("HTMLAssertionGui.modifyTestElement() called"); 109 110 configureTestElement(inElement); 111 112 String errorThresholdString = errorThresholdField.getText(); 113 long errorThreshold = 0; 114 115 try { 116 errorThreshold = Long.parseLong(errorThresholdString); 117 } catch (NumberFormatException e) { 118 errorThreshold = 0; 119 } 120 ((HTMLAssertion) inElement).setErrorThreshold(errorThreshold); 121 122 String warningThresholdString = warningThresholdField.getText(); 123 long warningThreshold = 0; 124 try { 125 warningThreshold = Long.parseLong(warningThresholdString); 126 } catch (NumberFormatException e) { 127 warningThreshold = 0; 128 } 129 ((HTMLAssertion) inElement).setWarningThreshold(warningThreshold); 130 131 String docTypeString = docTypeBox.getSelectedItem().toString(); 132 ((HTMLAssertion) inElement).setDoctype(docTypeString); 133 134 boolean trackErrorsOnly = errorsOnly.isSelected(); 135 ((HTMLAssertion) inElement).setErrorsOnly(trackErrorsOnly); 136 137 if (htmlRadioButton.isSelected()) { 138 ((HTMLAssertion) inElement).setHTML(); 139 } else if (xhtmlRadioButton.isSelected()) { 140 ((HTMLAssertion) inElement).setXHTML(); 141 } else { 142 ((HTMLAssertion) inElement).setXML(); 143 } 144 ((HTMLAssertion) inElement).setFilename(filePanel.getFilename()); 145 } 146 147 /** 148 * Implements JMeterGUIComponent.clearGui 149 */ 150 public void clearGui() { 151 super.clearGui(); 152 153 docTypeBox.setSelectedIndex(0); 154 htmlRadioButton.setSelected(true); 155 xhtmlRadioButton.setSelected(false); 156 xmlRadioButton.setSelected(false); 157 errorThresholdField.setText("0"); //$NON-NLS-1$ 158 warningThresholdField.setText("0"); //$NON-NLS-1$ 159 filePanel.setFilename(""); //$NON-NLS-1$ 160 errorsOnly.setSelected(false); 161 } 162 163 /** 164 * Configures the associated test element. 165 * 166 * @param inElement 167 */ 168 public void configure(TestElement inElement) { 169 super.configure(inElement); 170 HTMLAssertion lAssertion = (HTMLAssertion) inElement; 171 errorThresholdField.setText(String.valueOf(lAssertion.getErrorThreshold())); 172 warningThresholdField.setText(String.valueOf(lAssertion.getWarningThreshold())); 173 errorsOnly.setSelected(lAssertion.isErrorsOnly()); 174 docTypeBox.setSelectedItem(lAssertion.getDoctype()); 175 if (lAssertion.isHTML()) { 176 htmlRadioButton.setSelected(true); 177 } else if (lAssertion.isXHTML()) { 178 xhtmlRadioButton.setSelected(true); 179 } else { 180 xmlRadioButton.setSelected(true); 181 } 182 if (lAssertion.isErrorsOnly()) { 183 warningThresholdField.setEnabled(false); 184 warningThresholdField.setEditable(false); 185 } 186 else { 187 warningThresholdField.setEnabled(true); 188 warningThresholdField.setEditable(true); 189 } 190 filePanel.setFilename(lAssertion.getFilename()); 191 } 192 193 /** 194 * Inits the GUI. 195 */ 196 private void init() { 197 198 setLayout(new BorderLayout(0, 10)); 199 setBorder(makeBorder()); 200 201 add(makeTitlePanel(), BorderLayout.NORTH); 202 203 JPanel mainPanel = new JPanel(new BorderLayout()); 204 205 // USER_INPUT 206 VerticalPanel assertionPanel = new VerticalPanel(); 207 assertionPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Tidy Settings")); 208 209 // doctype 210 HorizontalPanel docTypePanel = new HorizontalPanel(); 211 docTypeBox = new JComboBox(new Object[] { "omit", "auto", "strict", "loose" }); 212 // docTypePanel.add(new 213 // JLabel(JMeterUtils.getResString("duration_assertion_label"))); //$NON-NLS-1$ 214 docTypePanel.add(new JLabel("Doctype:")); 215 docTypePanel.add(docTypeBox); 216 assertionPanel.add(docTypePanel); 217 218 // format (HMTL, XHTML, XML) 219 VerticalPanel formatPanel = new VerticalPanel(); 220 formatPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Format")); 221 htmlRadioButton = new JRadioButton("HTML", true); //$NON-NLS-1$ 222 xhtmlRadioButton = new JRadioButton("XHTML", false); //$NON-NLS-1$ 223 xmlRadioButton = new JRadioButton("XML", false); //$NON-NLS-1$ 224 ButtonGroup buttonGroup = new ButtonGroup(); 225 buttonGroup.add(htmlRadioButton); 226 buttonGroup.add(xhtmlRadioButton); 227 buttonGroup.add(xmlRadioButton); 228 formatPanel.add(htmlRadioButton); 229 formatPanel.add(xhtmlRadioButton); 230 formatPanel.add(xmlRadioButton); 231 assertionPanel.add(formatPanel); 232 233 // errors only 234 errorsOnly = new JCheckBox("Errors only", false); 235 errorsOnly.addActionListener(this); 236 assertionPanel.add(errorsOnly); 237 238 // thresholds 239 HorizontalPanel thresholdPanel = new HorizontalPanel(); 240 thresholdPanel.add(new JLabel("Error threshold:")); 241 errorThresholdField = new JTextField("0", 5); // $NON-NLS-1$ 242 errorThresholdField.setName(ERROR_THRESHOLD_FIELD); 243 errorThresholdField.addKeyListener(this); 244 thresholdPanel.add(errorThresholdField); 245 thresholdPanel.add(new JLabel("Warning threshold:")); 246 warningThresholdField = new JTextField("0", 5); // $NON-NLS-1$ 247 warningThresholdField.setName(WARNING_THRESHOLD_FIELD); 248 warningThresholdField.addKeyListener(this); 249 thresholdPanel.add(warningThresholdField); 250 assertionPanel.add(thresholdPanel); 251 252 // file panel 253 filePanel = new FilePanel(JMeterUtils.getResString("html_assertion_file"), ".txt"); //$NON-NLS-1$ //$NON-NLS-2$ 254 assertionPanel.add(filePanel); 255 256 mainPanel.add(assertionPanel, BorderLayout.NORTH); 257 add(mainPanel, BorderLayout.CENTER); 258 } 259 260 /** 261 * This method is called if one of the threshold field looses the focus 262 * 263 * @param inEvent 264 */ 265 public void focusLost(FocusEvent inEvent) { 266 log.debug("HTMLAssertionGui.focusLost() called"); 267 268 String errorThresholdString = errorThresholdField.getText(); 269 if (errorThresholdString != null) { 270 boolean isInvalid = false; 271 try { 272 long errorThreshold = Long.parseLong(errorThresholdString); 273 if (errorThreshold < 0) { 274 isInvalid = true; 275 } 276 } catch (NumberFormatException ex) { 277 isInvalid = true; 278 } 279 if (isInvalid) { 280 log.warn("HTMLAssertionGui: Error threshold Not a valid number!"); 281 JOptionPane.showMessageDialog(null, "Threshold for errors is invalid", "Error", 282 JOptionPane.ERROR_MESSAGE); 283 } 284 } 285 286 String warningThresholdString = warningThresholdField.getText(); 287 if (warningThresholdString != null) { 288 boolean isInvalid = false; 289 try { 290 long warningThreshold = Long.parseLong(warningThresholdString); 291 if (warningThreshold < 0) { 292 isInvalid = true; 293 } 294 } catch (NumberFormatException ex) { 295 isInvalid = true; 296 } 297 if (isInvalid) { 298 log.warn("HTMLAssertionGui: Error threshold Not a valid number!"); 299 JOptionPane.showMessageDialog(null, "Threshold for warnings is invalid", "Error", 300 JOptionPane.ERROR_MESSAGE); 301 } 302 } 303 } 304 305 /** 306 * @see java.awt.event.FocusListener#focusGained(java.awt.event.FocusEvent) 307 */ 308 public void focusGained(FocusEvent e) { 309 310 } 311 312 /** 313 * This method is called from erros-only checkbox 314 * 315 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent) 316 */ 317 public void actionPerformed(ActionEvent e) { 318 if (errorsOnly.isSelected()) { 319 warningThresholdField.setEnabled(false); 320 warningThresholdField.setEditable(false); 321 } else { 322 warningThresholdField.setEnabled(true); 323 warningThresholdField.setEditable(true); 324 } 325 } 326 327 public void keyPressed(KeyEvent e) { 328 } 329 330 public void keyReleased(KeyEvent e) { 331 String fieldName = e.getComponent().getName(); 332 333 if (fieldName.equals(WARNING_THRESHOLD_FIELD)) { 334 validateInteger(warningThresholdField); 335 } 336 337 if (fieldName.equals(ERROR_THRESHOLD_FIELD)) { 338 validateInteger(errorThresholdField); 339 } 340 } 341 342 private void validateInteger(JTextField field){ 343 try { 344 Integer.parseInt(field.getText()); 345 } catch (NumberFormatException nfe) { 346 int length = field.getText().length(); 347 if (length > 0) { 348 JOptionPane.showMessageDialog(this, "Only digits allowed", "Invalid data", 349 JOptionPane.WARNING_MESSAGE); 350 // Drop the last character: 351 field.setText(field.getText().substring(0, length-1)); 352 } 353 } 354 355 } 356 public void keyTyped(KeyEvent e) { 357 } 358 359 }