Skip to content

Commit d3f7956

Browse files
Advanced boolean parsing. Fixes #2095 (#2097)
1. Empty string will result in an illegal argument exception 2. Using any value other then true/false 1/0 or yes/no will result in an illegal argument exception 3. `CucumberEngineOptions` delegates to JUnit 5 via `getBoolean` 4. This (strictly parsing booleans) is a new feature. Co-authored-by: M.P. Korstanje <[email protected]>
1 parent 8477e65 commit d3f7956

File tree

5 files changed

+83
-9
lines changed

5 files changed

+83
-9
lines changed

CHANGELOG.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,14 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
88
## [Unreleased] (In Git)
99

1010
### Added
11-
11+
* Boolean system properties and environment variables (`cucumber.*` and `CUCUMBER_*`)
12+
are strictly parsed. The values `0`, `false`, `no` are interpreted as `false`.
13+
The values `1`, `true`, `yes` are interpreted as `true`. All other values will
14+
throw an exception.
15+
([#2095](https://github.com/cucumber/cucumber-jvm/pull/2097)
16+
[#2097](https://github.com/cucumber/cucumber-jvm/pull/2097)
17+
Aslak Hellesøy)
18+
1219
### Changed
1320

1421
### Deprecated
@@ -24,7 +31,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
2431
* Publish instructions now recommend using `src/test/resources/cucumber.properties`.
2532
([#2096](https://github.com/cucumber/cucumber-jvm/pull/2096)
2633
Aslak Hellesøy)
27-
34+
2835
## [6.5.0] (2020-08-17)
2936

3037
### Added
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package io.cucumber.core.options;
2+
3+
final class BooleanString {
4+
5+
static boolean parseBoolean(String s) {
6+
if (s == null) {
7+
return false;
8+
}
9+
10+
if ("true".equalsIgnoreCase(s)) {
11+
return true;
12+
} else if ("false".equalsIgnoreCase(s)) {
13+
return false;
14+
}
15+
16+
if ("1".equalsIgnoreCase(s)) {
17+
return true;
18+
} else if ("0".equalsIgnoreCase(s)) {
19+
return false;
20+
}
21+
22+
if ("yes".equalsIgnoreCase(s)) {
23+
return true;
24+
} else if ("no".equalsIgnoreCase(s)) {
25+
return false;
26+
}
27+
28+
throw new IllegalArgumentException(
29+
String.format("'%s' Was not a valid boolean value. Please use either 'true' or 'false'.", s));
30+
}
31+
}

core/src/main/java/io/cucumber/core/options/CucumberPropertiesParser.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,12 @@ public RuntimeOptionsBuilder parse(Map<String, String> properties) {
4242

4343
parse(properties,
4444
ANSI_COLORS_DISABLED_PROPERTY_NAME,
45-
Boolean::parseBoolean,
45+
BooleanString::parseBoolean,
4646
builder::setMonochrome);
4747

4848
parse(properties,
4949
EXECUTION_DRY_RUN_PROPERTY_NAME,
50-
Boolean::parseBoolean,
50+
BooleanString::parseBoolean,
5151
builder::setDryRun);
5252

5353
parse(properties,
@@ -62,7 +62,7 @@ public RuntimeOptionsBuilder parse(Map<String, String> properties) {
6262

6363
parse(properties,
6464
EXECUTION_STRICT_PROPERTY_NAME,
65-
Boolean::parseBoolean,
65+
BooleanString::parseBoolean,
6666
CucumberPropertiesParser::errorOnNonStrict);
6767

6868
parseAll(properties,
@@ -107,12 +107,12 @@ public RuntimeOptionsBuilder parse(Map<String, String> properties) {
107107

108108
parse(properties,
109109
PLUGIN_PUBLISH_ENABLED_PROPERTY_NAME,
110-
Boolean::parseBoolean,
110+
BooleanString::parseBoolean,
111111
builder::setPublish);
112112

113113
parse(properties,
114114
PLUGIN_PUBLISH_QUIET_PROPERTY_NAME,
115-
Boolean::parseBoolean,
115+
BooleanString::parseBoolean,
116116
builder::setPublishQuiet);
117117

118118
parse(properties,
@@ -122,7 +122,7 @@ public RuntimeOptionsBuilder parse(Map<String, String> properties) {
122122

123123
parse(properties,
124124
WIP_PROPERTY_NAME,
125-
Boolean::parseBoolean,
125+
BooleanString::parseBoolean,
126126
builder::setWip);
127127

128128
return builder;
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package io.cucumber.core.options;
2+
3+
import org.junit.jupiter.api.Test;
4+
import org.junit.jupiter.params.ParameterizedTest;
5+
import org.junit.jupiter.params.provider.ValueSource;
6+
7+
import static org.hamcrest.CoreMatchers.is;
8+
import static org.hamcrest.MatcherAssert.assertThat;
9+
import static org.junit.jupiter.api.Assertions.assertThrows;
10+
11+
class BooleanStringTest {
12+
13+
@Test
14+
public void null_is_false() {
15+
assertThat(BooleanString.parseBoolean(null), is(false));
16+
}
17+
18+
@ParameterizedTest
19+
@ValueSource(strings = { "false", "no", "0"})
20+
public void falsy_values_are_false(String value) {
21+
assertThat(BooleanString.parseBoolean(value), is(false));
22+
}
23+
24+
@ParameterizedTest
25+
@ValueSource(strings = { "true", "yes", "1" })
26+
public void truthy_values_are_true(String value) {
27+
assertThat(BooleanString.parseBoolean(value), is(true));
28+
}
29+
30+
@ParameterizedTest
31+
@ValueSource(strings = { "y", "n", "-1", " ", "" })
32+
public void unknown_values_throw_illegal_argument_exception(String value) {
33+
assertThrows(IllegalArgumentException.class, () -> BooleanString.parseBoolean(value));
34+
}
35+
36+
}

junit-platform-engine/src/main/java/io/cucumber/junit/platform/engine/CucumberEngineOptions.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ public Class<? extends ObjectFactory> getObjectFactoryClass() {
156156

157157
boolean isParallelExecutionEnabled() {
158158
return configurationParameters
159-
.get(PARALLEL_EXECUTION_ENABLED_PROPERTY_NAME, Boolean::parseBoolean)
159+
.getBoolean(PARALLEL_EXECUTION_ENABLED_PROPERTY_NAME)
160160
.orElse(false);
161161
}
162162

0 commit comments

Comments
 (0)