Skip to content

Commit b2245c7

Browse files
committed
Add @CloudConfigUri
Closes gh-74
1 parent 32ee7a9 commit b2245c7

File tree

3 files changed

+107
-0
lines changed

3 files changed

+107
-0
lines changed

README.adoc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,16 @@ The value is calculated as `http://{host}:{port}{contextRoot}`.
267267
* port - a valid SpEL expression that determines the port to use for the URL (default port)
268268
* contextRoot - the context root to use (default is empty String)
269269

270+
==== @CloudConfigUri
271+
272+
This simplifies mapping the to the property `spring.cloud.config.uri`.
273+
The value is calculated as `http://{host}:{port}{contextRoot}` such that:
274+
275+
* host - the host to use (default is `localhost`)
276+
* port - a valid SpEL expression that determines the port to use for the URL (default port)
277+
* contextRoot - the context root to use (default is empty String)
278+
279+
270280
==== @OAuth2ClientProviderIssuerUri
271281

272282
This provides a mapping to issuer-uri of https://docs.spring.io/spring-boot/docs/current/reference/html/application-properties.html#application-properties.security.spring.security.oauth2.client.provider[the OAuth provider details].
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Copyright 2012-2023 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.experimental.boot.test.context;
18+
19+
import java.lang.annotation.Documented;
20+
import java.lang.annotation.ElementType;
21+
import java.lang.annotation.Retention;
22+
import java.lang.annotation.RetentionPolicy;
23+
import java.lang.annotation.Target;
24+
25+
import org.springframework.core.annotation.AliasFor;
26+
27+
/**
28+
* A composed annotation for {@link DynamicProperty} for specifying the property name
29+
* "spring.cloud.config.uri" to the value "http://{host}:{port}{contextRoot}" such that:
30+
*
31+
* <ul>
32+
* <li>port - the</li>
33+
* </ul>
34+
*
35+
*
36+
* <code>
37+
* &#64;Bean
38+
* &#64;CloudConfigUri
39+
* CommonsExecWebServerFactoryBean configServer() {
40+
* return CommonsExecWebServerFactoryBean.builder()
41+
* // ...
42+
* .classpath((cp) -> cp
43+
* .entries(springBootStarter("web"))
44+
* .entries(new MavenClasspathEntry("org.springframework.cloud:spring-cloud-config-server:4.2.0"))
45+
* );
46+
* }
47+
* </code>
48+
*
49+
* @author Rob Winch
50+
*/
51+
@Target(ElementType.METHOD)
52+
@Retention(RetentionPolicy.RUNTIME)
53+
@Documented
54+
@DynamicPortUrl(name = "spring.cloud.config.uri")
55+
public @interface CloudConfigUri {
56+
57+
/**
58+
* Allows overriding the value of the property. The default is "localhost".
59+
* @return the value of the property.
60+
*/
61+
@AliasFor(annotation = DynamicPortUrl.class)
62+
String host() default "localhost";
63+
64+
/**
65+
* Allows specifying a context root. The default is to have no context root.
66+
* @return the name of the provider used in the property name.
67+
*/
68+
@AliasFor(annotation = DynamicPortUrl.class)
69+
String contextRoot() default "";
70+
71+
}

spring-boot-testjars/src/test/java/org/springframework/experimental/boot/test/context/DynamicPropertyRegistryPropertyFactoryTests.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,24 @@ void dynamicPortUrlWhenOverride() throws Exception {
9898
assertThat(registryProperty.value().get()).isEqualTo("http://127.0.0.1:1234/messages");
9999
}
100100

101+
@Test
102+
void cloudConfigUriWhenDefault() throws Exception {
103+
MergedAnnotation<DynamicProperty> dynamicProperty = dynamicPropertyFrom("cloudConfigUriWithDefault");
104+
DynamicPropertyRegistryProperty registryProperty = this.propertyFactory.createRegistryProperty(dynamicProperty,
105+
() -> new WebServer());
106+
assertThat(registryProperty.name()).isEqualTo("spring.cloud.config.uri");
107+
assertThat(registryProperty.value().get()).isEqualTo("http://localhost:1234");
108+
}
109+
110+
@Test
111+
void cloudConfigUriWhenOverride() throws Exception {
112+
MergedAnnotation<DynamicProperty> dynamicProperty = dynamicPropertyFrom("cloudConfigUriWithOverride");
113+
DynamicPropertyRegistryProperty registryProperty = this.propertyFactory.createRegistryProperty(dynamicProperty,
114+
() -> new WebServer());
115+
assertThat(registryProperty.name()).isEqualTo("spring.cloud.config.uri");
116+
assertThat(registryProperty.value().get()).isEqualTo("http://127.0.0.1:1234/config");
117+
}
118+
101119
private MergedAnnotation<DynamicProperty> dynamicPropertyFrom(String methodName) throws NoSuchMethodException {
102120
MergedAnnotations mergedAnnotations = MergedAnnotations.from(getClass().getDeclaredMethod(methodName));
103121
return mergedAnnotations.get(DynamicProperty.class);
@@ -136,6 +154,14 @@ static void dynamicPortUrlWithDefault() {
136154
static void dynamicPortUrlWithOverride() {
137155
}
138156

157+
@CloudConfigUri
158+
static void cloudConfigUriWithDefault() {
159+
}
160+
161+
@CloudConfigUri(host = "127.0.0.1", contextRoot = "/config")
162+
static void cloudConfigUriWithOverride() {
163+
}
164+
139165
@Retention(RetentionPolicy.RUNTIME)
140166
@DynamicProperty(name = "message", value = "'Hello {firstName}'")
141167
@interface ValueWithVariable {

0 commit comments

Comments
 (0)