Skip to content

Commit 4e99be9

Browse files
committed
#7: clean up
Signed-off-by: andreilisa <[email protected]>
1 parent f645b98 commit 4e99be9

File tree

4 files changed

+68
-37
lines changed

4 files changed

+68
-37
lines changed

samples/grpc-server/src/test/java/org/springframework/grpc/sample/InProcessApplicationContextInitializerTests.java

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,3 @@
1-
/*
2-
* Copyright 2024-2024 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-
171
package org.springframework.grpc.sample;
182

193
import io.grpc.ManagedChannel;
@@ -30,14 +14,12 @@
3014
import static org.assertj.core.api.Assertions.assertThat;
3115
import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy;
3216

33-
@SpringBootTest
3417
class InProcessApplicationContextInitializerTests {
3518

3619
private GenericApplicationContext context;
3720

3821
@BeforeEach
3922
public void setUp() {
40-
System.clearProperty("spring.grpc.inprocess");
4123
context = new AnnotationConfigApplicationContext();
4224
}
4325

@@ -51,7 +33,6 @@ class WhenDefaultEnabled {
5133

5234
@Test
5335
void shouldInitializeInProcessServer() {
54-
System.setProperty("spring.grpc.inprocess", "true");
5536
new InProcessApplicationContextInitializer().initialize(context);
5637
context.refresh();
5738

@@ -62,26 +43,38 @@ void shouldInitializeInProcessServer() {
6243
}
6344

6445
@Nested
46+
@SpringBootTest(properties = { "spring.grpc.server.host=127.0.0.1", "spring.grpc.server.port=0",
47+
"spring.grpc.inprocess.enabled=false" })
6548
class WhenDisabledByProperty {
6649

6750
@Test
6851
void shouldNotInitializeInProcessServer() {
69-
System.setProperty("spring.grpc.inprocess", "false");
70-
new InProcessApplicationContextInitializer().initialize(context);
7152
context.refresh();
72-
7353
assertThatThrownBy(() -> context.getBean("grpcInProcessChannel", ManagedChannel.class))
7454
.isInstanceOf(NoSuchBeanDefinitionException.class);
7555
}
7656

7757
}
7858

59+
@Nested
60+
class WhenNoPropertyIsSet {
61+
62+
@Test
63+
void shouldUseDefaultTrueAndInitializeInProcessServer() {
64+
new InProcessApplicationContextInitializer().initialize(context);
65+
context.refresh();
66+
67+
ManagedChannel channel = context.getBean("grpcInProcessChannel", ManagedChannel.class);
68+
assertThat(channel).isNotNull().isInstanceOf(ManagedChannel.class);
69+
}
70+
71+
}
72+
7973
@Nested
8074
class WhenShutdownIsCalled {
8175

8276
@Test
8377
void shouldShutdownInProcessServer() {
84-
System.setProperty("spring.grpc.inprocess", "true");
8578
new InProcessApplicationContextInitializer().initialize(context);
8679
context.registerShutdownHook();
8780
context.refresh();
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright 2024-2024 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.grpc.test;
18+
19+
import org.springframework.core.env.Environment;
20+
21+
/**
22+
* A configuration class that holds the properties related to in-process gRPC
23+
* communication. It reads the `spring.grpc.inprocess.enabled` property from the Spring
24+
* environment and provides access to it via a getter method.
25+
* <p>
26+
* This class is typically used to determine whether the in-process gRPC server should be
27+
* enabled in the application. It supports reading the property from external sources such
28+
* as `application.properties` or `application.yml`.
29+
* <p>
30+
* The default value for the `spring.grpc.inprocess.enabled` property is `true` if not
31+
* explicitly defined.
32+
*
33+
* @author Andrei Lisa
34+
*/
35+
36+
class GrpcProperties {
37+
38+
private static final String PROPERTY_SOURCE_NAME = "spring.grpc.inprocess.enabled";
39+
40+
private final boolean inProcessEnabled;
41+
42+
GrpcProperties(Environment environment) {
43+
this.inProcessEnabled = Boolean.parseBoolean(environment.getProperty(PROPERTY_SOURCE_NAME, "true"));
44+
}
45+
46+
public boolean isInProcessEnabled() {
47+
return inProcessEnabled;
48+
}
49+
50+
}

spring-grpc-test/src/main/java/org/springframework/grpc/test/InProcessApplicationContextInitializer.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,12 @@
3636
public class InProcessApplicationContextInitializer
3737
implements ApplicationContextInitializer<ConfigurableApplicationContext> {
3838

39-
private static final String PROPERTY_SOURCE_NAME = "spring.grpc.inprocess";
40-
4139
private static final String CHANNEL_NAME = "grpcInProcessChannel";
4240

4341
@Override
4442
public void initialize(ConfigurableApplicationContext applicationContext) {
45-
String inProcessEnabled = applicationContext.getEnvironment().getProperty(PROPERTY_SOURCE_NAME);
46-
47-
if ("true".equalsIgnoreCase(inProcessEnabled) && isJarOnClasspath()) {
43+
GrpcProperties grpcProperties = new GrpcProperties(applicationContext.getEnvironment());
44+
if (grpcProperties.isInProcessEnabled() && isJarOnClasspath()) {
4845
try {
4946
String serverName = InProcessServerBuilder.generateName();
5047

spring-grpc-test/src/main/resources/additional-spring-configuration-metadata.json

Lines changed: 0 additions & 9 deletions
This file was deleted.

0 commit comments

Comments
 (0)