1 18 19 22 package org.apache.jmeter.engine.util; 23 24 import java.util.HashMap ; 25 import java.util.Map ; 26 27 import junit.framework.TestCase; 28 29 import org.apache.jmeter.samplers.SampleResult; 30 import org.apache.jmeter.testelement.property.JMeterProperty; 31 import org.apache.jmeter.testelement.property.StringProperty; 32 import org.apache.jmeter.threads.JMeterContext; 33 import org.apache.jmeter.threads.JMeterContextService; 34 import org.apache.jmeter.threads.JMeterVariables; 35 36 public class PackageTest extends TestCase 37 { 38 Map variables; 39 SampleResult result; 40 ReplaceStringWithFunctions transformer; 41 44 public PackageTest(String arg0) 45 { 46 super(arg0); 47 } 49 50 private JMeterContext jmctx = null; 51 52 public void setUp() 53 { 54 jmctx =JMeterContextService.getContext(); 55 variables = new HashMap (); 56 variables.put("my_regex", ".*"); 57 variables.put("server", "jakarta.apache.org"); 58 result = new SampleResult(); 59 result.setResponseData( 60 "<html>hello world</html> costs: $3.47,$5.67".getBytes()); 61 transformer = 62 new ReplaceStringWithFunctions(new CompoundVariable(), variables); 63 jmctx.setVariables(new JMeterVariables()); 64 jmctx.setSamplingStarted(true); 65 jmctx.setPreviousResult(result); 66 jmctx.getVariables().put( 67 "server", 68 "jakarta.apache.org"); 69 jmctx.getVariables().put("my_regex", ".*"); 70 } 71 72 public void testFunctionParse1() throws Exception 73 { 74 StringProperty prop = 75 new StringProperty( 76 "date", 77 "${__javaScript((new Date().getDate() / 100).toString()." 78 + "substr(${__javaScript(1+1,d\\,ay)}\\,2),heute)}"); 79 JMeterProperty newProp = transformer.transformValue(prop); 80 newProp.setRunningVersion(true); 81 assertEquals( 82 "org.apache.jmeter.testelement.property.FunctionProperty", 83 newProp.getClass().getName()); 84 newProp.recoverRunningVersion(null); 85 assertTrue(Integer.parseInt(newProp.getStringValue()) > -1); 86 assertEquals( 87 "2", 88 jmctx.getVariables().getObject("d,ay")); 89 } 90 91 public void testParseExample1() throws Exception 92 { 93 StringProperty prop = 94 new StringProperty( 95 "html", 96 "${__regexFunction(<html>(.*)</html>,$1$)}"); 97 JMeterProperty newProp = transformer.transformValue(prop); 98 newProp.setRunningVersion(true); 99 assertEquals( 100 "org.apache.jmeter.testelement.property.FunctionProperty", 101 newProp.getClass().getName()); 102 assertEquals("hello world", newProp.getStringValue()); 103 } 104 105 public void testParseExample2() throws Exception 106 { 107 StringProperty prop = 108 new StringProperty( 109 "html", 110 "It should say:\\${${__regexFunction(<html>(.*)</html>,$1$)}}"); 111 JMeterProperty newProp = transformer.transformValue(prop); 112 newProp.setRunningVersion(true); 113 assertEquals( 114 "org.apache.jmeter.testelement.property.FunctionProperty", 115 newProp.getClass().getName()); 116 assertEquals("It should say:${hello world}", newProp.getStringValue()); 117 } 118 119 public void testParseExample3() throws Exception 120 { 121 StringProperty prop = 122 new StringProperty( 123 "html", 124 "${__regexFunction(<html>(.*)</html>,$1$)}" 125 + "${__regexFunction(<html>(.*o)(.*o)(.*)</html>," 126 + "$1$$3$)}"); 127 JMeterProperty newProp = transformer.transformValue(prop); 128 newProp.setRunningVersion(true); 129 assertEquals( 130 "org.apache.jmeter.testelement.property.FunctionProperty", 131 newProp.getClass().getName()); 132 assertEquals("hello worldhellorld", newProp.getStringValue()); 133 } 134 135 public void testParseExample4() throws Exception 136 { 137 StringProperty prop = 138 new StringProperty("html", "${non-existing function}"); 139 JMeterProperty newProp = transformer.transformValue(prop); 140 newProp.setRunningVersion(true); 141 assertEquals( 142 "org.apache.jmeter.testelement.property.FunctionProperty", 143 newProp.getClass().getName()); 144 assertEquals("${non-existing function}", newProp.getStringValue()); 145 } 146 147 public void testParseExample6() throws Exception 148 { 149 StringProperty prop = new StringProperty("html", "${server}"); 150 JMeterProperty newProp = transformer.transformValue(prop); 151 newProp.setRunningVersion(true); 152 assertEquals( 153 "org.apache.jmeter.testelement.property.FunctionProperty", 154 newProp.getClass().getName()); 155 assertEquals("jakarta.apache.org", newProp.getStringValue()); 156 } 157 158 public void testParseExample5() throws Exception 159 { 160 StringProperty prop = new StringProperty("html", ""); 161 JMeterProperty newProp = transformer.transformValue(prop); 162 newProp.setRunningVersion(true); 163 assertEquals( 164 "org.apache.jmeter.testelement.property.StringProperty", 165 newProp.getClass().getName()); 166 assertEquals("", newProp.getStringValue()); 167 } 168 169 public void testParseExample7() throws Exception 170 { 171 StringProperty prop = 172 new StringProperty( 173 "html", 174 "${__regexFunction(\\<([a-z]*)\\>,$1$)}"); 175 JMeterProperty newProp = transformer.transformValue(prop); 176 newProp.setRunningVersion(true); 177 assertEquals( 178 "org.apache.jmeter.testelement.property.FunctionProperty", 179 newProp.getClass().getName()); 180 assertEquals("html", newProp.getStringValue()); 181 } 182 183 public void testParseExample8() throws Exception 184 { 185 StringProperty prop = 186 new StringProperty( 187 "html", 188 "${__regexFunction((\\\\$\\d+\\.\\d+),$1$)}"); 189 JMeterProperty newProp = transformer.transformValue(prop); 190 newProp.setRunningVersion(true); 191 assertEquals( 192 "org.apache.jmeter.testelement.property.FunctionProperty", 193 newProp.getClass().getName()); 194 assertEquals("$3.47", newProp.getStringValue()); 195 } 196 197 public void testParseExample9() throws Exception 198 { 199 StringProperty prop = 200 new StringProperty( 201 "html", 202 "${__regexFunction(([$]\\d+\\.\\d+),$1$)}"); 203 JMeterProperty newProp = transformer.transformValue(prop); 204 newProp.setRunningVersion(true); 205 assertEquals( 206 "org.apache.jmeter.testelement.property.FunctionProperty", 207 newProp.getClass().getName()); 208 assertEquals("$3.47", newProp.getStringValue()); 209 } 210 211 public void testParseExample10() throws Exception 212 { 213 StringProperty prop = 214 new StringProperty( 215 "html", 216 "${__regexFunction(\\ " 217 + "(\\\\\\$\\d+\\.\\d+\\,\\\\$\\d+\\.\\d+),$1$)}"); 218 JMeterProperty newProp = transformer.transformValue(prop); 219 newProp.setRunningVersion(true); 220 assertEquals( 221 "org.apache.jmeter.testelement.property.FunctionProperty", 222 newProp.getClass().getName()); 223 assertEquals("$3.47,$5.67", newProp.getStringValue()); 224 } 225 226 public void testNestedExample1() throws Exception 227 { 228 StringProperty prop = 229 new StringProperty( 230 "html", 231 "${__regexFunction(<html>(${my_regex})</html>," 232 + "$1$)}${__regexFunction(<html>(.*o)(.*o)(.*)" 233 + "</html>,$1$$3$)}"); 234 JMeterProperty newProp = transformer.transformValue(prop); 235 newProp.setRunningVersion(true); 236 assertEquals( 237 "org.apache.jmeter.testelement.property.FunctionProperty", 238 newProp.getClass().getName()); 239 assertEquals("hello worldhellorld", newProp.getStringValue()); 240 } 241 242 public void testNestedExample2() throws Exception 243 { 244 StringProperty prop = 245 new StringProperty( 246 "html", 247 "${__regexFunction(<html>(${my_regex})</html>,$1$)}"); 248 JMeterProperty newProp = transformer.transformValue(prop); 249 newProp.setRunningVersion(true); 250 assertEquals( 251 "org.apache.jmeter.testelement.property.FunctionProperty", 252 newProp.getClass().getName()); 253 assertEquals("hello world", newProp.getStringValue()); 254 } 255 256 } 257 | Popular Tags |