Skip to content

Commit 3dd4d31

Browse files
committed
Decouple arguments/properties parsing and processing
Use streams and dedicated mappers. References #286
1 parent ce1c803 commit 3dd4d31

File tree

2 files changed

+32
-13
lines changed

2 files changed

+32
-13
lines changed

src/main/java/com/rabbitmq/perf/PerfTest.java

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved.
1+
// Copyright (c) 2007-2022 VMware, Inc. or its affiliates. All rights reserved.
22
//
33
// This software, the RabbitMQ Java client library, is triple-licensed under the
44
// Mozilla Public License 2.0 ("MPL"), the GNU General Public License version 2
@@ -23,6 +23,7 @@
2323
import com.rabbitmq.client.impl.DefaultExceptionHandler;
2424
import com.rabbitmq.client.impl.nio.NioParams;
2525
import io.micrometer.core.instrument.composite.CompositeMeterRegistry;
26+
import java.util.stream.Collectors;
2627
import org.apache.commons.cli.*;
2728
import org.slf4j.Logger;
2829
import org.slf4j.LoggerFactory;
@@ -756,22 +757,24 @@ static Map<String, Object> convertKeyValuePairs(String arg) {
756757
if (arg == null || arg.trim().isEmpty()) {
757758
return null;
758759
}
759-
Map<String, Object> properties = new HashMap<>();
760-
for (String entry : arg.split(",")) {
760+
return Arrays.stream(arg.split(",")).map(entry -> {
761761
String [] keyValue = entry.split("=");
762-
if (keyValue.length == 1 ||
763-
keyValue[0].equals("x-dead-letter-exchange") &&
764-
keyValue[1].equals("amq.default")) {
765-
properties.put(keyValue[0], "");
762+
if (keyValue.length == 1) {
763+
return new Object[] {keyValue[0], ""};
766764
} else {
767765
try {
768-
properties.put(keyValue[0], Long.parseLong(keyValue[1]));
766+
return new Object[] {keyValue[0], Long.parseLong(keyValue[1])};
769767
} catch(NumberFormatException e) {
770-
properties.put(keyValue[0], keyValue[1]);
768+
return new Object[] {keyValue[0], keyValue[1]};
771769
}
772770
}
773-
}
774-
return properties;
771+
}).map(keyValue -> {
772+
if ("x-dead-letter-exchange".equals(keyValue[0]) && "amq.default".equals(keyValue[1])) {
773+
return new String[] {"x-dead-letter-exchange", ""};
774+
} else {
775+
return keyValue;
776+
}
777+
}).collect(Collectors.toMap(keyEntry -> keyEntry[0].toString(), keyEntry -> keyEntry[1]));
775778
}
776779

777780
private static String getExchangeName(CommandLineProxy cmd, String def) {

src/test/java/com/rabbitmq/perf/PerfTestTest.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2018-2020 VMware, Inc. or its affiliates. All rights reserved.
1+
// Copyright (c) 2018-2022 VMware, Inc. or its affiliates. All rights reserved.
22
//
33
// This software, the RabbitMQ Java client library, is triple-licensed under the
44
// Mozilla Public License 2.0 ("MPL"), the GNU General Public License version 2
@@ -15,12 +15,12 @@
1515

1616
package com.rabbitmq.perf;
1717

18-
import org.assertj.core.api.Assertions;
1918
import org.junit.jupiter.api.Test;
2019

2120
import java.util.HashMap;
2221
import java.util.Map;
2322

23+
import static com.rabbitmq.perf.PerfTest.convertKeyValuePairs;
2424
import static org.assertj.core.api.Assertions.assertThat;
2525
import static org.junit.jupiter.api.Assertions.*;
2626

@@ -97,4 +97,20 @@ Map<String, Integer> reasons(String reasons) {
9797
}
9898
return result;
9999
}
100+
101+
@Test
102+
void convertPostProcessKeyValuePairs() {
103+
assertThat(convertKeyValuePairs("x-queue-type=quorum,max-length-bytes=100000"))
104+
.hasSize(2)
105+
.containsEntry("x-queue-type", "quorum")
106+
.containsEntry("max-length-bytes", 100000L);
107+
assertThat(convertKeyValuePairs("x-dead-letter-exchange=,x-queue-type=quorum"))
108+
.hasSize(2)
109+
.containsEntry("x-dead-letter-exchange", "")
110+
.containsEntry("x-queue-type", "quorum");
111+
assertThat(convertKeyValuePairs("x-dead-letter-exchange=amq.default,x-queue-type=quorum"))
112+
.hasSize(2)
113+
.containsEntry("x-dead-letter-exchange", "")
114+
.containsEntry("x-queue-type", "quorum");
115+
}
100116
}

0 commit comments

Comments
 (0)