Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: trim whitespace when parsing numeric properties #6283

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
}
}
}
Loading