Skip to content

Commit 5b82b64

Browse files
committed
Use Java 17
1 parent 724690b commit 5b82b64

File tree

6 files changed

+111
-54
lines changed

6 files changed

+111
-54
lines changed

java/src/test/java/io/cucumber/cucumberexpressions/CucumberExpressionParserTest.java

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import static java.nio.file.Files.newDirectoryStream;
2323
import static java.nio.file.Files.newInputStream;
24+
import static java.util.Objects.requireNonNull;
2425
import static org.hamcrest.MatcherAssert.assertThat;
2526
import static org.hamcrest.Matchers.is;
2627
import static org.junit.jupiter.api.Assertions.assertThrows;
@@ -43,7 +44,7 @@ static List<Path> acceptance_tests_pass() throws IOException {
4344
void acceptance_tests_pass(@ConvertWith(Converter.class) Expectation expectation) {
4445
if (expectation.exception == null) {
4546
Node node = parser.parse(expectation.expression);
46-
assertThat(node, is(expectation.expected_ast.toNode()));
47+
assertThat(node, is(expectation.expectedAst.toNode()));
4748
} else {
4849
CucumberExpressionException exception = assertThrows(
4950
CucumberExpressionException.class,
@@ -53,9 +54,15 @@ void acceptance_tests_pass(@ConvertWith(Converter.class) Expectation expectation
5354
}
5455

5556
static class Expectation {
56-
public String expression;
57-
public YamlableNode expected_ast;
58-
public String exception;
57+
public final String expression;
58+
public final YamlableNode expectedAst;
59+
public final @Nullable String exception;
60+
61+
Expectation(String expression, YamlableNode expectedAst, String exception) {
62+
this.expression = expression;
63+
this.expectedAst = expectedAst;
64+
this.exception = exception;
65+
}
5966
}
6067

6168
static class Converter implements ArgumentConverter {
@@ -74,17 +81,23 @@ public Expectation convert(@Nullable Object source, ParameterContext context) th
7481
}
7582

7683
static class YamlableNode {
77-
public @Nullable Node.Type type;
78-
public @Nullable List<YamlableNode> nodes;
79-
public @Nullable String token;
84+
public final Node.Type type;
85+
public final @Nullable List<YamlableNode> nodes;
86+
public final @Nullable String token;
8087
public int start;
8188
public int end;
8289

90+
YamlableNode(Node.Type type, List<YamlableNode> nodes, String token) {
91+
this.type = type;
92+
this.nodes = nodes;
93+
this.token = token;
94+
}
95+
8396
public Node toNode() {
8497
if (token != null) {
8598
return new Node(type, start, end, token);
8699
} else {
87-
return new Node(type, start, end, nodes.stream().map(YamlableNode::toNode).collect(Collectors.toList()));
100+
return new Node(type, start, end, requireNonNull(nodes).stream().map(YamlableNode::toNode).collect(Collectors.toList()));
88101
}
89102
}
90103
}

java/src/test/java/io/cucumber/cucumberexpressions/CucumberExpressionTest.java

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package io.cucumber.cucumberexpressions;
22

3+
import org.jspecify.annotations.NullMarked;
4+
import org.jspecify.annotations.Nullable;
35
import org.junit.jupiter.api.Test;
46
import org.junit.jupiter.api.extension.ParameterContext;
57
import org.junit.jupiter.api.function.Executable;
@@ -19,6 +21,7 @@
1921
import java.nio.file.Path;
2022
import java.nio.file.Paths;
2123
import java.util.ArrayList;
24+
import java.util.Arrays;
2225
import java.util.Comparator;
2326
import java.util.List;
2427
import java.util.Locale;
@@ -32,12 +35,14 @@
3235
import static org.hamcrest.Matchers.equalTo;
3336
import static org.hamcrest.core.Is.is;
3437
import static org.junit.jupiter.api.Assertions.assertEquals;
38+
import static org.junit.jupiter.api.Assertions.assertNotNull;
3539
import static org.junit.jupiter.api.Assertions.assertThrows;
3640

41+
@NullMarked
3742
class CucumberExpressionTest {
3843
private final ParameterTypeRegistry parameterTypeRegistry = new ParameterTypeRegistry(Locale.ENGLISH);
3944

40-
private static List<Path> acceptance_tests_pass() throws IOException {
45+
static List<Path> acceptance_tests_pass() throws IOException {
4146
List<Path> paths = new ArrayList<>();
4247
Path path = Paths.get("..", "testdata", "cucumber-expression", "matching");
4348
try (DirectoryStream<Path> directories = newDirectoryStream(path)){
@@ -57,7 +62,7 @@ void acceptance_tests_pass(@ConvertWith(Converter.class) Expectation expectation
5762
.map(Argument::getValue)
5863
.collect(Collectors.toList());
5964

60-
assertThat(values, CustomMatchers.equalOrCloseTo(expectation.expected_args));
65+
assertThat(values, CustomMatchers.equalOrCloseTo(expectation.expectedArgs));
6166
} else {
6267
Executable executable = () -> {
6368
CucumberExpression expression = new CucumberExpression(expectation.expression, parameterTypeRegistry);
@@ -69,17 +74,29 @@ void acceptance_tests_pass(@ConvertWith(Converter.class) Expectation expectation
6974
}
7075

7176
static class Expectation {
72-
public String expression;
73-
public String text;
74-
public List<?> expected_args;
75-
public String exception;
77+
public final String expression;
78+
public final String text;
79+
public final List<?> expectedArgs;
80+
public final @Nullable String exception;
81+
82+
Expectation(String expression, String text, List<?> expectedArgs, String exception) {
83+
this.expression = expression;
84+
this.text = text;
85+
this.expectedArgs = expectedArgs;
86+
this.exception = exception;
87+
}
7688
}
7789

90+
@NullMarked
7891
static class Converter implements ArgumentConverter {
7992
Yaml yaml = new Yaml();
8093

8194
@Override
82-
public Expectation convert(Object source, ParameterContext context) throws ArgumentConversionException {
95+
public Expectation convert(@Nullable Object source, ParameterContext context) throws ArgumentConversionException {
96+
if (source == null) {
97+
throw new ArgumentConversionException("Could not load null");
98+
}
99+
83100
try {
84101
Path path = (Path) source;
85102
InputStream inputStream = newInputStream(path);
@@ -109,6 +126,7 @@ void documents_match_arguments() {
109126
String expr = "I have {int} cuke(s)";
110127
Expression expression = new CucumberExpression(expr, parameterTypeRegistry);
111128
List<Argument<?>> args = expression.match("I have 7 cukes");
129+
assertNotNull(args);
112130
assertEquals(7, args.get(0).getValue());
113131
}
114132

@@ -149,7 +167,7 @@ void matches_double_with_comma_for_locale_using_comma() {
149167
@Test
150168
void matches_float_with_zero() {
151169
List<?> values = match("{float}", "0", Locale.ENGLISH);
152-
assertEquals(0.0f, values.get(0));
170+
assertEquals(singletonList(0.0f), values);
153171
}
154172

155173
@Test
@@ -158,12 +176,11 @@ void unmatched_optional_groups_have_null_values() {
158176
parameterTypeRegistry.defineParameterType(new ParameterType<>(
159177
"textAndOrNumber",
160178
singletonList("([A-Z]+)?(?: )?([0-9]+)?"),
161-
new TypeReference<List<String>>() {
162-
}.getType(),
179+
new TypeReference<List<String>>() {}.getType(),
163180
new CaptureGroupTransformer<List<String>>() {
164181
@Override
165-
public List<String> transform(String... args) {
166-
return asList(args);
182+
public List<String> transform(@Nullable String[] args) {
183+
return Arrays.asList(args);
167184
}
168185
},
169186
false,
@@ -173,15 +190,18 @@ public List<String> transform(String... args) {
173190
assertThat(match("{textAndOrNumber}", "123", parameterTypeRegistry), is(singletonList(asList(null, "123"))));
174191
}
175192

193+
@Nullable
176194
private List<?> match(String expr, String text, Type... typeHints) {
177195
return match(expr, text, parameterTypeRegistry, typeHints);
178196
}
179197

198+
@Nullable
180199
private List<?> match(String expr, String text, Locale locale, Type... typeHints) {
181200
ParameterTypeRegistry parameterTypeRegistry = new ParameterTypeRegistry(locale);
182201
return match(expr, text, parameterTypeRegistry, typeHints);
183202
}
184203

204+
@Nullable
185205
private List<?> match(String expr, String text, ParameterTypeRegistry parameterTypeRegistry, Type... typeHints) {
186206
CucumberExpression expression = new CucumberExpression(expr, parameterTypeRegistry);
187207
List<Argument<?>> args = expression.match(text, typeHints);

java/src/test/java/io/cucumber/cucumberexpressions/CucumberExpressionTokenizerTest.java

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package io.cucumber.cucumberexpressions;
22

33
import io.cucumber.cucumberexpressions.Ast.Token;
4-
import io.cucumber.cucumberexpressions.Ast.Token.Type;
4+
import org.jspecify.annotations.NullMarked;
5+
import org.jspecify.annotations.Nullable;
56
import org.junit.jupiter.api.extension.ParameterContext;
67
import org.junit.jupiter.params.ParameterizedTest;
78
import org.junit.jupiter.params.converter.ArgumentConversionException;
@@ -12,6 +13,7 @@
1213

1314
import java.io.IOException;
1415
import java.io.InputStream;
16+
import java.nio.file.DirectoryStream;
1517
import java.nio.file.Path;
1618
import java.nio.file.Paths;
1719
import java.util.ArrayList;
@@ -29,9 +31,11 @@ class CucumberExpressionTokenizerTest {
2931

3032
private final CucumberExpressionTokenizer tokenizer = new CucumberExpressionTokenizer();
3133

32-
private static List<Path> acceptance_tests_pass() throws IOException {
34+
static List<Path> acceptance_tests_pass() throws IOException {
3335
List<Path> paths = new ArrayList<>();
34-
newDirectoryStream(Paths.get("..", "testdata", "cucumber-expression", "tokenizer")).forEach(paths::add);
36+
try(DirectoryStream<Path> directories = newDirectoryStream(Paths.get("..", "testdata", "cucumber-expression", "tokenizer"))) {
37+
directories.forEach(paths::add);
38+
}
3539
paths.sort(Comparator.naturalOrder());
3640
return paths;
3741
}
@@ -41,7 +45,7 @@ private static List<Path> acceptance_tests_pass() throws IOException {
4145
void acceptance_tests_pass(@ConvertWith(Converter.class) Expectation expectation) {
4246
if (expectation.exception == null) {
4347
List<Token> tokens = tokenizer.tokenize(expectation.expression);
44-
List<Token> expectedTokens = expectation.expected_tokens
48+
List<Token> expectedTokens = expectation.expectedTokens
4549
.stream()
4650
.map(YamlableToken::toToken)
4751
.collect(Collectors.toList());
@@ -55,16 +59,26 @@ void acceptance_tests_pass(@ConvertWith(Converter.class) Expectation expectation
5559
}
5660

5761
static class Expectation {
58-
public String expression;
59-
public List<YamlableToken> expected_tokens;
60-
public String exception;
62+
public final String expression;
63+
public final List<YamlableToken> expectedTokens;
64+
public final @Nullable String exception;
65+
66+
Expectation(String expression, List<YamlableToken> expectedTokens, @Nullable String exception) {
67+
this.expression = expression;
68+
this.expectedTokens = expectedTokens;
69+
this.exception = exception;
70+
}
6171
}
6272

73+
@NullMarked
6374
static class Converter implements ArgumentConverter {
6475
Yaml yaml = new Yaml();
6576

6677
@Override
67-
public Expectation convert(Object source, ParameterContext context) throws ArgumentConversionException {
78+
public Expectation convert(@Nullable Object source, ParameterContext context) throws ArgumentConversionException {
79+
if (source == null) {
80+
throw new ArgumentConversionException("Could not load null");
81+
}
6882
try {
6983
Path path = (Path) source;
7084
InputStream inputStream = newInputStream(path);
@@ -76,10 +90,17 @@ public Expectation convert(Object source, ParameterContext context) throws Argum
7690
}
7791

7892
static class YamlableToken {
79-
public String text;
80-
public Type type;
81-
public int start;
82-
public int end;
93+
public final String text;
94+
public final Token.Type type;
95+
public final int start;
96+
public final int end;
97+
98+
YamlableToken(String text, Token.Type type, int start, int end) {
99+
this.text = text;
100+
this.type = type;
101+
this.start = start;
102+
this.end = end;
103+
}
83104

84105
public Token toToken() {
85106
return new Token(text, type, start, end);

java/src/test/java/io/cucumber/cucumberexpressions/CucumberExpressionTransformationTest.java

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package io.cucumber.cucumberexpressions;
22

3+
import org.jspecify.annotations.NullMarked;
4+
import org.jspecify.annotations.Nullable;
35
import org.junit.jupiter.api.extension.ParameterContext;
46
import org.junit.jupiter.params.ParameterizedTest;
57
import org.junit.jupiter.params.converter.ArgumentConversionException;
@@ -10,6 +12,7 @@
1012

1113
import java.io.IOException;
1214
import java.io.InputStream;
15+
import java.nio.file.DirectoryStream;
1316
import java.nio.file.Path;
1417
import java.nio.file.Paths;
1518
import java.util.ArrayList;
@@ -24,9 +27,11 @@
2427
class CucumberExpressionTransformationTest {
2528
private final ParameterTypeRegistry parameterTypeRegistry = new ParameterTypeRegistry(Locale.ENGLISH);
2629

27-
private static List<Path> acceptance_tests_pass() throws IOException {
30+
static List<Path> acceptance_tests_pass() throws IOException {
2831
List<Path> paths = new ArrayList<>();
29-
newDirectoryStream(Paths.get("..", "testdata", "cucumber-expression", "transformation")).forEach(paths::add);
32+
try(DirectoryStream<Path> directories = newDirectoryStream(Paths.get("..", "testdata", "cucumber-expression", "transformation"))) {
33+
directories.forEach(paths::add);
34+
}
3035
paths.sort(Comparator.naturalOrder());
3136
return paths;
3237
}
@@ -39,15 +44,24 @@ void acceptance_tests_pass(@ConvertWith(Converter.class) Expectation expectation
3944
}
4045

4146
static class Expectation {
42-
public String expression;
43-
public String expected_regex;
47+
public final String expression;
48+
public final String expected_regex;
49+
50+
Expectation(String expression, String expectedRegex) {
51+
this.expression = expression;
52+
expected_regex = expectedRegex;
53+
}
4454
}
4555

56+
@NullMarked
4657
static class Converter implements ArgumentConverter {
4758
Yaml yaml = new Yaml();
4859

4960
@Override
50-
public Expectation convert(Object source, ParameterContext context) throws ArgumentConversionException {
61+
public Expectation convert(@Nullable Object source, ParameterContext context) throws ArgumentConversionException {
62+
if (source == null) {
63+
throw new ArgumentConversionException("Could not load null");
64+
}
5165
try {
5266
Path path = (Path) source;
5367
InputStream inputStream = newInputStream(path);

java/src/test/java/io/cucumber/cucumberexpressions/CustomMatchers.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public static Matcher<Iterable<?>> equalOrCloseTo(List<?> list) {
2525
private static class EqualOrCloseTo<T> extends BaseMatcher<T> {
2626
private final Object expectedValue;
2727

28-
public EqualOrCloseTo(Object expectedValue) {
28+
EqualOrCloseTo(Object expectedValue) {
2929
this.expectedValue = expectedValue;
3030
}
3131

0 commit comments

Comments
 (0)