Skip to content

Commit da44302

Browse files
committed
Rename AnnotationHttpServiceRegistrar
Align with the name of the import annotation. In preparation of adding a client annotation with another registrar. See gh-35244
1 parent 4ae5d0d commit da44302

File tree

6 files changed

+151
-102
lines changed

6 files changed

+151
-102
lines changed

spring-web/src/main/java/org/springframework/web/service/registry/HttpServiceGroup.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
public interface HttpServiceGroup {
3131

3232
/**
33-
* The name of the group to add HTTP Services to when a group isn't specified.
33+
* The name of the default group to add HTTP Services to when a group is not specified.
3434
*/
3535
String DEFAULT_GROUP_NAME = "default";
3636

spring-web/src/main/java/org/springframework/web/service/registry/ImportHttpServices.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
@Retention(RetentionPolicy.RUNTIME)
5454
@Documented
5555
@Repeatable(ImportHttpServices.Container.class)
56-
@Import(AnnotationHttpServiceRegistrar.class)
56+
@Import(ImportHttpServicesRegistrar.class)
5757
public @interface ImportHttpServices {
5858

5959
/**
@@ -106,7 +106,7 @@
106106
@Target(ElementType.TYPE)
107107
@Retention(RetentionPolicy.RUNTIME)
108108
@Documented
109-
@Import(AnnotationHttpServiceRegistrar.class)
109+
@Import(ImportHttpServicesRegistrar.class)
110110
@interface Container {
111111

112112
ImportHttpServices[] value();
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
* @author Olga Maciaszek-Sharma
3030
* @since 7.0
3131
*/
32-
class AnnotationHttpServiceRegistrar extends AbstractHttpServiceRegistrar {
32+
class ImportHttpServicesRegistrar extends AbstractHttpServiceRegistrar {
3333

3434
@Override
3535
protected void registerHttpServices(GroupRegistry registry, AnnotationMetadata metadata) {
Lines changed: 12 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,7 @@
1616

1717
package org.springframework.web.service.registry;
1818

19-
import java.util.Arrays;
20-
import java.util.LinkedHashMap;
21-
import java.util.LinkedHashSet;
2219
import java.util.Map;
23-
import java.util.Set;
2420
import java.util.function.BiConsumer;
2521

2622
import org.junit.jupiter.api.Test;
@@ -43,12 +39,12 @@
4339
import static org.assertj.core.api.Assertions.assertThat;
4440

4541
/**
46-
* Tests for {@link AnnotationHttpServiceRegistrar}.
42+
* Tests for {@link ImportHttpServicesRegistrar}.
4743
*
4844
* @author Rossen Stoyanchev
4945
* @author Stephane Nicoll
5046
*/
51-
public class AnnotationHttpServiceRegistrarTests {
47+
public class ImportHttpServicesRegistrarTests {
5248

5349
private static final String ECHO_GROUP = "echo";
5450

@@ -57,13 +53,13 @@ public class AnnotationHttpServiceRegistrarTests {
5753

5854
private final TestGroupRegistry groupRegistry = new TestGroupRegistry();
5955

60-
private final TestAnnotationHttpServiceRegistrar registrar = new TestAnnotationHttpServiceRegistrar();
56+
private final ImportHttpServicesRegistrar registrar = new ImportHttpServicesRegistrar();
6157

6258

6359
@Test
6460
void basicListing() {
6561
doRegister(ListingConfig.class);
66-
assertGroups(StubGroup.ofListing(ECHO_GROUP, EchoA.class, EchoB.class));
62+
assertGroups(TestGroup.ofListing(ECHO_GROUP, EchoA.class, EchoB.class));
6763
}
6864

6965
@Test
@@ -83,8 +79,8 @@ void basicListingWithAot() {
8379
void basicScan() {
8480
doRegister(ScanConfig.class);
8581
assertGroups(
86-
StubGroup.ofPackageClasses(ECHO_GROUP, EchoA.class),
87-
StubGroup.ofPackageClasses(GREETING_GROUP, GreetingA.class));
82+
TestGroup.ofPackageClasses(ECHO_GROUP, EchoA.class),
83+
TestGroup.ofPackageClasses(GREETING_GROUP, GreetingA.class));
8884
}
8985

9086
@Test
@@ -105,8 +101,8 @@ void basicScanWithAot() {
105101
void clientType() {
106102
doRegister(ClientTypeConfig.class);
107103
assertGroups(
108-
StubGroup.ofListing(ECHO_GROUP, ClientType.WEB_CLIENT, EchoA.class),
109-
StubGroup.ofListing(GREETING_GROUP, ClientType.WEB_CLIENT, GreetingA.class));
104+
TestGroup.ofListing(ECHO_GROUP, ClientType.WEB_CLIENT, EchoA.class),
105+
TestGroup.ofListing(GREETING_GROUP, ClientType.WEB_CLIENT, GreetingA.class));
110106
}
111107

112108
private void doRegister(Class<?> configClass) {
@@ -133,11 +129,11 @@ private GenericApplicationContext toFreshApplicationContext(
133129
return freshApplicationContext;
134130
}
135131

136-
private void assertGroups(StubGroup... expectedGroups) {
137-
Map<String, StubGroup> groupMap = this.groupRegistry.groupMap();
132+
private void assertGroups(TestGroup... expectedGroups) {
133+
Map<String, TestGroup> groupMap = this.groupRegistry.groupMap();
138134
assertThat(groupMap.size()).isEqualTo(expectedGroups.length);
139-
for (StubGroup expected : expectedGroups) {
140-
StubGroup actual = groupMap.get(expected.name());
135+
for (TestGroup expected : expectedGroups) {
136+
TestGroup actual = groupMap.get(expected.name());
141137
assertThat(actual.httpServiceTypes()).isEqualTo(expected.httpServiceTypes());
142138
assertThat(actual.clientType()).isEqualTo(expected.clientType());
143139
assertThat(actual.packageNames()).isEqualTo(expected.packageNames());
@@ -159,86 +155,4 @@ static class ScanConfig {
159155
@ImportHttpServices(clientType = ClientType.WEB_CLIENT, group = GREETING_GROUP, types = { GreetingA.class })
160156
static class ClientTypeConfig {
161157
}
162-
163-
164-
private static class TestAnnotationHttpServiceRegistrar extends AnnotationHttpServiceRegistrar {
165-
166-
@Override
167-
public void registerHttpServices(GroupRegistry registry, AnnotationMetadata metadata) {
168-
super.registerHttpServices(registry, metadata);
169-
}
170-
}
171-
172-
173-
private static class TestGroupRegistry implements AbstractHttpServiceRegistrar.GroupRegistry {
174-
175-
private final Map<String, StubGroup> groupMap = new LinkedHashMap<>();
176-
177-
public Map<String, StubGroup> groupMap() {
178-
return this.groupMap;
179-
}
180-
181-
@Override
182-
public GroupSpec forGroup(String name, ClientType clientType) {
183-
return new TestGroupSpec(this.groupMap, name, clientType);
184-
}
185-
186-
187-
private record TestGroupSpec(Map<String, StubGroup> groupMap, String groupName,
188-
ClientType clientType) implements GroupSpec {
189-
190-
@Override
191-
public GroupSpec register(Class<?>... serviceTypes) {
192-
getOrCreateGroup().httpServiceTypes().addAll(Arrays.asList(serviceTypes));
193-
return this;
194-
}
195-
196-
@Override
197-
public GroupSpec detectInBasePackages(Class<?>... packageClasses) {
198-
getOrCreateGroup().packageClasses().addAll(Arrays.asList(packageClasses));
199-
return this;
200-
}
201-
202-
@Override
203-
public GroupSpec detectInBasePackages(String... packageNames) {
204-
getOrCreateGroup().packageNames().addAll(Arrays.asList(packageNames));
205-
return this;
206-
}
207-
208-
private StubGroup getOrCreateGroup() {
209-
return this.groupMap.computeIfAbsent(this.groupName, name -> new StubGroup(name, this.clientType));
210-
}
211-
}
212-
}
213-
214-
215-
private record StubGroup(
216-
String name, ClientType clientType, Set<Class<?>> httpServiceTypes,
217-
Set<Class<?>> packageClasses, Set<String> packageNames) implements HttpServiceGroup {
218-
219-
StubGroup(String name, ClientType clientType) {
220-
this(name, clientType, new LinkedHashSet<>(), new LinkedHashSet<>(), new LinkedHashSet<>());
221-
}
222-
223-
public static StubGroup ofListing(String name, Class<?>... httpServiceTypes) {
224-
return ofListing(name, ClientType.UNSPECIFIED, httpServiceTypes);
225-
}
226-
227-
public static StubGroup ofListing(String name, ClientType clientType, Class<?>... httpServiceTypes) {
228-
StubGroup group = new StubGroup(name, clientType);
229-
group.httpServiceTypes().addAll(Arrays.asList(httpServiceTypes));
230-
return group;
231-
}
232-
233-
public static StubGroup ofPackageClasses(String name, Class<?>... packageClasses) {
234-
return ofPackageClasses(name, ClientType.UNSPECIFIED, packageClasses);
235-
}
236-
237-
public static StubGroup ofPackageClasses(String name, ClientType clientType, Class<?>... packageClasses) {
238-
StubGroup group = new StubGroup(name, clientType);
239-
group.packageClasses().addAll(Arrays.asList(packageClasses));
240-
return group;
241-
}
242-
}
243-
244158
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright 2002-present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.web.service.registry;
18+
19+
20+
import java.util.Arrays;
21+
import java.util.LinkedHashSet;
22+
import java.util.Set;
23+
24+
/**
25+
* A stub implementation of {@link HttpServiceGroup}.
26+
*
27+
* @author Rossen Stoyanchev
28+
*/
29+
record TestGroup(
30+
String name, ClientType clientType, Set<Class<?>> httpServiceTypes,
31+
Set<Class<?>> packageClasses, Set<String> packageNames) implements HttpServiceGroup {
32+
33+
TestGroup(String name, ClientType clientType) {
34+
this(name, clientType, new LinkedHashSet<>(), new LinkedHashSet<>(), new LinkedHashSet<>());
35+
}
36+
37+
public static TestGroup ofListing(String name, Class<?>... httpServiceTypes) {
38+
return ofListing(name, ClientType.UNSPECIFIED, httpServiceTypes);
39+
}
40+
41+
public static TestGroup ofListing(String name, ClientType clientType, Class<?>... httpServiceTypes) {
42+
TestGroup group = new TestGroup(name, clientType);
43+
group.httpServiceTypes().addAll(Arrays.asList(httpServiceTypes));
44+
return group;
45+
}
46+
47+
public static TestGroup ofPackageClasses(String name, Class<?>... packageClasses) {
48+
return ofPackageClasses(name, ClientType.UNSPECIFIED, packageClasses);
49+
}
50+
51+
public static TestGroup ofPackageClasses(String name, ClientType clientType, Class<?>... packageClasses) {
52+
TestGroup group = new TestGroup(name, clientType);
53+
group.packageClasses().addAll(Arrays.asList(packageClasses));
54+
return group;
55+
}
56+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* Copyright 2002-present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.web.service.registry;
18+
19+
20+
import java.util.Arrays;
21+
import java.util.LinkedHashMap;
22+
import java.util.Map;
23+
24+
import org.springframework.util.ClassUtils;
25+
import org.springframework.web.service.registry.AbstractHttpServiceRegistrar.GroupRegistry;
26+
27+
/**
28+
* A {@link GroupRegistry} that records the inputs given and creates {@link TestGroup}s
29+
*
30+
* @author Rossen Stoyanchev
31+
*/
32+
class TestGroupRegistry implements GroupRegistry {
33+
34+
private final Map<String, TestGroup> groupMap = new LinkedHashMap<>();
35+
36+
public Map<String, TestGroup> groupMap() {
37+
return this.groupMap;
38+
}
39+
40+
@Override
41+
public GroupSpec forGroup(String name, HttpServiceGroup.ClientType clientType) {
42+
return new TestGroupSpec(this.groupMap, name, clientType);
43+
}
44+
45+
46+
private record TestGroupSpec(
47+
Map<String, TestGroup> groupMap, String groupName,
48+
HttpServiceGroup.ClientType clientType) implements GroupSpec {
49+
50+
@Override
51+
public GroupSpec register(Class<?>... serviceTypes) {
52+
getOrCreateGroup().httpServiceTypes().addAll(Arrays.asList(serviceTypes));
53+
return this;
54+
}
55+
56+
@Override
57+
public GroupSpec registerTypeNames(String... serviceTypes) {
58+
return register(Arrays.stream(serviceTypes)
59+
.map(className -> ClassUtils.resolveClassName(className, getClass().getClassLoader()))
60+
.toArray(Class[]::new));
61+
}
62+
63+
@Override
64+
public GroupSpec detectInBasePackages(Class<?>... packageClasses) {
65+
getOrCreateGroup().packageClasses().addAll(Arrays.asList(packageClasses));
66+
return this;
67+
}
68+
69+
@Override
70+
public GroupSpec detectInBasePackages(String... packageNames) {
71+
getOrCreateGroup().packageNames().addAll(Arrays.asList(packageNames));
72+
return this;
73+
}
74+
75+
private TestGroup getOrCreateGroup() {
76+
return this.groupMap.computeIfAbsent(this.groupName, name -> new TestGroup(name, this.clientType));
77+
}
78+
}
79+
}

0 commit comments

Comments
 (0)