|
| 1 | +/* |
| 2 | + * Copyright 2024-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 | +package org.springframework.grpc.test; |
| 17 | + |
| 18 | +import java.util.List; |
| 19 | +import java.util.Objects; |
| 20 | + |
| 21 | +import org.jspecify.annotations.Nullable; |
| 22 | + |
| 23 | +import org.springframework.boot.test.util.TestPropertyValues; |
| 24 | +import org.springframework.context.ConfigurableApplicationContext; |
| 25 | +import org.springframework.test.context.ContextConfigurationAttributes; |
| 26 | +import org.springframework.test.context.ContextCustomizer; |
| 27 | +import org.springframework.test.context.ContextCustomizerFactory; |
| 28 | +import org.springframework.test.context.MergedContextConfiguration; |
| 29 | +import org.springframework.test.context.TestContextAnnotationUtils; |
| 30 | + |
| 31 | +/** |
| 32 | + * {@link ContextCustomizerFactory} that starts an in-process gRPC server and replaces the |
| 33 | + * regular server and channel factories (e.g. Netty). The customizer can be disabled via |
| 34 | + * the {@link AutoConfigureInProcessTransport} annotation or the |
| 35 | + * {@value #ENABLED_PROPERTY} property. |
| 36 | + * |
| 37 | + * @author Chris Bono |
| 38 | + */ |
| 39 | +class InProcessTransportContextCustomizerFactory implements ContextCustomizerFactory { |
| 40 | + |
| 41 | + static final String ENABLED_PROPERTY = "spring.grpc.test.inprocess.enabled"; |
| 42 | + |
| 43 | + @Override |
| 44 | + public ContextCustomizer createContextCustomizer(Class<?> testClass, |
| 45 | + List<ContextConfigurationAttributes> configAttributes) { |
| 46 | + AutoConfigureInProcessTransport annotation = TestContextAnnotationUtils.findMergedAnnotation(testClass, |
| 47 | + AutoConfigureInProcessTransport.class); |
| 48 | + return new InProcessTransportContextCustomizer(annotation); |
| 49 | + } |
| 50 | + |
| 51 | + private static class InProcessTransportContextCustomizer implements ContextCustomizer { |
| 52 | + |
| 53 | + private final @Nullable AutoConfigureInProcessTransport annotation; |
| 54 | + |
| 55 | + InProcessTransportContextCustomizer(@Nullable AutoConfigureInProcessTransport annotation) { |
| 56 | + this.annotation = annotation; |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public void customizeContext(ConfigurableApplicationContext context, |
| 61 | + MergedContextConfiguration mergedContextConfiguration) { |
| 62 | + if (this.annotation == null |
| 63 | + || !context.getEnvironment().getProperty(ENABLED_PROPERTY, Boolean.class, true)) { |
| 64 | + return; |
| 65 | + } |
| 66 | + TestPropertyValues |
| 67 | + .of("spring.grpc.test.inprocess.enabled=true", "spring.grpc.client.inprocess.exclusive=true", |
| 68 | + "spring.grpc.server.inprocess.exclusive=true") |
| 69 | + .applyTo(context); |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public boolean equals(@Nullable Object o) { |
| 74 | + if (this == o) { |
| 75 | + return true; |
| 76 | + } |
| 77 | + if (o == null || getClass() != o.getClass()) { |
| 78 | + return false; |
| 79 | + } |
| 80 | + InProcessTransportContextCustomizer that = (InProcessTransportContextCustomizer) o; |
| 81 | + return Objects.equals(this.annotation, that.annotation); |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + public int hashCode() { |
| 86 | + return Objects.hash(this.annotation); |
| 87 | + } |
| 88 | + |
| 89 | + } |
| 90 | + |
| 91 | +} |
0 commit comments