forked from spring-projects/spring-boot
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request spring-projects#33743 from msobeck
* spring-projectsgh-33743: Polish "Test Micrometer config to property exposure" Test Micrometer config to property exposure Closes spring-projectsgh-33743
- Loading branch information
Showing
24 changed files
with
253 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
...actuate/autoconfigure/metrics/export/properties/AbstractPropertiesConfigAdapterTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/* | ||
* Copyright 2012-2023 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.boot.actuate.autoconfigure.metrics.export.properties; | ||
|
||
import java.lang.reflect.Method; | ||
import java.util.Arrays; | ||
import java.util.Set; | ||
import java.util.TreeSet; | ||
import java.util.stream.Collectors; | ||
|
||
import io.micrometer.core.instrument.config.validate.Validated; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import org.springframework.core.annotation.AnnotatedElementUtils; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
/** | ||
* Base class for testing properties config adapters. | ||
* | ||
* @param <P> the properties used by the adapter | ||
* @param <A> the adapter under test | ||
* @author Andy Wilkinson | ||
* @author Mirko Sobeck | ||
*/ | ||
public abstract class AbstractPropertiesConfigAdapterTests<P, A extends PropertiesConfigAdapter<P>> { | ||
|
||
private final Class<? extends A> adapter; | ||
|
||
protected AbstractPropertiesConfigAdapterTests(Class<? extends A> adapter) { | ||
this.adapter = adapter; | ||
} | ||
|
||
@Test | ||
protected void adapterOverridesAllConfigMethods() { | ||
adapterOverridesAllConfigMethodsExcept(); | ||
} | ||
|
||
protected final void adapterOverridesAllConfigMethodsExcept(String... nonConfigMethods) { | ||
Class<?> config = findImplementedConfig(); | ||
Set<String> expectedConfigMethodNames = Arrays.stream(config.getDeclaredMethods()) | ||
.filter(Method::isDefault) | ||
.filter(this::hasNoParameters) | ||
.filter(this::isNotValidationMethod) | ||
.filter(this::isNotDeprecated) | ||
.map(Method::getName) | ||
.collect(Collectors.toCollection(TreeSet::new)); | ||
expectedConfigMethodNames.removeAll(Arrays.asList(nonConfigMethods)); | ||
Set<String> actualConfigMethodNames = new TreeSet<>(); | ||
Class<?> currentClass = this.adapter; | ||
while (!Object.class.equals(currentClass)) { | ||
actualConfigMethodNames.addAll(Arrays.stream(currentClass.getDeclaredMethods()) | ||
.map(Method::getName) | ||
.filter(expectedConfigMethodNames::contains) | ||
.collect(Collectors.toList())); | ||
currentClass = currentClass.getSuperclass(); | ||
} | ||
assertThat(actualConfigMethodNames).containsExactlyInAnyOrderElementsOf(expectedConfigMethodNames); | ||
} | ||
|
||
private Class<?> findImplementedConfig() { | ||
Class<?>[] interfaces = this.adapter.getInterfaces(); | ||
if (interfaces.length == 1) { | ||
return interfaces[0]; | ||
} | ||
throw new IllegalStateException(this.adapter + " is not a config implementation"); | ||
} | ||
|
||
private boolean isNotDeprecated(Method method) { | ||
return !AnnotatedElementUtils.hasAnnotation(method, Deprecated.class); | ||
} | ||
|
||
private boolean hasNoParameters(Method method) { | ||
return method.getParameterCount() == 0; | ||
} | ||
|
||
private boolean isNotValidationMethod(Method method) { | ||
return !Validated.class.equals(method.getReturnType()); | ||
} | ||
|
||
} |
Oops, something went wrong.