Skip to content
Draft
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,12 @@ plugins.withId("org.jetbrains.kotlin.jvm") {
kotlin {
license()
trimTrailingWhitespace()
ktlint("0.40.0")
ktlint("0.40.0") {
filter {
// TODO: remove exclusion when update ktlint
exclude("**/org/apache/jorphan/locale/PlainValue.kt")
}
}
endWithNewline()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import org.apache.jmeter.threads.JMeterContext;
import org.apache.jmeter.threads.JMeterVariables;
import org.apache.jmeter.util.JMeterUtils;
import org.apache.jorphan.util.EnumUtils;
import org.apache.jorphan.locale.ResourceKeyed;
import org.apache.jorphan.util.JMeterStopThreadException;
import org.apache.jorphan.util.JOrphanUtils;
import org.apache.jorphan.util.StringUtilities;
Expand Down Expand Up @@ -70,20 +70,20 @@
public class CSVDataSet extends ConfigTestElement
implements TestBean, LoopIterationListener, NoConfigMerge {

public enum ShareMode {
public enum ShareMode implements ResourceKeyed {
ALL("shareMode.all"),
GROUP("shareMode.group"),
THREAD("shareMode.thread");

private final String value;
private final String propertyName;

ShareMode(String value) {
this.value = value;
ShareMode(String propertyName) {
this.propertyName = propertyName;
}

@Override
public String toString() {
return value;
public String getResourceKey() {
return propertyName;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public class CSVDataSetBeanInfo extends BeanInfoSupport {
for (CSVDataSet.ShareMode value : CSVDataSet.ShareMode.values()) {
@SuppressWarnings("EnumOrdinal")
int index = value.ordinal();
SHARE_TAGS[index] = value.toString();
SHARE_TAGS[index] = value.getResourceKey();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.apache.jmeter.threads.JMeterContextService;
import org.apache.jmeter.util.JMeterUtils;
import org.apache.jorphan.collections.IdentityKey;
import org.apache.jorphan.locale.ResourceKeyed;
import org.apache.jorphan.util.EnumUtils;
import org.apiguardian.api.API;

Expand Down Expand Up @@ -71,7 +72,7 @@ private static class ThroughputInfo{
/**
* This enum defines the calculation modes used by the ConstantThroughputTimer.
*/
public enum Mode {
public enum Mode implements ResourceKeyed {
ThisThreadOnly("calcMode.1"), // NOSONAR Keep naming for compatibility
AllActiveThreads("calcMode.2"), // NOSONAR Keep naming for compatibility
AllActiveThreadsInCurrentThreadGroup("calcMode.3"), // NOSONAR Keep naming for compatibility
Expand All @@ -89,6 +90,11 @@ public enum Mode {
public String toString() {
return propertyName;
}

@Override
public String getResourceKey() {
return propertyName;
}
}

/**
Expand Down Expand Up @@ -161,15 +167,13 @@ public Mode getMode() {
}

@Deprecated
@SuppressWarnings("EnumOrdinal")
public void setCalcMode(int mode) {
setMode(EnumUtils.values(Mode.class).get(mode));
setMode(EnumUtils.getEnumValues(Mode.class).get(mode));
}

@SuppressWarnings("EnumOrdinal")
@API(status = API.Status.MAINTAINED, since = "6.0.0")
public void setMode(Mode newMode) {
getSchema().getCalcMode().set(this, newMode.toString());
getSchema().getCalcMode().set(this, newMode.getResourceKey());
}

/**
Expand Down Expand Up @@ -296,7 +300,6 @@ public String toString() {
* so the conversion only needs to happen once.
*/
@Override
@SuppressWarnings("EnumOrdinal")
public void setProperty(JMeterProperty property) {
String propertyName = property.getName();
if (propertyName.equals("calcMode")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
*/
public class ConstantThroughputTimerBeanInfo extends BeanInfoSupport {

@SuppressWarnings("EnumOrdinal")
public ConstantThroughputTimerBeanInfo() {
super(ConstantThroughputTimer.class);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,20 @@ public class TestPlanGui extends AbstractJMeterGuiComponent {
private final JBooleanPropertyEditor functionalMode =
new JBooleanPropertyEditor(
TestPlanSchema.INSTANCE.getFunctionalMode(),
JMeterUtils.getResString("functional_mode"));
"functional_mode",
JMeterUtils::getResString);

private final JBooleanPropertyEditor serializedMode =
new JBooleanPropertyEditor(
TestPlanSchema.INSTANCE.getSerializeThreadgroups(),
JMeterUtils.getResString("testplan.serialized"));
"testplan.serialized",
JMeterUtils::getResString);

private final JBooleanPropertyEditor tearDownOnShutdown =
new JBooleanPropertyEditor(
TestPlanSchema.INSTANCE.getTearDownOnShutdown(),
JMeterUtils.getResString("teardown_on_shutdown"));
"teardown_on_shutdown",
JMeterUtils::getResString);

/** A panel allowing the user to define variables. */
private final ArgumentsPanel argsPanel;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,15 @@ public class TransactionControllerGui extends AbstractControllerGui {
private final JBooleanPropertyEditor generateParentSample =
new JBooleanPropertyEditor(
TransactionControllerSchema.INSTANCE.getGenearteParentSample(),
JMeterUtils.getResString("transaction_controller_parent"));
"transaction_controller_parent",
JMeterUtils::getResString);

/** if selected, add duration of timers to total runtime */
private final JBooleanPropertyEditor includeTimers =
new JBooleanPropertyEditor(
TransactionControllerSchema.INSTANCE.getIncludeTimers(),
JMeterUtils.getResString("transaction_controller_include_timers"));
"transaction_controller_parent",
JMeterUtils::getResString);

/**
* Create a new TransactionControllerGui instance.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@
import org.apache.jmeter.testelement.TestElementSchema;
import org.apache.jmeter.util.JMeterUtils;
import org.apache.jmeter.visualizers.Printable;
import org.apache.jorphan.gui.JEditableTextArea;
import org.apache.jorphan.gui.JFactory;
import org.apache.jorphan.gui.ResetMode;
import org.apache.jorphan.locale.LocalizedString;
import org.apiguardian.api.API;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -81,7 +84,23 @@ public abstract class AbstractJMeterGuiComponent extends JPanel implements JMete
@SuppressWarnings("DeprecatedIsStillUsed")
protected NamePanel namePanel;

private final JTextArea commentField = JFactory.tabMovesFocus(new JTextArea());
private final JEditableTextArea commentEditor = createCommentEditor();
// The legacy commentField reference still points at the inner JTextArea
// so existing setText / getText callers keep working unchanged.
private final JTextArea commentField = JFactory.tabMovesFocus(commentEditor.getInnerTextArea());

private static JEditableTextArea createCommentEditor() {
JEditableTextArea editor = new JEditableTextArea(
new JEditableTextArea.Configuration(
new ResetMode.Allow(new LocalizedString("reset", JMeterUtils::getResString))));
// Comment-field semantics: the gutter lights up while the comment
// is non-empty. There is no concept of "explicit empty" for a
// comment, so we recompute the modified flag from the live text on
// every value change (programmatic loads as well as typing).
editor.addPropertyChangeListener(JEditableTextArea.VALUE_PROPERTY,
evt -> editor.setModified(!editor.getValue().isEmpty()));
return editor;
}

/**
* Stores a collection of property editors, so GuiCompoenent can have default implementations that
Expand Down Expand Up @@ -305,7 +324,7 @@ protected Container makeTitlePanel() {
titlePanel.add(labelFor(nameField, "testplan_comments"));
commentField.setWrapStyleWord(true);
commentField.setLineWrap(true);
titlePanel.add(commentField);
titlePanel.add(commentEditor);

// Note: VerticalPanel has a workaround for Box layout which aligns elements, so we can't
// use trivial JPanel.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package org.apache.jmeter.samplers;

import java.io.IOException;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
Expand Down Expand Up @@ -161,6 +162,8 @@ public class SampleResult implements Serializable, Cloneable, Searchable {

private byte[] responseData = EMPTY_BA;

private String contentEncoding; // Stores gzip/deflate encoding if response is compressed

private String responseCode = "";// Never return null

private String label = "";// Never return null
Expand Down Expand Up @@ -792,6 +795,16 @@ public void setResponseData(final String response, final String encoding) {
* @return the responseData value (cannot be null)
*/
public byte[] getResponseData() {
if (responseData == null) {
return EMPTY_BA;
}
if (contentEncoding != null && responseData.length > 0) {
try {
return ResponseDecoderRegistry.decode(contentEncoding, responseData);
} catch (IOException e) {
log.warn("Failed to decompress response data", e);
}
}
return responseData;
}

Expand All @@ -803,12 +816,12 @@ public byte[] getResponseData() {
public String getResponseDataAsString() {
try {
if(responseDataAsString == null) {
responseDataAsString= new String(responseData,getDataEncodingWithDefault());
responseDataAsString= new String(getResponseData(),getDataEncodingWithDefault());
}
return responseDataAsString;
} catch (UnsupportedEncodingException e) {
log.warn("Using platform default as {} caused {}", getDataEncodingWithDefault(), e.getLocalizedMessage());
return new String(responseData,Charset.defaultCharset()); // N.B. default charset is used deliberately here
return new String(getResponseData(),Charset.defaultCharset()); // N.B. default charset is used deliberately here
}
}

Expand Down Expand Up @@ -1666,4 +1679,15 @@ public TestLogicalAction getTestLogicalAction() {
public void setTestLogicalAction(TestLogicalAction testLogicalAction) {
this.testLogicalAction = testLogicalAction;
}

/**
* Sets the response data and its contentEncoding.
* @param data The response data
* @param contentEncoding The content contentEncoding (e.g. gzip, deflate)
*/
public void setResponseData(byte[] data, String contentEncoding) {
responseData = data == null ? EMPTY_BA : data;
this.contentEncoding = contentEncoding;
responseDataAsString = null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import javax.swing.JList;

import org.apache.jmeter.gui.ClearGui;
import org.apache.jorphan.locale.ResourceKeyed;
import org.apache.jorphan.util.EnumUtils;

/**
Expand All @@ -41,34 +42,32 @@
* The provided GUI is a combo box with an option for each value in the enum.
* <p>
*/
class EnumEditor extends PropertyEditorSupport implements ClearGui {
class EnumEditor<T extends Enum<?> & ResourceKeyed> extends PropertyEditorSupport implements ClearGui {
private final JComboBox<T> combo;

private final JComboBox<Enum<?>> combo;
private final T defaultValue;

private final Enum<?> defaultValue;

public EnumEditor(final PropertyDescriptor descriptor, final Class<? extends Enum<?>> enumClazz, final ResourceBundle rb) {
DefaultComboBoxModel<Enum<?>> model = new DefaultComboBoxModel<>();
public EnumEditor(final PropertyDescriptor descriptor, final Class<T> enumClass, final ResourceBundle rb) {
DefaultComboBoxModel<T> model = new DefaultComboBoxModel<>();
combo = new JComboBox<>(model);
combo.setEditable(false);
combo.setRenderer(
new DefaultListCellRenderer() {
@Override
public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
JLabel label = (JLabel) super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
Enum<?> enumValue = (Enum<?>) value;
label.setText(rb.getString(EnumUtils.getStringValue(enumValue)));
label.setText(rb.getString(enumClass.cast(value).getResourceKey()));
return label;
}
}
);
List<? extends Enum<?>> values = EnumUtils.values(enumClazz);
for(Enum<?> e : values) {
List<T> values = EnumUtils.getEnumValues(enumClass);
for (T e : values) {
model.addElement(e);
}
Object def = descriptor.getValue(GenericTestBeanCustomizer.DEFAULT);
if (def instanceof Enum<?> enumValue) {
defaultValue = enumValue;
defaultValue = enumClass.cast(enumValue);
} else if (def instanceof Integer index) {
defaultValue = values.get(index);
} else {
Expand Down Expand Up @@ -99,25 +98,24 @@ public void setValue(Object value) {
} else if (value instanceof Integer integer) {
combo.setSelectedIndex(integer);
} else if (value instanceof String string) {
ComboBoxModel<Enum<?>> model = combo.getModel();
for (int i = 0; i < model.getSize(); i++) {
Enum<?> element = model.getElementAt(i);
if (EnumUtils.getStringValue(element).equals(string)) {
combo.setSelectedItem(element);
return;
}
}
setAsText(string);
}
}

@Override
public void setAsText(String value) {
throw new UnsupportedOperationException("Not supported yet. Use enum value rather than text, got " + value);
ComboBoxModel<T> model = combo.getModel();
for (int i = 0; i < model.getSize(); i++) {
T element = model.getElementAt(i);
if (value.equals(element.getResourceKey())) {
combo.setSelectedItem(element);
return;
}
}
}

@Override
public void clearGui() {
combo.setSelectedItem(defaultValue);
}

}
Loading
Loading