Skip to content

Commit b556df1

Browse files
committed
Use Java 17
1 parent eef53c6 commit b556df1

File tree

9 files changed

+55
-54
lines changed

9 files changed

+55
-54
lines changed

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

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

33
import org.apiguardian.api.API;
4+
import org.jspecify.annotations.Nullable;
45

56
import java.text.Collator;
67
import java.util.ArrayList;
@@ -34,7 +35,7 @@ public class GeneratedExpression {
3435
this.parameterTypes = parameterTypes;
3536
}
3637

37-
private static boolean isJavaKeyword(String keyword) {
38+
private static boolean isJavaKeyword(@Nullable String keyword) {
3839
return (Arrays.binarySearch(JAVA_KEYWORDS, keyword, ENGLISH_COLLATOR) >= 0);
3940
}
4041

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

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,13 @@ static boolean isValidParameterTypeName(String name) {
4040
}
4141

4242
static ParameterType<Object> createAnonymousParameterType(String regexp) {
43-
return new ParameterType<>("", singletonList(regexp), Object.class, new CaptureGroupTransformer<Object>() {
44-
45-
@Override
46-
public Object transform(String[] arg) {
47-
throw new UnsupportedOperationException("Anonymous transform must be deanonymized before use");
48-
}
43+
return new ParameterType<>("", singletonList(regexp), Object.class, arg -> {
44+
throw new UnsupportedOperationException("Anonymous transform must be deanonymized before use");
4945
}, false, true, false, true);
5046
}
5147

52-
@SuppressWarnings("unchecked")
53-
static <E extends Enum> ParameterType<E> fromEnum(final Class<E> enumClass) {
54-
Enum[] enumConstants = enumClass.getEnumConstants();
48+
static <E extends Enum<E>> ParameterType<E> fromEnum(final Class<E> enumClass) {
49+
var enumConstants = enumClass.getEnumConstants();
5550
StringBuilder regexpBuilder = new StringBuilder();
5651
for (int i = 0; i < enumConstants.length; i++) {
5752
if (i > 0)
@@ -62,7 +57,7 @@ static <E extends Enum> ParameterType<E> fromEnum(final Class<E> enumClass) {
6257
enumClass.getSimpleName(),
6358
regexpBuilder.toString(),
6459
enumClass,
65-
(@Nullable String arg) -> arg == null ? null : (E) Enum.valueOf(enumClass, arg)
60+
(@Nullable String arg) -> arg == null ? null : Enum.valueOf(enumClass, arg)
6661
);
6762
}
6863

@@ -210,7 +205,7 @@ public boolean preferForRegexpMatch() {
210205
}
211206

212207
/**
213-
* Indicates whether or not this is a parameter type should be used for generating
208+
* Indicates whether this is a parameter type should be used for generating
214209
* {@link GeneratedExpression}s from text. Typically, parameter types with greedy regexps
215210
* should return false.
216211
*
@@ -248,13 +243,13 @@ T transform(List<String> groupValues) {
248243
if (transformer instanceof TransformerAdaptor) {
249244
if (groupValues.size() > 1) {
250245
if (isAnonymous()) {
251-
throw new CucumberExpressionException(String.format("" +
246+
throw new CucumberExpressionException(String.format(
252247
"Anonymous ParameterType has multiple capture groups %s. " +
253-
"You can only use a single capture group in an anonymous ParameterType.", regexps));
248+
"You can only use a single capture group in an anonymous ParameterType.", regexps));
254249
}
255-
throw new CucumberExpressionException(String.format("" +
250+
throw new CucumberExpressionException(String.format(
256251
"ParameterType {%s} was registered with a Transformer but has multiple capture groups %s. " +
257-
"Did you mean to use a CaptureGroupTransformer?", name, regexps));
252+
"Did you mean to use a CaptureGroupTransformer?", name, regexps));
258253
}
259254
}
260255

@@ -296,7 +291,7 @@ private TransformerAdaptor(Transformer<T> transformer) {
296291
}
297292

298293
@Override
299-
public @Nullable T transform(String[] args) throws Throwable {
294+
public @Nullable T transform(@Nullable String[] args) throws Throwable {
300295
return transformer.transform(args[0]);
301296
}
302297

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,8 @@
1515
import static java.util.regex.Pattern.compile;
1616
import static org.assertj.core.api.Assertions.assertThat;
1717
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
18-
import static org.junit.jupiter.api.Assertions.assertEquals;
1918
import static org.junit.jupiter.api.Assertions.assertNotNull;
2019
import static org.junit.jupiter.api.Assertions.assertThrows;
21-
import static org.junit.jupiter.api.Assertions.fail;
2220

2321
class CustomParameterTypeTest {
2422

@@ -226,7 +224,7 @@ void conflicting_parameter_type_is_not_detected_for_regexp() {
226224
void matches_RegularExpression_arguments_with_custom_parameter_type_without_name() {
227225
parameterTypeRegistry = new ParameterTypeRegistry(Locale.ENGLISH);
228226
parameterTypeRegistry.defineParameterType(new ParameterType<>(
229-
null,
227+
"null",
230228
"red|blue|yellow",
231229
Color.class,
232230
(@Nullable String name) -> new Color(requireNonNull(name)),

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@
22

33
import org.junit.jupiter.api.Test;
44

5-
import java.util.List;
65
import java.util.Locale;
76

8-
import static org.junit.jupiter.api.Assertions.assertEquals;
7+
import static org.assertj.core.api.Assertions.assertThat;
98

10-
public class EnumParameterTypeTest {
9+
class EnumParameterTypeTest {
1110

1211
public enum Mood {
1312
happy,
@@ -16,13 +15,15 @@ public enum Mood {
1615
}
1716

1817
@Test
19-
public void converts_to_enum() {
20-
ParameterTypeRegistry registry = new ParameterTypeRegistry(Locale.ENGLISH);
18+
void converts_to_enum() {
19+
var registry = new ParameterTypeRegistry(Locale.ENGLISH);
2120
registry.defineParameterType(ParameterType.fromEnum(Mood.class));
2221

23-
CucumberExpression expression = new CucumberExpression("I am {Mood}", registry);
24-
List<Argument<?>> args = expression.match("I am happy");
25-
assertEquals(Mood.happy, args.get(0).getValue());
22+
var expression = new CucumberExpression("I am {Mood}", registry);
23+
var args = expression.match("I am happy");
24+
assertThat(args).singleElement()
25+
.extracting(Argument::getValue)
26+
.isEqualTo(Mood.happy);
2627
}
2728

2829
}
Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
package io.cucumber.cucumberexpressions;
22

3+
import org.assertj.core.api.Assertions;
4+
import org.jspecify.annotations.Nullable;
35
import org.junit.jupiter.api.Test;
46

7+
import java.lang.reflect.Type;
58
import java.util.List;
69
import java.util.Locale;
10+
import java.util.Objects;
711

812
import static java.util.Arrays.asList;
913
import static java.util.Collections.singletonList;
14+
import static java.util.Objects.requireNonNull;
15+
import static org.assertj.core.api.Assertions.assertThat;
1016
import static org.junit.jupiter.api.Assertions.assertEquals;
1117

1218
public class GenericParameterTypeTest {
@@ -17,20 +23,16 @@ public void transforms_to_a_list_of_string() {
1723
parameterTypeRegistry.defineParameterType(new ParameterType<>(
1824
"stringlist",
1925
singletonList(".*"),
20-
new TypeReference<List<String>>() {
21-
}.getType(),
22-
new CaptureGroupTransformer<List<String>>() {
23-
@Override
24-
public List<String> transform(String... args) {
25-
return asList(args[0].split(","));
26-
}
27-
},
26+
new TypeReference<List<String>>() {}.getType(),
27+
(@Nullable String arg) -> requireNonNull(arg).split(","),
2828
false,
2929
false)
3030
);
31-
Expression expression = new CucumberExpression("I have {stringlist} yay", parameterTypeRegistry);
32-
List<Argument<?>> args = expression.match("I have three,blind,mice yay");
33-
assertEquals(asList("three", "blind", "mice"), args.get(0).getValue());
31+
var expression = new CucumberExpression("I have {stringlist} yay", parameterTypeRegistry);
32+
var args = expression.match("I have three,blind,mice yay");
33+
assertThat(args).singleElement()
34+
.extracting(Argument::getValue)
35+
.isEqualTo(asList("three", "blind", "mice"));
3436
}
3537

3638
}

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,11 @@ void can_parse_float() {
2929

3030
@Test
3131
void can_parse_double() {
32-
assertEquals(1042.000000000000002, english.parseDouble("1,042.000000000000002"), 0);
33-
assertEquals(1042.000000000000002, canadian.parseDouble("1,042.000000000000002"), 0);
34-
35-
assertEquals(1042.000000000000002, german.parseDouble("1.042,000000000000002"), 0);
36-
assertEquals(1042.000000000000002, canadianFrench.parseDouble("1.042,000000000000002"), 0);
37-
assertEquals(1042.000000000000002, norwegian.parseDouble("1.042,000000000000002"), 0);
32+
assertEquals(Double.parseDouble("1042.000000000000002"), english.parseDouble("1,042.000000000000002"), 0);
33+
assertEquals(Double.parseDouble("1042.000000000000002"), canadian.parseDouble("1,042.000000000000002"), 0);
34+
assertEquals(Double.parseDouble("1042.000000000000002"), german.parseDouble("1.042,000000000000002"), 0);
35+
assertEquals(Double.parseDouble("1042.000000000000002"), canadianFrench.parseDouble("1.042,000000000000002"), 0);
36+
assertEquals(Double.parseDouble("1042.000000000000002"), norwegian.parseDouble("1.042,000000000000002"), 0);
3837
}
3938

4039
@Test

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import com.fasterxml.jackson.databind.ObjectMapper;
44
import com.fasterxml.jackson.databind.type.TypeFactory;
55
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
6+
import org.jspecify.annotations.NullMarked;
7+
import org.jspecify.annotations.Nullable;
68
import org.junit.jupiter.params.ParameterizedTest;
79
import org.junit.jupiter.params.provider.MethodSource;
810

@@ -140,6 +142,7 @@ public void should_convert_to_enum(final ParameterByTypeTransformer defaultTrans
140142
assertEquals(TestEnum.TEST, defaultTransformer.transform("TEST", TestEnum.class));
141143
}
142144

145+
@NullMarked
143146
private static class TestJacksonDefaultTransformer implements ParameterByTypeTransformer {
144147
ObjectMapper delegate = initMapper();
145148

@@ -150,7 +153,7 @@ private static ObjectMapper initMapper() {
150153
}
151154

152155
@Override
153-
public Object transform(String fromValue, Type toValueType) {
156+
public Object transform(@Nullable String fromValue, Type toValueType) {
154157
TypeFactory typeFactory = delegate.getTypeFactory();
155158
return delegate.convertValue(fromValue, typeFactory.constructType(toValueType));
156159
}

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

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

33
import org.junit.jupiter.api.Test;
4+
import org.junit.jupiter.params.shadow.de.siegmar.fastcsv.util.Nullable;
45

56
import java.util.ArrayList;
67
import java.util.List;
@@ -14,25 +15,25 @@
1415
public class ParameterTypeComparatorTest {
1516

1617
public static class A {
17-
A(String s) {
18+
A(@Nullable String s) {
1819
assertNotNull(s);
1920
}
2021
}
2122

2223
public static class B {
23-
B(String s) {
24+
B(@Nullable String s) {
2425
assertNotNull(s);
2526
}
2627
}
2728

2829
public static class C {
29-
C(String s) {
30+
C(@Nullable String s) {
3031
assertNotNull(s);
3132
}
3233
}
3334

3435
public static class D {
35-
D(String s) {
36+
D(@Nullable String s) {
3637
assertNotNull(s);
3738
}
3839
}
@@ -46,7 +47,7 @@ public void sorts_parameter_types_by_preferential_then_name() {
4647
set.add(new ParameterType<>("b", "b", B.class, B::new, false, true));
4748

4849
List<String> names = new ArrayList<>();
49-
for (ParameterType parameterType : set) {
50+
for (ParameterType<?> parameterType : set) {
5051
names.add(parameterType.getName());
5152
}
5253
assertEquals(asList("b", "c", "a", "d"), names);

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import org.junit.jupiter.api.Test;
44
import org.junit.jupiter.api.function.Executable;
5+
import org.junit.jupiter.params.shadow.de.siegmar.fastcsv.util.Nullable;
56

67
import java.math.BigDecimal;
78
import java.util.Locale;
@@ -20,19 +21,19 @@ public class ParameterTypeRegistryTest {
2021
private static final String CAPITALISED_WORD = "[A-Z]+\\w+";
2122

2223
public static class Name {
23-
Name(String s) {
24+
Name(@Nullable String s) {
2425
assertNotNull(s);
2526
}
2627
}
2728

2829
public static class Person {
29-
Person(String s) {
30+
Person(@Nullable String s) {
3031
assertNotNull(s);
3132
}
3233
}
3334

3435
public static class Place {
35-
Place(String s) {
36+
Place(@Nullable String s) {
3637
assertNotNull(s);
3738
}
3839
}

0 commit comments

Comments
 (0)