Skip to content

Commit e98321b

Browse files
committed
Prohibit unnecessary value on @EnumSource
1 parent 5c0a2df commit e98321b

File tree

6 files changed

+44
-14
lines changed

6 files changed

+44
-14
lines changed

buildSrc/src/main/java/org/springframework/boot/build/architecture/ArchitectureCheck.java

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,8 @@ public ArchitectureCheck() {
9494
noClassesShouldCallURLEncoderWithStringEncoding(), noClassesShouldCallURLDecoderWithStringEncoding(),
9595
noClassesShouldLoadResourcesUsingResourceUtils(), noClassesShouldCallStringToUpperCaseWithoutLocale(),
9696
noClassesShouldCallStringToLowerCaseWithoutLocale(),
97-
conditionalOnMissingBeanShouldNotSpecifyOnlyATypeThatIsTheSameAsMethodReturnType());
97+
conditionalOnMissingBeanShouldNotSpecifyOnlyATypeThatIsTheSameAsMethodReturnType(),
98+
enumSourceShouldNotSpecifyOnlyATypeThatIsTheSameAsMethodParameterType());
9899
getRules().addAll(getProhibitObjectsRequireNonNull()
99100
.map((prohibit) -> prohibit ? noClassesShouldCallObjectsRequireNonNull() : Collections.emptyList()));
100101
getRuleDescriptions().set(getRules().map((rules) -> rules.stream().map(ArchRule::getDescription).toList()));
@@ -299,6 +300,35 @@ public void check(JavaMethod item, ConditionEvents events) {
299300
};
300301
}
301302

303+
private ArchRule enumSourceShouldNotSpecifyOnlyATypeThatIsTheSameAsMethodParameterType() {
304+
return ArchRuleDefinition.methods()
305+
.that()
306+
.areAnnotatedWith("org.junit.jupiter.params.provider.EnumSource")
307+
.should(notSpecifyOnlyATypeThatIsTheSameAsTheMethodParameterType())
308+
.allowEmptyShould(true);
309+
}
310+
311+
private ArchCondition<? super JavaMethod> notSpecifyOnlyATypeThatIsTheSameAsTheMethodParameterType() {
312+
return new ArchCondition<>("not specify only a type that is the same as the method's parameter type") {
313+
314+
@Override
315+
public void check(JavaMethod item, ConditionEvents events) {
316+
JavaAnnotation<JavaMethod> conditional = item
317+
.getAnnotationOfType("org.junit.jupiter.params.provider.EnumSource");
318+
Map<String, Object> properties = conditional.getProperties();
319+
if (properties.size() == 1 && item.getParameterTypes().size() == 1) {
320+
conditional.get("value").ifPresent((value) -> {
321+
if (value.equals(item.getParameterTypes().get(0))) {
322+
events.add(SimpleConditionEvent.violated(item, conditional.getDescription()
323+
+ " should not specify only a value that is the same as the method's parameter type"));
324+
}
325+
});
326+
}
327+
}
328+
329+
};
330+
}
331+
302332
public void setClasses(FileCollection classes) {
303333
this.classes = classes;
304334
}

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/tracing/BaggagePropagationIntegrationTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ void setup() {
6161
}
6262

6363
@ParameterizedTest
64-
@EnumSource(AutoConfig.class)
64+
@EnumSource
6565
void shouldSetEntriesToMdcFromSpanWithBaggage(AutoConfig autoConfig) {
6666
autoConfig.get().run((context) -> {
6767
Tracer tracer = tracer(context);
@@ -87,7 +87,7 @@ void shouldSetEntriesToMdcFromSpanWithBaggage(AutoConfig autoConfig) {
8787
}
8888

8989
@ParameterizedTest
90-
@EnumSource(AutoConfig.class)
90+
@EnumSource
9191
void shouldRemoveEntriesFromMdcForNullSpan(AutoConfig autoConfig) {
9292
autoConfig.get().run((context) -> {
9393
Tracer tracer = tracer(context);

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/wavefront/WavefrontPropertiesTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -80,7 +80,7 @@ void wavefrontApiTokenTypeWhenNotUsingProxy() {
8080
}
8181

8282
@ParameterizedTest
83-
@EnumSource(TokenType.class)
83+
@EnumSource
8484
void wavefrontApiTokenMapping(TokenType from) {
8585
WavefrontProperties properties = new WavefrontProperties();
8686
properties.setApiTokenType(from);

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jms/AcknowledgeModeTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -32,7 +32,7 @@
3232
class AcknowledgeModeTests {
3333

3434
@ParameterizedTest
35-
@EnumSource(Mapping.class)
35+
@EnumSource
3636
void stringIsMappedToInt(Mapping mapping) {
3737
assertThat(AcknowledgeMode.of(mapping.actual)).extracting(AcknowledgeMode::getMode).isEqualTo(mapping.expected);
3838
}

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mustache/MustacheAutoConfigurationTests.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -158,7 +158,7 @@ void cacheCanBeCustomizedOnServletViewResolver() {
158158
}
159159

160160
@ParameterizedTest
161-
@EnumSource(ViewResolverKind.class)
161+
@EnumSource
162162
void charsetCanBeCustomizedOnViewResolver(ViewResolverKind kind) {
163163
assertViewResolverProperty(kind, "spring.mustache.charset=UTF-16", "charset", "UTF-16");
164164
}
@@ -182,21 +182,21 @@ void exposeSpringMacroHelpersCanBeCustomizedOnServletViewResolver() {
182182
}
183183

184184
@ParameterizedTest
185-
@EnumSource(ViewResolverKind.class)
185+
@EnumSource
186186
void prefixCanBeCustomizedOnViewResolver(ViewResolverKind kind) {
187187
assertViewResolverProperty(kind, "spring.mustache.prefix=classpath:/mustache-templates/", "prefix",
188188
"classpath:/mustache-templates/");
189189
}
190190

191191
@ParameterizedTest
192-
@EnumSource(ViewResolverKind.class)
192+
@EnumSource
193193
void requestContextAttributeCanBeCustomizedOnViewResolver(ViewResolverKind kind) {
194194
assertViewResolverProperty(kind, "spring.mustache.request-context-attribute=test", "requestContextAttribute",
195195
"test");
196196
}
197197

198198
@ParameterizedTest
199-
@EnumSource(ViewResolverKind.class)
199+
@EnumSource
200200
void suffixCanBeCustomizedOnViewResolver(ViewResolverKind kind) {
201201
assertViewResolverProperty(kind, "spring.mustache.suffix=.tache", "suffix", ".tache");
202202
}

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/servlet/server/AbstractServletWebServerFactoryTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -871,7 +871,7 @@ void sessionCookieConfiguration() {
871871
}
872872

873873
@ParameterizedTest
874-
@EnumSource(SameSite.class)
874+
@EnumSource
875875
void sessionCookieSameSiteAttributeCanBeConfiguredAndOnlyAffectsSessionCookies(SameSite sameSite) throws Exception {
876876
AbstractServletWebServerFactory factory = getFactory();
877877
factory.getSession().getCookie().setSameSite(sameSite);
@@ -886,7 +886,7 @@ void sessionCookieSameSiteAttributeCanBeConfiguredAndOnlyAffectsSessionCookies(S
886886
}
887887

888888
@ParameterizedTest
889-
@EnumSource(SameSite.class)
889+
@EnumSource
890890
void sessionCookieSameSiteAttributeCanBeConfiguredAndOnlyAffectsSessionCookiesWhenUsingCustomName(SameSite sameSite)
891891
throws Exception {
892892
AbstractServletWebServerFactory factory = getFactory();

0 commit comments

Comments
 (0)