1 18 19 package org.apache.jmeter.protocol.http.control.gui; 20 21 import java.awt.Font ; 22 23 import javax.swing.JLabel ; 24 import javax.swing.JPanel ; 25 import javax.swing.JCheckBox ; 26 import javax.swing.border.Border ; 27 import javax.swing.border.EmptyBorder ; 28 import javax.swing.event.ChangeListener ; 29 import javax.swing.event.ChangeEvent ; 30 import javax.swing.JOptionPane ; 31 32 import org.apache.jmeter.protocol.http.sampler.AccessLogSampler; 33 import org.apache.jmeter.samplers.gui.AbstractSamplerGui; 34 import org.apache.jmeter.testelement.TestElement; 35 import org.apache.jmeter.util.JMeterUtils; 36 import org.apache.jmeter.gui.UnsharedComponent; 37 import org.apache.jmeter.gui.util.FilePanel; 38 import org.apache.jorphan.gui.JLabeledTextField; 39 import org.apache.jorphan.gui.layout.VerticalLayout; 40 41 import junit.framework.TestCase; 42 43 75 public class AccessLogSamplerGui 76 extends AbstractSamplerGui 77 implements ChangeListener , UnsharedComponent 78 { 79 80 JLabeledTextField parserClassName = 81 new JLabeledTextField(JMeterUtils.getResString("log_parser")); 82 JLabeledTextField generatorClassName = 83 new JLabeledTextField(JMeterUtils.getResString("generator")); 84 JLabeledTextField HOSTNAME = 85 new JLabeledTextField(JMeterUtils.getResString("servername")); 86 JLabeledTextField PORT = 87 new JLabeledTextField(JMeterUtils.getResString("port")); 88 FilePanel logFile = 89 new FilePanel(JMeterUtils.getResString("log_file"), ".txt"); 90 private JCheckBox getImages; 91 92 protected int PORTNUMBER = 80; 93 94 public String DEFAULT_GENERATOR = 95 "org.apache.jmeter.protocol.http.util.accesslog.StandardGenerator"; 96 public String DEFAULT_PARSER = 97 "org.apache.jmeter.protocol.http.util.accesslog.TCLogParser"; 98 private AccessLogSampler SAMPLER = null; 99 100 103 Font plainText = new Font ("plain", Font.PLAIN, 10); 104 105 JLabel noteMessage = 106 new JLabel (JMeterUtils.getResString("als_message")); 107 JLabel noteMessage2 = 108 new JLabel (JMeterUtils.getResString("als_message2")); 109 JLabel noteMessage3 = 110 new JLabel (JMeterUtils.getResString("als_message3")); 111 112 public AccessLogSamplerGui() 113 { 114 init(); 115 } 116 117 120 public String getLabelResource() 121 { 122 return "log_sampler"; 123 } 124 125 128 public TestElement createTestElement() 129 { 130 if (SAMPLER == null){ 131 SAMPLER = new AccessLogSampler(); 132 this.configureTestElement(SAMPLER); 133 SAMPLER.setParserClassName(parserClassName.getText()); 134 SAMPLER.setGeneratorClassName(generatorClassName.getText()); 135 SAMPLER.setLogFile(logFile.getFilename()); 136 SAMPLER.setDomain(HOSTNAME.getText()); 137 SAMPLER.setPort(getPortNumber()); 138 139 } 140 return SAMPLER; 141 } 142 143 149 public int getPortNumber(){ 150 try { 151 int port = Integer.parseInt(PORT.getText()); 152 return port; 153 } catch (NumberFormatException exception){ 154 return 80; 158 } 159 } 160 161 165 public void modifyTestElement(TestElement s) 166 { 167 SAMPLER = (AccessLogSampler) s; 168 this.configureTestElement(SAMPLER); 169 SAMPLER.setParserClassName(parserClassName.getText()); 170 SAMPLER.setGeneratorClassName(generatorClassName.getText()); 171 SAMPLER.setLogFile(logFile.getFilename()); 172 SAMPLER.setDomain(HOSTNAME.getText()); 173 SAMPLER.setPort(getPortNumber()); 174 if (getImages.isSelected()){ 175 SAMPLER.setImageParser(true); 176 } else { 177 SAMPLER.setImageParser(false); 178 } 179 } 180 181 185 private void init() 186 { 187 this.setLayout( 188 new VerticalLayout(5, VerticalLayout.LEFT, VerticalLayout.TOP)); 189 190 JPanel mainPanel = new JPanel (); 192 Border margin = new EmptyBorder (10, 10, 5, 10); 193 mainPanel.setBorder(margin); 194 mainPanel.setLayout(new VerticalLayout(5, VerticalLayout.LEFT)); 195 196 JLabel panelTitleLabel = new JLabel (getStaticLabel()); 198 Font curFont = panelTitleLabel.getFont(); 199 int curFontSize = curFont.getSize(); 200 curFontSize += 4; 201 panelTitleLabel.setFont( 202 new Font (curFont.getFontName(), curFont.getStyle(), curFontSize)); 203 mainPanel.add(panelTitleLabel); 204 mainPanel.add(getNamePanel()); 206 mainPanel.add(HOSTNAME); 207 mainPanel.add(PORT); 208 209 mainPanel.add(parserClassName); 210 mainPanel.add(generatorClassName); 211 mainPanel.add(logFile); 212 HOSTNAME.addChangeListener(this); 213 PORT.addChangeListener(this); 214 logFile.addChangeListener(this); 215 parserClassName.addChangeListener(this); 216 generatorClassName.addChangeListener(this); 217 218 JPanel retrieveImagesPanel = new JPanel (); 220 getImages = 221 new JCheckBox ( 222 JMeterUtils.getResString("web_testing_retrieve_images")); 223 retrieveImagesPanel.add(getImages); 224 mainPanel.add(retrieveImagesPanel); 225 mainPanel.add(noteMessage); 226 mainPanel.add(noteMessage2); 227 mainPanel.add(noteMessage3); 228 229 this.add(mainPanel); 230 } 231 232 236 public void configure(TestElement el) 237 { 238 super.configure(el); 239 SAMPLER = (AccessLogSampler) el; 240 if (SAMPLER.getParserClassName().length() > 0){ 241 parserClassName.setText(SAMPLER.getParserClassName()); 242 } else { 243 parserClassName.setText(this.DEFAULT_PARSER); 244 } 245 if (SAMPLER.getGeneratorClassName().length() > 0){ 246 generatorClassName.setText(SAMPLER.getGeneratorClassName()); 247 } else { 248 generatorClassName.setText(this.DEFAULT_GENERATOR); 249 } 250 logFile.setFilename(SAMPLER.getLogFile()); 251 HOSTNAME.setText(SAMPLER.getDomain()); 252 PORT.setText(String.valueOf(SAMPLER.getPort())); 253 getImages.setSelected(SAMPLER.isImageParser()); 254 255 } 256 257 263 public void stateChanged(ChangeEvent event) 264 { 265 if (event.getSource() == parserClassName) 266 { 267 SAMPLER.setParserClassName(parserClassName.getText()); 268 handleParserEvent(); 269 } 270 if (event.getSource() == logFile) 271 { 272 } 274 if (event.getSource() == generatorClassName){ 275 SAMPLER.setGeneratorClassName(generatorClassName.getText()); 276 handleGeneratorEvent(); 277 } 278 if (event.getSource() == HOSTNAME){ 279 } 280 if (event.getSource() == PORT){ 281 } 282 } 283 284 289 public void handleParserEvent(){ 290 if(!SAMPLER.checkParser()){ 291 JOptionPane.showConfirmDialog( 293 this, 294 JMeterUtils.getResString("log_parser_cnf_msg"), 295 "Warning", 296 JOptionPane.OK_CANCEL_OPTION, 297 JOptionPane.ERROR_MESSAGE); 298 } 299 } 300 301 306 public void handleGeneratorEvent(){ 307 if(!SAMPLER.checkGenerator()){ 308 JOptionPane.showConfirmDialog( 310 this, 311 JMeterUtils.getResString("generator_cnf_msg"), 312 "Warning", 313 JOptionPane.OK_CANCEL_OPTION, 314 JOptionPane.ERROR_MESSAGE); 315 } 316 } 317 318 322 public static class Test extends TestCase 323 { 324 AccessLogSamplerGui gui; 325 326 public Test(String name) 327 { 328 super(name); 329 } 330 331 public void setUp() 332 { 333 gui = new AccessLogSamplerGui(); 334 } 335 336 public void testCloneSampler() throws Exception 337 { 338 AccessLogSampler sampler = (AccessLogSampler)gui.createTestElement(); 339 sampler.addArgument("param","value"); 340 AccessLogSampler clonedSampler = (AccessLogSampler)sampler.clone(); 341 clonedSampler.setRunningVersion(true); 342 sampler.getArguments().getArgument(0).setValue("new value"); 343 assertEquals( 344 "Sampler didn't clone correctly", 345 "new value", 346 sampler.getArguments().getArgument(0).getValue()); 347 } 348 } 349 350 } 351 | Popular Tags |