View | Details | Raw Unified | Return to bug 52061
Collapse All | Expand All

(-)src/core/org/apache/jmeter/resources/messages.properties (-2 / +3 lines)
Lines 748-758 Link Here
748
regex_params_values_field=Parameter values regex group number
748
regex_params_values_field=Parameter values regex group number
749
regex_params_ref_name_field=Regular Expression Reference Name
749
regex_params_ref_name_field=Regular Expression Reference Name
750
regex_params_title=RegEx User Parameters
750
regex_params_title=RegEx User Parameters
751
regex_source=Response Field to check
751
regex_source=Field to check
752
regex_src_body=Body
752
regex_src_body=Body
753
regex_src_body_as_document=Body as a Document
753
regex_src_body_as_document=Body as a Document
754
regex_src_body_unescaped=Body (unescaped)
754
regex_src_body_unescaped=Body (unescaped)
755
regex_src_hdrs=Headers
755
regex_src_hdrs_resp=Resp. Headers
756
regex_src_hdrs_req=Req. Headers
756
regex_src_url=URL
757
regex_src_url=URL
757
regexfunc_param_1=Regular expression used to search previous sample - or variable.
758
regexfunc_param_1=Regular expression used to search previous sample - or variable.
758
regexfunc_param_2=Template for the replacement string, using groups from the regular expression.  Format is $[group]$.  Example $1$.
759
regexfunc_param_2=Template for the replacement string, using groups from the regular expression.  Format is $[group]$.  Example $1$.
(-)src/components/org/apache/jmeter/extractor/RegexExtractor.java (-50 / +49 lines)
Lines 18-27 Link Here
18
18
19
package org.apache.jmeter.extractor;
19
package org.apache.jmeter.extractor;
20
20
21
import java.io.Serializable;
22
import java.util.ArrayList;
23
import java.util.List;
24
25
import org.apache.commons.lang3.StringEscapeUtils;
21
import org.apache.commons.lang3.StringEscapeUtils;
26
import org.apache.jmeter.processor.PostProcessor;
22
import org.apache.jmeter.processor.PostProcessor;
27
import org.apache.jmeter.samplers.SampleResult;
23
import org.apache.jmeter.samplers.SampleResult;
Lines 34-46 Link Here
34
import org.apache.jorphan.logging.LoggingManager;
30
import org.apache.jorphan.logging.LoggingManager;
35
import org.apache.log.Logger;
31
import org.apache.log.Logger;
36
import org.apache.oro.text.MalformedCachePatternException;
32
import org.apache.oro.text.MalformedCachePatternException;
37
import org.apache.oro.text.regex.MatchResult;
33
import org.apache.oro.text.regex.*;
38
import org.apache.oro.text.regex.Pattern;
39
import org.apache.oro.text.regex.PatternMatcher;
40
import org.apache.oro.text.regex.PatternMatcherInput;
41
import org.apache.oro.text.regex.Perl5Compiler;
42
import org.apache.oro.text.regex.Perl5Matcher;
43
34
35
import java.io.Serializable;
36
import java.util.ArrayList;
37
import java.util.List;
38
44
// @see org.apache.jmeter.extractor.TestRegexExtractor for unit tests
39
// @see org.apache.jmeter.extractor.TestRegexExtractor for unit tests
45
40
46
public class RegexExtractor extends AbstractScopedTestElement implements PostProcessor, Serializable {
41
public class RegexExtractor extends AbstractScopedTestElement implements PostProcessor, Serializable {
Lines 60-66 Link Here
60
     *
55
     *
61
     *  Do not change these values!
56
     *  Do not change these values!
62
    */
57
    */
63
    public static final String USE_HDRS = "true"; // $NON-NLS-1$
58
    public static final String USE_RESPONSE_HDRS = "response_headers"; // $NON-NLS-1$
59
    public static final String USE_REQUEST_HDRS = "request_headers"; // $NON-NLS-1$
64
    public static final String USE_BODY = "false"; // $NON-NLS-1$
60
    public static final String USE_BODY = "false"; // $NON-NLS-1$
65
    public static final String USE_BODY_UNESCAPED = "unescaped"; // $NON-NLS-1$
61
    public static final String USE_BODY_UNESCAPED = "unescaped"; // $NON-NLS-1$
66
    public static final String USE_BODY_AS_DOCUMENT = "as_document"; // $NON-NLS-1$
62
    public static final String USE_BODY_AS_DOCUMENT = "as_document"; // $NON-NLS-1$
Lines 107-113 Link Here
107
        int matchNumber = getMatchNumber();
103
        int matchNumber = getMatchNumber();
108
104
109
        final String defaultValue = getDefaultValue();
105
        final String defaultValue = getDefaultValue();
110
        if (defaultValue.length() > 0){// Only replace default if it is provided
106
        if (defaultValue.length() > 0) {// Only replace default if it is provided
111
            vars.put(refName, defaultValue);
107
            vars.put(refName, defaultValue);
112
        }
108
        }
113
        Perl5Matcher matcher = JMeterUtils.getMatcher();
109
        Perl5Matcher matcher = JMeterUtils.getMatcher();
Lines 123-132 Link Here
123
                try {
119
                try {
124
                    prevCount = Integer.parseInt(prevString);
120
                    prevCount = Integer.parseInt(prevString);
125
                } catch (NumberFormatException e1) {
121
                } catch (NumberFormatException e1) {
126
                    log.warn("Could not parse "+prevString+" "+e1);
122
                    log.warn("Could not parse " + prevString + " " + e1);
127
                }
123
                }
128
            }
124
            }
129
            int matchCount=0;// Number of refName_n variable sets to keep
125
            int matchCount = 0;// Number of refName_n variable sets to keep
