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 Cy...@rcomext.com on 2003/07/08 14:22:27 UTC
New function : IfController
Please find attached the source code for IfController & IfControllerPanel.
This is intended to be placed within another controller (or Thread Group).
A condition is evaluated and if true all the sub-items of the condition is
executed.
For more information please see the attached javadoc.
Also, please note that the following entries needs to be
added to the message.properties file :
if_controller_title=If Controller
if_controller_label=Condition
===============================================
package org.apache.jmeter.control;
import java.io.Serializable;
import junit.framework.TestSuite;
import org.apache.jmeter.samplers.Sampler;
import org.apache.jmeter.testelement.TestElement;
import org.apache.jmeter.testelement.property.StringProperty;
import org.apache.jorphan.logging.LoggingManager;
import org.apache.log.Logger;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.Scriptable;
import org.apache.jmeter.control.GenericController;
/****************************************
*@author Cyrus Montakab
*@created $Date: 2003/06/30
*@version 1.0
* This is a Conditional Controller; it will execute the set of statemstns
* (samplers/controllers, etc)
* until if the 'condition' is true.
* In a programming world - this is equivalant of :
* if (condition) {
* statements ....
* }
* In JMeter you may have :
* Thread-Group (set to loop a number of times or indefinitely,
* ... Samplers ... (e.g. Counter )
* ... Other Controllers ....
* ... IfController ( condition set to something like -
${counter}<10
* ... staetements to perform if condition is true ...
* ... Other Controllers /Samplers
* }
*
***************************************/
public class IfController extends GenericController implements Serializable
{
private static Logger logger =
LoggingManager.getLoggerFor("jmeter.controller.IfController");
private final static String CONDITION = "IfController.condition";
/**
* constructor
*/
public IfController() {
super();
}
/**
* constructor
*/
public IfController(String condition) {
super();
this.setCondition(condition);
}
/**
* Condition Accessor - this is gonna be like ${count}<10
*/
public void setCondition(String condition) {
setProperty(new StringProperty(CONDITION, condition));
}
/**
* Condition Accessor - this is gonna be like ${count}<10
*/
public String getCondition() {
return getPropertyAsString(CONDITION);
}
/**
* evaluate the condition clause
* Throw Exception If bad condition -
*/
private boolean evaluateCondition() throws Exception {
// condition string is going to be of the form : ${counter}<10
// the following replaces the ${xxx} with actual valuess
logger.debug(" getCondition() : [" + getCondition() + "]");
String resultStr = "";
boolean result = false;
// now evaluate the condition using JavaScript
Context cx = Context.enter();
try {
Scriptable scope = cx.initStandardObjects(null);
Object cxResultObject =
cx.evaluateString(scope, getCondition()
/*** conditionString ***/
, "<cmd>", 1, null);
resultStr = Context.toString(cxResultObject);
if (resultStr.equals("false")) {
result = false;
} else if (resultStr.equals("true")) {
result = true;
} else {
throw new Exception(" BAD CONDITION :: " +
getCondition());
}
logger.debug(
" >> evaluate Condition - [ "
+ getCondition()
+ "] results is ["
+ result
+ "]");
} finally {
Context.exit();
}
return result;
}
/**
* This is overriding the parent method.
* IsDone indicates whether the termination condition is reached.
* I.e. if the condition evaluates to False - then isDone() returns
TRUE
*/
/* public boolean isDone () {
boolean result = super.isDone();
// setDone(true);
return result;
}
*/
/**
* This is overriding the parent method.
* IsDone indicates whether the termination condition is reached.
* I.e. if the condition evaluates to False - then isDone() returns
TRUE
*/
public boolean isDone() {
boolean result = true;
try {
result = !evaluateCondition();
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
setDone(true);
return result;
}
/**
* @see org.apache.jmeter.control.Controller#next()
* 'JMeterThread' iterates thru the Controller by calling this
method.
* IF a valid 'Sampler' is returned, then it executes the sampler
* (calls sampler.sampler(xxx) method) .
* So here we make sure that the samplers belonging to this
* Controller do not get called
* - if isDone is true
* - if its the first time this is run. The first time is special
* cause it is called prior the iteration even starts !
*/
public Sampler next() {
Sampler currentElement = super.next();
// if (!isFirst() && !isDone () ){
if (!isDone()) {
return currentElement;
} else {
return null;
}
}
/**
* Tester
*/
public static void main(String args[]) {
junit.textui.TestRunner.run(suite());
}
/**
* For JUnit test
*/
public static TestSuite suite() {
TestSuite suite = new TestSuite();
suite.addTest(new Test("testProcessing"));
return suite;
}
/**
* JUnit test
*/
public static class Test extends junit.framework.TestCase {
public Test(String name) {
super(name);
}
public void testProcessing() throws Exception {
GenericController controller = new GenericController();
controller.addTestElement(new IfController("false
==false"));
controller.addTestElement(new IfController(" \"a\".equals
(\"a\")"));
controller.addTestElement(new IfController("2<100"));
/* GenericController sub_1 = new
GenericController();
sub_1.addTestElement(new IfController("3
==3"));
controller.addTestElement(sub_1);
controller.addTestElement(new IfController
("false==true"));
*/
/*
GenericController controller = new GenericController();
GenericController sub_1 = new GenericController();
sub_1.addTestElement(new IfController("10<100"));
sub_1.addTestElement(new IfController("true==false"));
controller.addTestElement(sub_1);
controller.addTestElement(new IfController("false
==false"));
IfController sub_2 = new IfController();
sub_2.setCondition( "10<10000");
GenericController sub_3 = new GenericController();
sub_2.addTestElement(new IfController( " \"a\".equals
(\"a\")" ) );
sub_3.addTestElement(new IfController("2>100"));
sub_3.addTestElement(new IfController("false==true"));
sub_2.addTestElement(sub_3);
sub_2.addTestElement(new IfController("2==3"));
controller.addTestElement(sub_2);
*/
/* IfController controller = new IfController("12
==12");
controller.initialize();
*/
logger.debug(">>>>> testProcessing : Starting the
iteration ");
TestElement sampler = null;
while ((sampler = controller.next()) != null) {
logger.debug(
" ->>> Gonna assertTrue :"
+ sampler.getClass().getName()
+ " Property is ---->>>"
+
sampler.getPropertyAsString(TestElement.NAME));
}
}
}
}
===================================================================================
package org.apache.jmeter.control.gui;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Box;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import org.apache.jmeter.control.IfController;
import org.apache.jmeter.gui.util.FocusRequester;
import org.apache.jmeter.testelement.TestElement;
import org.apache.jmeter.util.JMeterUtils;
/**
* The user interface for a controller which specifies that its
subcomponents
* should be executed some number of times in a loop. This component can
be
* used standalone or embedded into some other component.
* Copyright: 2000
*
* @author Cyrus Montakab
* @version $Revision: 1.10 $
*/
public class IfControllerPanel
extends AbstractControllerGui
implements ActionListener
{
private static final String CONDITION_TITLE = "if_controller_title";
private static final String CONDITION_LABEL = "if_controller_label";
/**
* A field allowing the user to specify the number of times the
controller
* should loop.
*/
private JTextField theCondition;
/**
* Boolean indicating whether or not this component should display its
* name. If true, this is a standalone component. If false, this
component
* is intended to be used as a subpanel for another component.
*/
private boolean displayName = true;
/** The name of the loops field component. */
private static final String CONDITION = "JS_Condition";
/**
* Create a new LoopControlPanel as a standalone component.
*/
public IfControllerPanel() {
this(true);
}
/**
* Create a new LoopControlPanel as either a standalone or an embedded
* component.
*
* @param displayName indicates whether or not this component should
* display its name. If true, this is a standalone
* component. If false, this component is intended
* to be used as a subpanel for another component.
*/
public IfControllerPanel (boolean displayName)
{
this.displayName = displayName;
init();
}
/**
* A newly created component can be initialized with the contents of
* a Test Element object by calling this method. The component is
* responsible for querying the Test Element object for the
* relevant information to display in its GUI.
*
* @param element the TestElement to configure
*/
public void configure(TestElement element) {
super.configure(element);
if (element instanceof IfController) {
theCondition.setText(((IfController) element).getCondition());
}
}
/**
* Implements JMeterGUIComponent.createTestElement()
*/
public TestElement createTestElement() {
IfController controller = new IfController();
modifyTestElement(controller);
return controller;
}
/**
* Implements JMeterGUIComponent.modifyTestElement(TestElement)
*/
public void modifyTestElement(TestElement controller) {
configureTestElement(controller);
if (controller instanceof IfController)
{
if (theCondition.getText().length() > 0) {
((IfController)
controller).setCondition(theCondition.getText());
} else {
((IfController) controller).setCondition("");
}
}
}
/**
* Invoked when an action occurs. This implementation assumes that the
* target component is the infinite loops checkbox.
*
* @param event the event that has occurred
*/
public void actionPerformed(ActionEvent event) {
new FocusRequester(theCondition);
}
/* Implements JMeterGUIComponent.getStaticLabel() */
public String getStaticLabel() {
return JMeterUtils.getResString(CONDITION_TITLE);
}
/**
* Initialize the GUI components and layout for this component.
*/
private void init()
{
// Standalone
if (displayName) {
setLayout(new BorderLayout(0, 5));
setBorder(makeBorder());
add(makeTitlePanel(), BorderLayout.NORTH);
JPanel mainPanel = new JPanel(new BorderLayout());
mainPanel.add(createConditionPanel(), BorderLayout.NORTH);
add(mainPanel, BorderLayout.CENTER);
} else {
// Embedded
setLayout(new BorderLayout());
add(createConditionPanel(), BorderLayout.NORTH);
}
}
/**
* Create a GUI panel containing the components related to the number
of
* loops which should be executed.
*
* @return a GUI panel containing the loop count components
*/
private JPanel createConditionPanel() {
JPanel conditionPanel = new JPanel(new BorderLayout(5, 0));
// Condition LABEL
JLabel conditionLabel =
new JLabel(JMeterUtils.getResString( CONDITION_LABEL ));
conditionPanel.add(conditionLabel, BorderLayout.WEST);
// TEXT FIELD
theCondition = new JTextField("");
theCondition.setName(CONDITION);
conditionLabel.setLabelFor(theCondition);
conditionPanel.add(theCondition, BorderLayout.CENTER);
theCondition.addActionListener(this);
conditionPanel.add(
Box.createHorizontalStrut( conditionLabel.getPreferredSize
().width
+ theCondition.getPreferredSize().width)
, BorderLayout.NORTH);
return conditionPanel;
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: jmeter-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: jmeter-dev-help@jakarta.apache.org