Author: sebb
Date: Wed Dec  5 14:35:11 2007
New Revision: 601554

URL: http://svn.apache.org/viewvc?rev=601554&view=rev
Log:
Bug 43379 - Switch Controller now supports selection by name as well as number

Modified:
    
jakarta/jmeter/trunk/src/components/org/apache/jmeter/control/SwitchController.java
    
jakarta/jmeter/trunk/test/src/org/apache/jmeter/control/TestSwitchController.java
    jakarta/jmeter/trunk/xdocs/changes.xml
    jakarta/jmeter/trunk/xdocs/usermanual/component_reference.xml

Modified: 
jakarta/jmeter/trunk/src/components/org/apache/jmeter/control/SwitchController.java
URL: 
http://svn.apache.org/viewvc/jakarta/jmeter/trunk/src/components/org/apache/jmeter/control/SwitchController.java?rev=601554&r1=601553&r2=601554&view=diff
==============================================================================
--- 
jakarta/jmeter/trunk/src/components/org/apache/jmeter/control/SwitchController.java
 (original)
+++ 
jakarta/jmeter/trunk/src/components/org/apache/jmeter/control/SwitchController.java
 Wed Dec  5 14:35:11 2007
@@ -19,10 +19,34 @@
 package org.apache.jmeter.control;
 
 import java.io.Serializable;
+import java.util.Iterator;
 
 import org.apache.jmeter.samplers.Sampler;
+import org.apache.jmeter.testelement.TestElement;
 import org.apache.jmeter.testelement.property.StringProperty;
 
+// For unit tests @see TestSwitchController
+
+/**
+ * <p>
+ * Implements a controller which selects at most one of its children
+ * based on the condition value, which may be a number or a string.
+ * </p>
+ * <p>
+ * For numeric input, the controller processes the appropriate child,
+ * where the numbering starts from 0. 
+ * If the number is out of range, then the first (0th) child is selected.
+ * If the condition is the empty string, then it is assumed to be 0.
+ * </p>
+ * <p>
+ * For non-empty non-numeric input, the child is selected by name.
+ * This may be the name of the controller or a sampler.
+ * If the string does not match any of the names, then the controller
+ * with the name "default" (any case) is processed.
+ * If there is no default entry, then unlike the numeric case,
+ * no child is selected.
+ * </p>
+ */
 public class SwitchController extends GenericController implements 
