1 18 19 package org.apache.jmeter.engine.util; 20 21 import java.util.ArrayList ; 22 import java.util.Collection ; 23 import java.util.HashMap ; 24 import java.util.Iterator ; 25 import java.util.LinkedList ; 26 import java.util.List ; 27 import java.util.Map ; 28 29 import junit.framework.TestCase; 30 31 import org.apache.jmeter.config.ConfigTestElement; 32 import org.apache.jmeter.functions.InvalidVariableException; 33 import org.apache.jmeter.testelement.TestElement; 34 import org.apache.jmeter.testelement.TestPlan; 35 import org.apache.jmeter.testelement.property.CollectionProperty; 36 import org.apache.jmeter.testelement.property.JMeterProperty; 37 import org.apache.jmeter.testelement.property.MultiProperty; 38 import org.apache.jmeter.testelement.property.PropertyIterator; 39 import org.apache.jmeter.testelement.property.StringProperty; 40 import org.apache.jmeter.threads.JMeterContextService; 41 import org.apache.jmeter.threads.JMeterVariables; 42 import org.apache.jorphan.logging.LoggingManager; 43 import org.apache.log.Logger; 44 45 50 public class ValueReplacer 51 { 52 transient private static Logger log = LoggingManager.getLoggerForClass(); 53 CompoundVariable masterFunction = new CompoundVariable(); 54 Map variables = new HashMap (); 55 56 public ValueReplacer() 57 { 58 } 59 60 public ValueReplacer(TestPlan tp) 61 { 62 setUserDefinedVariables(tp.getUserDefinedVariables()); 63 } 64 65 public void setUserDefinedVariables(Map variables) 66 { 67 this.variables = variables; 68 } 69 70 public void replaceValues(TestElement el) throws InvalidVariableException 71 { 72 Collection newProps = 73 replaceValues( 74 el.propertyIterator(), 75 new ReplaceStringWithFunctions(masterFunction, variables)); 76 setProperties(el, newProps); 77 } 78 79 private void setProperties(TestElement el, Collection newProps) 80 { 81 Iterator iter = newProps.iterator(); 82 el.clear(); 83 while(iter.hasNext()) 84 { 85 el.setProperty((JMeterProperty)iter.next()); 86 } 87 } 88 89 public void reverseReplace(TestElement el) throws InvalidVariableException 90 { 91 Collection newProps = 92 replaceValues( 93 el.propertyIterator(), 94 new ReplaceFunctionsWithStrings(masterFunction, variables)); 95 setProperties(el, newProps); 96 } 97 98 public void undoReverseReplace(TestElement el) 99 throws InvalidVariableException 100 { 101 Collection newProps = 102 replaceValues( 103 el.propertyIterator(), 104 new UndoVariableReplacement(masterFunction, variables)); 105 setProperties(el, newProps); 106 } 107 108 public void addVariable(String name, String value) 109 { 110 variables.put(name, value); 111 } 112 113 118 public void addVariables(Map vars) 119 { 120 variables.putAll(vars); 121 } 122 123 private Collection replaceValues( 124 PropertyIterator iter, 125 ValueTransformer transform) 126 throws InvalidVariableException 127 { 128 List props = new LinkedList (); 129 while(iter.hasNext()) 130 { 131 JMeterProperty val = iter.next(); 132 if (log.isDebugEnabled()) 133 { 134 log.debug("About to replace in property of tipe: " 135 +val.getClass()+": "+val); 136 } 137 if (val instanceof StringProperty) 138 { 139 val = transform.transformValue((StringProperty) val); 140 if (log.isDebugEnabled()) 141 { 142 log.debug("Replacement result: " +val); 143 } 144 } 145 else if (val instanceof MultiProperty) 146 { 147 MultiProperty multiVal = (MultiProperty)val; 148 Collection newValues = 149 replaceValues(multiVal.iterator(), transform); 150 multiVal.clear(); 151 Iterator propIter = newValues.iterator(); 152 while(propIter.hasNext()) 153 { 154 multiVal.addProperty((JMeterProperty) propIter.next()); 155 } 156 if (log.isDebugEnabled()) 157 { 158 log.debug("Replacement result: " +multiVal); 159 } 160 } 161 else { 162 if (log.isDebugEnabled()) 163 { 164 log.debug("Won't replace."); 165 } 166 } 167 props.add(val); 168 } 169 return props; 170 } 171 172 173 174 public static class Test extends TestCase 175 { 176 TestPlan variables; 177 178 public Test(String name) 179 { 180 super(name); 181 } 182 183 public void setUp() 184 { 185 variables = new TestPlan(); 186 variables.addParameter("server", "jakarta.apache.org"); 187 variables.addParameter("username", "jack"); 188 variables.addParameter("password", "jacks_password"); 189 variables.addParameter("regex", ".*"); 190 JMeterVariables vars = new JMeterVariables(); 191 vars.put("server", "jakarta.apache.org"); 192 JMeterContextService.getContext().setVariables(vars); 193 JMeterContextService.getContext().setSamplingStarted(true); 194 } 195 196 197 198 public void testReverseReplacement() throws Exception 199 { 200 ValueReplacer replacer = new ValueReplacer(variables); 201 assertTrue( 202 variables.getUserDefinedVariables().containsKey("server")); 203 assertTrue(replacer.variables.containsKey("server")); 204 TestElement element = new TestPlan(); 205 element.setProperty( 206 new StringProperty("domain", "jakarta.apache.org")); 207 List args = new ArrayList (); 208 args.add("username is jack"); 209 args.add("jacks_password"); 210 element.setProperty(new CollectionProperty("args", args)); 211 replacer.reverseReplace(element); 212 assertEquals("${server}", element.getPropertyAsString("domain")); 213 args = (List ) element.getProperty("args").getObjectValue(); 214 assertEquals( 215 "${password}", 216 ((JMeterProperty) args.get(1)).getStringValue()); 217 } 218 219 public void testReplace() throws Exception 220 { 221 ValueReplacer replacer = new ValueReplacer(); 222 replacer.setUserDefinedVariables( 223 variables.getUserDefinedVariables()); 224 TestElement element = new ConfigTestElement(); 225 element.setProperty(new StringProperty("domain", "${server}")); 226 replacer.replaceValues(element); 227 log.debug("domain property = " + element.getProperty("domain")); 228 element.setRunningVersion(true); 229 assertEquals( 230 "jakarta.apache.org", 231 element.getPropertyAsString("domain")); 232 } 233 234 237 protected void tearDown() throws Exception 238 { 239 JMeterContextService.getContext().setSamplingStarted(false); 240 } 241 } 242 } 243 | Popular Tags |