Skip to content

Commit

Permalink
refactor: factor out getEnabledArguments to avoid "isEnabled" check a…
Browse files Browse the repository at this point in the history
…ll over the place
  • Loading branch information
vlsi committed Jan 14, 2025
1 parent 4ea8f7e commit 88567af
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 54 deletions.
49 changes: 48 additions & 1 deletion src/core/src/main/java/org/apache/jmeter/config/Arguments.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,19 @@

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.jmeter.testelement.TestElement;
import org.apache.jmeter.testelement.property.CollectionProperty;
import org.apache.jmeter.testelement.property.JMeterProperty;
import org.apache.jmeter.testelement.property.PropertyIterator;
import org.apache.jmeter.testelement.property.TestElementProperty;
import org.apache.jmeter.testelement.schema.PropertiesAccessor;
import org.apache.xpath.Arg;
import org.apiguardian.api.API;

/**
* A set of Argument objects.
Expand Down Expand Up @@ -100,7 +104,7 @@ public Map<String, String> getArgumentsAsMap() {
// that this element's values prevail over defaults provided by
// configuration
// elements:
if (!argMap.containsKey(arg.getName())) {
if (!argMap.containsKey(arg.getName()) && arg.isEnabled()) {
argMap.put(arg.getName(), arg.getValue());
}
}
Expand Down Expand Up @@ -173,6 +177,49 @@ public PropertyIterator iterator() {
return getArguments().iterator();
}

/**
* Returns the list of enabled arguments.
* @return the list of enabled arguments
*/
@API(since = "5.6", status = API.Status.EXPERIMENTAL)
public Iterable<JMeterProperty> getEnabledArguments() {
return () -> {
PropertyIterator iterator = iterator();
return new Iterator<>() {
JMeterProperty next;

@Override
public boolean hasNext() {
if (next != null) {
return true;
}
while (iterator.hasNext()) {
next = iterator.next();
Object value = next.getObjectValue();
if (!(value instanceof Argument)) {
next = null;
continue;
}
Argument arg = (Argument) value;
if (arg.isSkippable(next.getName()) || !arg.isEnabled()) {
next = null;
continue;
}
break;
}
return next != null;
}

@Override
public JMeterProperty next() {
JMeterProperty result = next;
next = null;
return result;
}
};
};
}