Serializable {
        // Package access for use by Test code
        final static String SWITCH_VALUE = "SwitchController.value";
@@ -67,13 +91,32 @@
                String sel = getSelection();
                try {
                        ret = Integer.parseInt(sel);
+                       if (ret < 0 || ret >= getSubControllers().size()) {
+                               ret = 0;
+                       }
                } catch (NumberFormatException e) {
-                       ret = 0;
-               }
-               if (ret < 0 || ret >= getSubControllers().size()) {
-                       ret = 0;
+                       if (sel.length()==0) {
+                               ret = 0;
+                       } else {
+                               ret = scanControllerNames(sel);
+                       }
                }
                return ret;
+       }
+
+       private int scanControllerNames(String sel){
+               Iterator iter = getSubControllers().iterator();
+               int i = 0;
+               int default_pos = Integer.MAX_VALUE;
+               while(iter.hasNext()) {
+                       String name;
+                   TestElement el = (TestElement)iter.next();
+                   name=el.getName();
+                       if (name.equals(sel)) return i;
+               if (name.equalsIgnoreCase("default")) default_pos = i;
+                       i++;
+               }
+               return default_pos;     
        }
 
        public String getSelection() {

Modified: 
jakarta/jmeter/trunk/test/src/org/apache/jmeter/control/TestSwitchController.java
URL: 
http://svn.apache.org/viewvc/jakarta/jmeter/trunk/test/src/org/apache/jmeter/control/TestSwitchController.java?rev=601554&r1=601553&r2=601554&view=diff
==============================================================================
--- 
jakarta/jmeter/trunk/test/src/org/apache/jmeter/control/TestSwitchController.java
 (original)
+++ 
jakarta/jmeter/trunk/test/src/org/apache/jmeter/control/TestSwitchController.java
 Wed Dec  5 14:35:11 2007
@@ -65,14 +65,17 @@
 
                public void test1() throws Exception {
                        runSimpleTests("1", "one");
+                       runSimpleTests("one", "one"); // Match by name
                }
 
                public void test2() throws Exception {
                        runSimpleTests("2", "two");
+                       runSimpleTests("two", "two"); // Match by name
                }
 
                public void test3() throws Exception {
                        runSimpleTests("3", "three");
+                       runSimpleTests("three", "three"); // Match by name
                }
 
                public void test4() throws Exception {
@@ -80,16 +83,27 @@
                }
 
                public void testX() throws Exception {
-                       runSimpleTests("X", "zero");
+                       runSimpleTests("X", null); // should not run any 
children
+                       runSimpleTest2("X", "one", "Default"); // should match 
the default entry
                }
 
-               public void runSimpleTests(String cond, String exp) throws 
Exception {
+               private void runSimpleTests(String cond, String exp) throws 
Exception {
                        runSimpleTest(cond, exp);
-                       runSimpleTest2(cond, exp);
+                       runSimpleTest2(cond, exp, "one");
                }
 
-               // Simple test with single Selection controller
-               public void runSimpleTest(String cond, String exp) throws 
Exception {
+               /*
+                *  Simple test with single Selection controller
+                *  Generic Controller
+                *  + Sampler "before"
+                *  + Switch Controller
+                *  + + Sampler "zero"
+                *  + + Sampler "one"
+                *  + + Sampler "two"
+                *  + + Sampler "three"
+                *  + Sampler "after"
+                */
+               private void runSimpleTest(String cond, String exp) throws 
Exception {
                        GenericController controller = new GenericController();
 
                        SwitchController switch_cont = new SwitchController();
@@ -109,7 +123,9 @@
 
                        for (int i = 1; i <= 3; i++) {
                                assertEquals("Loop " + i, "before", 
nextName(controller));
-                               assertEquals("Loop " + i, exp, 
nextName(controller));
+                               if (exp!=null){
+                                       assertEquals("Loop " + i, exp, 
nextName(controller));
+                               }
                                assertEquals("Loop " + i, "after", 
nextName(controller));
                                assertNull(nextName(controller));
                        }
@@ -129,7 +145,7 @@
                 * + + + three
                 * + After
                 */
-               public void runSimpleTest2(String cond, String exp) throws 
Exception {
+               private void runSimpleTest2(String cond, String exp, String 
sub1Name) throws Exception {
                        GenericController controller = new GenericController();
                        GenericController sub_1 = new GenericController();
                        GenericController sub_2 = new GenericController();
@@ -140,11 +156,13 @@
                        switch_cont.addTestElement(new TestSampler("zero"));
                        switch_cont.addTestElement(sub_1);
                        sub_1.addTestElement(new TestSampler("one"));
+                       sub_1.setName(sub1Name);
 
                        switch_cont.addTestElement(new TestSampler("two"));
 
                        switch_cont.addTestElement(sub_2);
                        sub_2.addTestElement(new TestSampler("three"));
+                       sub_2.setName("three");
 
                        controller.addTestElement(new TestSampler("before"));
                        controller.addTestElement(switch_cont);
@@ -152,7 +170,9 @@
                        controller.initialize();
                        for (int i = 1; i <= 3; i++) {
                                assertEquals("Loop="+i,"before", 
nextName(controller));
-                               assertEquals("Loop="+i,exp, 
nextName(controller));
+                               if (exp!=null){
+                                       assertEquals("Loop="+i,exp, 
nextName(controller));
+                               }
                                assertEquals("Loop="+i,"after", 
nextName(controller));
                                assertNull("Loop="+i,nextName(controller));
                        }
@@ -187,7 +207,7 @@
                 * cond  = Switch condition 
                 * exp[] = expected results
                 */
-               public void runTest2(String cond, String exp[]) throws 
Exception {
+               private void runTest2(String cond, String exp[]) throws 
Exception {
                        int loops = 3;
                        LoopController controller = new LoopController();
                        controller.setLoops(loops);

Modified: jakarta/jmeter/trunk/xdocs/changes.xml
URL: 
http://svn.apache.org/viewvc/jakarta/jmeter/trunk/xdocs/changes.xml?rev=601554&r1=601553&r2=601554&view=diff
==============================================================================
--- jakarta/jmeter/trunk/xdocs/changes.xml (original)
+++ jakarta/jmeter/trunk/xdocs/changes.xml Wed Dec  5 14:35:11 2007
@@ -62,6 +62,7 @@
 <li>Make Random function variable name optional</li>
 <li>Reduce class loading in non-GUI mode by only looking for Functions in 
class names
 that contain '.functions.' and don't contain '.gui.'</li>
+<li>Bug 43379 - Switch Controller now supports selection by name as well as 
number</li>
 </ul>
 
 <h4>Non-functional changes</h4>

Modified: jakarta/jmeter/trunk/xdocs/usermanual/component_reference.xml
URL: 
http://svn.apache.org/viewvc/jakarta/jmeter/trunk/xdocs/usermanual/component_reference.xml?rev=601554&r1=601553&r2=601554&view=diff
==============================================================================
--- jakarta/jmeter/trunk/xdocs/usermanual/component_reference.xml (original)
+++ jakarta/jmeter/trunk/xdocs/usermanual/component_reference.xml Wed Dec  5 
14:35:11 2007
@@ -1454,14 +1454,24 @@
 <p>
 The Switch Controller acts like the <complink name="Interleave Controller"/> 
 in that it runs one of the subordinate elements on each iteration, but rather 
than
-run them in sequence, the controller runs the element number defined by the 
switch value.
+run them in sequence, the controller runs the element defined by the switch 
value.
+</p>
+<p>
+Note: In versions of JMeter after 2.3.1, the switch value can also be a name.
 </p>
 <p>If the switch value is out of range, it will run the zeroth element, 
-which therefore acts as the default.</p>
+which therefore acts as the default for the numeric case.
+It also runs the zeroth element if the value is the empty string.</p>
+<p>
+If the value is non-numeric (and non-empty), then the Switch Controller looks 
for the
+element with the same name (case is significant).
+If none of the names match, then the element named "default" (case not 
significant) is selected.
+If there is no default, then no element is selected, and the controller will 
not run anything.
+</p>
 </description>
 <properties>
        <property name="Name" required="Yes">Descriptive name for this 
controller that is shown in the tree, and used to name the 
transaction.</property>
-       <property name="Switch Value" required="Yes">The number of the 
subordinate element to be invoked. Elements are numbered from 0.</property>
+       <property name="Switch Value" required="Yes">The number (or name) of 
the subordinate element to be invoked. Elements are numbered from 0.</property>
 </properties>
 </component>
 



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to