|
| 1 | +/* |
| 2 | + * |
| 3 | + * * |
| 4 | + * * * |
| 5 | + * * * * |
| 6 | + * * * * * |
| 7 | + * * * * * * Copyright 2019-2025 the original author or authors. |
| 8 | + * * * * * * |
| 9 | + * * * * * * Licensed under the Apache License, Version 2.0 (the "License"); |
| 10 | + * * * * * * you may not use this file except in compliance with the License. |
| 11 | + * * * * * * You may obtain a copy of the License at |
| 12 | + * * * * * * |
| 13 | + * * * * * * https://www.apache.org/licenses/LICENSE-2.0 |
| 14 | + * * * * * * |
| 15 | + * * * * * * Unless required by applicable law or agreed to in writing, software |
| 16 | + * * * * * * distributed under the License is distributed on an "AS IS" BASIS, |
| 17 | + * * * * * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 18 | + * * * * * * See the License for the specific language governing permissions and |
| 19 | + * * * * * * limitations under the License. |
| 20 | + * * * * * |
| 21 | + * * * * |
| 22 | + * * * |
| 23 | + * * |
| 24 | + * |
| 25 | + */ |
| 26 | + |
| 27 | +package test.org.springdoc.api.v31.app39; |
| 28 | + |
| 29 | +import java.lang.reflect.Method; |
| 30 | +import java.lang.reflect.Type; |
| 31 | +import java.util.Arrays; |
| 32 | + |
| 33 | +import org.junit.jupiter.api.Test; |
| 34 | +import org.springdoc.core.configuration.SpringDocDataRestConfiguration; |
| 35 | + |
| 36 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; |
| 37 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass; |
| 38 | +import org.springframework.context.annotation.Bean; |
| 39 | + |
| 40 | +import static org.assertj.core.api.Assertions.assertThat; |
| 41 | + |
| 42 | +/** |
| 43 | + * Verifies that HateoasProperties-dependent bean methods are isolated in nested |
| 44 | + * configuration classes guarded by {@code @ConditionalOnClass} / |
| 45 | + * {@code @ConditionalOnMissingClass}, so that when HateoasProperties is absent |
| 46 | + * from the classpath, Spring never loads the class that references it. |
| 47 | + * |
| 48 | + * @author bnasslahsen |
| 49 | + */ |
| 50 | +class SpringDocApp39Test { |
| 51 | + |
| 52 | + private static final String HATEOAS_PROPERTIES_CLASS = "org.springframework.boot.hateoas.autoconfigure.HateoasProperties"; |
| 53 | + |
| 54 | + @Test |
| 55 | + void dataRestConfigOuterClassDoesNotReferenceHateoasProperties() { |
| 56 | + assertOuterBeanMethodsDoNotReferenceHateoasProperties(SpringDocDataRestConfiguration.class); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + void dataRestConfigHasGuardedNestedClasses() { |
| 61 | + assertHasGuardedNestedClasses(SpringDocDataRestConfiguration.class); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Asserts that no {@code @Bean} method declared directly on the outer |
| 66 | + * configuration class has {@code HateoasProperties} in its generic |
| 67 | + * parameter types. Before the fix, the outer class had methods like |
| 68 | + * {@code halProvider(Optional<HateoasProperties>...)} which caused |
| 69 | + * {@code TypeNotPresentException} when HateoasProperties was absent. |
| 70 | + */ |
| 71 | + private void assertOuterBeanMethodsDoNotReferenceHateoasProperties(Class<?> configClass) { |
| 72 | + for (Method method : configClass.getDeclaredMethods()) { |
| 73 | + if (method.isAnnotationPresent(Bean.class)) { |
| 74 | + for (Type type : method.getGenericParameterTypes()) { |
| 75 | + assertThat(type.getTypeName()) |
| 76 | + .as("@Bean method %s.%s() should not reference HateoasProperties directly", |
| 77 | + configClass.getSimpleName(), method.getName()) |
| 78 | + .doesNotContain("HateoasProperties"); |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Asserts that the configuration class has at least one nested class |
| 86 | + * guarded by {@code @ConditionalOnClass(name = "...HateoasProperties")} |
| 87 | + * and one by {@code @ConditionalOnMissingClass("...HateoasProperties")}. |
| 88 | + */ |
| 89 | + private void assertHasGuardedNestedClasses(Class<?> configClass) { |
| 90 | + boolean hasConditionalOnClass = false; |
| 91 | + boolean hasConditionalOnMissingClass = false; |
| 92 | + |
| 93 | + for (Class<?> inner : configClass.getDeclaredClasses()) { |
| 94 | + ConditionalOnClass onClass = inner.getAnnotation(ConditionalOnClass.class); |
| 95 | + if (onClass != null && Arrays.asList(onClass.name()).contains(HATEOAS_PROPERTIES_CLASS)) { |
| 96 | + hasConditionalOnClass = true; |
| 97 | + } |
| 98 | + ConditionalOnMissingClass onMissingClass = inner.getAnnotation(ConditionalOnMissingClass.class); |
| 99 | + if (onMissingClass != null && Arrays.asList(onMissingClass.value()).contains(HATEOAS_PROPERTIES_CLASS)) { |
| 100 | + hasConditionalOnMissingClass = true; |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + assertThat(hasConditionalOnClass) |
| 105 | + .as("%s should have a nested class with @ConditionalOnClass for HateoasProperties", |
| 106 | + configClass.getSimpleName()) |
| 107 | + .isTrue(); |
| 108 | + assertThat(hasConditionalOnMissingClass) |
| 109 | + .as("%s should have a nested class with @ConditionalOnMissingClass for HateoasProperties", |
| 110 | + configClass.getSimpleName()) |
| 111 | + .isTrue(); |
| 112 | + } |
| 113 | + |
| 114 | +} |
0 commit comments