Skip to content

Commit

Permalink
fix: trim whitespace when parsing numeric properties
Browse files Browse the repository at this point in the history
Motivation: if the value parses after trimming, then it would likely be the one
the user wants.

Currently, a trailing whitespace in UI is hard to notice, and it breaks test
execution. For instance ThreadGroup.threadNum becomes 0 without any warning,
and it invalidates test results.
  • Loading branch information
vlsi committed May 30, 2024
1 parent bacaa8b commit 289b3d1
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,11 @@ public AbstractProperty clone() {
@Override
public int getIntValue() {
String val = getStringValue();
if (val == null || val.length()==0) {
if (val == null || val.isEmpty()) {
return 0;
}
try {
return Integer.parseInt(val);
return Integer.parseInt(val.trim());
} catch (NumberFormatException e) {
return 0;
}
Expand All @@ -126,11 +126,11 @@ public int getIntValue() {
@Override
public long getLongValue() {
String val = getStringValue();
if (val == null || val.length()==0) {
if (val == null || val.isEmpty()) {
return 0;
}
try {
return Long.parseLong(val);
return Long.parseLong(val.trim());
} catch (NumberFormatException e) {
return 0;
}
Expand All @@ -144,11 +144,11 @@ public long getLongValue() {
@Override
public double getDoubleValue() {
String val = getStringValue();
if (val == null || val.length()==0) {
if (val == null || val.isEmpty()) {
return 0;
}
try {
return Double.parseDouble(val);
return Double.parseDouble(val.trim());
} catch (NumberFormatException e) {
log.error("Tried to parse a non-number string to an integer", e);
return 0;
Expand All @@ -163,11 +163,11 @@ public double getDoubleValue() {
@Override
public float getFloatValue() {
String val = getStringValue();
if (val == null || val.length()==0) {
if (val == null || val.isEmpty()) {
return 0;
}
try {
return Float.parseFloat(val);
return Float.parseFloat(val.trim());
} catch (NumberFormatException e) {
log.error("Tried to parse a non-number string to an integer", e);
return 0;
Expand All @@ -182,10 +182,10 @@ public float getFloatValue() {
@Override
public boolean getBooleanValue() {
String val = getStringValue();
if (val == null || val.length()==0) {
if (val == null || val.isEmpty()) {
return false;
}
return Boolean.parseBoolean(val);
return Boolean.parseBoolean(val.trim());
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to you under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.jmeter.threads

import org.apache.jmeter.control.LoopController
import org.apache.jmeter.junit.JMeterTestCase
import org.apache.jmeter.test.assertions.executePlanAndCollectEvents
import org.apache.jmeter.test.samplers.ThreadSleep
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
import kotlin.time.Duration.Companion.seconds

class ThreadGroupTest : JMeterTestCase() {
@Test
fun `threadNum with trailing whitespace`() {
val events = executePlanAndCollectEvents(10.seconds) {
ThreadGroup::class {
props {
it[numThreads] = "1 "
}
rampUp = 0
scheduler = true
delay = 0
duration = 1
setSamplerController(
LoopController().apply {
loops = 1
setContinueForever(false)
}
)

ThreadSleep::class {
duration = 0.seconds
}
}
}
assertEquals(1, events.size) {
"ThreadGroup.threadNum has trailing whitespace, it should be trimmed, so one event should be generated. " +
"Actual events are $events"
}
}
}

0 comments on commit 289b3d1

Please sign in to comment.