|
| 1 | +package io.temporal.spring.boot.autoconfigure; |
| 2 | + |
| 3 | +import static org.assertj.core.api.Assertions.assertThat; |
| 4 | +import static org.junit.jupiter.api.Assumptions.assumeTrue; |
| 5 | + |
| 6 | +import java.util.ArrayList; |
| 7 | +import java.util.List; |
| 8 | +import org.junit.jupiter.api.Test; |
| 9 | +import org.springframework.boot.autoconfigure.AutoConfigurations; |
| 10 | +import org.springframework.boot.test.context.runner.ApplicationContextRunner; |
| 11 | + |
| 12 | +/** |
| 13 | + * Tests that {@link MetricsScopeAutoConfiguration} and {@link OpenTracingAutoConfiguration} |
| 14 | + * correctly order themselves after their dependency auto-configurations via |
| 15 | + * {@code @AutoConfigureAfter}. |
| 16 | + * |
| 17 | + * <p>Without correct ordering, {@code @ConditionalOnBean} evaluates before the dependency beans |
| 18 | + * exist, silently skipping our entire auto-configuration. This happens because our FQCNs ({@code |
| 19 | + * io.temporal...}) sort alphabetically before Spring's ({@code org.springframework...}), which is |
| 20 | + * the default processing order when no ordering constraints are specified. |
| 21 | + */ |
| 22 | +class AutoConfigOrderingTest { |
| 23 | + |
| 24 | + /** |
| 25 | + * Try to load a class by name, returning the first match found. Returns null if none of the class |
| 26 | + * names exist on the classpath. |
| 27 | + */ |
| 28 | + private static Class<?> findClass(String... classNames) { |
| 29 | + for (String name : classNames) { |
| 30 | + try { |
| 31 | + return Class.forName(name); |
| 32 | + } catch (ClassNotFoundException ignored) { |
| 33 | + } |
| 34 | + } |
| 35 | + return null; |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Verifies that {@link MetricsScopeAutoConfiguration} runs after {@code |
| 40 | + * CompositeMeterRegistryAutoConfiguration} and finds the {@code MeterRegistry} bean it creates. |
| 41 | + * The {@code CompositeMeterRegistryAutoConfiguration} class moved packages in Spring Boot 4 — |
| 42 | + * both old and new names must be in {@code @AutoConfigureAfter}. |
| 43 | + */ |
| 44 | + @Test |
| 45 | + void metricsScopeCreatedWithAutoConfigOrdering() { |
| 46 | + Class<?> compositeClass = |
| 47 | + findClass( |
| 48 | + // SB 2.7 / 3.x |
| 49 | + "org.springframework.boot.actuate.autoconfigure.metrics.CompositeMeterRegistryAutoConfiguration", |
| 50 | + // SB 4.0 |
| 51 | + "org.springframework.boot.micrometer.metrics.autoconfigure.CompositeMeterRegistryAutoConfiguration"); |
| 52 | + |
| 53 | + assumeTrue(compositeClass != null, "CompositeMeterRegistryAutoConfiguration not on classpath"); |
| 54 | + |
| 55 | + // MetricsAutoConfiguration provides the Clock bean that CompositeMeterRegistryAutoConfiguration |
| 56 | + // inner configs need to activate. |
| 57 | + List<Class<?>> autoConfigs = new ArrayList<>(); |
| 58 | + autoConfigs.add(MetricsScopeAutoConfiguration.class); |
| 59 | + autoConfigs.add(compositeClass); |
| 60 | + Class<?> metricsAutoConfig = |
| 61 | + findClass( |
| 62 | + "org.springframework.boot.actuate.autoconfigure.metrics.MetricsAutoConfiguration", |
| 63 | + "org.springframework.boot.micrometer.metrics.autoconfigure.MetricsAutoConfiguration"); |
| 64 | + if (metricsAutoConfig != null) { |
| 65 | + autoConfigs.add(metricsAutoConfig); |
| 66 | + } |
| 67 | + |
| 68 | + new ApplicationContextRunner() |
| 69 | + .withConfiguration(AutoConfigurations.of(autoConfigs.toArray(new Class<?>[0]))) |
| 70 | + .run( |
| 71 | + context -> { |
| 72 | + assertThat(context).hasNotFailed(); |
| 73 | + assertThat(context).hasBean("temporalMetricsScope"); |
| 74 | + }); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Verifies that {@link OpenTracingAutoConfiguration} runs after the OpenTelemetry |
| 79 | + * auto-configuration and finds the {@code OpenTelemetry} bean it creates. The producing class |
| 80 | + * moved across Spring Boot versions: |
| 81 | + * |
| 82 | + * <ul> |
| 83 | + * <li>SB 3.2+: {@code actuate.autoconfigure.opentelemetry.OpenTelemetryAutoConfiguration} |
| 84 | + * <li>SB 4.0: {@code opentelemetry.autoconfigure.OpenTelemetrySdkAutoConfiguration} |
| 85 | + * </ul> |
| 86 | + * |
| 87 | + * <p>On SB 2.7 (no native OTel auto-config) this test is skipped. |
| 88 | + */ |
| 89 | + @Test |
| 90 | + void openTracingTracerCreatedWithAutoConfigOrdering() { |
| 91 | + Class<?> otelAutoConfig = |
| 92 | + findClass( |
| 93 | + // SB 3.2+ (generic OTel auto-config, uses ObjectProvider for optional deps) |
| 94 | + "org.springframework.boot.actuate.autoconfigure.opentelemetry.OpenTelemetryAutoConfiguration", |
| 95 | + // SB 4.0 (relocated to separate module) |
| 96 | + "org.springframework.boot.opentelemetry.autoconfigure.OpenTelemetrySdkAutoConfiguration"); |
| 97 | + |
| 98 | + assumeTrue(otelAutoConfig != null, "OpenTelemetry auto-configuration not on classpath"); |
| 99 | + |
| 100 | + new ApplicationContextRunner() |
| 101 | + .withConfiguration( |
| 102 | + AutoConfigurations.of(OpenTracingAutoConfiguration.class, otelAutoConfig)) |
| 103 | + .run( |
| 104 | + context -> { |
| 105 | + assertThat(context).hasNotFailed(); |
| 106 | + assertThat(context).hasBean("temporalOtTracer"); |
| 107 | + }); |
| 108 | + } |
| 109 | +} |
0 commit comments