You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jmeter-dev@jakarta.apache.org by js...@apache.org on 2004/02/26 00:13:44 UTC

cvs commit: jakarta-jmeter/src/core/org/apache/jmeter/samplers ExampleSampler.java

jsalvata    2004/02/25 15:13:44

  Added:       src/examples/org/apache/jmeter/examples/sampler/gui
                        ExampleSamplerGui.java
               src/examples/org/apache/jmeter/examples/sampler
                        ExampleSampler.java
  Removed:     src/core/org/apache/jmeter/samplers/gui
                        ExampleSamplerGui.java
               src/core/org/apache/jmeter/samplers ExampleSampler.java
  Log:
  Moved the ExampleSampler to the examples source tree.
  
  Revision  Changes    Path
  1.1                  jakarta-jmeter/src/examples/org/apache/jmeter/examples/sampler/gui/ExampleSamplerGui.java
  
  Index: ExampleSamplerGui.java
  ===================================================================
  // $Header: /home/cvs/jakarta-jmeter/src/examples/org/apache/jmeter/examples/sampler/gui/ExampleSamplerGui.java,v 1.1 2004/02/25 23:13:44 jsalvata Exp $
  /*
   * Copyright 2004 The Apache Software Foundation.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *   http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   * 
  */
  
  
  /*
   * Example Sampler GUI (non-beans version)
   */
   
  package org.apache.jmeter.examples.sampler.gui;
  
  import java.awt.BorderLayout;
  import java.awt.Component;
  
  import javax.swing.JLabel;
  import javax.swing.JPanel;
  import javax.swing.JTextField;
  
  import org.apache.jmeter.examples.sampler.ExampleSampler;
  import org.apache.jmeter.samplers.gui.AbstractSamplerGui;
  import org.apache.jmeter.testelement.TestElement;
  import org.apache.jmeter.util.JMeterUtils;
  
  /**
   *  Example Sampler (non-Bean version)
   * 
   * This class is responsible for ensuring that the Sampler data is
   * kept in step with the GUI.
   * 
   * The GUI class is not invoked in non-GUI mode, so it should not
   * perform any additional setup that a test would need at run-time
   * 
   * @author sebb AT apache DOT org
   * @version $Revision: 1.1 $ $Date: 2004/02/25 23:13:44 $
   */
  public class ExampleSamplerGui extends AbstractSamplerGui
  {
  
  	private JTextField data;
  		
  	public ExampleSamplerGui()
  	{
  		init();
  	}
  
      /* (non-Javadoc)
       * @see org.apache.jmeter.gui.JMeterGUIComponent#getStaticLabel()
       */
      public String getStaticLabel()
      {
  		return JMeterUtils.getResString("example_title");
      }
  
      /* (non-Javadoc)
       * Copy the data from the test element to the GUI
       * 
       * @see org.apache.jmeter.gui.JMeterGUIComponent#configure(org.apache.jmeter.testelement.TestElement)
       */
  	public void configure(TestElement element)
  	{
  		data.setText(element.getPropertyAsString(ExampleSampler.DATA));
  		super.configure(element);
  	}
  
      /* (non-Javadoc)
       * Create the corresponding Test Element and set up its data
       * 
       * @see org.apache.jmeter.gui.JMeterGUIComponent#createTestElement()
       */
      public TestElement createTestElement()
      {
  		ExampleSampler sampler = new ExampleSampler();
  		modifyTestElement(sampler);
  		return sampler;
      }
  
  	/* (non-Javadoc)
  	 * Modifies a given TestElement to mirror the data in the gui components.
  	 * @see org.apache.jmeter.gui.JMeterGUIComponent#modifyTestElement(TestElement)
  	 */
      public void modifyTestElement(TestElement te)
      {
  		te.clear();
  		configureTestElement(te);
  		te.setProperty(ExampleSampler.DATA,data.getText());
      }
  
  
      /*
       * Helper method to set up the GUI screen
       */
  	private void init()
  	{
  		// Standard setup
  		setLayout(new BorderLayout(0, 5));
  		setBorder(makeBorder());
  		add(makeTitlePanel(),BorderLayout.NORTH); // Add the standard title
  		
  		// Specific setup
  		add(createDataPanel(),BorderLayout.CENTER);
  	}
  
      /*
       * Create a data input text field
       * 
       * @return the panel for entering the data
       */
      private Component createDataPanel()
      {
  		JLabel label = new JLabel(JMeterUtils.getResString("example_data"));
  		
  		data = new JTextField(10);
  		data.setName(ExampleSampler.DATA);
  		label.setLabelFor(data);
  
  		JPanel dataPanel = new JPanel(new BorderLayout(5, 0));
  		dataPanel.add(label, BorderLayout.WEST);
  		dataPanel.add(data, BorderLayout.CENTER);
  
          return dataPanel;
      }
  }
  
  
  1.1                  jakarta-jmeter/src/examples/org/apache/jmeter/examples/sampler/ExampleSampler.java
  
  Index: ExampleSampler.java
  ===================================================================
  // $Header: /home/cvs/jakarta-jmeter/src/examples/org/apache/jmeter/examples/sampler/ExampleSampler.java,v 1.1 2004/02/25 23:13:44 jsalvata Exp $
  /*
   * Copyright 2004 The Apache Software Foundation.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *   http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   * 
  */
  
  package org.apache.jmeter.examples.sampler;
  
  import org.apache.jmeter.samplers.AbstractSampler;
  import org.apache.jmeter.samplers.Entry;
  import org.apache.jmeter.samplers.SampleResult;
  import org.apache.jorphan.logging.LoggingManager;
  import org.apache.log.Logger;
  
  /**
   * Example Sampler (non-Bean version)
   * 
   * JMeter creates an instance of a sampler class for every
   * occurrence of the element in every thread.
   * [some additional copies may be created before the test run starts]
   *
   * Thus each sampler is guaranteed to be called by a single thread -
   * there is no need to synchronize access to instance variables.
   * 
   * However, access to class fields must be synchronized.
   *  
   * @author sebb AT apache DOT org
   * @version $Revision: 1.1 $ $Date: 2004/02/25 23:13:44 $
   */
  public class ExampleSampler extends AbstractSampler
  {
  
  	protected static Logger log = LoggingManager.getLoggerForClass();
  
      // The name of the property used to hold our data
  	public final static String DATA = "ExampleSampler.data";   //$NON-NLS-1$
  	
  	private transient static int classCount=0; // keep track of classes created
  	// (for instructional purposes only!)
  	
  	public ExampleSampler()
  	{
  		classCount++;
  		trace("ExampleSampler()");
  	}
  
      /* (non-Javadoc)
       * Performs the sample, and returns the result
       * 
       * @see org.apache.jmeter.samplers.Sampler#sample(org.apache.jmeter.samplers.Entry)
       */
      public SampleResult sample(Entry e)
      {
      	trace("sample()");
  		SampleResult res = new SampleResult();
  		boolean isOK = false; // Did sample succeed?
  		String data=getData(); // Sampler data
  		String response=null;
  		
  		res.setSampleLabel(getTitle());
  		/*
  		 * Perform the sampling
  		 */
  		res.sampleStart(); //Start timing
  		try {
  			
  			// Do something here ...
  			
  			response=Thread.currentThread().getName();
  			
  			/*
  			 * Set up the sample result details
  			 */
  			res.setSamplerData(data);
  			res.setResponseData(response.getBytes());
  			res.setDataType(SampleResult.TEXT);
  			
  			res.setResponseCode("200");
  			res.setResponseMessage("OK");
  			isOK = true;
  		}
  		catch (Exception ex){
  			log.debug("",ex);
  			res.setResponseCode("500");
  			res.setResponseMessage(ex.toString());
  		}
  		res.sampleEnd(); //End timimg
  		
  		res.setSuccessful(isOK);
  
          return res;
      }
  
      /**
       * @return a string for the sampleResult Title
       */
      private String getTitle()
      {
          return this.getName();
      }
      
      /**
       * @return the data for the sample
       */
      public String getData()
      {
      	return getPropertyAsString(DATA);
      }
      
      /*
       * Helper method
       */
  	private void trace(String s)
  	{
  		String tl = getTitle();
  		String tn = Thread.currentThread().getName();
  		String th = this.toString();
  		log.debug(tn+" ("+classCount+") "+tl+" "+s+" "+th);
  	}
  }
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: jmeter-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: jmeter-dev-help@jakarta.apache.org