From 6ad48639c2761230de802644e1b613ac75c47105 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Fri, 28 Mar 2025 16:48:50 +0000 Subject: [PATCH] Back port 246c4a2 to 3.3.x The changes do not apply cleanly when cherry-picked so they have been redone manually. Closes gh-44941 --- .../build/architecture/ArchitectureRules.java | 29 +++++++++++++++++++ .../BaggagePropagationIntegrationTests.java | 6 ++-- .../wavefront/WavefrontPropertiesTests.java | 4 +-- .../jms/AcknowledgeModeTests.java | 4 +-- .../MustacheAutoConfigurationTests.java | 8 ++--- .../AbstractServletWebServerFactoryTests.java | 4 +-- 6 files changed, 42 insertions(+), 13 deletions(-) diff --git a/buildSrc/src/main/java/org/springframework/boot/build/architecture/ArchitectureRules.java b/buildSrc/src/main/java/org/springframework/boot/build/architecture/ArchitectureRules.java index a52bc400c25..a9ba9bd1b36 100644 --- a/buildSrc/src/main/java/org/springframework/boot/build/architecture/ArchitectureRules.java +++ b/buildSrc/src/main/java/org/springframework/boot/build/architecture/ArchitectureRules.java @@ -87,6 +87,7 @@ final class ArchitectureRules { rules.add(noClassesShouldCallStringToUpperCaseWithoutLocale()); rules.add(noClassesShouldCallStringToLowerCaseWithoutLocale()); rules.add(conditionalOnMissingBeanShouldNotSpecifyOnlyATypeThatIsTheSameAsMethodReturnType()); + rules.add(enumSourceShouldNotSpecifyOnlyATypeThatIsTheSameAsMethodParameterType()); return List.copyOf(rules); } @@ -201,6 +202,34 @@ final class ArchitectureRules { }); } + private static ArchRule enumSourceShouldNotSpecifyOnlyATypeThatIsTheSameAsMethodParameterType() { + return ArchRuleDefinition.methods() + .that() + .areAnnotatedWith("org.junit.jupiter.params.provider.EnumSource") + .should(notSpecifyOnlyATypeThatIsTheSameAsTheMethodParameterType()) + .allowEmptyShould(true); + } + + private static ArchCondition notSpecifyOnlyATypeThatIsTheSameAsTheMethodParameterType() { + return check("not specify only a type that is the same as the method's parameter type", + ArchitectureRules::notSpecifyOnlyATypeThatIsTheSameAsTheMethodParameterType); + } + + private static void notSpecifyOnlyATypeThatIsTheSameAsTheMethodParameterType(JavaMethod item, + ConditionEvents events) { + JavaAnnotation enumSourceAnnotation = item + .getAnnotationOfType("org.junit.jupiter.params.provider.EnumSource"); + Map properties = enumSourceAnnotation.getProperties(); + if (properties.size() == 1 && item.getParameterTypes().size() == 1) { + enumSourceAnnotation.get("value").ifPresent((value) -> { + if (value.equals(item.getParameterTypes().get(0))) { + addViolation(events, item, enumSourceAnnotation.getDescription() + + " should not specify only a value that is the same as the method's parameter type"); + } + }); + } + } + private static boolean containsOnlySingleType(JavaType[] types, JavaType type) { return types.length == 1 && type.equals(types[0]); } diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/tracing/BaggagePropagationIntegrationTests.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/tracing/BaggagePropagationIntegrationTests.java index 50af8b2b0d7..8d1593be1df 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/tracing/BaggagePropagationIntegrationTests.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/tracing/BaggagePropagationIntegrationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2024 the original author or authors. + * Copyright 2012-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -57,7 +57,7 @@ class BaggagePropagationIntegrationTests { } @ParameterizedTest - @EnumSource(AutoConfig.class) + @EnumSource void shouldSetEntriesToMdcFromSpanWithBaggage(AutoConfig autoConfig) { autoConfig.get().run((context) -> { Tracer tracer = tracer(context); @@ -83,7 +83,7 @@ class BaggagePropagationIntegrationTests { } @ParameterizedTest - @EnumSource(AutoConfig.class) + @EnumSource void shouldRemoveEntriesFromMdcForNullSpan(AutoConfig autoConfig) { autoConfig.get().run((context) -> { Tracer tracer = tracer(context); diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/wavefront/WavefrontPropertiesTests.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/wavefront/WavefrontPropertiesTests.java index b9aaca91439..403a949f595 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/wavefront/WavefrontPropertiesTests.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/wavefront/WavefrontPropertiesTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2023 the original author or authors. + * Copyright 2012-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -80,7 +80,7 @@ class WavefrontPropertiesTests { } @ParameterizedTest - @EnumSource(TokenType.class) + @EnumSource void wavefrontApiTokenMapping(TokenType from) { WavefrontProperties properties = new WavefrontProperties(); properties.setApiTokenType(from); diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jms/AcknowledgeModeTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jms/AcknowledgeModeTests.java index 77957f5a967..bdfba2ef242 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jms/AcknowledgeModeTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jms/AcknowledgeModeTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2023 the original author or authors. + * Copyright 2012-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -32,7 +32,7 @@ import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException class AcknowledgeModeTests { @ParameterizedTest - @EnumSource(Mapping.class) + @EnumSource void stringIsMappedToInt(Mapping mapping) { assertThat(AcknowledgeMode.of(mapping.actual)).extracting(AcknowledgeMode::getMode).isEqualTo(mapping.expected); } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mustache/MustacheAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mustache/MustacheAutoConfigurationTests.java index 7c2075e34a2..125bce1ef88 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mustache/MustacheAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mustache/MustacheAutoConfigurationTests.java @@ -159,7 +159,7 @@ class MustacheAutoConfigurationTests { } @ParameterizedTest - @EnumSource(ViewResolverKind.class) + @EnumSource void charsetCanBeCustomizedOnViewResolver(ViewResolverKind kind) { assertViewResolverProperty(kind, "spring.mustache.charset=UTF-16", "charset", "UTF-16"); if (kind == ViewResolverKind.SERVLET) { @@ -187,21 +187,21 @@ class MustacheAutoConfigurationTests { } @ParameterizedTest - @EnumSource(ViewResolverKind.class) + @EnumSource void prefixCanBeCustomizedOnViewResolver(ViewResolverKind kind) { assertViewResolverProperty(kind, "spring.mustache.prefix=classpath:/mustache-templates/", "prefix", "classpath:/mustache-templates/"); } @ParameterizedTest - @EnumSource(ViewResolverKind.class) + @EnumSource void requestContextAttributeCanBeCustomizedOnViewResolver(ViewResolverKind kind) { assertViewResolverProperty(kind, "spring.mustache.request-context-attribute=test", "requestContextAttribute", "test"); } @ParameterizedTest - @EnumSource(ViewResolverKind.class) + @EnumSource void suffixCanBeCustomizedOnViewResolver(ViewResolverKind kind) { assertViewResolverProperty(kind, "spring.mustache.suffix=.tache", "suffix", ".tache"); } diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/servlet/server/AbstractServletWebServerFactoryTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/servlet/server/AbstractServletWebServerFactoryTests.java index c3eb610bc37..fede1b5926d 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/servlet/server/AbstractServletWebServerFactoryTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/servlet/server/AbstractServletWebServerFactoryTests.java @@ -885,7 +885,7 @@ public abstract class AbstractServletWebServerFactoryTests { } @ParameterizedTest - @EnumSource(SameSite.class) + @EnumSource void sessionCookieSameSiteAttributeCanBeConfiguredAndOnlyAffectsSessionCookies(SameSite sameSite) throws Exception { AbstractServletWebServerFactory factory = getFactory(); factory.getSession().getCookie().setSameSite(sameSite); @@ -900,7 +900,7 @@ public abstract class AbstractServletWebServerFactoryTests { } @ParameterizedTest - @EnumSource(SameSite.class) + @EnumSource void sessionCookieSameSiteAttributeCanBeConfiguredAndOnlyAffectsSessionCookiesWhenUsingCustomName(SameSite sameSite) throws Exception { AbstractServletWebServerFactory factory = getFactory();