001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: /*
020: * Created on Jul 25, 2003
021: */
022: package org.apache.jmeter.engine.util;
023:
024: import java.util.HashMap;
025: import java.util.Map;
026:
027: import junit.framework.TestCase;
028:
029: import org.apache.jmeter.samplers.SampleResult;
030: import org.apache.jmeter.testelement.property.JMeterProperty;
031: import org.apache.jmeter.testelement.property.StringProperty;
032: import org.apache.jmeter.threads.JMeterContext;
033: import org.apache.jmeter.threads.JMeterContextService;
034: import org.apache.jmeter.threads.JMeterVariables;
035:
036: public class PackageTest extends TestCase {
037: Map variables;
038:
039: SampleResult result;
040:
041: ReplaceStringWithFunctions transformer;
042:
043: /**
044: * @param arg0
045: */
046: public PackageTest(String arg0) {
047: super (arg0);
048: // TODO Auto-generated constructor stub
049: }
050:
051: private JMeterContext jmctx = null;
052:
053: public void setUp() {
054: jmctx = JMeterContextService.getContext();
055: variables = new HashMap();
056: variables.put("my_regex", ".*");
057: variables.put("server", "jakarta.apache.org");
058: result = new SampleResult();
059: result
060: .setResponseData("<html>hello world</html> costs: $3.47,$5.67"
061: .getBytes());
062: transformer = new ReplaceStringWithFunctions(
063: new CompoundVariable(), variables);
064: jmctx.setVariables(new JMeterVariables());
065: jmctx.setSamplingStarted(true);
066: jmctx.setPreviousResult(result);
067: jmctx.getVariables().put("server", "jakarta.apache.org");
068: jmctx.getVariables().put("my_regex", ".*");
069: }
070:
071: public void testFunctionParse1() throws Exception {
072: StringProperty prop = new StringProperty(
073: "date",
074: "${__javaScript((new Date().getDate() / 100).toString()."
075: + "substr(${__javaScript(1+1,d\\,ay)}\\,2),heute)}");
076: JMeterProperty newProp = transformer.transformValue(prop);
077: newProp.setRunningVersion(true);
078: assertEquals(
079: "org.apache.jmeter.testelement.property.FunctionProperty",
080: newProp.getClass().getName());
081: newProp.recoverRunningVersion(null);
082: assertTrue(Integer.parseInt(newProp.getStringValue()) > -1);
083: assertEquals("2", jmctx.getVariables().getObject("d,ay"));
084: }
085:
086: public void testParseExample1() throws Exception {
087: StringProperty prop = new StringProperty("html",
088: "${__regexFunction(<html>(.*)</html>,$1$)}");
089: JMeterProperty newProp = transformer.transformValue(prop);
090: newProp.setRunningVersion(true);
091: assertEquals(
092: "org.apache.jmeter.testelement.property.FunctionProperty",
093: newProp.getClass().getName());
094: assertEquals("hello world", newProp.getStringValue());
095: }
096:
097: public void testParseExample2() throws Exception {
098: StringProperty prop = new StringProperty("html",
099: "It should say:\\${${__regexFunction(<html>(.*)</html>,$1$)}}");
100: JMeterProperty newProp = transformer.transformValue(prop);
101: newProp.setRunningVersion(true);
102: assertEquals(
103: "org.apache.jmeter.testelement.property.FunctionProperty",
104: newProp.getClass().getName());
105: assertEquals("It should say:${hello world}", newProp
106: .getStringValue());
107: }
108:
109: public void testParseExample3() throws Exception {
110: StringProperty prop = new StringProperty(
111: "html",
112: "${__regexFunction(<html>(.*)</html>,$1$)}"
113: + "${__regexFunction(<html>(.*o)(.*o)(.*)</html>,"
114: + "$1$$3$)}");
115: JMeterProperty newProp = transformer.transformValue(prop);
116: newProp.setRunningVersion(true);
117: assertEquals(
118: "org.apache.jmeter.testelement.property.FunctionProperty",
119: newProp.getClass().getName());
120: assertEquals("hello worldhellorld", newProp.getStringValue());
121: }
122:
123: public void testParseExample4() throws Exception {
124: StringProperty prop = new StringProperty("html",
125: "${non-existing function}");
126: JMeterProperty newProp = transformer.transformValue(prop);
127: newProp.setRunningVersion(true);
128: assertEquals(
129: "org.apache.jmeter.testelement.property.FunctionProperty",
130: newProp.getClass().getName());
131: assertEquals("${non-existing function}", newProp
132: .getStringValue());
133: }
134:
135: public void testParseExample6() throws Exception {
136: StringProperty prop = new StringProperty("html", "${server}");
137: JMeterProperty newProp = transformer.transformValue(prop);
138: newProp.setRunningVersion(true);
139: assertEquals(
140: "org.apache.jmeter.testelement.property.FunctionProperty",
141: newProp.getClass().getName());
142: assertEquals("jakarta.apache.org", newProp.getStringValue());
143: }
144:
145: public void testParseExample5() throws Exception {
146: StringProperty prop = new StringProperty("html", "");
147: JMeterProperty newProp = transformer.transformValue(prop);
148: newProp.setRunningVersion(true);
149: assertEquals(
150: "org.apache.jmeter.testelement.property.StringProperty",
151: newProp.getClass().getName());
152: assertEquals("", newProp.getStringValue());
153: }
154:
155: public void testParseExample7() throws Exception {
156: StringProperty prop = new StringProperty("html",
157: "${__regexFunction(\\<([a-z]*)\\>,$1$)}");
158: JMeterProperty newProp = transformer.transformValue(prop);
159: newProp.setRunningVersion(true);
160: assertEquals(
161: "org.apache.jmeter.testelement.property.FunctionProperty",
162: newProp.getClass().getName());
163: assertEquals("html", newProp.getStringValue());
164: }
165:
166: public void testParseExample8() throws Exception {
167: StringProperty prop = new StringProperty("html",
168: "${__regexFunction((\\\\$\\d+\\.\\d+),$1$)}");
169: JMeterProperty newProp = transformer.transformValue(prop);
170: newProp.setRunningVersion(true);
171: assertEquals(
172: "org.apache.jmeter.testelement.property.FunctionProperty",
173: newProp.getClass().getName());
174: assertEquals("$3.47", newProp.getStringValue());
175: }
176:
177: public void testParseExample9() throws Exception {
178: StringProperty prop = new StringProperty("html",
179: "${__regexFunction(([$]\\d+\\.\\d+),$1$)}");
180: JMeterProperty newProp = transformer.transformValue(prop);
181: newProp.setRunningVersion(true);
182: assertEquals(
183: "org.apache.jmeter.testelement.property.FunctionProperty",
184: newProp.getClass().getName());
185: assertEquals("$3.47", newProp.getStringValue());
186: }
187:
188: public void testParseExample10() throws Exception {
189: StringProperty prop = new StringProperty(
190: "html",
191: "${__regexFunction(\\ "
192: + "(\\\\\\$\\d+\\.\\d+\\,\\\\$\\d+\\.\\d+),$1$)}");
193: JMeterProperty newProp = transformer.transformValue(prop);
194: newProp.setRunningVersion(true);
195: assertEquals(
196: "org.apache.jmeter.testelement.property.FunctionProperty",
197: newProp.getClass().getName());
198: assertEquals("$3.47,$5.67", newProp.getStringValue());
199: }
200:
201: public void testNestedExample1() throws Exception {
202: StringProperty prop = new StringProperty("html",
203: "${__regexFunction(<html>(${my_regex})</html>,"
204: + "$1$)}${__regexFunction(<html>(.*o)(.*o)(.*)"
205: + "</html>,$1$$3$)}");
206: JMeterProperty newProp = transformer.transformValue(prop);
207: newProp.setRunningVersion(true);
208: assertEquals(
209: "org.apache.jmeter.testelement.property.FunctionProperty",
210: newProp.getClass().getName());
211: assertEquals("hello worldhellorld", newProp.getStringValue());
212: }
213:
214: public void testNestedExample2() throws Exception {
215: StringProperty prop = new StringProperty("html",
216: "${__regexFunction(<html>(${my_regex})</html>,$1$)}");
217: JMeterProperty newProp = transformer.transformValue(prop);
218: newProp.setRunningVersion(true);
219: assertEquals(
220: "org.apache.jmeter.testelement.property.FunctionProperty",
221: newProp.getClass().getName());
222: assertEquals("hello world", newProp.getStringValue());
223: }
224:
225: }
|