1 18 19 package org.apache.jmeter.control; 20 21 import java.io.Serializable ; 22 23 import org.apache.jmeter.samplers.Sampler; 24 import org.apache.jmeter.threads.JMeterContext; 25 import org.apache.jmeter.testelement.property.BooleanProperty; 26 import org.apache.jmeter.testelement.property.StringProperty; 27 import org.apache.jorphan.logging.LoggingManager; 28 import org.apache.log.Logger; 29 30 36 public class ForeachController extends GenericController implements Serializable 37 { 38 private static final Logger log = LoggingManager.getLoggerForClass(); 39 40 private final static String INPUTVAL = "ForeachController.inputVal"; 41 private final static String RETURNVAL ="ForeachController.returnVal"; 42 private final static String USE_SEPARATOR ="ForeachController.useSeparator"; 43 private int loopCount = 0; 44 45 private static final String DEFAULT_SEPARATOR = "_"; 46 47 public ForeachController() 48 { 49 } 50 51 public void setInputVal(String inputValue) 52 { 53 setProperty(new StringProperty(INPUTVAL, inputValue)); 54 } 55 56 private String getInputVal() 57 { 58 getProperty(INPUTVAL).recoverRunningVersion(null); 59 return getInputValString(); 60 } 61 public String getInputValString() 62 { 63 return getPropertyAsString(INPUTVAL); 64 } 65 66 public void setReturnVal(String inputValue) 67 { 68 setProperty(new StringProperty(RETURNVAL, inputValue)); 69 } 70 71 private String getReturnVal() 72 { 73 getProperty(RETURNVAL).recoverRunningVersion(null); 74 return getReturnValString(); 75 } 76 public String getReturnValString() 77 { 78 return getPropertyAsString(RETURNVAL); 79 } 80 81 private String getSeparator() 82 { 83 return getUseSeparator() ? DEFAULT_SEPARATOR : ""; 84 } 85 86 public void setUseSeparator(boolean b) 87 { 88 setProperty(new BooleanProperty(USE_SEPARATOR, b)); 89 } 90 91 public boolean getUseSeparator() 92 { 93 return getPropertyAsBoolean(USE_SEPARATOR,true); 94 } 95 96 99 public boolean isDone() 100 { 101 JMeterContext context = getThreadContext(); 102 String inputVariable=getInputVal()+getSeparator()+(loopCount+1); 103 if (context.getVariables().get(inputVariable) != null) 104 { 105 context.getVariables().put(getReturnVal(), context.getVariables().get(inputVariable)); 106 log.debug("ForEach resultstring isDone="+context.getVariables().get(getReturnVal())); 107 return false; 108 } 109 return super.isDone(); 110 } 111 112 private boolean endOfArguments() 113 { 114 JMeterContext context = getThreadContext(); 115 String inputVariable=getInputVal()+getSeparator()+(loopCount+1); 116 if (context.getVariables().get(inputVariable) != null) 117 { 118 log.debug("ForEach resultstring eofArgs= false"); 119 return false; 120 } else { 121 log.debug("ForEach resultstring eofArgs= true"); 122 return true; 123 } 124 } 125 126 public Sampler next() 128 { 129 if(emptyList()) 130 { 131 reInitialize(); 132 return null; 133 } 134 return super.next(); 135 } 136 137 142 private boolean emptyList() { 143 JMeterContext context = getThreadContext(); 144 String inputVariable=getInputVal()+getSeparator()+"1"; 145 if (context.getVariables().get(inputVariable) != null) 146 { 147 return false; 148 } 149 else 150 { 151 log.debug("No entries found - null first entry: "+inputVariable); 152 return true; 153 } 154 } 155 156 159 protected Sampler nextIsNull() throws NextIsNullException 160 { 161 reInitialize(); 162 if (endOfArguments()) 163 { 164 resetLoopCount(); 166 return null; 167 } 168 else 169 { 170 return next(); 171 } 172 } 173 174 protected void incrementLoopCount() 175 { 176 loopCount++; 177 } 178 179 protected void resetLoopCount() 180 { 181 loopCount = 0; 182 } 183 184 187 protected int getIterCount() 188 { 189 return loopCount + 1; 190 } 191 192 195 protected void reInitialize() 196 { 197 setFirst(true); 198 resetCurrent(); 199 incrementLoopCount(); 200 } 201 } | Popular Tags |