|
| 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.boot.actuate.autoconfigure.metrics.export.properties; |
| 18 | + |
| 19 | +import java.lang.reflect.Method; |
| 20 | +import java.util.Arrays; |
| 21 | +import java.util.Set; |
| 22 | +import java.util.TreeSet; |
| 23 | +import java.util.stream.Collectors; |
| 24 | + |
| 25 | +import io.micrometer.core.instrument.config.validate.Validated; |
| 26 | +import org.junit.jupiter.api.Test; |
| 27 | + |
| 28 | +import org.springframework.core.annotation.AnnotatedElementUtils; |
| 29 | + |
| 30 | +import static org.assertj.core.api.Assertions.assertThat; |
| 31 | + |
| 32 | +/** |
| 33 | + * Base class for testing properties config adapters. |
| 34 | + * |
| 35 | + * @param <P> the properties used by the adapter |
| 36 | + * @param <A> the adapter under test |
| 37 | + * @author Andy Wilkinson |
| 38 | + * @author Mirko Sobeck |
| 39 | + */ |
| 40 | +public abstract class AbstractPropertiesConfigAdapterTests<P, A extends PropertiesConfigAdapter<P>> { |
| 41 | + |
| 42 | + private final Class<? extends A> adapter; |
| 43 | + |
| 44 | + protected AbstractPropertiesConfigAdapterTests(Class<? extends A> adapter) { |
| 45 | + this.adapter = adapter; |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + protected void adapterOverridesAllConfigMethods() { |
| 50 | + adapterOverridesAllConfigMethodsExcept(); |
| 51 | + } |
| 52 | + |
| 53 | + protected final void adapterOverridesAllConfigMethodsExcept(String... nonConfigMethods) { |
| 54 | + Class<?> config = findImplementedConfig(); |
| 55 | + Set<String> expectedConfigMethodNames = Arrays.stream(config.getDeclaredMethods()) |
| 56 | + .filter(Method::isDefault) |
| 57 | + .filter(this::hasNoParameters) |
| 58 | + .filter(this::isNotValidationMethod) |
| 59 | + .filter(this::isNotDeprecated) |
| 60 | + .map(Method::getName) |
| 61 | + .collect(Collectors.toCollection(TreeSet::new)); |
| 62 | + expectedConfigMethodNames.removeAll(Arrays.asList(nonConfigMethods)); |
| 63 | + Set<String> actualConfigMethodNames = new TreeSet<>(); |
| 64 | + Class<?> currentClass = this.adapter; |
| 65 | + while (!Object.class.equals(currentClass)) { |
| 66 | + actualConfigMethodNames.addAll(Arrays.stream(currentClass.getDeclaredMethods()) |
| 67 | + .map(Method::getName) |
| 68 | + .filter(expectedConfigMethodNames::contains) |
| 69 | + .collect(Collectors.toList())); |
| 70 | + currentClass = currentClass.getSuperclass(); |
| 71 | + } |
| 72 | + assertThat(actualConfigMethodNames).containsExactlyInAnyOrderElementsOf(expectedConfigMethodNames); |
| 73 | + } |
| 74 | + |
| 75 | + private Class<?> findImplementedConfig() { |
| 76 | + Class<?>[] interfaces = this.adapter.getInterfaces(); |
| 77 | + if (interfaces.length == 1) { |
| 78 | + return interfaces[0]; |
| 79 | + } |
| 80 | + throw new IllegalStateException(this.adapter + " is not a config implementation"); |
| 81 | + } |
| 82 | + |
| 83 | + private boolean isNotDeprecated(Method method) { |
| 84 | + return !AnnotatedElementUtils.hasAnnotation(method, Deprecated.class); |
| 85 | + } |
| 86 | + |
| 87 | + private boolean hasNoParameters(Method method) { |
| 88 | + return method.getParameterCount() == 0; |
| 89 | + } |
| 90 | + |
| 91 | + private boolean isNotValidationMethod(Method method) { |
| 92 | + return !Validated.class.equals(method.getReturnType()); |
| 93 | + } |
| 94 | + |
| 95 | +} |
0 commit comments