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.extractor;
020:
021: import java.net.URL;
022:
023: import junit.framework.TestCase;
024:
025: import org.apache.jmeter.samplers.SampleResult;
026: import org.apache.jmeter.threads.JMeterContext;
027: import org.apache.jmeter.threads.JMeterContextService;
028: import org.apache.jmeter.threads.JMeterVariables;
029:
030: /**
031: * @version $Revision: 581862 $
032: */
033: public class TestRegexExtractor extends TestCase {
034: RegexExtractor extractor;
035:
036: SampleResult result;
037:
038: JMeterVariables vars;
039:
040: public TestRegexExtractor(String name) {
041: super (name);
042: }
043:
044: private JMeterContext jmctx = null;
045:
046: public void setUp() {
047: jmctx = JMeterContextService.getContext();
048: extractor = new RegexExtractor();
049: extractor.setThreadContext(jmctx);// This would be done by the run
050: // command
051: extractor.setRefName("regVal");
052: result = new SampleResult();
053: String data = "<company-xmlext-query-ret>" + "<row>"
054: + "<value field=\"RetCode\">LIS_OK</value>"
055: + "<value field=\"RetCodeExtension\"></value>"
056: + "<value field=\"alias\"></value>"
057: + "<value field=\"positioncount\"></value>"
058: + "<value field=\"invalidpincount\">0</value>"
059: + "<value field=\"pinposition1\">1</value>"
060: + "<value field=\"pinpositionvalue1\"></value>"
061: + "<value field=\"pinposition2\">5</value>"
062: + "<value field=\"pinpositionvalue2\"></value>"
063: + "<value field=\"pinposition3\">6</value>"
064: + "<value field=\"pinpositionvalue3\"></value>"
065: + "</row>" + "</company-xmlext-query-ret>";
066: result.setResponseData(data.getBytes());
067: result.setResponseHeaders("Header1: Value1\nHeader2: Value2");
068: result.setResponseCode("abcd");
069: result.setResponseMessage("The quick brown fox");
070: vars = new JMeterVariables();
071: jmctx.setVariables(vars);
072: jmctx.setPreviousResult(result);
073: }
074:
075: public void testVariableExtraction() throws Exception {
076: extractor
077: .setRegex("<value field=\"(pinposition\\d+)\">(\\d+)</value>");
078: extractor.setTemplate("$2$");
079: extractor.setMatchNumber(2);
080: extractor.process();
081: assertEquals("5", vars.get("regVal"));
082: assertEquals("pinposition2", vars.get("regVal_g1"));
083: assertEquals("5", vars.get("regVal_g2"));
084: assertEquals("<value field=\"pinposition2\">5</value>", vars
085: .get("regVal_g0"));
086: assertNull(vars.get("regVal_g3"));
087: assertEquals("2", vars.get("regVal_g"));
088: }
089:
090: static void templateSetup(RegexExtractor rex, String tmp) {
091: rex.setRegex("<company-(\\w+?)-(\\w+?)-(\\w+?)>");
092: rex.setMatchNumber(1);
093: rex.setTemplate(tmp);
094: rex.process();
095: }
096:
097: public void testTemplate1() throws Exception {
098: templateSetup(extractor, "");
099: assertEquals("<company-xmlext-query-ret>", vars
100: .get("regVal_g0"));
101: assertEquals("xmlext", vars.get("regVal_g1"));
102: assertEquals("query", vars.get("regVal_g2"));
103: assertEquals("ret", vars.get("regVal_g3"));
104: assertEquals("", vars.get("regVal"));
105: assertEquals("3", vars.get("regVal_g"));
106: }
107:
108: public void testTemplate2() throws Exception {
109: templateSetup(extractor, "ABC");
110: assertEquals("ABC", vars.get("regVal"));
111: }
112:
113: public void testTemplate3() throws Exception {
114: templateSetup(extractor, "$2$");
115: assertEquals("query", vars.get("regVal"));
116: }
117:
118: public void testTemplate4() throws Exception {
119: templateSetup(extractor, "PRE$2$");
120: assertEquals("PREquery", vars.get("regVal"));
121: }
122:
123: public void testTemplate5() throws Exception {
124: templateSetup(extractor, "$2$POST");
125: assertEquals("queryPOST", vars.get("regVal"));
126: }
127:
128: public void testTemplate6() throws Exception {
129: templateSetup(extractor, "$2$$1$");
130: assertEquals("queryxmlext", vars.get("regVal"));
131: }
132:
133: public void testTemplate7() throws Exception {
134: templateSetup(extractor, "$2$MID$1$");
135: assertEquals("queryMIDxmlext", vars.get("regVal"));
136: }
137:
138: public void testVariableExtraction2() throws Exception {
139: extractor
140: .setRegex("<value field=\"(pinposition\\d+)\">(\\d+)</value>");
141: extractor.setTemplate("$1$");
142: extractor.setMatchNumber(3);
143: extractor.process();
144: assertEquals("pinposition3", vars.get("regVal"));
145: }
146:
147: public void testVariableExtraction6() throws Exception {
148: extractor
149: .setRegex("<value field=\"(pinposition\\d+)\">(\\d+)</value>");
150: extractor.setTemplate("$2$");
151: extractor.setMatchNumber(4);
152: extractor.setDefaultValue("default");
153: extractor.process();
154: assertEquals("default", vars.get("regVal"));
155: }
156:
157: public void testVariableExtraction3() throws Exception {
158: extractor
159: .setRegex("<value field=\"(pinposition\\d+)\">(\\d+)</value>");
160: extractor.setTemplate("_$1$");
161: extractor.setMatchNumber(2);
162: extractor.process();
163: assertEquals("_pinposition2", vars.get("regVal"));
164: }
165:
166: public void testVariableExtraction5() throws Exception {
167: extractor
168: .setRegex("<value field=\"(pinposition\\d+)\">(\\d+)</value>");
169: extractor.setTemplate("$1$");
170: extractor.setMatchNumber(-1);
171: extractor.process();
172: assertEquals("3", vars.get("regVal_matchNr"));
173: assertEquals("pinposition1", vars.get("regVal_1"));
174: assertEquals("pinposition2", vars.get("regVal_2"));
175: assertEquals("pinposition3", vars.get("regVal_3"));
176: assertEquals("pinposition1", vars.get("regVal_1_g1"));
177: assertEquals("1", vars.get("regVal_1_g2"));
178: assertEquals("<value field=\"pinposition1\">1</value>", vars
179: .get("regVal_1_g0"));
180: assertNull(vars.get("regVal_4"));
181:
182: // Check old values don't hang around:
183: extractor.setRegex("(\\w+)count"); // fewer matches
184: extractor.process();
185: assertEquals("2", vars.get("regVal_matchNr"));
186: assertEquals("position", vars.get("regVal_1"));
187: assertEquals("invalidpin", vars.get("regVal_2"));
188: assertNull("Unused variables should be null", vars
189: .get("regVal_3"));
190: assertNull("Unused variables should be null", vars
191: .get("regVal_3_g0"));
192: assertNull("Unused variables should be null", vars
193: .get("regVal_3_g1"));
194: }
195:
196: public void testVariableExtraction7() throws Exception {
197: extractor.setRegex("Header1: (\\S+)");
198: extractor.setTemplate("$1$");
199: extractor.setMatchNumber(1);
200: assertTrue("useBody should be true", extractor.useBody());
201: assertFalse("useHdrs should be false", extractor.useHeaders());
202: assertFalse("useURL should be false", extractor.useUrl());
203: extractor.setUseField(RegexExtractor.USE_BODY);
204: assertTrue("useBody should be true", extractor.useBody());
205: assertFalse("useHdrs should be false", extractor.useHeaders());
206: assertFalse("useURL should be false", extractor.useUrl());
207: extractor.setUseField(RegexExtractor.USE_HDRS);
208: assertTrue("useHdrs should be true", extractor.useHeaders());
209: assertFalse("useBody should be false", extractor.useBody());
210: assertFalse("useURL should be false", extractor.useUrl());
211: extractor.process();
212: assertEquals("Value1", vars.get("regVal"));
213: extractor.setUseField(RegexExtractor.USE_URL);
214: assertFalse("useHdrs should be false", extractor.useHeaders());
215: assertFalse("useBody should be false", extractor.useBody());
216: assertTrue("useURL should be true", extractor.useUrl());
217: }
218:
219: public void testVariableExtraction8() throws Exception {
220: extractor.setRegex("http://jakarta\\.apache\\.org/(\\w+)");
221: extractor.setTemplate("$1$");
222: extractor.setMatchNumber(1);
223: extractor.setUseField(RegexExtractor.USE_URL);
224: assertFalse("useHdrs should be false", extractor.useHeaders());
225: assertFalse("useBody should be false", extractor.useBody());
226: assertTrue("useURL should be true", extractor.useUrl());
227: extractor.process();
228: assertNull(vars.get("regVal"));
229: result.setURL(new URL(
230: "http://jakarta.apache.org/index.html?abcd"));
231: extractor.process();
232: assertEquals("index", vars.get("regVal"));
233: }
234:
235: public void testVariableExtraction9() throws Exception {
236: extractor.setRegex("(\\w+)");
237: extractor.setTemplate("$1$");
238: extractor.setMatchNumber(1);
239: extractor.setUseField(RegexExtractor.USE_CODE);
240: assertFalse("useHdrs should be false", extractor.useHeaders());
241: assertFalse("useBody should be false", extractor.useBody());
242: assertFalse("useURL should be false", extractor.useUrl());
243: assertFalse("useMessage should be false", extractor
244: .useMessage());
245: assertTrue("useCode should be true", extractor.useCode());
246: extractor.process();
247: assertEquals("abcd", vars.get("regVal"));
248: extractor.setUseField(RegexExtractor.USE_MESSAGE);
249: assertFalse("useHdrs should be false", extractor.useHeaders());
250: assertFalse("useBody should be false", extractor.useBody());
251: assertFalse("useURL should be false", extractor.useUrl());
252: assertTrue("useMessage should be true", extractor.useMessage());
253: assertFalse("useCode should be falsee", extractor.useCode());
254: extractor.setMatchNumber(3);
255: extractor.process();
256: assertEquals("brown", vars.get("regVal"));
257: }
258:
259: public void testNoDefault() throws Exception {
260: extractor
261: .setRegex("<value field=\"(pinposition\\d+)\">(\\d+)</value>");
262: extractor.setTemplate("$2$");
263: extractor.setMatchNumber(4);
264: //extractor.setDefaultValue("default");
265: vars.put("regVal", "initial");
266: assertEquals("initial", vars.get("regVal"));
267: extractor.process();
268: assertEquals("initial", vars.get("regVal"));
269: }
270:
271: public void testDefault() throws Exception {
272: extractor
273: .setRegex("<value field=\"(pinposition\\d+)\">(\\d+)</value>");
274: extractor.setTemplate("$2$");
275: extractor.setMatchNumber(999);
276: extractor.setDefaultValue("default");
277: vars.put("regVal", "initial");
278: assertEquals("initial", vars.get("regVal"));
279: extractor.process();
280: assertEquals("default", vars.get("regVal"));
281: assertNull(vars.get("regVal_g0"));
282: assertNull(vars.get("regVal_g1"));
283: }
284:
285: public void testStaleVariables() throws Exception {
286: extractor
287: .setRegex("<value field=\"(pinposition\\d+)\">(\\d+)</value>");
288: extractor.setTemplate("$2$");
289: extractor.setMatchNumber(1);
290: extractor.setDefaultValue("default");
291: extractor.process();
292: assertEquals("1", vars.get("regVal"));
293: assertEquals("1", vars.get("regVal_g2"));
294: assertEquals("2", vars.get("regVal_g"));
295: assertNotNull(vars.get("regVal_g0"));
296: assertNotNull(vars.get("regVal_g1"));
297: // Now rerun with match fail
298: extractor.setMatchNumber(10);
299: extractor.process();
300: assertEquals("default", vars.get("regVal"));
301: assertNull(vars.get("regVal_g0"));
302: assertNull(vars.get("regVal_g1"));
303: assertNull(vars.get("regVal_g"));
304: }
305:
306: }
|