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.protocol.java.control.gui; 20 21 import java.awt.BorderLayout; 22 import java.awt.event.ActionEvent; 23 import java.awt.event.ActionListener; 24 import java.io.File; 25 import java.io.IOException; 26 import java.lang.reflect.Method; 27 import java.util.List; 28 29 import javax.swing.JCheckBox; 30 import javax.swing.JComboBox; 31 import javax.swing.JLabel; 32 import javax.swing.JOptionPane; 33 import javax.swing.JPanel; 34 import javax.swing.event.ChangeEvent; 35 import javax.swing.event.ChangeListener; 36 37 import junit.framework.TestCase; 38 39 import org.apache.jmeter.gui.util.VerticalPanel; 40 import org.apache.jmeter.protocol.java.sampler.JUnitSampler; 41 import org.apache.jmeter.samplers.gui.AbstractSamplerGui; 42 import org.apache.jmeter.testelement.TestElement; 43 import org.apache.jmeter.util.JMeterUtils; 44 import org.apache.jorphan.gui.JLabeledTextField; 45 import org.apache.jorphan.logging.LoggingManager; 46 import org.apache.jorphan.reflect.ClassFinder; 47 import org.apache.jorphan.util.JOrphanUtils; 48 import org.apache.log.Logger; 49 50 /** 51 * The <code>JUnitTestSamplerGui</code> class provides the user interface 52 * for the {@link JUnitSampler}. 53 * 54 */ 55 public class JUnitTestSamplerGui extends AbstractSamplerGui 56 implements ChangeListener, ActionListener 57 { 58 private static final Logger log = LoggingManager.getLoggerForClass(); 59 60 /** The name of the classnameCombo JComboBox */ 61 private static final String CLASSNAMECOMBO = "classnamecombo"; //$NON-NLS-1$ 62 private static final String METHODCOMBO = "methodcombo"; //$NON-NLS-1$ 63 private static final String PREFIX = "test"; //$NON-NLS-1$ 64 65 // Names of JUnit methods 66 private static final String ONETIMESETUP = "oneTimeSetUp"; //$NON-NLS-1$ 67 private static final String ONETIMETEARDOWN = "oneTimeTearDown"; //$NON-NLS-1$ 68 private static final String SUITE = "suite"; //$NON-NLS-1$ 69 70 private static final String[] SPATHS; 71 72 static { 73 String paths[]; 74 String ucp = JMeterUtils.getProperty("user.classpath"); 75 if (ucp!=null){ 76 String parts[] = ucp.split(File.pathSeparator); 77 paths = new String[parts.length+1]; 78 paths[0] = JMeterUtils.getJMeterHome() + "/lib/junit/"; //$NON-NLS-1$ 79 for(int i=0; i < parts.length; i++){ 80 paths[i+1]=parts[i]; 81 } 82 } else { 83 paths = new String[]{ 84 JMeterUtils.getJMeterHome() + "/lib/junit/" //$NON-NLS-1$ 85 }; 86 } 87 SPATHS = paths; 88 } 89 90 private JLabeledTextField constructorLabel = 91 new JLabeledTextField( 92 JMeterUtils.getResString("junit_constructor_string")); //$NON-NLS-1$ 93 94 private JLabel methodLabel = 95 new JLabel( 96 JMeterUtils.getResString("junit_test_method")); //$NON-NLS-1$ 97 98 private JLabeledTextField successMsg = 99 new JLabeledTextField( 100 JMeterUtils.getResString("junit_success_msg")); //$NON-NLS-1$ 101 102 private JLabeledTextField failureMsg = 103 new JLabeledTextField( 104 JMeterUtils.getResString("junit_failure_msg")); //$NON-NLS-1$ 105 106 private JLabeledTextField errorMsg = 107 new JLabeledTextField( 108 JMeterUtils.getResString("junit_error_msg")); //$NON-NLS-1$ 109 110 private JLabeledTextField successCode = 111 new JLabeledTextField( 112 JMeterUtils.getResString("junit_success_code")); //$NON-NLS-1$ 113 114 private JLabeledTextField failureCode = 115 new JLabeledTextField( 116 JMeterUtils.getResString("junit_failure_code")); //$NON-NLS-1$ 117 118 private JLabeledTextField errorCode = 119 new JLabeledTextField( 120 JMeterUtils.getResString("junit_error_code")); //$NON-NLS-1$ 121 122 private JLabeledTextField filterpkg = 123 new JLabeledTextField( 124 JMeterUtils.getResString("junit_pkg_filter")); //$NON-NLS-1$ 125 126 private JCheckBox doSetup = new JCheckBox(JMeterUtils.getResString("junit_do_setup_teardown")); //$NON-NLS-1$ 127 private JCheckBox appendError = new JCheckBox(JMeterUtils.getResString("junit_append_error")); //$NON-NLS-1$ 128 private JCheckBox appendExc = new JCheckBox(JMeterUtils.getResString("junit_append_exception")); //$NON-NLS-1$ 129 130 /** A combo box allowing the user to choose a test class. */ 131 private JComboBox classnameCombo; 132 private JComboBox methodName; 133 private transient TestCase TESTCLASS = null; 134 private List METHODLIST = null; 135 136 private transient ClassFilter FILTER = new ClassFilter(); 137 private List CLASSLIST = null; 138 139 /** 140 * Constructor for JUnitTestSamplerGui 141 */ 142 public JUnitTestSamplerGui() 143 { 144 super(); 145 init(); 146 } 147 148 public String getLabelResource() 149 { 150 return "junit_request"; //$NON-NLS-1$ 151 } 152 153 /** 154 * Initialize the GUI components and layout. 155 */ 156 private void init() 157 { 158 setLayout(new BorderLayout(0, 5)); 159 setBorder(makeBorder()); 160 161 add(makeTitlePanel(), BorderLayout.NORTH); 162 163 164 add(createClassPanel(), BorderLayout.CENTER); 165 } 166 167 private JPanel createClassPanel() 168 { 169 METHODLIST = new java.util.ArrayList(); 170 171 try 172 { 173 // Find all the classes which extend junit.framework.TestCase 174 CLASSLIST = 175 ClassFinder.findClassesThatExtend( 176 SPATHS, 177 new Class[] { TestCase.class }); 178 } 179 catch (IOException e) 180 { 181 log.error("Exception getting interfaces.", e); 182 } 183 184 JLabel label = 185 new JLabel(JMeterUtils.getResString("protocol_java_classname")); //$NON-NLS-1$ 186 187 classnameCombo = new JComboBox(CLASSLIST.toArray()); 188 classnameCombo.addActionListener(this); 189 classnameCombo.setName(CLASSNAMECOMBO); 190 classnameCombo.setEditable(false); 191 label.setLabelFor(classnameCombo); 192 193 if (FILTER != null && FILTER.size() > 0) { 194 methodName = new JComboBox(FILTER.filterArray(METHODLIST)); 195 } else { 196 methodName = new JComboBox(METHODLIST.toArray()); 197 } 198 methodName.addActionListener(this); 199 methodName.setName(METHODCOMBO); 200 methodLabel.setLabelFor(methodName); 201 202 VerticalPanel panel = new VerticalPanel(); 203 panel.add(filterpkg); 204 panel.add(label); 205 filterpkg.addChangeListener(this); 206 207 if (classnameCombo != null){ 208 panel.add(classnameCombo); 209 } 210 constructorLabel.setText(""); 211 panel.add(constructorLabel); 212 panel.add(methodLabel); 213 if (methodName != null){ 214 panel.add(methodName); 215 } 216 panel.add(successMsg); 217 panel.add(successCode); 218 panel.add(failureMsg); 219 panel.add(failureCode); 220 panel.add(errorMsg); 221 panel.add(errorCode); 222 panel.add(doSetup); 223 panel.add(appendError); 224 panel.add(appendExc); 225 return panel; 226 } 227 228 private void initGui(){ // TODO - unfinished? 229 appendError.setSelected(false); 230 appendExc.setSelected(false); 231 doSetup.setSelected(false); 232 filterpkg.setText(""); //$NON-NLS-1$ 233 constructorLabel.setText(""); //$NON-NLS-1$ 234 successCode.setText(JMeterUtils.getResString("junit_success_default_code")); //$NON-NLS-1$ 235 successMsg.setText(JMeterUtils.getResString("junit_success_default_msg")); //$NON-NLS-1$ 236 failureCode.setText(JMeterUtils.getResString("junit_failure_default_code")); //$NON-NLS-1$ 237 failureMsg.setText(JMeterUtils.getResString("junit_failure_default_msg")); //$NON-NLS-1$ 238 errorMsg.setText(JMeterUtils.getResString("junit_error_default_msg")); //$NON-NLS-1$ 239 errorCode.setText(JMeterUtils.getResString("junit_error_default_code")); //$NON-NLS-1$ 240 } 241 242 public void clearGui() { 243 super.clearGui(); 244 initGui(); 245 } 246 247 /* Implements JMeterGuiComponent.createTestElement() */ 248 public TestElement createTestElement() 249 { 250 JUnitSampler sampler = new JUnitSampler(); 251 modifyTestElement(sampler); 252 return sampler; 253 } 254 255 /* Implements JMeterGuiComponent.modifyTestElement(TestElement) */ 256 public void modifyTestElement(TestElement el) 257 { 258 JUnitSampler sampler = (JUnitSampler)el; 259 configureTestElement(sampler); 260 if (classnameCombo.getSelectedItem() != null && 261 classnameCombo.getSelectedItem() instanceof String) { 262 sampler.setClassname((String)classnameCombo.getSelectedItem()); 263 } 264 sampler.setConstructorString(constructorLabel.getText()); 265 if (methodName.getSelectedItem() != null) { 266 Object mobj = methodName.getSelectedItem(); 267 sampler.setMethod((String)mobj); 268 } 269 sampler.setFilterString(filterpkg.getText()); 270 sampler.setSuccess(successMsg.getText()); 271 sampler.setSuccessCode(successCode.getText()); 272 sampler.setFailure(failureMsg.getText()); 273 sampler.setFailureCode(failureCode.getText()); 274 sampler.setDoNotSetUpTearDown(doSetup.isSelected()); 275 sampler.setAppendError(appendError.isSelected()); 276 sampler.setAppendException(appendExc.isSelected()); 277 } 278 279 /* Overrides AbstractJMeterGuiComponent.configure(TestElement) */ 280 public void configure(TestElement el) 281 { 282 super.configure(el); 283 JUnitSampler sampler = (JUnitSampler)el; 284 classnameCombo.setSelectedItem(sampler.getClassname()); 285 instantiateClass(); 286 methodName.setSelectedItem(sampler.getMethod()); 287 filterpkg.setText(sampler.getFilterString()); 288 constructorLabel.setText(sampler.getConstructorString()); 289 if (sampler.getSuccessCode().length() > 0) { 290 successCode.setText(sampler.getSuccessCode()); 291 } else { 292 successCode.setText(JMeterUtils.getResString("junit_success_default_code")); //$NON-NLS-1$ 293 } 294 if (sampler.getSuccess().length() > 0) { 295 successMsg.setText(sampler.getSuccess()); 296 } else { 297 successMsg.setText(JMeterUtils.getResString("junit_success_default_msg")); //$NON-NLS-1$ 298 } 299 if (sampler.getFailureCode().length() > 0) { 300 failureCode.setText(sampler.getFailureCode()); 301 } else { 302 failureCode.setText(JMeterUtils.getResString("junit_failure_default_code")); //$NON-NLS-1$ 303 } 304 if (sampler.getFailure().length() > 0) { 305 failureMsg.setText(sampler.getFailure()); 306 } else { 307 failureMsg.setText(JMeterUtils.getResString("junit_failure_default_msg")); //$NON-NLS-1$ 308 } 309 if (sampler.getError().length() > 0) { 310 errorMsg.setText(sampler.getError()); 311 } else { 312 errorMsg.setText(JMeterUtils.getResString("junit_error_default_msg")); //$NON-NLS-1$ 313 } 314 if (sampler.getErrorCode().length() > 0) { 315 errorCode.setText(sampler.getErrorCode()); 316 } else { 317 errorCode.setText(JMeterUtils.getResString("junit_error_default_code")); //$NON-NLS-1$ 318 } 319 doSetup.setSelected(sampler.getDoNotSetUpTearDown()); 320 appendError.setSelected(sampler.getAppendError()); 321 appendExc.setSelected(sampler.getAppendException()); 322 } 323 324 public void instantiateClass(){ 325 String className = 326 ((String) classnameCombo.getSelectedItem()); 327 if (className != null) { 328 TESTCLASS = (TestCase)JUnitSampler.getClassInstance(className, 329 constructorLabel.getText()); 330 if (TESTCLASS == null) { 331 clearMethodCombo(); 332 } 333 configureMethodCombo(); 334 } 335 } 336 337 public void showErrorDialog() { 338 JOptionPane.showConfirmDialog(this, 339 JMeterUtils.getResString("junit_constructor_error"), //$NON-NLS-1$ 340 "Warning", 341 JOptionPane.OK_CANCEL_OPTION, JOptionPane.ERROR_MESSAGE); 342 } 343 344 public void configureMethodCombo(){ 345 if (TESTCLASS != null) { 346 clearMethodCombo(); 347 String [] names = getMethodNames(getMethods(TESTCLASS,METHODLIST)); 348 for (int idx=0; idx < names.length; idx++){ 349 methodName.addItem(names[idx]); 350 METHODLIST.add(names[idx]); 351 } 352 methodName.repaint(); 353 } 354 } 355 356 public void clearMethodCombo(){ 357 methodName.removeAllItems(); 358 METHODLIST.clear(); 359 } 360 361 public Method[] getMethods(Object obj, List list) 362 { 363 Method[] meths = obj.getClass().getMethods(); 364 for (int idx=0; idx < meths.length; idx++){ 365 if (meths[idx].getName().startsWith(PREFIX) || 366 meths[idx].getName().equals(ONETIMESETUP) || 367 meths[idx].getName().equals(ONETIMETEARDOWN) || 368 meths[idx].getName().equals(SUITE)) { 369 list.add(meths[idx]); 370 } 371 } 372 if (list.size() > 0){ 373 Method[] rmeth = new Method[list.size()]; 374 return (Method[])list.toArray(rmeth); 375 } 376 return new Method[0]; 377 } 378 379 public String[] getMethodNames(Method[] meths) 380 { 381 String[] names = new String[meths.length]; 382 for (int idx=0; idx < meths.length; idx++){ 383 names[idx] = meths[idx].getName(); 384 } 385 return names; 386 } 387 388 public Class[] filterClasses(Class[] clz) { 389 if (clz != null && clz.length > 0){ 390 Class[] nclz = null; 391 return nclz; 392 } 393 return clz; 394 } 395 396 /** 397 * Handle action events for this component. This method currently handles 398 * events for the classname combo box. 399 * 400 * @param evt the ActionEvent to be handled 401 */ 402 public void actionPerformed(ActionEvent evt) 403 { 404 if (evt.getSource() == classnameCombo) 405 { 406 instantiateClass(); 407 } 408 } 409 410 /** 411 * the current implementation checks to see if the source 412 * of the event is the filterpkg field. 413 */ 414 public void stateChanged(ChangeEvent event) { 415 if ( event.getSource() == filterpkg) { 416 FILTER.setPackges(JOrphanUtils.split(filterpkg.getText(),",")); //$NON-NLS-1$ 417 classnameCombo.removeAllItems(); 418 // change the classname drop down 419 Object[] clist = FILTER.filterArray(CLASSLIST); 420 for (int idx=0; idx < clist.length; idx++) { 421 classnameCombo.addItem(clist[idx]); 422 } 423 } 424 } 425 } 426