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: package org.apache.jmeter.functions;
020:
021: import java.util.Collection;
022: import java.util.LinkedList;
023:
024: import org.apache.jmeter.engine.util.CompoundVariable;
025: import org.apache.jmeter.junit.JMeterTestCase;
026: import org.apache.jmeter.samplers.SampleResult;
027: import org.apache.jmeter.threads.JMeterContext;
028: import org.apache.jmeter.threads.JMeterContextService;
029: import org.apache.jmeter.threads.JMeterVariables;
030:
031: public class TestRegexFunction extends JMeterTestCase {
032: RegexFunction variable;
033:
034: SampleResult result;
035:
036: Collection params;
037:
038: private JMeterVariables vars;
039:
040: private JMeterContext jmctx = null;
041:
042: public TestRegexFunction(String name) {
043: super (name);
044: }
045:
046: public void setUp() {
047: variable = new RegexFunction();
048: result = new SampleResult();
049: jmctx = JMeterContextService.getContext();
050: String data = "<company-xmlext-query-ret><row>"
051: + "<value field=\"RetCode\">" + "LIS_OK</value><value"
052: + " field=\"RetCodeExtension\"></value>"
053: + "<value field=\"alias\"></value><value"
054: + " field=\"positioncount\"></value>"
055: + "<value field=\"invalidpincount\">0</value><value"
056: + " field=\"pinposition1\">1</value><value"
057: + " field=\"pinpositionvalue1\"></value><value"
058: + " field=\"pinposition2\">5</value><value"
059: + " field=\"pinpositionvalue2\"></value><value"
060: + " field=\"pinposition3\">6</value><value"
061: + " field=\"pinpositionvalue3\"></value>"
062: + "</row></company-xmlext-query-ret>";
063: result.setResponseData(data.getBytes());
064: vars = new JMeterVariables();
065: jmctx.setVariables(vars);
066: jmctx.setPreviousResult(result);
067: }
068:
069: public void testVariableExtraction() throws Exception {
070: params = new LinkedList();
071: params.add(new CompoundVariable(
072: "<value field=\"(pinposition\\d+)\">(\\d+)</value>"));
073: params.add(new CompoundVariable("$2$"));
074: params.add(new CompoundVariable("2"));
075: variable.setParameters(params);
076: String match = variable.execute(result, null);
077: assertEquals("5", match);
078: }
079:
080: public void testVariableExtraction2() throws Exception {
081: params = new LinkedList();
082: params.add(new CompoundVariable(
083: "<value field=\"(pinposition\\d+)\">(\\d+)</value>"));
084: params.add(new CompoundVariable("$1$"));
085: params.add(new CompoundVariable("3"));
086: variable.setParameters(params);
087: String match = variable.execute(result, null);
088: assertEquals("pinposition3", match);
089: }
090:
091: public void testVariableExtraction5() throws Exception {
092: params = new LinkedList();
093: params.add(new CompoundVariable(
094: "<value field=\"(pinposition\\d+)\">(\\d+)</value>"));
095: params.add(new CompoundVariable("$1$"));
096: params.add(new CompoundVariable("ALL"));
097: params.add(new CompoundVariable("_"));
098: variable.setParameters(params);
099: String match = variable.execute(result, null);
100: assertEquals("pinposition1_pinposition2_pinposition3", match);
101: }
102:
103: public void testVariableExtraction6() throws Exception {
104: params = new LinkedList();
105: params.add(new CompoundVariable(
106: "<value field=\"(pinposition\\d+)\">(\\d+)</value>"));
107: params.add(new CompoundVariable("$2$"));
108: params.add(new CompoundVariable("4"));
109: params.add(new CompoundVariable(""));
110: params.add(new CompoundVariable("default"));
111: variable.setParameters(params);
112: String match = variable.execute(result, null);
113: assertEquals("default", match);
114: }
115:
116: public void testComma() throws Exception {
117: params = new LinkedList();
118: params.add(new CompoundVariable(
119: "<value,? field=\"(pinposition\\d+)\">(\\d+)</value>"));
120: params.add(new CompoundVariable("$1$"));
121: params.add(new CompoundVariable("3"));
122: variable.setParameters(params);
123: String match = variable.execute(result, null);
124: assertEquals("pinposition3", match);
125: }
126:
127: public void testVariableExtraction3() throws Exception {
128: params = new LinkedList();
129: params.add(new CompoundVariable(
130: "<value field=\"(pinposition\\d+)\">(\\d+)</value>"));
131: params.add(new CompoundVariable("_$1$"));
132: params.add(new CompoundVariable("2"));
133: variable.setParameters(params);
134: String match = variable.execute(result, null);
135: assertEquals("_pinposition2", match);
136: }
137:
138: public void testVariableExtraction4() throws Exception {
139: params = new LinkedList();
140: params.add(new CompoundVariable(
141: "<value field=\"(pinposition\\d+)\">(\\d+)</value>"));
142: params.add(new CompoundVariable("$2$, "));
143: params.add(new CompoundVariable(".333"));
144: variable.setParameters(params);
145: String match = variable.execute(result, null);
146: assertEquals("1, ", match);
147: }
148:
149: public void testDefaultValue() throws Exception {
150: params = new LinkedList();
151: params.add(new CompoundVariable(
152: "<value,, field=\"(pinposition\\d+)\">(\\d+)</value>"));
153: params.add(new CompoundVariable("$2$, "));
154: params.add(new CompoundVariable(".333"));
155: params.add(new CompoundVariable(""));
156: params.add(new CompoundVariable("No Value Found"));
157: variable.setParameters(params);
158: String match = variable.execute(result, null);
159: assertEquals("No Value Found", match);
160: }
161: }
|