|
| 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; |
| 18 | + |
| 19 | +import java.lang.reflect.Method; |
| 20 | +import java.time.Duration; |
| 21 | +import java.util.ArrayList; |
| 22 | +import java.util.Arrays; |
| 23 | +import java.util.List; |
| 24 | +import java.util.stream.Collectors; |
| 25 | + |
| 26 | +import org.junit.Assert; |
| 27 | + |
| 28 | +import org.springframework.boot.actuate.autoconfigure.metrics.export.properties.PropertiesConfigAdapter; |
| 29 | + |
| 30 | +/** |
| 31 | + * Utility to test that all Micrometer config values are exposed via properties and |
| 32 | + * settable using the corresponding {@link PropertiesConfigAdapter} implementation. |
| 33 | + * |
| 34 | + * @author Mirko Sobeck |
| 35 | + */ |
| 36 | +public final class TestConfigsToPropertiesExposure { |
| 37 | + |
| 38 | + private TestConfigsToPropertiesExposure() { |
| 39 | + |
| 40 | + } |
| 41 | + |
| 42 | + private static final List<Class<?>> classesForPropertiesList = Arrays.asList(Boolean.class, Byte.class, |
| 43 | + Character.class, Short.class, Integer.class, Long.class, Double.class, Float.class, String.class, |
| 44 | + Duration.class); |
| 45 | + |
| 46 | + /** |
| 47 | + * Assertion to test if default methods of a given config are overridden by the |
| 48 | + * adapter which implements it. This can be an indicator for micrometer config fields, |
| 49 | + * that have been forgotten to expose via spring properties. Not overridden default |
| 50 | + * methods in adapters are the most common cause of forgotten field exposure, because |
| 51 | + * they do not for force an override. |
| 52 | + * @param config micrometer config |
| 53 | + * @param adapter adapter for properties {@link PropertiesConfigAdapter} |
| 54 | + * @param excludedConfigMethods config methods that should be excluded for the |
| 55 | + * assertion |
| 56 | + */ |
| 57 | + public static void assertThatAllConfigDefaultMethodsAreOverriddenByAdapter(Class<?> config, |
| 58 | + Class<? extends PropertiesConfigAdapter<?>> adapter, String... excludedConfigMethods) { |
| 59 | + List<String> configDefaultMethodNames = Arrays.stream(config.getDeclaredMethods()) |
| 60 | + .filter((method) -> method.isDefault() && isSettableUsingProperties(method.getReturnType())) |
| 61 | + .map(Method::getName).collect(Collectors.toList()); |
| 62 | + |
| 63 | + configDefaultMethodNames.removeAll(Arrays.stream(excludedConfigMethods).collect(Collectors.toList())); |
| 64 | + List<String> notOverriddenDefaultMethods = new ArrayList<>(configDefaultMethodNames); |
| 65 | + |
| 66 | + Class<?> currentClass = adapter; |
| 67 | + // loop through adapter class and superclasses |
| 68 | + // to find not overridden config methods |
| 69 | + while (!Object.class.equals(currentClass)) { |
| 70 | + List<String> overriddenClassDefaultMethods = Arrays.stream(currentClass.getDeclaredMethods()) |
| 71 | + .map(Method::getName).filter(configDefaultMethodNames::contains).collect(Collectors.toList()); |
| 72 | + |
| 73 | + notOverriddenDefaultMethods.removeAll(overriddenClassDefaultMethods); |
| 74 | + currentClass = currentClass.getSuperclass(); |
| 75 | + } |
| 76 | + |
| 77 | + if (notOverriddenDefaultMethods.size() >= 1) { |
| 78 | + Assert.fail( |
| 79 | + "Found config default methods that are not overridden by the related PropertiesConfigAdapter: \n" |
| 80 | + + notOverriddenDefaultMethods + "\n" |
| 81 | + + "This could be an indicator for not exposed properties fields.\n" |
| 82 | + + "Please check if the fields are meant to be exposed and if not, " |
| 83 | + + "exclude them from this test by providing them to the method."); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Guess if a class can be set using properties. This will only catch the basic use |
| 89 | + * cases regarding the micrometer configs to filter out methods that are not likely to |
| 90 | + * be designed to be set via properties. <pre> |
| 91 | + * isSettableUsingProperties(String.class) = true |
| 92 | + * isSettableUsingProperties(boolean.class) = true |
| 93 | + * isSettableUsingProperties(Object.class) = false |
| 94 | + * </pre> |
| 95 | + * @param clazz Class |
| 96 | + * @return is likely to be settable using properties |
| 97 | + */ |
| 98 | + private static boolean isSettableUsingProperties(Class<?> clazz) { |
| 99 | + if (Void.TYPE.equals(clazz)) { |
| 100 | + return false; |
| 101 | + } |
| 102 | + |
| 103 | + if (clazz.isPrimitive()) { |
| 104 | + return true; |
| 105 | + } |
| 106 | + |
| 107 | + return classesForPropertiesList.contains(clazz); |
| 108 | + } |
| 109 | + |
| 110 | +} |
0 commit comments