简体   繁体   中英

JAVA in complaining about regular expression pattern in JMeter BeanShell sampler

Could anyone please help me resolve a regexp issue?

So, I am checking an SSH console output via BeanShell assertion in JMeter. The output looks like this:

field1: value1
field2: value2
...
fieldN: valueN

I use variables to define the expected values, like:

fieldNames = "field1|field2|field3";
fieldValues = "value1,value2,value3";

BeanShell assertion code is like this:

import java.util.regex.*;

Failure = false;
FailureMessage = "failed";

String[] fieldNames = {};
String[] fieldValues = {};
String resp = SampleResult.getResponseDataAsString();

fieldNames = "${lFieldNames}".split("|");
fieldValues = "${lExpectedValues}".split(",");
int fieldNumber = -1;

for (int i = 0; i < fieldNames.length; i++)
{
    Pattern pattern = Pattern.compile(fieldNames[i] + "\\W\\s(\\d+|\\w+)");
    Matcher matcher = pattern.matcher(resp);
}

Each time I am getting this response:

Assertion error: true
Assertion failure: false
Assertion failure message: org.apache.jorphan.util.JMeterException: Error invoking bsh method: eval Sourced file: inline evaluation of: ``import java.util.regex.*;  Failure = false; FailureMessage = "failed";  String[] . . . '' Token Parsing Error: Lexical error at line 16, column 62.  Encountered: "W" (87), after : "\"(\\"

I checked this regular expression in Perl and also in Linux console using egrep - it works fine. So I cannot really get what does Java dislike here.

I would be very thankful for any help around this issue.

I think something is wrong with the variable fieldNames[i] .

Try this code instead for viewing the built pattern.

for (int i = 0; i < fieldNames.length; i++)
{
    String tmp = fieldNames[i] + "\\W\\s(\\d+|\\w+)";
    System.out.println(">> " +tmp);
    Pattern pattern = Pattern.compile(tmp);
    Matcher matcher = pattern.matcher(resp);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM