1 18 19 package org.apache.jmeter.control; 20 21 import java.io.Serializable ; 22 23 import org.apache.jmeter.junit.JMeterTestCase; 24 import org.apache.jmeter.junit.stubs.TestSampler; 25 import org.apache.jmeter.samplers.Sampler; 26 import org.apache.jmeter.testelement.TestElement; 27 import org.apache.jmeter.testelement.property.StringProperty; 28 29 public class SwitchController extends InterleaveControl implements Serializable 30 { 31 private final static String SWITCH_VALUE = "SwitchController.value"; 32 33 34 public SwitchController() 35 { 36 super(); 37 this.setStyle(USE_SUB_CONTROLLERS); 38 } 39 public void reInitialize() 40 { 41 int was = current; 42 super.reInitialize(); 43 current=getSelectionAsInt(); 44 } 45 46 49 protected void resetCurrent() 50 { 51 int c = getSubControllers().size(); 52 if (c > 0) 53 { 54 current = getSelectionAsInt(); 55 } 56 else 57 { 58 current = 0; 59 } 60 } 61 62 65 protected void incrementCurrent() 66 { 67 int was = current; 68 super.incrementCurrent(); 69 current = getSelectionAsInt(); 70 } 71 72 public void setSelection(String inputValue) 73 { 74 setProperty(new StringProperty(SWITCH_VALUE, inputValue)); 75 } 76 77 private int getSelectionAsInt() 78 { 79 int ret; 80 getProperty(SWITCH_VALUE).recoverRunningVersion(null); 81 String sel =getSelection(); 82 try{ 83 ret = Integer.parseInt(sel); 84 } catch (NumberFormatException e){ 85 ret=0; 86 } 87 if (ret < 0 || ret >= getSubControllers().size()){ 88 ret = 0; 89 } 90 return ret; 91 } 92 public String getSelection() 93 { 94 return getPropertyAsString(SWITCH_VALUE); 95 } 96 97 public static class Test extends JMeterTestCase 98 { 99 static{ 100 } 103 104 public Test(String name) 105 { 106 super(name); 107 } 108 109 private String nextName(GenericController c){ 111 Sampler s = c.next(); 112 String n; 113 if (s==null){ 114 return null; 115 } else { 116 n=s.getPropertyAsString(TestElement.NAME); 117 return n; 118 } 119 } 120 public void test() throws Exception 121 { 122 runSimpleTests("","zero"); 123 } 124 public void test0() throws Exception 125 { 126 runSimpleTests("0","zero"); 127 } 128 public void test1() throws Exception 129 { 130 runSimpleTests("1","one"); 131 } 132 public void test2() throws Exception 133 { 134 runSimpleTests("2","two"); 135 } 136 public void test3() throws Exception 137 { 138 runSimpleTests("3","three"); 139 } 140 public void test4() throws Exception 141 { 142 runSimpleTests("4","zero"); 143 } 144 public void testX() throws Exception 145 { 146 runSimpleTests("X","zero"); 147 } 148 149 public void runSimpleTests(String cond, String exp) throws Exception 150 { 151 runSimpleTest(cond,exp); 152 runSimpleTest2(cond,exp); 153 } 154 155 public void runSimpleTest(String cond, String exp) throws Exception 157 { 158 GenericController controller = new GenericController(); 159 160 SwitchController switch_cont = new SwitchController(); 161 switch_cont.setSelection(cond); 162 163 controller.addTestElement(new TestSampler("before")); 164 controller.addTestElement(switch_cont); 165 166 switch_cont.addTestElement(new TestSampler("zero")); 167 switch_cont.addTestElement(new TestSampler("one")); 168 switch_cont.addTestElement(new TestSampler("two")); 169 switch_cont.addTestElement(new TestSampler("three")); 170 171 controller.addTestElement(new TestSampler("after")); 172 173 controller.initialize(); 174 175 for (int i=1;i<=3;i++){ 176 assertEquals("Loop "+i,"before",nextName(controller)); 177 assertEquals("Loop "+i,exp,nextName(controller)); 178 assertEquals("Loop "+i,"after",nextName(controller)); 179 assertNull(nextName(controller)); 180 } 181 } 182 183 public void runSimpleTest2(String cond, String exp) throws Exception 185 { 186 GenericController controller = new GenericController(); 187 GenericController sub_1 = new GenericController(); 188 GenericController sub_2 = new GenericController(); 189 190 SwitchController switch_cont = new SwitchController(); 191 switch_cont.setSelection(cond); 192 193 switch_cont.addTestElement(new TestSampler("zero")); 194 switch_cont.addTestElement(sub_1); 195 sub_1.addTestElement(new TestSampler("one")); 196 197 switch_cont.addTestElement(new TestSampler("two")); 198 199 switch_cont.addTestElement(sub_2); 200 sub_2.addTestElement(new TestSampler("three")); 201 202 controller.addTestElement(new TestSampler("before")); 203 controller.addTestElement(switch_cont); 204 controller.addTestElement(new TestSampler("after")); 205 controller.initialize(); 206 for (int i=1;i<=3;i++){ 207 assertEquals("before",nextName(controller)); 208 assertEquals(exp,nextName(controller)); 209 assertEquals("after",nextName(controller)); 210 assertNull(nextName(controller)); 211 } 212 } 213 214 public void testTest2() throws Exception 215 { 216 runTest2("",new String []{"zero"}); 217 runTest2("0",new String []{"zero"}); 218 runTest2("7",new String []{"zero"}); 219 runTest2("5",new String []{"zero"}); 220 runTest2("4",new String []{"six"}); 221 runTest2("3",new String []{"five"}); 222 runTest2("1",new String []{"one","two"}); 223 runTest2("2",new String []{"three","four"}); 224 } 225 240 public void runTest2(String cond, String exp[]) throws Exception 241 { 242 int loops = 3; 243 LoopController controller = new LoopController(); 244 controller.setLoops(loops); 245 controller.setContinueForever(false); 246 GenericController sub_1 = new GenericController(); 247 GenericController sub_2 = new GenericController(); 248 249 SwitchController switch_cont = new SwitchController(); 250 switch_cont.setSelection(cond); 251 252 switch_cont.addTestElement(new TestSampler("zero")); 253 switch_cont.addTestElement(sub_1); 254 sub_1.addTestElement(new TestSampler("one")); 255 sub_1.addTestElement(new TestSampler("two")); 256 257 switch_cont.addTestElement(sub_2); 258 sub_2.addTestElement(new TestSampler("three")); 259 sub_2.addTestElement(new TestSampler("four")); 260 261 switch_cont.addTestElement(new TestSampler("five")); 262 switch_cont.addTestElement(new TestSampler("six")); 263 264 controller.addTestElement(new TestSampler("before")); 265 controller.addTestElement(switch_cont); 266 controller.addTestElement(new TestSampler("after")); 267 controller.initialize(); 268 for (int i=1;i<=3;i++){ 269 assertEquals("Loop:"+i,"before",nextName(controller)); 270 for (int j=0;j<exp.length;j++) { 271 assertEquals("Loop:"+i,exp[j],nextName(controller)); 272 } 273 assertEquals("Loop:"+i,"after",nextName(controller)); 274 } 275 assertNull("Loops:"+loops,nextName(controller)); 276 } 277 278 } 279 } 280 | Popular Tags |