/**
* Create a string representation of the arguments.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,11 +261,8 @@ private static String computePostBody(Arguments arguments) {
*/
private static String computePostBody(Arguments arguments, boolean crlfToLF) {
StringBuilder postBody = new StringBuilder();
for (JMeterProperty argument : arguments) {
for (JMeterProperty argument : arguments.getEnabledArguments()) {
HTTPArgument arg = (HTTPArgument) argument.getObjectValue();
if (!arg.isEnabled()) {
continue; // Skip parameters if they've been disabled from GUI using the checkbox
}
String value = arg.getValue();
if (crlfToLF) {
value = value.replaceAll("\r\n", "\n"); // See modifyTestElement
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ private String setConnectionHeaders(URL url, String host, String method)
setString(HTTPConstants.APPLICATION_X_WWW_FORM_URLENCODED);
StringBuilder sb = new StringBuilder();
boolean first = true;
for (JMeterProperty arg : getArguments()) {
for (JMeterProperty arg : getArguments().getEnabledArguments()) {
if (first) {
first = false;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1571,15 +1571,9 @@ protected String setupHttpEntityEnclosingRequestData(HttpEntityEnclosingRequestB
}
// Create the parts
// Add any parameters
for (JMeterProperty jMeterProperty : getArguments()) {
for (JMeterProperty jMeterProperty : getArguments().getEnabledArguments()) {
HTTPArgument arg = (HTTPArgument) jMeterProperty.getObjectValue();
String parameterName = arg.getName();
if (arg.isSkippable(parameterName)) {
continue;
}
if (!arg.isEnabled()) {
continue; // Skip parameters if they've been disabled from GUI using the checkbox
}
ContentType contentType;
if (arg.getContentType().indexOf(';') >= 0) {
// assume, that the content type contains charset info
Expand Down Expand Up @@ -1656,11 +1650,8 @@ else if(ADD_CONTENT_TYPE_TO_POST_IF_MISSING) {

// Just append all the parameter values, and use that as the post body
StringBuilder postBody = new StringBuilder();
for (JMeterProperty jMeterProperty : getArguments()) {
for (JMeterProperty jMeterProperty : getArguments().getEnabledArguments()) {
HTTPArgument arg = (HTTPArgument) jMeterProperty.getObjectValue();
if (!arg.isEnabled()) {
continue; // Skip parameters if they've been disabled from GUI using the checkbox
}
postBody.append(arg.getEncodedValue(contentEncoding));
}
// Let StringEntity perform the encoding
Expand Down Expand Up @@ -1802,20 +1793,13 @@ else if(getSendParameterValuesAsPostBody()) {
private UrlEncodedFormEntity createUrlEncodedFormEntity(final String urlContentEncoding) throws UnsupportedEncodingException {
// It is a normal request, with parameter names and values
// Add the parameters
PropertyIterator args = getArguments().iterator();
List<NameValuePair> nvps = new ArrayList<>();
while (args.hasNext()) {
HTTPArgument arg = (HTTPArgument) args.next().getObjectValue();
for (JMeterProperty jMeterProperty: getArguments().getEnabledArguments()) {
HTTPArgument arg = (HTTPArgument) jMeterProperty.getObjectValue();
// The HTTPClient always urlencodes both name and value,
// so if the argument is already encoded, we have to decode
// it before adding it to the post request
String parameterName = arg.getName();
if (arg.isSkippable(parameterName)) {
continue;
}
if (!arg.isEnabled()) {
continue; // Skip parameters if they've been disabled from GUI using the checkbox
}
String parameterValue = arg.getValue();
if (!arg.isAlwaysEncoded()) {
// The value is already encoded by the user
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -408,15 +408,7 @@ public boolean getSendParameterValuesAsPostBody() {
if (getPostBodyRaw()) {
return true;
} else {
boolean hasArguments = false;
for (JMeterProperty jMeterProperty : getArguments()) {
hasArguments = true;
HTTPArgument arg = (HTTPArgument) jMeterProperty.getObjectValue();
if (arg.getName() != null && !arg.getName().isEmpty()) {
return false;
}
}
return hasArguments;
return getArguments().getEnabledArguments().iterator().hasNext();
}
}

Expand Down Expand Up @@ -1155,9 +1147,10 @@ public String getQueryString() {
*/
public String getQueryString(final String contentEncoding) {

CollectionProperty arguments = getArguments().getArguments();
Arguments args = getArguments();
Iterator<JMeterProperty> iter = args.getEnabledArguments().iterator();
// Optimisation : avoid building useless objects if empty arguments
if(arguments.isEmpty()) {
if (!iter.hasNext()) {
return "";
}
String lContentEncoding = contentEncoding;
Expand All @@ -1167,8 +1160,7 @@ public String getQueryString(final String contentEncoding) {
lContentEncoding = EncoderCache.URL_ARGUMENT_ENCODING;
}

StringBuilder buf = new StringBuilder(arguments.size() * 15);
PropertyIterator iter = arguments.iterator();
StringBuilder buf = new StringBuilder(args.getArgumentCount() * 15);
boolean first = true;
while (iter.hasNext()) {
HTTPArgument item = null;
Expand All @@ -1186,12 +1178,6 @@ public String getQueryString(final String contentEncoding) {
item = new HTTPArgument((Argument) objectValue);
}
final String encodedName = item.getEncodedName();
if (encodedName.isEmpty()) {
continue; // Skip parameters with a blank name (allows use of optional variables in parameter lists)
}
if(!item.isEnabled()){
continue; // Skip parameters if they've been disabled from GUI using the checkbox
}
if (!first) {
buf.append(QRY_SEP);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,12 +190,9 @@ public void setHeaders(URLConnection connection, HTTPSamplerBase sampler) throws
ByteArrayOutputStream bos = new ByteArrayOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(bos, contentEncoding);
// Add any parameters
for (JMeterProperty jMeterProperty : sampler.getArguments()) {
for (JMeterProperty jMeterProperty : sampler.getArguments().getEnabledArguments()) {
HTTPArgument arg = (HTTPArgument) jMeterProperty.getObjectValue();
String parameterName = arg.getName();
if (arg.isSkippable(parameterName)) {
continue;
}
// Write multipart for parameter
writeFormMultipart(osw, contentEncoding, parameterName, arg.getValue(), sampler.getDoBrowserCompatibleMultipart());
}
Expand Down Expand Up @@ -299,7 +296,7 @@ public void setHeaders(URLConnection connection, HTTPSamplerBase sampler) throws

// Just append all the parameter values, and use that as the post body
StringBuilder postBodyBuffer = new StringBuilder();
for (JMeterProperty jMeterProperty : sampler.getArguments()) {
for (JMeterProperty jMeterProperty : sampler.getArguments().getEnabledArguments()) {
HTTPArgument arg = (HTTPArgument) jMeterProperty.getObjectValue();
postBodyBuffer.append(arg.getEncodedValue(contentEncoding));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,8 @@ else if(sampler.getSendParameterValuesAsPostBody()) {

// Just append all the parameter values, and use that as the put body
StringBuilder putBodyBuffer = new StringBuilder();
for (JMeterProperty jMeterProperty : sampler.getArguments()) {
for (JMeterProperty jMeterProperty : sampler.getArguments().getEnabledArguments()) {
HTTPArgument arg = (HTTPArgument) jMeterProperty.getObjectValue();
if (!arg.isEnabled()) {
continue; // Skip parameters if they've been disabled from GUI using the checkbox
}
putBodyBuffer.append(arg.getEncodedValue(contentEncoding));
}

Expand Down

0 comments on commit 88567af

Please sign in to comment.