Skip to content

Commit 7ff53c1

Browse files
committed
Use Java 17
1 parent 985efed commit 7ff53c1

File tree

7 files changed

+115
-116
lines changed

7 files changed

+115
-116
lines changed

java/src/main/java/io/cucumber/cucumberexpressions/ParameterTypeRegistry.java

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -70,66 +70,69 @@ private ParameterTypeRegistry(ParameterByTypeTransformer defaultParameterTransfo
7070

7171
defineParameterType(new ParameterType<>("biginteger", INTEGER_REGEXPS, BigInteger.class, new Transformer<>() {
7272
@Override
73-
public BigInteger transform(@Nullable String arg) throws Throwable {
74-
return (BigInteger) requireNonNull(internalParameterTransformer.transform(arg, BigInteger.class));
73+
public @Nullable BigInteger transform(@Nullable String arg) throws Throwable {
74+
return (BigInteger) internalParameterTransformer.transform(arg, BigInteger.class);
7575
}
7676
}, false, false, false));
7777
defineParameterType(new ParameterType<>("bigdecimal", localizedFloatRegexp, BigDecimal.class, new Transformer<>() {
7878
@Override
79-
public BigDecimal transform(@Nullable String arg) throws Throwable {
80-
return (BigDecimal) requireNonNull(internalParameterTransformer.transform(arg, BigDecimal.class));
79+
public @Nullable BigDecimal transform(@Nullable String arg) throws Throwable {
80+
return (BigDecimal) internalParameterTransformer.transform(arg, BigDecimal.class);
8181
}
8282
}, false, false, false));
8383
defineParameterType(new ParameterType<>("byte", INTEGER_REGEXPS, Byte.class, new Transformer<>() {
8484
@Override
85-
public Byte transform(@Nullable String arg) throws Throwable {
86-
return (Byte) requireNonNull(internalParameterTransformer.transform(arg, Byte.class));
85+
public @Nullable Byte transform(@Nullable String arg) throws Throwable {
86+
return (Byte) internalParameterTransformer.transform(arg, Byte.class);
8787
}
8888
}, false, false, false));
8989
defineParameterType(new ParameterType<>("short", INTEGER_REGEXPS, Short.class, new Transformer<>() {
9090
@Override
91-
public Short transform(@Nullable String arg) throws Throwable {
92-
return (Short) requireNonNull(internalParameterTransformer.transform(arg, Short.class));
91+
public @Nullable Short transform(@Nullable String arg) throws Throwable {
92+
return (Short) internalParameterTransformer.transform(arg, Short.class);
9393
}
9494
}, false, false, false));
9595
defineParameterType(new ParameterType<>("int", INTEGER_REGEXPS, Integer.class, new Transformer<>() {
9696
@Override
97-
public Integer transform(@Nullable String arg) throws Throwable {
98-
return (Integer) requireNonNull(internalParameterTransformer.transform(arg, Integer.class));
97+
public @Nullable Integer transform(@Nullable String arg) throws Throwable {
98+
return (Integer) internalParameterTransformer.transform(arg, Integer.class);
9999
}
100100
}, true, true, false));
101101
defineParameterType(new ParameterType<>("long", INTEGER_REGEXPS, Long.class, new Transformer<>() {
102102
@Override
103-
public Long transform(@Nullable String arg) throws Throwable {
104-
return (Long) requireNonNull(internalParameterTransformer.transform(arg, Long.class));
103+
public @Nullable Long transform(@Nullable String arg) throws Throwable {
104+
return (Long) internalParameterTransformer.transform(arg, Long.class);
105105
}
106106
}, false, false));
107107
defineParameterType(new ParameterType<>("float", localizedFloatRegexp, Float.class, new Transformer<>() {
108108
@Override
109-
public Float transform(@Nullable String arg) throws Throwable {
110-
return (Float) requireNonNull(internalParameterTransformer.transform(arg, Float.class));
109+
public @Nullable Float transform(@Nullable String arg) throws Throwable {
110+
return (Float) internalParameterTransformer.transform(arg, Float.class);
111111
}
112112
}, false, false));
113113
defineParameterType(new ParameterType<>("double", localizedFloatRegexp, Double.class, new Transformer<>() {
114114
@Override
115-
public Double transform(@Nullable String arg) throws Throwable {
116-
return (Double) requireNonNull(internalParameterTransformer.transform(arg, Double.class));
115+
public @Nullable Double transform(@Nullable String arg) throws Throwable {
116+
return (Double) internalParameterTransformer.transform(arg, Double.class);
117117
}
118118
}, true, true, false));
119119
defineParameterType(new ParameterType<>("word", WORD_REGEXPS, String.class, new Transformer<>() {
120120
@Override
121-
public String transform(@Nullable String arg) throws Throwable {
122-
return (String) requireNonNull(internalParameterTransformer.transform(arg, String.class));
121+
public @Nullable String transform(@Nullable String arg) throws Throwable {
122+
return (String) internalParameterTransformer.transform(arg, String.class);
123123
}
124124
}, false, false, false));
125125
defineParameterType(new ParameterType<>("string", STRING_REGEXPS, String.class, new CaptureGroupTransformer<String>() {
126126
@Override
127-
public String transform(@Nullable String[] args) throws Throwable {
128-
String arg = requireNonNull(args[0] != null ? args[0] : args[1]);
129-
return (String) requireNonNull(internalParameterTransformer.transform(arg
127+
public @Nullable String transform(@Nullable String[] args) throws Throwable {
128+
String arg = args[0] != null ? args[0] : args[1];
129+
if (arg == null) {
130+
return null;
131+
}
132+
return (String) internalParameterTransformer.transform(arg
130133
.replaceAll("\\\\\"", "\"")
131134
.replaceAll("\\\\'", "'"),
132-
String.class));
135+
String.class);
133136
}
134137
}, true, false, false));
135138

Lines changed: 39 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,31 @@
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;
68
import org.junit.jupiter.params.converter.ArgumentConverter;
79
import org.junit.jupiter.params.converter.ConvertWith;
810
import org.junit.jupiter.params.provider.MethodSource;
9-
import org.junit.jupiter.params.shadow.de.siegmar.fastcsv.util.Nullable;
1011
import org.yaml.snakeyaml.Yaml;
1112

1213
import java.io.IOException;
1314
import java.io.InputStream;
14-
import java.nio.file.DirectoryStream;
1515
import java.nio.file.Path;
1616
import java.nio.file.Paths;
1717
import java.util.ArrayList;
18+
import java.util.Collections;
1819
import java.util.Comparator;
1920
import java.util.List;
21+
import java.util.Map;
2022
import java.util.stream.Collectors;
2123

2224
import static java.nio.file.Files.newDirectoryStream;
2325
import static java.nio.file.Files.newInputStream;
2426
import static java.util.Objects.requireNonNull;
2527
import static org.hamcrest.MatcherAssert.assertThat;
28+
import static org.hamcrest.Matchers.equalTo;
2629
import static org.hamcrest.Matchers.is;
2730
import static org.junit.jupiter.api.Assertions.assertThrows;
2831

@@ -32,7 +35,7 @@ class CucumberExpressionParserTest {
3235

3336
static List<Path> acceptance_tests_pass() throws IOException {
3437
List<Path> paths = new ArrayList<>();
35-
try (DirectoryStream<Path> directories = newDirectoryStream(Paths.get("..", "testdata", "cucumber-expression", "parser"))){
38+
try (var directories = newDirectoryStream(Paths.get("..", "testdata", "cucumber-expression", "parser"))) {
3639
directories.forEach(paths::add);
3740
}
3841
paths.sort(Comparator.naturalOrder());
@@ -44,7 +47,7 @@ static List<Path> acceptance_tests_pass() throws IOException {
4447
void acceptance_tests_pass(@ConvertWith(Converter.class) Expectation expectation) {
4548
if (expectation.exception == null) {
4649
Node node = parser.parse(expectation.expression);
47-
assertThat(node, is(expectation.expectedAst.toNode()));
50+
assertThat(node, equalTo(expectation.expectedAst));
4851
} else {
4952
CucumberExpressionException exception = assertThrows(
5053
CucumberExpressionException.class,
@@ -53,52 +56,57 @@ void acceptance_tests_pass(@ConvertWith(Converter.class) Expectation expectation
5356
}
5457
}
5558

56-
static class Expectation {
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-
}
66-
}
67-
59+
@NullMarked
6860
static class Converter implements ArgumentConverter {
6961
Yaml yaml = new Yaml();
7062

7163
@Override
7264
public Expectation convert(@Nullable Object source, ParameterContext context) throws ArgumentConversionException {
65+
if (source == null) {
66+
throw new ArgumentConversionException("Could not load null");
67+
}
7368
try {
7469
Path path = (Path) source;
7570
InputStream inputStream = newInputStream(path);
76-
return yaml.loadAs(inputStream, Expectation.class);
71+
Map<String, ?> expectation = yaml.loadAs(inputStream, Map.class);
72+
return new Expectation(
73+
(String) requireNonNull(expectation.get("expression")),
74+
convertExpectedAst(expectation),
75+
(String) expectation.get("exception")
76+
);
7777
} catch (IOException e) {
7878
throw new ArgumentConversionException("Could not load " + source, e);
7979
}
8080
}
81-
}
82-
83-
static class YamlableNode {
84-
public final Node.Type type;
85-
public final @Nullable List<YamlableNode> nodes;
86-
public final @Nullable String token;
87-
public int start;
88-
public int end;
8981

90-
YamlableNode(Node.Type type, List<YamlableNode> nodes, String token) {
91-
this.type = type;
92-
this.nodes = nodes;
93-
this.token = token;
82+
@SuppressWarnings("unchecked")
83+
private @Nullable Node convertExpectedAst(Map<String, ?> expectation) {
84+
var expectedAst = (Map<String, ?>) expectation.get("expected_ast");
85+
return expectedAst == null ? null : convertNode(expectedAst);
9486
}
9587

96-
public Node toNode() {
88+
private Node convertNode(Map<String, ?> expectation){
89+
var type = Node.Type.valueOf((String) requireNonNull(expectation.get("type")));
90+
var nodes = getNodes(expectation).stream().map(this::convertNode).collect(Collectors.toList());
91+
var token = (String) expectation.get("token");
92+
var start = (int) requireNonNull(expectation.get("start"));
93+
var end = (int) requireNonNull(expectation.get("end"));
9794
if (token != null) {
9895
return new Node(type, start, end, token);
9996
} else {
100-
return new Node(type, start, end, requireNonNull(nodes).stream().map(YamlableNode::toNode).collect(Collectors.toList()));
97+
return new Node(type, start, end, nodes);
10198
}
10299
}
100+
101+
@SuppressWarnings("unchecked")
102+
private List<Map<String, ?>> getNodes(Map<String, ?> expectation) {
103+
var nodes = expectation.get("nodes");
104+
return nodes != null ? (List<Map<String, ?>>) nodes : Collections.emptyList();
105+
}
106+
}
107+
108+
record Expectation(String expression, @Nullable Node expectedAst, @Nullable String exception) {
109+
103110
}
111+
104112
}

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

Lines changed: 24 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@
1919
import java.util.ArrayList;
2020
import java.util.Comparator;
2121
import java.util.List;
22+
import java.util.Map;
2223
import java.util.stream.Collectors;
2324

2425
import static java.nio.file.Files.newDirectoryStream;
2526
import static java.nio.file.Files.newInputStream;
27+
import static java.util.Objects.requireNonNull;
2628
import static org.hamcrest.MatcherAssert.assertThat;
2729
import static org.hamcrest.Matchers.is;
2830
import static org.junit.jupiter.api.Assertions.assertThrows;
@@ -33,7 +35,7 @@ class CucumberExpressionTokenizerTest {
3335

3436
static List<Path> acceptance_tests_pass() throws IOException {
3537
List<Path> paths = new ArrayList<>();
36-
try(DirectoryStream<Path> directories = newDirectoryStream(Paths.get("..", "testdata", "cucumber-expression", "tokenizer"))) {
38+
try (DirectoryStream<Path> directories = newDirectoryStream(Paths.get("..", "testdata", "cucumber-expression", "tokenizer"))) {
3739
directories.forEach(paths::add);
3840
}
3941
paths.sort(Comparator.naturalOrder());
@@ -45,11 +47,7 @@ static List<Path> acceptance_tests_pass() throws IOException {
4547
void acceptance_tests_pass(@ConvertWith(Converter.class) Expectation expectation) {
4648
if (expectation.exception == null) {
4749
List<Token> tokens = tokenizer.tokenize(expectation.expression);
48-
List<Token> expectedTokens = expectation.expectedTokens
49-
.stream()
50-
.map(YamlableToken::toToken)
51-
.collect(Collectors.toList());
52-
assertThat(tokens, is(expectedTokens));
50+
assertThat(tokens, is(expectation.expectedTokens));
5351
} else {
5452
CucumberExpressionException exception = assertThrows(
5553
CucumberExpressionException.class,
@@ -58,18 +56,6 @@ void acceptance_tests_pass(@ConvertWith(Converter.class) Expectation expectation
5856
}
5957
}
6058

61-
static class Expectation {
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-
}
71-
}
72-
7359
@NullMarked
7460
static class Converter implements ArgumentConverter {
7561
Yaml yaml = new Yaml();
@@ -82,28 +68,33 @@ public Expectation convert(@Nullable Object source, ParameterContext context) th
8268
try {
8369
Path path = (Path) source;
8470
InputStream inputStream = newInputStream(path);
85-
return yaml.loadAs(inputStream, Expectation.class);
71+
Map<String, ?> expectation = yaml.loadAs(inputStream, Map.class);
72+
return new Expectation(
73+
(String) requireNonNull(expectation.get("expression")),
74+
convertExpectedTokens(expectation),
75+
(String) expectation.get("exception")
76+
);
8677
} catch (IOException e) {
8778
throw new ArgumentConversionException("Could not load " + source, e);
8879
}
8980
}
90-
}
9181

92-
static class YamlableToken {
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;
82+
@SuppressWarnings("unchecked")
83+
private @Nullable List<Token> convertExpectedTokens(Map<String, ?> expectation) {
84+
var expectedAst = (List<Map<String, ?>>) expectation.get("expected_tokens");
85+
return expectedAst == null ? null : expectedAst.stream().map(this::convertToken).collect(Collectors.toList());
10386
}
10487

105-
public Token toToken() {
106-
return new Token(text, type, start, end);
88+
private Token convertToken(Map<String, ?> expectation) {
89+
var token = (String) requireNonNull(expectation.get("text"));
90+
var type = Token.Type.valueOf((String) requireNonNull(expectation.get("type")));
91+
var start = (int) requireNonNull(expectation.get("start"));
92+
var end = (int) requireNonNull(expectation.get("end"));
93+
return new Token(token, type, start, end);
10794
}
95+
96+
}
97+
98+
record Expectation(String expression, @Nullable List<Token> expectedTokens, @Nullable String exception) {
10899
}
109100
}

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

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@
1919
import java.util.Comparator;
2020
import java.util.List;
2121
import java.util.Locale;
22+
import java.util.Map;
2223

2324
import static java.nio.file.Files.newDirectoryStream;
2425
import static java.nio.file.Files.newInputStream;
26+
import static java.util.Objects.requireNonNull;
2527
import static org.junit.jupiter.api.Assertions.assertEquals;
2628

2729
class CucumberExpressionTransformationTest {
@@ -40,17 +42,10 @@ static List<Path> acceptance_tests_pass() throws IOException {
4042
@MethodSource
4143
void acceptance_tests_pass(@ConvertWith(Converter.class) Expectation expectation) {
4244
CucumberExpression expression = new CucumberExpression(expectation.expression, parameterTypeRegistry);
43-
assertEquals(expectation.expected_regex, expression.getRegexp().pattern());
45+
assertEquals(expectation.expectedRegex, expression.getRegexp().pattern());
4446
}
4547

46-
static class Expectation {
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-
}
48+
record Expectation(String expression, String expectedRegex) {
5449
}
5550

5651
@NullMarked
@@ -65,7 +60,10 @@ public Expectation convert(@Nullable Object source, ParameterContext context) th
6560
try {
6661
Path path = (Path) source;
6762
InputStream inputStream = newInputStream(path);
68-
return yaml.loadAs(inputStream, Expectation.class);
63+
Map<String, ?> expectation = yaml.loadAs(inputStream, Map.class);
64+
return new Expectation(
65+
(String) requireNonNull(expectation.get("expression")),
66+
(String) requireNonNull(expectation.get("expected_regex")));
6967
} catch (IOException e) {
7068
throw new ArgumentConversionException("Could not load " + source, e);
7169
}

0 commit comments

Comments
 (0)