1 18 19 package org.apache.jmeter.threads; 20 21 import java.util.ArrayList ; 22 import java.util.HashMap ; 23 import java.util.HashSet ; 24 import java.util.Iterator ; 25 import java.util.LinkedList ; 26 import java.util.List ; 27 import java.util.ListIterator ; 28 import java.util.Map ; 29 import java.util.Set ; 30 31 import org.apache.jmeter.assertions.Assertion; 32 import org.apache.jmeter.config.ConfigTestElement; 33 import org.apache.jmeter.control.Controller; 34 import org.apache.jmeter.control.GenericController; 35 import org.apache.jmeter.engine.event.LoopIterationListener; 36 import org.apache.jmeter.processor.PostProcessor; 37 import org.apache.jmeter.processor.PreProcessor; 38 import org.apache.jmeter.samplers.AbstractSampler; 39 import org.apache.jmeter.samplers.SampleEvent; 40 import org.apache.jmeter.samplers.SampleListener; 41 import org.apache.jmeter.samplers.SampleResult; 42 import org.apache.jmeter.samplers.Sampler; 43 import org.apache.jmeter.testbeans.TestBean; 44 import org.apache.jmeter.testelement.AbstractTestElement; 45 import org.apache.jmeter.testelement.TestElement; 46 import org.apache.jmeter.timers.Timer; 47 import org.apache.jorphan.collections.HashTree; 48 import org.apache.jorphan.collections.HashTreeTraverser; 49 import org.apache.jorphan.collections.ListedHashTree; 50 import org.apache.jorphan.logging.LoggingManager; 51 import org.apache.log.Logger; 52 53 57 public class TestCompiler implements HashTreeTraverser, SampleListener 58 { 59 transient private static Logger log = LoggingManager.getLoggerForClass(); 60 61 LinkedList stack = new LinkedList (); 63 Map samplerConfigMap = new HashMap (); 64 HashTree testTree; 66 SampleResult previousResult; Sampler currentSampler; JMeterVariables threadVars; 70 75 private static Set pairing = new HashSet (); 76 77 List loopIterListeners = new ArrayList (); 78 79 public TestCompiler(HashTree testTree, JMeterVariables vars) 80 { 81 threadVars = vars; 82 this.testTree = testTree; 83 } 84 85 89 public static void initialize() 90 { 91 synchronized(pairing){ 93 pairing.clear(); 94 } 95 } 96 97 public void sampleOccurred(SampleEvent e) 98 { 99 previousResult = e.getResult(); 100 } 101 102 public void sampleStarted(SampleEvent e) 103 { 104 } 105 106 public void sampleStopped(SampleEvent e) 107 { 108 } 109 110 public SamplePackage configureSampler(Sampler sampler) 111 { 112 currentSampler = sampler; 113 SamplePackage pack = (SamplePackage) samplerConfigMap.get(sampler); 114 pack.setSampler(sampler); 115 configureWithConfigElements(sampler, pack.getConfigs()); 116 runPreProcessors(pack.getPreProcessors()); 117 return pack; 119 } 120 121 private void runPreProcessors(List preProcessors) 122 { 123 Iterator iter = preProcessors.iterator(); 124 while (iter.hasNext()) 125 { 126 PreProcessor ex = (PreProcessor) iter.next(); 127 if (log.isDebugEnabled()) 128 { 129 log.debug( 130 "Running preprocessor: " 131 + ((AbstractTestElement) ex).getName()); 132 } 133 if (ex instanceof TestBean) ((TestBean)ex).prepare(); 134 ex.process(); 135 } 136 } 137 138 public void done(SamplePackage pack) 139 { 140 pack.recoverRunningVersion(); 141 } 142 143 public void addNode(Object node, HashTree subTree) 144 { 145 stack.addLast(node); 146 } 147 148 public void subtractNode() 149 { 150 log.debug("Subtracting node, stack size = " + stack.size()); 151 TestElement child = (TestElement) stack.getLast(); 152 trackIterationListeners(stack); 153 if (child instanceof Sampler) 154 { 155 saveSamplerConfigs((Sampler) child); 156 } 157 stack.removeLast(); 158 if (stack.size() > 0) 159 { 160 ObjectPair pair = 161 new ObjectPair( 162 (TestElement) child, 163 (TestElement) stack.getLast()); 164 synchronized (pairing){ if (!pairing.contains(pair)) 166 { 167 pair.addTestElements(); 168 pairing.add(pair); 169 } 170 } 171 } 172 } 173 174 private void trackIterationListeners(LinkedList stack) 175 { 176 TestElement child = (TestElement) stack.getLast(); 177 if (child instanceof LoopIterationListener) 178 { 179 ListIterator iter = stack.listIterator(stack.size()); 180 while (iter.hasPrevious()) 181 { 182 TestElement item = (TestElement) iter.previous(); 183 if (item == child) 184 { 185 continue; 186 } 187 else 188 { 189 if (item instanceof Controller) 190 { 191 ((Controller) item).addIterationListener( 192 (LoopIterationListener) child); 193 break; 194 } 195 } 196 } 197 } 198 } 199 200 public void processPath() 201 { 202 } 203 204 private void saveSamplerConfigs(Sampler sam) 205 { 206 List configs = new LinkedList (); 207 List modifiers = new LinkedList (); 208 List responseModifiers = new LinkedList (); 209 List listeners = new LinkedList (); 210 List timers = new LinkedList (); 211 List assertions = new LinkedList (); 212 LinkedList posts = new LinkedList (); 213 LinkedList pres = new LinkedList (); 214 for (int i = stack.size(); i > 0; i--) 215 { 216 Iterator iter = testTree.list(stack.subList(0, i)).iterator(); 217 List tempPre = new LinkedList (); 218 List tempPost = new LinkedList (); 219 while (iter.hasNext()) 220 { 221 TestElement item = (TestElement) iter.next(); 222 if ((item instanceof ConfigTestElement)) 223 { 224 configs.add(item); 225 } 226 if (item instanceof SampleListener) 227 { 228 listeners.add(item); 229 } 230 if (item instanceof Timer) 231 { 232 timers.add(item); 233 } 234 if (item instanceof Assertion) 235 { 236 assertions.add(item); 237 } 238 if (item instanceof PostProcessor) 239 { 240 tempPost.add(item); 241 } 242 if (item instanceof PreProcessor) 243 { 244 tempPre.add(item); 245 } 246 } 247 pres.addAll(0,tempPre); 248 posts.addAll(0,tempPost); 249 } 250 251 SamplePackage pack = 252 new SamplePackage( 253 configs, 254 modifiers, 255 responseModifiers, 256 listeners, 257 timers, 258 assertions, 259 posts, 260 pres); 261 pack.setSampler(sam); 262 pack.setRunningVersion(true); 263 samplerConfigMap.put(sam, pack); 264 } 265 266 269 public static class Test extends junit.framework.TestCase 270 { 271 public Test(String name) 272 { 273 super(name); 274 } 275 276 public void testConfigGathering() throws Exception 277 { 278 ListedHashTree testing = new ListedHashTree(); 279 GenericController controller = new GenericController(); 280 ConfigTestElement config1 = new ConfigTestElement(); 281 config1.setName("config1"); 282 config1.setProperty("test.property", "A test value"); 283 TestSampler sampler = new TestSampler(); 284 sampler.setName("sampler"); 285 testing.add(controller, config1); 286 testing.add(controller, sampler); 287 TestCompiler.initialize(); 288 289 TestCompiler compiler = 290 new TestCompiler(testing, new JMeterVariables()); 291 testing.traverse(compiler); 292 sampler = 293 (TestSampler) compiler.configureSampler(sampler).getSampler(); 294 assertEquals( 295 "A test value", 296 sampler.getPropertyAsString("test.property")); 297 } 298 299 class TestSampler extends AbstractSampler 300 { 301 public SampleResult sample(org.apache.jmeter.samplers.Entry e) 302 { 303 return null; 304 } 305 public Object clone() 306 { 307 return new TestSampler(); 308 } 309 } 310 } 311 312 private class ObjectPair 313 { 314 TestElement child, parent; 315 316 public ObjectPair(TestElement one, TestElement two) 317 { 318 this.child = one; 319 this.parent = two; 320 } 321 322 public void addTestElements() 323 { 324 if (parent instanceof Controller 325 && (child instanceof Sampler || child instanceof Controller)) 326 { 327 parent.addTestElement(child); 328 } 329 } 330 331 public int hashCode() 332 { 333 return child.hashCode() + parent.hashCode(); 334 } 335 336 public boolean equals(Object o) 337 { 338 if (o instanceof ObjectPair) 339 { 340 return child == ((ObjectPair) o).child 341 && parent == ((ObjectPair) o).parent; 342 } 343 return false; 344 } 345 } 346 347 private void configureWithConfigElements(Sampler sam, List configs) 348 { 349 Iterator iter = configs.iterator(); 350 while (iter.hasNext()) 351 { 352 ConfigTestElement config = (ConfigTestElement)iter.next(); 353 sam.addTestElement(config); 354 } 355 } 356 } 357 | Popular Tags |