1 17 package org.apache.jmeter.control; 18 19 import java.io.Serializable ; 20 import java.util.ArrayList ; 21 import java.util.Iterator ; 22 import java.util.List ; 23 24 import junit.framework.TestSuite; 25 26 import org.apache.jmeter.junit.JMeterTestCase; 27 import org.apache.jmeter.junit.stubs.TestSampler; 28 import org.apache.jmeter.testelement.TestElement; 29 30 36 public class RandomOrderController 37 extends GenericController 38 implements Serializable 39 { 40 43 public RandomOrderController() 44 { 45 } 46 47 50 public void initialize() 51 { 52 super.initialize(); 53 this.reorder(); 54 } 55 56 59 public void reInitialize() 60 { 61 super.reInitialize(); 62 this.reorder(); 63 } 64 65 68 private void reorder() 69 { 70 int numElements = this.subControllersAndSamplers.size(); 71 72 List reordered = new ArrayList (this.subControllersAndSamplers.size()); 74 for (int i = 0; i < numElements; i++) 75 { 76 reordered.add(null); 77 } 78 79 for (Iterator i = this.subControllersAndSamplers.iterator(); 81 i.hasNext(); ) 82 { 83 int idx = (int)Math.floor(Math.random() * reordered.size()); 84 while (true) 85 { 86 if (idx == numElements) 87 { 88 idx = 0; 89 } 90 if (reordered.get(idx) == null) 91 { 92 reordered.set(idx, i.next()); 93 break; 94 } 95 idx++; 96 } 97 } 98 99 this.subControllersAndSamplers = reordered; 101 } 102 103 public static class Test extends JMeterTestCase 104 { 105 106 public Test(String name) 107 { 108 super(name); 109 } 110 111 public void testRandomOrder() 112 { 113 testLog.debug("Testing RandomOrderController"); 114 RandomOrderController roc = new RandomOrderController(); 115 roc.addTestElement(new TestSampler("zero")); 116 roc.addTestElement(new TestSampler("one")); 117 roc.addTestElement(new TestSampler("two")); 118 roc.addTestElement(new TestSampler("three")); 119 TestElement sampler = null; 120 List usedSamplers = new ArrayList (); 121 roc.initialize(); 122 while ((sampler = roc.next()) != null) 123 { 124 String samplerName = sampler.getPropertyAsString(TestSampler.NAME); 125 if (usedSamplers.contains(samplerName)) 126 { 127 assertTrue("Duplicate sampler returned from next()", false); 128 } 129 usedSamplers.add(samplerName); 130 } 131 assertTrue("All samplers were returned", 132 usedSamplers.size() == 4); 133 } 134 135 public void testRandomOrderNoElements() 136 { 137 RandomOrderController roc = new RandomOrderController(); 138 roc.initialize(); 139 assertTrue(roc.next() == null); 140 } 141 142 public void testRandomOrderOneElement() 143 { 144 RandomOrderController roc = new RandomOrderController(); 145 roc.addTestElement(new TestSampler("zero")); 146 TestElement sampler = null; 147 List usedSamplers = new ArrayList (); 148 roc.initialize(); 149 while ((sampler = roc.next()) != null) 150 { 151 String samplerName = sampler.getPropertyAsString(TestSampler.NAME); 152 if (usedSamplers.contains(samplerName)) 153 { 154 assertTrue("Duplicate sampler returned from next()", false); 155 } 156 usedSamplers.add(samplerName); 157 } 158 assertTrue("All samplers were returned", 159 usedSamplers.size() == 1); 160 } 161 } 162 163 public static void main(String args[]) 164 { 165 junit.textui.TestRunner.run(suite()); 166 } 167 168 public static TestSuite suite() 169 { 170 TestSuite suite = new TestSuite(); 171 suite.addTest(new Test("testRandomOrderController")); 172 return suite; 173 } 174 175 } 176 | Popular Tags |