1 18 19 package org.apache.jmeter.assertions.gui; 20 21 import java.awt.event.ActionEvent ; 22 import java.awt.event.ActionListener ; 23 import java.awt.event.FocusEvent ; 24 import java.awt.event.FocusListener ; 25 26 import javax.swing.BorderFactory ; 27 import javax.swing.Box ; 28 import javax.swing.ButtonGroup ; 29 import javax.swing.JLabel ; 30 import javax.swing.JOptionPane ; 31 import javax.swing.JPanel ; 32 import javax.swing.JRadioButton ; 33 import javax.swing.JTextField ; 34 35 import org.apache.jmeter.assertions.SizeAssertion; 36 import org.apache.jmeter.testelement.TestElement; 37 import org.apache.jmeter.util.JMeterUtils; 38 import org.apache.jorphan.gui.layout.VerticalLayout; 39 import org.apache.jorphan.logging.LoggingManager; 40 import org.apache.log.Logger; 41 42 45 public class SizeAssertionGui 46 extends AbstractAssertionGui 47 implements FocusListener , ActionListener 48 { 49 transient private static Logger log = LoggingManager.getLoggerForClass(); 50 51 private JTextField size; 52 private JRadioButton equalButton, 53 notequalButton, 54 greaterthanButton, 55 lessthanButton, 56 greaterthanequalButton, 57 lessthanequalButton; 58 private int execState; 60 public SizeAssertionGui() 61 { 62 init(); 63 } 64 65 public String getLabelResource() 66 { 67 return "size_assertion_title"; 68 } 69 70 public String getSizeAttributesTitle() 71 { 72 return JMeterUtils.getResString("size_assertion_size_test"); 73 } 74 75 public TestElement createTestElement() 76 { 77 SizeAssertion el = new SizeAssertion(); 78 modifyTestElement(el); 79 return el; 80 } 81 82 86 public void modifyTestElement(TestElement el) 87 { 88 configureTestElement(el); 89 String sizeString = size.getText(); 90 long assertionSize = 0; 91 try 92 { 93 assertionSize = Long.parseLong(sizeString); 94 } 95 catch (NumberFormatException e) 96 { 97 assertionSize = Long.MAX_VALUE; 98 } 99 ((SizeAssertion) el).setAllowedSize(assertionSize); 100 ((SizeAssertion) el).setCompOper(getState()); 101 } 102 103 public void configure(TestElement el) 104 { 105 super.configure(el); 106 SizeAssertion assertion = (SizeAssertion) el; 107 size.setText(String.valueOf(assertion.getAllowedSize())); 108 setState(assertion.getCompOper()); 109 } 110 111 114 public void setState(int state) 115 { 116 if (state == SizeAssertion.EQUAL) 117 { 118 equalButton.setSelected(true); 119 execState = state; 120 } 121 else if (state == SizeAssertion.NOTEQUAL) 122 { 123 notequalButton.setSelected(true); 124 execState = state; 125 } 126 else if (state == SizeAssertion.GREATERTHAN) 127 { 128 greaterthanButton.setSelected(true); 129 execState = state; 130 } 131 else if (state == SizeAssertion.LESSTHAN) 132 { 133 lessthanButton.setSelected(true); 134 execState = state; 135 } 136 else if (state == SizeAssertion.GREATERTHANEQUAL) 137 { 138 greaterthanequalButton.setSelected(true); 139 execState = state; 140 } 141 else if (state == SizeAssertion.LESSTHANEQUAL) 142 { 143 lessthanequalButton.setSelected(true); 144 execState = state; 145 } 146 } 147 148 151 public int getState() 152 { 153 return execState; 154 } 155 156 private void init() 157 { 158 setLayout( 159 new VerticalLayout(5, VerticalLayout.LEFT, VerticalLayout.TOP)); 160 setBorder(makeBorder()); 161 162 add(makeTitlePanel()); 163 164 JPanel sizePanel = new JPanel (); 166 sizePanel.setBorder( 167 BorderFactory.createTitledBorder( 168 BorderFactory.createEtchedBorder(), 169 getSizeAttributesTitle())); 170 171 sizePanel.add( 172 new JLabel (JMeterUtils.getResString("size_assertion_label"))); 173 size = new JTextField (5); 174 size.addFocusListener(this); 175 sizePanel.add(size); 176 177 sizePanel.add(createComparatorButtonPanel()); 178 179 add(sizePanel); 180 } 181 182 private Box createComparatorButtonPanel() 183 { 184 ButtonGroup group = new ButtonGroup (); 185 186 equalButton = createComparatorButton("=", SizeAssertion.EQUAL, group); 187 notequalButton = 188 createComparatorButton("!=", SizeAssertion.NOTEQUAL, group); 189 greaterthanButton = 190 createComparatorButton(">", SizeAssertion.GREATERTHAN, group); 191 lessthanButton = 192 createComparatorButton("<", SizeAssertion.LESSTHAN, group); 193 greaterthanequalButton = 194 createComparatorButton(">=", SizeAssertion.GREATERTHANEQUAL, group); 195 lessthanequalButton = 196 createComparatorButton("<=", SizeAssertion.LESSTHANEQUAL, group); 197 198 equalButton.setSelected(true); 199 execState = Integer.parseInt(equalButton.getActionCommand()); 200 201 Box checkPanel = Box.createVerticalBox(); 203 JLabel compareLabel = 204 new JLabel ( 205 JMeterUtils.getResString("size_assertion_comparator_label")); 206 checkPanel.add(compareLabel); 207 checkPanel.add(equalButton); 208 checkPanel.add(notequalButton); 209 checkPanel.add(greaterthanButton); 210 checkPanel.add(lessthanButton); 211 checkPanel.add(greaterthanequalButton); 212 checkPanel.add(lessthanequalButton); 213 return checkPanel; 214 } 215 216 private JRadioButton createComparatorButton( 217 String name, 218 int value, 219 ButtonGroup group) 220 { 221 JRadioButton button = new JRadioButton (name); 222 button.setActionCommand(String.valueOf(value)); 223 button.addActionListener(this); 224 group.add(button); 225 return button; 226 } 227 228 public void focusLost(FocusEvent e) 229 { 230 boolean isInvalid = false; 231 String sizeString = size.getText(); 232 if (sizeString != null) 233 { 234 try 235 { 236 long assertionSize = Long.parseLong(sizeString); 237 if (assertionSize < 0) 238 { 239 isInvalid = true; 240 } 241 } 242 catch (NumberFormatException ex) 243 { 244 isInvalid = true; 245 } 246 if (isInvalid) 247 { 248 log.warn("SizeAssertionGui: Not a valid number!"); 249 JOptionPane.showMessageDialog( 250 null, 251 JMeterUtils.getResString("size_assertion_input_error"), 252 "Error", 253 JOptionPane.ERROR_MESSAGE); 254 } 255 } 256 } 257 258 public void focusGained(FocusEvent e) 259 { 260 } 261 262 public void actionPerformed(ActionEvent e) 263 { 264 int comparator = Integer.parseInt(e.getActionCommand()); 265 execState = comparator; 266 } 267 } 268 | Popular Tags |