130
            try {
126
            try {
131
                MatchResult match;
127
                MatchResult match;
132
                if (matchNumber >= 0) {// Original match behaviour
128
                if (matchNumber >= 0) {// Original match behaviour
Lines 170-186 Link Here
170
166
171
    private String getInputString(SampleResult result) {
167
    private String getInputString(SampleResult result) {
172
        String inputString = useUrl() ? result.getUrlAsString() // Bug 39707
168
        String inputString = useUrl() ? result.getUrlAsString() // Bug 39707
173
                : useHeaders() ? result.getResponseHeaders()
169
                : useResponseHeaders() ? result.getResponseHeaders()
170
                : useRequestHeaders() ? result.getRequestHeaders()
174
                : useCode() ? result.getResponseCode() // Bug 43451
171
                : useCode() ? result.getResponseCode() // Bug 43451
175
                : useMessage() ? result.getResponseMessage() // Bug 43451
172
                : useMessage() ? result.getResponseMessage() // Bug 43451
176
                : useUnescapedBody() ? StringEscapeUtils.unescapeHtml4(result.getResponseDataAsString())
173
                : useUnescapedBody() ? StringEscapeUtils.unescapeHtml4(result.getResponseDataAsString())
177
                : useBodyAsDocument() ? Document.getTextFromDocument(result.getResponseData())
174
                : useBodyAsDocument() ? Document.getTextFromDocument(result.getResponseData())
178
                : result.getResponseDataAsString() // Bug 36898
175
                : result.getResponseDataAsString() // Bug 36898
179
                ;
176
                ;
180
       if (log.isDebugEnabled()) {
177
        if (log.isDebugEnabled()) {
181
           log.debug("Input = " + inputString);
178
            log.debug("Input = " + inputString);
182
       }
179
        }
183
       return inputString;
180
        return inputString;
184
    }
181
    }
185
182
186
    private List<MatchResult> processMatches(Pattern pattern, String regex, SampleResult result, int matchNumber, JMeterVariables vars) {
183
    private List<MatchResult> processMatches(Pattern pattern, String regex, SampleResult result, int matchNumber, JMeterVariables vars) {
Lines 192-199 Link Here
192
        List<MatchResult> matches = new ArrayList<MatchResult>();
189
        List<MatchResult> matches = new ArrayList<MatchResult>();
193
        int found = 0;
190
        int found = 0;
194
191
195
        if (isScopeVariable()){
192
        if (isScopeVariable()) {
196
            String inputString=vars.get(getVariableName());
193
            String inputString = vars.get(getVariableName());
197
            matchStrings(matchNumber, matcher, pattern, matches, found,
194
            matchStrings(matchNumber, matcher, pattern, matches, found,
198
                    inputString);
195
                    inputString);
199
        } else {
196
        } else {
Lines 202-208 Link Here
202
                String inputString = getInputString(sr);
199
                String inputString = getInputString(sr);
203
                found = matchStrings(matchNumber, matcher, pattern, matches, found,
200
                found = matchStrings(matchNumber, matcher, pattern, matches, found,
204
                        inputString);
201
                        inputString);
205
                if (matchNumber > 0 && found == matchNumber){// no need to process further
202
                if (matchNumber > 0 && found == matchNumber) {// no need to process further
206
                    break;
203
                    break;
207
                }
204
                }
208
            }
205
            }
Lines 211-220 Link Here
211
    }
208
    }
212
209
213
    private int matchStrings(int matchNumber, Perl5Matcher matcher,
210
    private int matchStrings(int matchNumber, Perl5Matcher matcher,
214
            Pattern pattern, List<MatchResult> matches, int found,
211
                             Pattern pattern, List<MatchResult> matches, int found,
215
            String inputString) {
212
                             String inputString) {
216
        PatternMatcherInput input = new PatternMatcherInput(inputString);
213
        PatternMatcherInput input = new PatternMatcherInput(inputString);
217
        while (matchNumber <=0 || found != matchNumber) {
214
        while (matchNumber <= 0 || found != matchNumber) {
218
            if (matcher.contains(input, pattern)) {
215
            if (matcher.contains(input, pattern)) {
219
                log.debug("RegexExtractor: Match found!");
216
                log.debug("RegexExtractor: Match found!");
220
                matches.add(matcher.getMatch());
217
                matches.add(matcher.getMatch());
Lines 235-248 Link Here
235
        StringBuilder buf = new StringBuilder();
232
        StringBuilder buf = new StringBuilder();
236
        buf.append(basename);
233
        buf.append(basename);
237
        buf.append("_g"); // $NON-NLS-1$
234
        buf.append("_g"); // $NON-NLS-1$
238
        int pfxlen=buf.length();
235
        int pfxlen = buf.length();
239
        String prevString=vars.get(buf.toString());
236
        String prevString = vars.get(buf.toString());
240
        int previous=0;
237
        int previous = 0;
241
        if (prevString!=null){
238
        if (prevString != null) {
242
            try {
239
            try {
243
                previous=Integer.parseInt(prevString);
240
                previous = Integer.parseInt(prevString);
244
            } catch (NumberFormatException e) {
241
            } catch (NumberFormatException e) {
245
                log.warn("Could not parse "+prevString+" "+e);
242
                log.warn("Could not parse " + prevString + " " + e);
246
            }
243
            }
247
        }
244
        }
248
        //Note: match.groups() includes group 0
245
        //Note: match.groups() includes group 0
Lines 252-259 Link Here
252
            vars.put(buf.toString(), match.group(x));
249
            vars.put(buf.toString(), match.group(x));
253
            buf.setLength(pfxlen);
250
            buf.setLength(pfxlen);
254
        }
251
        }
255
        vars.put(buf.toString(), Integer.toString(groups-1));
252
        vars.put(buf.toString(), Integer.toString(groups - 1));
256
        for (int i = groups; i <= previous; i++){
253
        for (int i = groups; i <= previous; i++) {
257
            buf.append(i);
254
            buf.append(i);
258
            vars.remove(buf.toString());// remove the remaining _gn vars
255
            vars.remove(buf.toString());// remove the remaining _gn vars
259
            buf.setLength(pfxlen);
256
            buf.setLength(pfxlen);
Lines 269-281 Link Here
269
        StringBuilder buf = new StringBuilder();
266
        StringBuilder buf = new StringBuilder();
270
        buf.append(basename);
267
        buf.append(basename);
271
        buf.append("_g"); // $NON-NLS-1$
268
        buf.append("_g"); // $NON-NLS-1$
272
        int pfxlen=buf.length();
269
        int pfxlen = buf.length();
273
        // How many groups are there?
270
        // How many groups are there?
274
        int groups;
271
        int groups;
275
        try {
272
        try {
276
            groups=Integer.parseInt(vars.get(buf.toString()));
273
            groups = Integer.parseInt(vars.get(buf.toString()));
277
        } catch (NumberFormatException e) {
274
        } catch (NumberFormatException e) {
278
            groups=0;
275
            groups = 0;
279
        }
276
        }
280
        vars.remove(buf.toString());// Remove the group count
277
        vars.remove(buf.toString());// Remove the group count
281
        for (int i = 0; i <= groups; i++) {
278
        for (int i = 0; i <= groups; i++) {
Lines 321-327 Link Here
321
        int beginOffset = 0;
318
        int beginOffset = 0;
322
        MatchResult currentResult;
319
        MatchResult currentResult;
323
        PatternMatcherInput pinput = new PatternMatcherInput(rawTemplate);
320
        PatternMatcherInput pinput = new PatternMatcherInput(rawTemplate);
324
        while(matcher.contains(pinput, templatePattern)) {
321
        while (matcher.contains(pinput, templatePattern)) {
325
            currentResult = matcher.getMatch();
322
            currentResult = matcher.getMatch();
326
            final int beginMatch = currentResult.beginOffset(0);
323
            final int beginMatch = currentResult.beginOffset(0);
327
            if (beginMatch > beginOffset) { // string is not empty
324
            if (beginMatch > beginOffset) { // string is not empty
Lines 334-343 Link Here
334
        if (beginOffset < rawTemplate.length()) { // trailing string is not empty
331
        if (beginOffset < rawTemplate.length()) { // trailing string is not empty
335
            combined.add(rawTemplate.substring(beginOffset, rawTemplate.length()));
332
            combined.add(rawTemplate.substring(beginOffset, rawTemplate.length()));
336
        }
333
        }
337
        if (log.isDebugEnabled()){
334
        if (log.isDebugEnabled()) {
338
            log.debug("Template item count: "+combined.size());
335
            log.debug("Template item count: " + combined.size());
339
            for(Object o : combined){
336
            for (Object o : combined) {
340
                log.debug(o.getClass().getSimpleName()+" '"+o.toString()+"'");
337
                log.debug(o.getClass().getSimpleName() + " '" + o.toString() + "'");
341
            }
338
            }
342
        }
339
        }
343
        template = combined;
340
        template = combined;
Lines 346-361 Link Here
346
    /**
343
    /**
347
     * Grab the appropriate result from the list.
344
     * Grab the appropriate result from the list.
348
     *
345
     *
349
     * @param matches
346
     * @param matches list of matches
350
     *            list of matches
347
     * @param entry   the entry number in the list
351
     * @param entry
352
     *            the entry number in the list
353
     * @return MatchResult
348
     * @return MatchResult
354
     */
349
     */
355
    private MatchResult getCorrectMatch(List<MatchResult> matches, int entry) {
350
    private MatchResult getCorrectMatch(List<MatchResult> matches, int entry) {
356
        int matchSize = matches.size();
351
        int matchSize = matches.size();
357
352
358
        if (matchSize <= 0 || entry > matchSize){
353
        if (matchSize <= 0 || entry > matchSize) {
359
            return null;
354
            return null;
360
        }
355
        }
361
356
Lines 426-439 Link Here
426
        return getPropertyAsString(TEMPLATE);
421
        return getPropertyAsString(TEMPLATE);
427
    }
422
    }
428
423
429
    public boolean useHeaders() {
424
    public boolean useResponseHeaders() {
430
        return USE_HDRS.equalsIgnoreCase( getPropertyAsString(MATCH_AGAINST));
425
        return USE_RESPONSE_HDRS.equalsIgnoreCase(getPropertyAsString(MATCH_AGAINST));
426
    }
427
428
    public boolean useRequestHeaders() {
429
        return USE_REQUEST_HDRS.equalsIgnoreCase(getPropertyAsString(MATCH_AGAINST));
431
    }
430
    }
432
431
433
    // Allow for property not yet being set (probably only applies to Test cases)
432
    // Allow for property not yet being set (probably only applies to Test cases)
434
    public boolean useBody() {
433
    public boolean useBody() {
435
        String prop = getPropertyAsString(MATCH_AGAINST);
434
        String prop = getPropertyAsString(MATCH_AGAINST);
436
        return prop.length()==0 || USE_BODY.equalsIgnoreCase(prop);// $NON-NLS-1$
435
        return prop.length() == 0 || USE_BODY.equalsIgnoreCase(prop);// $NON-NLS-1$
437
    }
436
    }
438
437
439
    public boolean useUnescapedBody() {
438
    public boolean useUnescapedBody() {
Lines 462-467 Link Here
462
    }
461
    }
463
462
464
    public void setUseField(String actionCommand) {
463
    public void setUseField(String actionCommand) {
465
        setProperty(MATCH_AGAINST,actionCommand);
464
        setProperty(MATCH_AGAINST, actionCommand);
466
    }
465
    }
467
}
466
}
(-)src/components/org/apache/jmeter/extractor/gui/RegexExtractorGui.java (-21 / +20 lines)
Lines 18-35 Link Here
18
18
19
package org.apache.jmeter.extractor.gui;
19
package org.apache.jmeter.extractor.gui;
20
20
21
import java.awt.BorderLayout;
22
import java.awt.GridBagConstraints;
23
import java.awt.GridBagLayout;
24
import java.util.List;
25
26
import javax.swing.BorderFactory;
27
import javax.swing.Box;
28
import javax.swing.ButtonGroup;
29
import javax.swing.JComponent;
30
import javax.swing.JPanel;
31
import javax.swing.JRadioButton;
32
33
import org.apache.jmeter.extractor.RegexExtractor;
21
import org.apache.jmeter.extractor.RegexExtractor;
34
import org.apache.jmeter.processor.gui.AbstractPostProcessorGui;
22
import org.apache.jmeter.processor.gui.AbstractPostProcessorGui;
35
import org.apache.jmeter.testelement.AbstractScopedTestElement;
23
import org.apache.jmeter.testelement.AbstractScopedTestElement;
Lines 37-42 Link Here
37
import org.apache.jmeter.util.JMeterUtils;
25
import org.apache.jmeter.util.JMeterUtils;
38
import org.apache.jorphan.gui.JLabeledTextField;
26
import org.apache.jorphan.gui.JLabeledTextField;
39
27
28
import javax.swing.*;
29
import java.awt.*;
30
import java.util.List;
31
40
/**
32
/**
41
 * Regular Expression Extractor Post-Processor GUI
33
 * Regular Expression Extractor Post-Processor GUI
42
 */
34
 */
Lines 59-66 Link Here
59
51
60
    private JRadioButton useBodyAsDocument;
52
    private JRadioButton useBodyAsDocument;
61
53
62
    private JRadioButton useHeaders;
54
    private JRadioButton useResponseHeaders;
63
55
56
    private JRadioButton useRequestHeaders;
57
64
    private JRadioButton useURL;
58
    private JRadioButton useURL;
65
59
66
    private JRadioButton useCode;
60
    private JRadioButton useCode;
Lines 82-91 Link Here
82
    @Override
76
    @Override
83
    public void configure(TestElement el) {
77
    public void configure(TestElement el) {
84
        super.configure(el);
78
        super.configure(el);
85
        if (el instanceof RegexExtractor){
79
        if (el instanceof RegexExtractor) {
86
            RegexExtractor re = (RegexExtractor) el;
80
            RegexExtractor re = (RegexExtractor) el;
87
            showScopeSettings(re, true);
81
            showScopeSettings(re, true);
88
            useHeaders.setSelected(re.useHeaders());
82
            useResponseHeaders.setSelected(re.useResponseHeaders());
83
            useRequestHeaders.setSelected(re.useRequestHeaders());
89
            useBody.setSelected(re.useBody());
84
            useBody.setSelected(re.useBody());
90
            useUnescapedBody.setSelected(re.useUnescapedBody());
85
            useUnescapedBody.setSelected(re.useUnescapedBody());
91
            useBodyAsDocument.setSelected(re.useBodyAsDocument());
86
            useBodyAsDocument.setSelected(re.useBodyAsDocument());
Lines 165-171 Link Here
165
        useBody = new JRadioButton(JMeterUtils.getResString("regex_src_body")); //$NON-NLS-1$
160
        useBody = new JRadioButton(JMeterUtils.getResString("regex_src_body")); //$NON-NLS-1$
166
        useUnescapedBody = new JRadioButton(JMeterUtils.getResString("regex_src_body_unescaped")); //$NON-NLS-1$
161
        useUnescapedBody = new JRadioButton(JMeterUtils.getResString("regex_src_body_unescaped")); //$NON-NLS-1$
167
        useBodyAsDocument = new JRadioButton(JMeterUtils.getResString("regex_src_body_as_document")); //$NON-NLS-1$
162
        useBodyAsDocument = new JRadioButton(JMeterUtils.getResString("regex_src_body_as_document")); //$NON-NLS-1$
168
        useHeaders = new JRadioButton(JMeterUtils.getResString("regex_src_hdrs")); //$NON-NLS-1$
163
        useResponseHeaders = new JRadioButton(JMeterUtils.getResString("regex_src_hdrs_resp")); //$NON-NLS-1$
164
        useRequestHeaders = new JRadioButton(JMeterUtils.getResString("regex_src_hdrs_req")); //$NON-NLS-1$
169
        useURL = new JRadioButton(JMeterUtils.getResString("regex_src_url")); //$NON-NLS-1$
165
        useURL = new JRadioButton(JMeterUtils.getResString("regex_src_url")); //$NON-NLS-1$
170
        useCode = new JRadioButton(JMeterUtils.getResString("assertion_code_resp")); //$NON-NLS-1$
166
        useCode = new JRadioButton(JMeterUtils.getResString("assertion_code_resp")); //$NON-NLS-1$
171
        useMessage = new JRadioButton(JMeterUtils.getResString("assertion_message_resp")); //$NON-NLS-1$
167
        useMessage = new JRadioButton(JMeterUtils.getResString("assertion_message_resp")); //$NON-NLS-1$
Lines 174-180 Link Here
174
        group.add(useBody);
170
        group.add(useBody);
175
        group.add(useUnescapedBody);
171
        group.add(useUnescapedBody);
176
        group.add(useBodyAsDocument);
172
        group.add(useBodyAsDocument);
177
        group.add(useHeaders);
173
        group.add(useResponseHeaders);
174
        group.add(useRequestHeaders);
178
        group.add(useURL);
175
        group.add(useURL);
179
        group.add(useCode);
176
        group.add(useCode);
180
        group.add(useMessage);
177
        group.add(useMessage);
Lines 182-188 Link Here
182
        panel.add(useBody);
179
        panel.add(useBody);
183
        panel.add(useUnescapedBody);
180
        panel.add(useUnescapedBody);
184
        panel.add(useBodyAsDocument);
181
        panel.add(useBodyAsDocument);
185
        panel.add(useHeaders);
182
        panel.add(useResponseHeaders);
183
        panel.add(useRequestHeaders);
186
        panel.add(useURL);
184
        panel.add(useURL);
187
        panel.add(useCode);
185
        panel.add(useCode);
188
        panel.add(useMessage);
186
        panel.add(useMessage);
Lines 193-199 Link Here
193
        useBody.setActionCommand(RegexExtractor.USE_BODY);
191
        useBody.setActionCommand(RegexExtractor.USE_BODY);
194
        useUnescapedBody.setActionCommand(RegexExtractor.USE_BODY_UNESCAPED);
192
        useUnescapedBody.setActionCommand(RegexExtractor.USE_BODY_UNESCAPED);
195
        useBodyAsDocument.setActionCommand(RegexExtractor.USE_BODY_AS_DOCUMENT);
193
        useBodyAsDocument.setActionCommand(RegexExtractor.USE_BODY_AS_DOCUMENT);
196
        useHeaders.setActionCommand(RegexExtractor.USE_HDRS);
194
        useResponseHeaders.setActionCommand(RegexExtractor.USE_RESPONSE_HDRS);
195
        useRequestHeaders.setActionCommand(RegexExtractor.USE_REQUEST_HDRS);
197
        useURL.setActionCommand(RegexExtractor.USE_URL);
196
        useURL.setActionCommand(RegexExtractor.USE_URL);
198
        useCode.setActionCommand(RegexExtractor.USE_CODE);
197
        useCode.setActionCommand(RegexExtractor.USE_CODE);
199
        useMessage.setActionCommand(RegexExtractor.USE_MESSAGE);
198
        useMessage.setActionCommand(RegexExtractor.USE_MESSAGE);
Lines 229-235 Link Here
229
        panel.add(item.get(0), gbc.clone());
228
        panel.add(item.get(0), gbc.clone());
230
        gbc.gridx++;
229
        gbc.gridx++;
231
        gbc.weightx = 1;
230
        gbc.weightx = 1;
232
        gbc.fill=GridBagConstraints.HORIZONTAL;
231
        gbc.fill = GridBagConstraints.HORIZONTAL;
233
        panel.add(item.get(1), gbc.clone());
232
        panel.add(item.get(1), gbc.clone());
234
    }
233
    }
235
234
Lines 238-244 Link Here
238
        gbc.gridx = 0;
237
        gbc.gridx = 0;
239
        gbc.gridy++;
238
        gbc.gridy++;
240
        gbc.weightx = 0;
239
        gbc.weightx = 0;
241
        gbc.fill=GridBagConstraints.NONE;
240
        gbc.fill = GridBagConstraints.NONE;
242
    }
241
    }
243
242
244
    private void initConstraints(GridBagConstraints gbc) {
243
    private void initConstraints(GridBagConstraints gbc) {
(-)test/src/org/apache/jmeter/extractor/TestRegexExtractor.java (-265 / +264 lines)
Lines 19-324 Link Here
19
package org.apache.jmeter.extractor;
19
package org.apache.jmeter.extractor;
20
20
21
21
22
import java.net.URL;
23
24
import junit.framework.TestCase;
22
import junit.framework.TestCase;
25
26
import org.apache.jmeter.samplers.SampleResult;
23
import org.apache.jmeter.samplers.SampleResult;
27
import org.apache.jmeter.threads.JMeterContext;
24
import org.apache.jmeter.threads.JMeterContext;
28
import org.apache.jmeter.threads.JMeterContextService;
25
import org.apache.jmeter.threads.JMeterContextService;
29
import org.apache.jmeter.threads.JMeterVariables;
26
import org.apache.jmeter.threads.JMeterVariables;
30
27
28
import java.net.URL;
29
31
public class TestRegexExtractor extends TestCase {
30
public class TestRegexExtractor extends TestCase {
32
        private RegexExtractor extractor;
31
    private RegexExtractor extractor;
33
32
34
        private SampleResult result;
33
    private SampleResult result;
35
34
36
        private JMeterVariables vars;
35
    private JMeterVariables vars;
37
36
38
        public TestRegexExtractor(String name) {
37
    public TestRegexExtractor(String name) {
39
            super(name);
38
        super(name);
40
        }
39
    }
41
40
42
        private JMeterContext jmctx;
41
    private JMeterContext jmctx;
43
42
44
        @Override
43
    @Override
45
        public void setUp() {
44
    public void setUp() {
46
            jmctx = JMeterContextService.getContext();
45
        jmctx = JMeterContextService.getContext();
47
            extractor = new RegexExtractor();
46
        extractor = new RegexExtractor();
48
            extractor.setThreadContext(jmctx);// This would be done by the run
47
        extractor.setThreadContext(jmctx);// This would be done by the run
49
                                                // command
48
        // command
50
            extractor.setRefName("regVal");
49
        extractor.setRefName("regVal");
51
            result = new SampleResult();
50
        result = new SampleResult();
52
            String data = "<company-xmlext-query-ret>" + "<row>" + "<value field=\"RetCode\">LIS_OK</value>"
51
        String data = "<company-xmlext-query-ret>" + "<row>" + "<value field=\"RetCode\">LIS_OK</value>"
53
                    + "<value field=\"RetCodeExtension\"></value>" + "<value field=\"alias\"></value>"
52
                + "<value field=\"RetCodeExtension\"></value>" + "<value field=\"alias\"></value>"
54
                    + "<value field=\"positioncount\"></value>" + "<value field=\"invalidpincount\">0</value>"
53
                + "<value field=\"positioncount\"></value>" + "<value field=\"invalidpincount\">0</value>"
55
                    + "<value field=\"pinposition1\">1</value>" + "<value field=\"pinpositionvalue1\"></value>"
54
                + "<value field=\"pinposition1\">1</value>" + "<value field=\"pinpositionvalue1\"></value>"
56
                    + "<value field=\"pinposition2\">5</value>" + "<value field=\"pinpositionvalue2\"></value>"
55
                + "<value field=\"pinposition2\">5</value>" + "<value field=\"pinpositionvalue2\"></value>"
57
                    + "<value field=\"pinposition3\">6</value>" + "<value field=\"pinpositionvalue3\"></value>"
56
                + "<value field=\"pinposition3\">6</value>" + "<value field=\"pinpositionvalue3\"></value>"
58
                    + "</row>" + "</company-xmlext-query-ret>";
57
                + "</row>" + "</company-xmlext-query-ret>";
59
            result.setResponseData(data, null);
58
        result.setResponseData(data, null);
60
            result.setResponseHeaders("Header1: Value1\nHeader2: Value2");
59
        result.setResponseHeaders("Header1: Value1\nHeader2: Value2");
61
            result.setResponseCode("abcd");
60
        result.setResponseCode("abcd");
62
            result.setResponseMessage("The quick brown fox");
61
        result.setResponseMessage("The quick brown fox");
63
            vars = new JMeterVariables();
62
        vars = new JMeterVariables();
64
            jmctx.setVariables(vars);
63
        jmctx.setVariables(vars);
65
            jmctx.setPreviousResult(result);
64
        jmctx.setPreviousResult(result);
66
        }
65
    }
67
66
68
        public void testVariableExtraction0() throws Exception {
67
    public void testVariableExtraction0() throws Exception {
69
            extractor.setRegex("<(value) field=\"");
68
        extractor.setRegex("<(value) field=\"");
70
            extractor.setTemplate("$1$");
69
        extractor.setTemplate("$1$");
71
            extractor.setMatchNumber(0);
70
        extractor.setMatchNumber(0);
72
            extractor.process();
71
        extractor.process();
73
            assertEquals("value", vars.get("regVal"));
72
        assertEquals("value", vars.get("regVal"));
74
        }
73
    }
75
74
76
        public void testVariableExtraction() throws Exception {
75
    public void testVariableExtraction() throws Exception {
77
            extractor.setRegex("<value field=\"(pinposition\\d+)\">(\\d+)</value>");
76
        extractor.setRegex("<value field=\"(pinposition\\d+)\">(\\d+)</value>");
78
            extractor.setTemplate("$2$");
77
        extractor.setTemplate("$2$");
79
            extractor.setMatchNumber(2);
78
        extractor.setMatchNumber(2);
80
            extractor.process();
79
        extractor.process();
81
            assertEquals("5", vars.get("regVal"));
80
        assertEquals("5", vars.get("regVal"));
82
            assertEquals("pinposition2", vars.get("regVal_g1"));
81
        assertEquals("pinposition2", vars.get("regVal_g1"));
83
            assertEquals("5", vars.get("regVal_g2"));
82
        assertEquals("5", vars.get("regVal_g2"));
84
            assertEquals("<value field=\"pinposition2\">5</value>", vars.get("regVal_g0"));
83
        assertEquals("<value field=\"pinposition2\">5</value>", vars.get("regVal_g0"));
85
            assertNull(vars.get("regVal_g3"));
84
        assertNull(vars.get("regVal_g3"));
86
            assertEquals("2",vars.get("regVal_g"));
85
        assertEquals("2", vars.get("regVal_g"));
87
        }
86
    }
88
87
89
        private static void templateSetup(RegexExtractor rex, String tmp) {
88
    private static void templateSetup(RegexExtractor rex, String tmp) {
90
            rex.setRegex("<company-(\\w+?)-(\\w+?)-(\\w+?)>");
89
        rex.setRegex("<company-(\\w+?)-(\\w+?)-(\\w+?)>");
91
            rex.setMatchNumber(1);
90
        rex.setMatchNumber(1);
92
            rex.setTemplate(tmp);
91
        rex.setTemplate(tmp);
93
            rex.process();
92
        rex.process();
94
        }
93
    }
95
94
96
        public void testTemplate1() throws Exception {
95
    public void testTemplate1() throws Exception {
97
            templateSetup(extractor, "");
96
        templateSetup(extractor, "");
98
            assertEquals("<company-xmlext-query-ret>", vars.get("regVal_g0"));
97
        assertEquals("<company-xmlext-query-ret>", vars.get("regVal_g0"));
99
            assertEquals("xmlext", vars.get("regVal_g1"));
98
        assertEquals("xmlext", vars.get("regVal_g1"));
100
            assertEquals("query", vars.get("regVal_g2"));
99
        assertEquals("query", vars.get("regVal_g2"));
101
            assertEquals("ret", vars.get("regVal_g3"));
100
        assertEquals("ret", vars.get("regVal_g3"));
102
            assertEquals("", vars.get("regVal"));
101
        assertEquals("", vars.get("regVal"));
103
            assertEquals("3",vars.get("regVal_g"));
102
        assertEquals("3", vars.get("regVal_g"));
104
        }
103
    }
105
104
106
        public void testTemplate2() throws Exception {
105
    public void testTemplate2() throws Exception {
107
            templateSetup(extractor, "ABC");
106
        templateSetup(extractor, "ABC");
108
            assertEquals("ABC", vars.get("regVal"));
107
        assertEquals("ABC", vars.get("regVal"));
109
        }
108
    }
110
109
111
        public void testTemplate3() throws Exception {
110
    public void testTemplate3() throws Exception {
112
            templateSetup(extractor, "$2$");
111
        templateSetup(extractor, "$2$");
113
            assertEquals("query", vars.get("regVal"));
112
        assertEquals("query", vars.get("regVal"));
114
        }
113
    }
115
114
116
        public void testTemplate4() throws Exception {
115
    public void testTemplate4() throws Exception {
117
            templateSetup(extractor, "PRE$2$");
116
        templateSetup(extractor, "PRE$2$");
118
            assertEquals("PREquery", vars.get("regVal"));
117
        assertEquals("PREquery", vars.get("regVal"));
119
        }
118
    }
120
119
121
        public void testTemplate5() throws Exception {
120
    public void testTemplate5() throws Exception {
122
            templateSetup(extractor, "$2$POST");
121
        templateSetup(extractor, "$2$POST");
123
            assertEquals("queryPOST", vars.get("regVal"));
122
        assertEquals("queryPOST", vars.get("regVal"));
124
        }
123
    }
125
124
126
        public void testTemplate6() throws Exception {
125
    public void testTemplate6() throws Exception {
127
            templateSetup(extractor, "$2$$1$");
126
        templateSetup(extractor, "$2$$1$");
128
            assertEquals("queryxmlext", vars.get("regVal"));
127
        assertEquals("queryxmlext", vars.get("regVal"));
129
        }
128
    }
130
129
131
        public void testTemplate7() throws Exception {
130
    public void testTemplate7() throws Exception {
132
            templateSetup(extractor, "$2$MID$1$");
131
        templateSetup(extractor, "$2$MID$1$");
133
            assertEquals("queryMIDxmlext", vars.get("regVal"));
132
        assertEquals("queryMIDxmlext", vars.get("regVal"));
134
        }
133
    }
135
134
136
        public void testVariableExtraction2() throws Exception {
135
    public void testVariableExtraction2() throws Exception {
137
            extractor.setRegex("<value field=\"(pinposition\\d+)\">(\\d+)</value>");
136
        extractor.setRegex("<value field=\"(pinposition\\d+)\">(\\d+)</value>");
138
            extractor.setTemplate("$1$");
137
        extractor.setTemplate("$1$");
139
            extractor.setMatchNumber(3);
138
        extractor.setMatchNumber(3);
140
            extractor.process();
139
        extractor.process();
141
            assertEquals("pinposition3", vars.get("regVal"));
140
        assertEquals("pinposition3", vars.get("regVal"));
142
        }
141
    }
143
142
144
        public void testVariableExtraction6() throws Exception {
143
    public void testVariableExtraction6() throws Exception {
145
            extractor.setRegex("<value field=\"(pinposition\\d+)\">(\\d+)</value>");
144
        extractor.setRegex("<value field=\"(pinposition\\d+)\">(\\d+)</value>");
146
            extractor.setTemplate("$2$");
145
        extractor.setTemplate("$2$");
147
            extractor.setMatchNumber(4);
146
        extractor.setMatchNumber(4);
148
            extractor.setDefaultValue("default");
147
        extractor.setDefaultValue("default");
149
            extractor.process();
148
        extractor.process();
150
            assertEquals("default", vars.get("regVal"));
149
        assertEquals("default", vars.get("regVal"));
151
        }
150
    }
152
151
153
        public void testVariableExtraction3() throws Exception {
152
    public void testVariableExtraction3() throws Exception {
154
            extractor.setRegex("<value field=\"(pinposition\\d+)\">(\\d+)</value>");
153
        extractor.setRegex("<value field=\"(pinposition\\d+)\">(\\d+)</value>");
155
            extractor.setTemplate("_$1$");
154
        extractor.setTemplate("_$1$");
156
            extractor.setMatchNumber(2);
155
        extractor.setMatchNumber(2);
157
            extractor.process();
156
        extractor.process();
158
            assertEquals("_pinposition2", vars.get("regVal"));
157
        assertEquals("_pinposition2", vars.get("regVal"));
159
        }
158
    }
160
159
161
        public void testVariableExtraction5() throws Exception {
160
    public void testVariableExtraction5() throws Exception {
162
            extractor.setRegex("<value field=\"(pinposition\\d+)\">(\\d+)</value>");
161
        extractor.setRegex("<value field=\"(pinposition\\d+)\">(\\d+)</value>");
163
            extractor.setTemplate("$1$");
162
        extractor.setTemplate("$1$");
164
            extractor.setMatchNumber(1);// Set up the non-wild variables
163
        extractor.setMatchNumber(1);// Set up the non-wild variables
165
            extractor.process();
164
        extractor.process();
166
            assertNotNull(vars.get("regVal"));
165
        assertNotNull(vars.get("regVal"));
167
            assertEquals("2",vars.get("regVal_g"));
166
        assertEquals("2", vars.get("regVal_g"));
168
            assertNotNull(vars.get("regVal_g0"));
167
        assertNotNull(vars.get("regVal_g0"));
169
            assertNotNull(vars.get("regVal_g1"));
168
        assertNotNull(vars.get("regVal_g1"));
170
            assertNotNull(vars.get("regVal_g2"));
169
        assertNotNull(vars.get("regVal_g2"));
171
170
172
            extractor.setMatchNumber(-1);
171
        extractor.setMatchNumber(-1);
173
            extractor.process();
172
        extractor.process();
174
            assertNotNull(vars.get("regVal"));// Should not clear this?
173
        assertNotNull(vars.get("regVal"));// Should not clear this?
175
            assertNull(vars.get("regVal_g"));
174
        assertNull(vars.get("regVal_g"));
176
            assertNull(vars.get("regVal_g1"));
175
        assertNull(vars.get("regVal_g1"));
177
            assertNull(vars.get("regVal_g2"));
176
        assertNull(vars.get("regVal_g2"));
178
            assertEquals("3", vars.get("regVal_matchNr"));
177
        assertEquals("3", vars.get("regVal_matchNr"));
179
            assertEquals("pinposition1", vars.get("regVal_1"));
178
        assertEquals("pinposition1", vars.get("regVal_1"));
180
            assertEquals("pinposition2", vars.get("regVal_2"));
179
        assertEquals("pinposition2", vars.get("regVal_2"));
181
            assertEquals("pinposition3", vars.get("regVal_3"));
180
        assertEquals("pinposition3", vars.get("regVal_3"));
182
            assertEquals("2", vars.get("regVal_1_g"));
181
        assertEquals("2", vars.get("regVal_1_g"));
183
            assertEquals("pinposition1", vars.get("regVal_1_g1"));
182
        assertEquals("pinposition1", vars.get("regVal_1_g1"));
184
            assertEquals("1", vars.get("regVal_1_g2"));
183
        assertEquals("1", vars.get("regVal_1_g2"));
185
            assertEquals("6", vars.get("regVal_3_g2"));
184
        assertEquals("6", vars.get("regVal_3_g2"));
186
            assertEquals("<value field=\"pinposition1\">1</value>", vars.get("regVal_1_g0"));
185
        assertEquals("<value field=\"pinposition1\">1</value>", vars.get("regVal_1_g0"));
187
            assertNull(vars.get("regVal_4"));
186
        assertNull(vars.get("regVal_4"));
188
187
189
            // Check old values don't hang around:
188
        // Check old values don't hang around:
190
            extractor.setRegex("(\\w+)count"); // fewer matches
189
        extractor.setRegex("(\\w+)count"); // fewer matches
191
            extractor.process();
190
        extractor.process();
192
            assertEquals("2", vars.get("regVal_matchNr"));
191
        assertEquals("2", vars.get("regVal_matchNr"));
193
            assertEquals("position", vars.get("regVal_1"));
192
        assertEquals("position", vars.get("regVal_1"));
194
            assertEquals("1", vars.get("regVal_1_g"));
193
        assertEquals("1", vars.get("regVal_1_g"));
195
            assertEquals("position", vars.get("regVal_1_g1"));
194
        assertEquals("position", vars.get("regVal_1_g1"));
196
            assertNull("Unused variables should be null", vars.get("regVal_1_g2"));
195
        assertNull("Unused variables should be null", vars.get("regVal_1_g2"));
197
            assertEquals("invalidpin", vars.get("regVal_2"));
196
        assertEquals("invalidpin", vars.get("regVal_2"));
198
            assertEquals("1", vars.get("regVal_2_g"));
197
        assertEquals("1", vars.get("regVal_2_g"));
199
            assertEquals("invalidpin", vars.get("regVal_2_g1"));
198
        assertEquals("invalidpin", vars.get("regVal_2_g1"));
200
            assertNull("Unused variables should be null", vars.get("regVal_2_g2"));
199
        assertNull("Unused variables should be null", vars.get("regVal_2_g2"));
201
            assertEquals("1", vars.get("regVal_1_g"));
200
        assertEquals("1", vars.get("regVal_1_g"));
202
            assertNull("Unused variables should be null", vars.get("regVal_3"));
201
        assertNull("Unused variables should be null", vars.get("regVal_3"));
203
            assertNull("Unused variables should be null", vars.get("regVal_3_g"));
202
        assertNull("Unused variables should be null", vars.get("regVal_3_g"));
204
            assertNull("Unused variables should be null", vars.get("regVal_3_g0"));
203
        assertNull("Unused variables should be null", vars.get("regVal_3_g0"));
205
            assertNull("Unused variables should be null", vars.get("regVal_3_g1"));
204
        assertNull("Unused variables should be null", vars.get("regVal_3_g1"));
206
            assertNull("Unused variables should be null", vars.get("regVal_3_g2"));
205
        assertNull("Unused variables should be null", vars.get("regVal_3_g2"));
207
206
208
            // Check when match fails
207
        // Check when match fails
209
            extractor.setRegex("xxxx(.)(.)");
208
        extractor.setRegex("xxxx(.)(.)");
210
            extractor.process();
209
        extractor.process();
211
            assertEquals("0", vars.get("regVal_matchNr"));
210
        assertEquals("0", vars.get("regVal_matchNr"));
212
            assertNull("Unused variables should be null", vars.get("regVal_1"));
211
        assertNull("Unused variables should be null", vars.get("regVal_1"));
213
            assertNull("Unused variables should be null", vars.get("regVal_1_g0"));
212
        assertNull("Unused variables should be null", vars.get("regVal_1_g0"));
214
            assertNull("Unused variables should be null", vars.get("regVal_1_g1"));
213
        assertNull("Unused variables should be null", vars.get("regVal_1_g1"));
215
            assertNull("Unused variables should be null", vars.get("regVal_1_g2"));
214
        assertNull("Unused variables should be null", vars.get("regVal_1_g2"));
216
        }
215
    }
217
216
218
        public void testVariableExtraction7() throws Exception {
217
    public void testVariableExtraction7() throws Exception {
219
            extractor.setRegex("Header1: (\\S+)");
218
        extractor.setRegex("Header1: (\\S+)");
220
            extractor.setTemplate("$1$");
219
        extractor.setTemplate("$1$");
221
            extractor.setMatchNumber(1);
220
        extractor.setMatchNumber(1);
222
            assertTrue("useBody should be true", extractor.useBody());
221
        assertTrue("useBody should be true", extractor.useBody());
223
            assertFalse("useHdrs should be false", extractor.useHeaders());
222
        assertFalse("useHdrs should be false", extractor.useResponseHeaders());
224
            assertFalse("useURL should be false", extractor.useUrl());
223
        assertFalse("useURL should be false", extractor.useUrl());
225
            extractor.setUseField(RegexExtractor.USE_BODY);
224
        extractor.setUseField(RegexExtractor.USE_BODY);
226
            assertTrue("useBody should be true", extractor.useBody());
225
        assertTrue("useBody should be true", extractor.useBody());
227
            assertFalse("useHdrs should be false", extractor.useHeaders());
226
        assertFalse("useHdrs should be false", extractor.useResponseHeaders());
228
            assertFalse("useURL should be false", extractor.useUrl());
227
        assertFalse("useURL should be false", extractor.useUrl());
229
            extractor.setUseField(RegexExtractor.USE_HDRS);
228
        extractor.setUseField(RegexExtractor.USE_RESPONSE_HDRS);
230
            assertTrue("useHdrs should be true", extractor.useHeaders());
229
        assertTrue("useHdrs should be true", extractor.useResponseHeaders());
231
            assertFalse("useBody should be false", extractor.useBody());
230
        assertFalse("useBody should be false", extractor.useBody());
232
            assertFalse("useURL should be false", extractor.useUrl());
231
        assertFalse("useURL should be false", extractor.useUrl());
233
            extractor.process();
232
        extractor.process();
234
            assertEquals("Value1", vars.get("regVal"));
233
        assertEquals("Value1", vars.get("regVal"));
235
            extractor.setUseField(RegexExtractor.USE_URL);
234
        extractor.setUseField(RegexExtractor.USE_URL);
236
            assertFalse("useHdrs should be false", extractor.useHeaders());
235
        assertFalse("useHdrs should be false", extractor.useResponseHeaders());
237
            assertFalse("useBody should be false", extractor.useBody());
236
        assertFalse("useBody should be false", extractor.useBody());
238
            assertTrue("useURL should be true", extractor.useUrl());
237
        assertTrue("useURL should be true", extractor.useUrl());
239
        }
238
    }
240
239
241
        public void testVariableExtraction8() throws Exception {
240
    public void testVariableExtraction8() throws Exception {
242
            extractor.setRegex("http://jakarta\\.apache\\.org/(\\w+)");
241
        extractor.setRegex("http://jakarta\\.apache\\.org/(\\w+)");
243
            extractor.setTemplate("$1$");
242
        extractor.setTemplate("$1$");
244
            extractor.setMatchNumber(1);
243
        extractor.setMatchNumber(1);
245
            extractor.setUseField(RegexExtractor.USE_URL);
244
        extractor.setUseField(RegexExtractor.USE_URL);
246
            assertFalse("useHdrs should be false", extractor.useHeaders());
245
        assertFalse("useHdrs should be false", extractor.useResponseHeaders());
247
            assertFalse("useBody should be false", extractor.useBody());
246
        assertFalse("useBody should be false", extractor.useBody());
248
            assertTrue("useURL should be true", extractor.useUrl());
247
        assertTrue("useURL should be true", extractor.useUrl());
249
            extractor.process();
248
        extractor.process();
250
            assertNull(vars.get("regVal"));
249
        assertNull(vars.get("regVal"));
251
            result.setURL(new URL("http://jakarta.apache.org/index.html?abcd"));
250
        result.setURL(new URL("http://jakarta.apache.org/index.html?abcd"));
252
            extractor.process();
251
        extractor.process();
253
            assertEquals("index",vars.get("regVal"));
252
        assertEquals("index", vars.get("regVal"));
254
        }
253
    }
255
254
256
        public void testVariableExtraction9() throws Exception {
255
    public void testVariableExtraction9() throws Exception {
257
            extractor.setRegex("(\\w+)");
256
        extractor.setRegex("(\\w+)");
258
            extractor.setTemplate("$1$");
257
        extractor.setTemplate("$1$");
259
            extractor.setMatchNumber(1);
258
        extractor.setMatchNumber(1);
260
            extractor.setUseField(RegexExtractor.USE_CODE);
259
        extractor.setUseField(RegexExtractor.USE_CODE);
261
            assertFalse("useHdrs should be false", extractor.useHeaders());
260
        assertFalse("useHdrs should be false", extractor.useResponseHeaders());
262
            assertFalse("useBody should be false", extractor.useBody());
261
        assertFalse("useBody should be false", extractor.useBody());
263
            assertFalse("useURL should be false", extractor.useUrl());
262
        assertFalse("useURL should be false", extractor.useUrl());
264
            assertFalse("useMessage should be false", extractor.useMessage());
263
        assertFalse("useMessage should be false", extractor.useMessage());
265
            assertTrue("useCode should be true", extractor.useCode());
264
        assertTrue("useCode should be true", extractor.useCode());
266
            extractor.process();
265
        extractor.process();
267
            assertEquals("abcd",vars.get("regVal"));
266
        assertEquals("abcd", vars.get("regVal"));
268
            extractor.setUseField(RegexExtractor.USE_MESSAGE);
267
        extractor.setUseField(RegexExtractor.USE_MESSAGE);
269
            assertFalse("useHdrs should be false", extractor.useHeaders());
268
        assertFalse("useHdrs should be false", extractor.useResponseHeaders());
270
            assertFalse("useBody should be false", extractor.useBody());
269
        assertFalse("useBody should be false", extractor.useBody());
271
            assertFalse("useURL should be false", extractor.useUrl());
270
        assertFalse("useURL should be false", extractor.useUrl());
272
            assertTrue("useMessage should be true", extractor.useMessage());
271
        assertTrue("useMessage should be true", extractor.useMessage());
273
            assertFalse("useCode should be falsee", extractor.useCode());
272
        assertFalse("useCode should be falsee", extractor.useCode());
274
            extractor.setMatchNumber(3);
273
        extractor.setMatchNumber(3);
275
            extractor.process();
274
        extractor.process();
276
            assertEquals("brown",vars.get("regVal"));
275
        assertEquals("brown", vars.get("regVal"));
277
        }
276
    }
278
277
279
        public void testNoDefault() throws Exception {
278
    public void testNoDefault() throws Exception {
280
            extractor.setRegex("<value field=\"(pinposition\\d+)\">(\\d+)</value>");
279
        extractor.setRegex("<value field=\"(pinposition\\d+)\">(\\d+)</value>");
281
            extractor.setTemplate("$2$");
280
        extractor.setTemplate("$2$");
282
            extractor.setMatchNumber(4);
281
        extractor.setMatchNumber(4);
283
            //extractor.setDefaultValue("default");
282
        //extractor.setDefaultValue("default");
284
            vars.put("regVal", "initial");
283
        vars.put("regVal", "initial");
285
            assertEquals("initial", vars.get("regVal"));
284
        assertEquals("initial", vars.get("regVal"));
286
            extractor.process();
285
        extractor.process();
287
            assertEquals("initial", vars.get("regVal"));
286
        assertEquals("initial", vars.get("regVal"));
288
        }
287
    }
289
288
290
        public void testDefault() throws Exception {
289
    public void testDefault() throws Exception {
291
            extractor.setRegex("<value field=\"(pinposition\\d+)\">(\\d+)</value>");
290
        extractor.setRegex("<value field=\"(pinposition\\d+)\">(\\d+)</value>");
292
            extractor.setTemplate("$2$");
291
        extractor.setTemplate("$2$");
293
            extractor.setMatchNumber(999);
292
        extractor.setMatchNumber(999);
294
            extractor.setDefaultValue("default");
293
        extractor.setDefaultValue("default");
295
            vars.put("regVal", "initial");
294
        vars.put("regVal", "initial");
296
            assertEquals("initial", vars.get("regVal"));
295
        assertEquals("initial", vars.get("regVal"));
297
            extractor.process();
296
        extractor.process();
298
            assertEquals("default", vars.get("regVal"));
297
        assertEquals("default", vars.get("regVal"));
299
            assertNull(vars.get("regVal_g0"));
298
        assertNull(vars.get("regVal_g0"));
300
            assertNull(vars.get("regVal_g1"));
299
        assertNull(vars.get("regVal_g1"));
301
        }
300
    }
302
301
303
        public void testStaleVariables() throws Exception {
302
    public void testStaleVariables() throws Exception {
304
            extractor.setRegex("<value field=\"(pinposition\\d+)\">(\\d+)</value>");
303
        extractor.setRegex("<value field=\"(pinposition\\d+)\">(\\d+)</value>");
305
            extractor.setTemplate("$2$");
304
        extractor.setTemplate("$2$");
306
            extractor.setMatchNumber(1);
305
        extractor.setMatchNumber(1);
307
            extractor.setDefaultValue("default");
306
        extractor.setDefaultValue("default");
308
            extractor.process();
307
        extractor.process();
309
            assertEquals("1", vars.get("regVal"));
308
        assertEquals("1", vars.get("regVal"));
310
            assertEquals("1", vars.get("regVal_g2"));
309
        assertEquals("1", vars.get("regVal_g2"));
311
            assertEquals("2", vars.get("regVal_g"));
310
        assertEquals("2", vars.get("regVal_g"));
312
            assertNotNull(vars.get("regVal_g0"));
311
        assertNotNull(vars.get("regVal_g0"));
313
            assertNotNull(vars.get("regVal_g1"));
312
        assertNotNull(vars.get("regVal_g1"));
314
            // Now rerun with match fail
313
        // Now rerun with match fail
315
            extractor.setMatchNumber(10);
314
        extractor.setMatchNumber(10);
316
            extractor.process();
315
        extractor.process();
317
            assertEquals("default", vars.get("regVal"));
316
        assertEquals("default", vars.get("regVal"));
318
            assertNull(vars.get("regVal_g0"));
317
        assertNull(vars.get("regVal_g0"));
319
            assertNull(vars.get("regVal_g1"));
318
        assertNull(vars.get("regVal_g1"));
320
            assertNull(vars.get("regVal_g"));
319
        assertNull(vars.get("regVal_g"));
321
        }
320
    }
322
321
323
    public void testScope1() throws Exception {
322
    public void testScope1() throws Exception {
324
        result.setResponseData("<title>ONE</title>", "ISO-8859-1");
323
        result.setResponseData("<title>ONE</title>", "ISO-8859-1");

Return to bug 52061