From da443020e04fc58091489f51ddbcb0925d4b535b Mon Sep 17 00:00:00 2001 From: rstoyanchev Date: Wed, 30 Jul 2025 10:36:00 +0100 Subject: [PATCH] Rename AnnotationHttpServiceRegistrar Align with the name of the import annotation. In preparation of adding a client annotation with another registrar. See gh-35244 --- .../service/registry/HttpServiceGroup.java | 2 +- .../service/registry/ImportHttpServices.java | 4 +- ....java => ImportHttpServicesRegistrar.java} | 2 +- ... => ImportHttpServicesRegistrarTests.java} | 110 ++---------------- .../web/service/registry/TestGroup.java | 56 +++++++++ .../service/registry/TestGroupRegistry.java | 79 +++++++++++++ 6 files changed, 151 insertions(+), 102 deletions(-) rename spring-web/src/main/java/org/springframework/web/service/registry/{AnnotationHttpServiceRegistrar.java => ImportHttpServicesRegistrar.java} (96%) rename spring-web/src/test/java/org/springframework/web/service/registry/{AnnotationHttpServiceRegistrarTests.java => ImportHttpServicesRegistrarTests.java} (62%) create mode 100644 spring-web/src/test/java/org/springframework/web/service/registry/TestGroup.java create mode 100644 spring-web/src/test/java/org/springframework/web/service/registry/TestGroupRegistry.java diff --git a/spring-web/src/main/java/org/springframework/web/service/registry/HttpServiceGroup.java b/spring-web/src/main/java/org/springframework/web/service/registry/HttpServiceGroup.java index acab4ffb924..27a115663ea 100644 --- a/spring-web/src/main/java/org/springframework/web/service/registry/HttpServiceGroup.java +++ b/spring-web/src/main/java/org/springframework/web/service/registry/HttpServiceGroup.java @@ -30,7 +30,7 @@ import java.util.Set; public interface HttpServiceGroup { /** - * The name of the group to add HTTP Services to when a group isn't specified. + * The name of the default group to add HTTP Services to when a group is not specified. */ String DEFAULT_GROUP_NAME = "default"; diff --git a/spring-web/src/main/java/org/springframework/web/service/registry/ImportHttpServices.java b/spring-web/src/main/java/org/springframework/web/service/registry/ImportHttpServices.java index fe4b3a9e471..3785f6834df 100644 --- a/spring-web/src/main/java/org/springframework/web/service/registry/ImportHttpServices.java +++ b/spring-web/src/main/java/org/springframework/web/service/registry/ImportHttpServices.java @@ -53,7 +53,7 @@ import org.springframework.web.service.annotation.HttpExchange; @Retention(RetentionPolicy.RUNTIME) @Documented @Repeatable(ImportHttpServices.Container.class) -@Import(AnnotationHttpServiceRegistrar.class) +@Import(ImportHttpServicesRegistrar.class) public @interface ImportHttpServices { /** @@ -106,7 +106,7 @@ public @interface ImportHttpServices { @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented - @Import(AnnotationHttpServiceRegistrar.class) + @Import(ImportHttpServicesRegistrar.class) @interface Container { ImportHttpServices[] value(); diff --git a/spring-web/src/main/java/org/springframework/web/service/registry/AnnotationHttpServiceRegistrar.java b/spring-web/src/main/java/org/springframework/web/service/registry/ImportHttpServicesRegistrar.java similarity index 96% rename from spring-web/src/main/java/org/springframework/web/service/registry/AnnotationHttpServiceRegistrar.java rename to spring-web/src/main/java/org/springframework/web/service/registry/ImportHttpServicesRegistrar.java index 8cc29cc04d7..c23845a3784 100644 --- a/spring-web/src/main/java/org/springframework/web/service/registry/AnnotationHttpServiceRegistrar.java +++ b/spring-web/src/main/java/org/springframework/web/service/registry/ImportHttpServicesRegistrar.java @@ -29,7 +29,7 @@ import org.springframework.core.type.AnnotationMetadata; * @author Olga Maciaszek-Sharma * @since 7.0 */ -class AnnotationHttpServiceRegistrar extends AbstractHttpServiceRegistrar { +class ImportHttpServicesRegistrar extends AbstractHttpServiceRegistrar { @Override protected void registerHttpServices(GroupRegistry registry, AnnotationMetadata metadata) { diff --git a/spring-web/src/test/java/org/springframework/web/service/registry/AnnotationHttpServiceRegistrarTests.java b/spring-web/src/test/java/org/springframework/web/service/registry/ImportHttpServicesRegistrarTests.java similarity index 62% rename from spring-web/src/test/java/org/springframework/web/service/registry/AnnotationHttpServiceRegistrarTests.java rename to spring-web/src/test/java/org/springframework/web/service/registry/ImportHttpServicesRegistrarTests.java index 84188de3804..d3f45d3c3ff 100644 --- a/spring-web/src/test/java/org/springframework/web/service/registry/AnnotationHttpServiceRegistrarTests.java +++ b/spring-web/src/test/java/org/springframework/web/service/registry/ImportHttpServicesRegistrarTests.java @@ -16,11 +16,7 @@ package org.springframework.web.service.registry; -import java.util.Arrays; -import java.util.LinkedHashMap; -import java.util.LinkedHashSet; import java.util.Map; -import java.util.Set; import java.util.function.BiConsumer; import org.junit.jupiter.api.Test; @@ -43,12 +39,12 @@ import org.springframework.web.service.registry.greeting.GreetingB; import static org.assertj.core.api.Assertions.assertThat; /** - * Tests for {@link AnnotationHttpServiceRegistrar}. + * Tests for {@link ImportHttpServicesRegistrar}. * * @author Rossen Stoyanchev * @author Stephane Nicoll */ -public class AnnotationHttpServiceRegistrarTests { +public class ImportHttpServicesRegistrarTests { private static final String ECHO_GROUP = "echo"; @@ -57,13 +53,13 @@ public class AnnotationHttpServiceRegistrarTests { private final TestGroupRegistry groupRegistry = new TestGroupRegistry(); - private final TestAnnotationHttpServiceRegistrar registrar = new TestAnnotationHttpServiceRegistrar(); + private final ImportHttpServicesRegistrar registrar = new ImportHttpServicesRegistrar(); @Test void basicListing() { doRegister(ListingConfig.class); - assertGroups(StubGroup.ofListing(ECHO_GROUP, EchoA.class, EchoB.class)); + assertGroups(TestGroup.ofListing(ECHO_GROUP, EchoA.class, EchoB.class)); } @Test @@ -83,8 +79,8 @@ public class AnnotationHttpServiceRegistrarTests { void basicScan() { doRegister(ScanConfig.class); assertGroups( - StubGroup.ofPackageClasses(ECHO_GROUP, EchoA.class), - StubGroup.ofPackageClasses(GREETING_GROUP, GreetingA.class)); + TestGroup.ofPackageClasses(ECHO_GROUP, EchoA.class), + TestGroup.ofPackageClasses(GREETING_GROUP, GreetingA.class)); } @Test @@ -105,8 +101,8 @@ public class AnnotationHttpServiceRegistrarTests { void clientType() { doRegister(ClientTypeConfig.class); assertGroups( - StubGroup.ofListing(ECHO_GROUP, ClientType.WEB_CLIENT, EchoA.class), - StubGroup.ofListing(GREETING_GROUP, ClientType.WEB_CLIENT, GreetingA.class)); + TestGroup.ofListing(ECHO_GROUP, ClientType.WEB_CLIENT, EchoA.class), + TestGroup.ofListing(GREETING_GROUP, ClientType.WEB_CLIENT, GreetingA.class)); } private void doRegister(Class configClass) { @@ -133,11 +129,11 @@ public class AnnotationHttpServiceRegistrarTests { return freshApplicationContext; } - private void assertGroups(StubGroup... expectedGroups) { - Map groupMap = this.groupRegistry.groupMap(); + private void assertGroups(TestGroup... expectedGroups) { + Map groupMap = this.groupRegistry.groupMap(); assertThat(groupMap.size()).isEqualTo(expectedGroups.length); - for (StubGroup expected : expectedGroups) { - StubGroup actual = groupMap.get(expected.name()); + for (TestGroup expected : expectedGroups) { + TestGroup actual = groupMap.get(expected.name()); assertThat(actual.httpServiceTypes()).isEqualTo(expected.httpServiceTypes()); assertThat(actual.clientType()).isEqualTo(expected.clientType()); assertThat(actual.packageNames()).isEqualTo(expected.packageNames()); @@ -159,86 +155,4 @@ public class AnnotationHttpServiceRegistrarTests { @ImportHttpServices(clientType = ClientType.WEB_CLIENT, group = GREETING_GROUP, types = { GreetingA.class }) static class ClientTypeConfig { } - - - private static class TestAnnotationHttpServiceRegistrar extends AnnotationHttpServiceRegistrar { - - @Override - public void registerHttpServices(GroupRegistry registry, AnnotationMetadata metadata) { - super.registerHttpServices(registry, metadata); - } - } - - - private static class TestGroupRegistry implements AbstractHttpServiceRegistrar.GroupRegistry { - - private final Map groupMap = new LinkedHashMap<>(); - - public Map groupMap() { - return this.groupMap; - } - - @Override - public GroupSpec forGroup(String name, ClientType clientType) { - return new TestGroupSpec(this.groupMap, name, clientType); - } - - - private record TestGroupSpec(Map groupMap, String groupName, - ClientType clientType) implements GroupSpec { - - @Override - public GroupSpec register(Class... serviceTypes) { - getOrCreateGroup().httpServiceTypes().addAll(Arrays.asList(serviceTypes)); - return this; - } - - @Override - public GroupSpec detectInBasePackages(Class... packageClasses) { - getOrCreateGroup().packageClasses().addAll(Arrays.asList(packageClasses)); - return this; - } - - @Override - public GroupSpec detectInBasePackages(String... packageNames) { - getOrCreateGroup().packageNames().addAll(Arrays.asList(packageNames)); - return this; - } - - private StubGroup getOrCreateGroup() { - return this.groupMap.computeIfAbsent(this.groupName, name -> new StubGroup(name, this.clientType)); - } - } - } - - - private record StubGroup( - String name, ClientType clientType, Set> httpServiceTypes, - Set> packageClasses, Set packageNames) implements HttpServiceGroup { - - StubGroup(String name, ClientType clientType) { - this(name, clientType, new LinkedHashSet<>(), new LinkedHashSet<>(), new LinkedHashSet<>()); - } - - public static StubGroup ofListing(String name, Class... httpServiceTypes) { - return ofListing(name, ClientType.UNSPECIFIED, httpServiceTypes); - } - - public static StubGroup ofListing(String name, ClientType clientType, Class... httpServiceTypes) { - StubGroup group = new StubGroup(name, clientType); - group.httpServiceTypes().addAll(Arrays.asList(httpServiceTypes)); - return group; - } - - public static StubGroup ofPackageClasses(String name, Class... packageClasses) { - return ofPackageClasses(name, ClientType.UNSPECIFIED, packageClasses); - } - - public static StubGroup ofPackageClasses(String name, ClientType clientType, Class... packageClasses) { - StubGroup group = new StubGroup(name, clientType); - group.packageClasses().addAll(Arrays.asList(packageClasses)); - return group; - } - } - } diff --git a/spring-web/src/test/java/org/springframework/web/service/registry/TestGroup.java b/spring-web/src/test/java/org/springframework/web/service/registry/TestGroup.java new file mode 100644 index 00000000000..d4ed645a0b1 --- /dev/null +++ b/spring-web/src/test/java/org/springframework/web/service/registry/TestGroup.java @@ -0,0 +1,56 @@ +/* + * Copyright 2002-present 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. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.web.service.registry; + + +import java.util.Arrays; +import java.util.LinkedHashSet; +import java.util.Set; + +/** + * A stub implementation of {@link HttpServiceGroup}. + * + * @author Rossen Stoyanchev + */ +record TestGroup( + String name, ClientType clientType, Set> httpServiceTypes, + Set> packageClasses, Set packageNames) implements HttpServiceGroup { + + TestGroup(String name, ClientType clientType) { + this(name, clientType, new LinkedHashSet<>(), new LinkedHashSet<>(), new LinkedHashSet<>()); + } + + public static TestGroup ofListing(String name, Class... httpServiceTypes) { + return ofListing(name, ClientType.UNSPECIFIED, httpServiceTypes); + } + + public static TestGroup ofListing(String name, ClientType clientType, Class... httpServiceTypes) { + TestGroup group = new TestGroup(name, clientType); + group.httpServiceTypes().addAll(Arrays.asList(httpServiceTypes)); + return group; + } + + public static TestGroup ofPackageClasses(String name, Class... packageClasses) { + return ofPackageClasses(name, ClientType.UNSPECIFIED, packageClasses); + } + + public static TestGroup ofPackageClasses(String name, ClientType clientType, Class... packageClasses) { + TestGroup group = new TestGroup(name, clientType); + group.packageClasses().addAll(Arrays.asList(packageClasses)); + return group; + } +} diff --git a/spring-web/src/test/java/org/springframework/web/service/registry/TestGroupRegistry.java b/spring-web/src/test/java/org/springframework/web/service/registry/TestGroupRegistry.java new file mode 100644 index 00000000000..2f059d0674a --- /dev/null +++ b/spring-web/src/test/java/org/springframework/web/service/registry/TestGroupRegistry.java @@ -0,0 +1,79 @@ +/* + * Copyright 2002-present 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. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.web.service.registry; + + +import java.util.Arrays; +import java.util.LinkedHashMap; +import java.util.Map; + +import org.springframework.util.ClassUtils; +import org.springframework.web.service.registry.AbstractHttpServiceRegistrar.GroupRegistry; + +/** + * A {@link GroupRegistry} that records the inputs given and creates {@link TestGroup}s + * + * @author Rossen Stoyanchev + */ +class TestGroupRegistry implements GroupRegistry { + + private final Map groupMap = new LinkedHashMap<>(); + + public Map groupMap() { + return this.groupMap; + } + + @Override + public GroupSpec forGroup(String name, HttpServiceGroup.ClientType clientType) { + return new TestGroupSpec(this.groupMap, name, clientType); + } + + + private record TestGroupSpec( + Map groupMap, String groupName, + HttpServiceGroup.ClientType clientType) implements GroupSpec { + + @Override + public GroupSpec register(Class... serviceTypes) { + getOrCreateGroup().httpServiceTypes().addAll(Arrays.asList(serviceTypes)); + return this; + } + + @Override + public GroupSpec registerTypeNames(String... serviceTypes) { + return register(Arrays.stream(serviceTypes) + .map(className -> ClassUtils.resolveClassName(className, getClass().getClassLoader())) + .toArray(Class[]::new)); + } + + @Override + public GroupSpec detectInBasePackages(Class... packageClasses) { + getOrCreateGroup().packageClasses().addAll(Arrays.asList(packageClasses)); + return this; + } + + @Override + public GroupSpec detectInBasePackages(String... packageNames) { + getOrCreateGroup().packageNames().addAll(Arrays.asList(packageNames)); + return this; + } + + private TestGroup getOrCreateGroup() { + return this.groupMap.computeIfAbsent(this.groupName, name -> new TestGroup(name, this.clientType)); + } + } +}