Skip to content

Commit

Permalink
Merge branch '6.2.x'
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrannen committed Jan 3, 2025
2 parents bab8a83 + 0da4ae9 commit 087641c
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -710,8 +710,7 @@ public static void doWithFields(Class<?> clazz, FieldCallback fc, @Nullable Fiel
// Keep backing up the inheritance hierarchy.
Class<?> targetClass = clazz;
do {
Field[] fields = getDeclaredFields(targetClass);
for (Field field : fields) {
for (Field field : getDeclaredFields(targetClass)) {
if (ff != null && !ff.matches(field)) {
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,11 @@
package org.springframework.util;

import java.util.Properties;
import java.util.stream.Stream;

import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.junit.jupiter.params.provider.CsvSource;

import org.springframework.util.PropertyPlaceholderHelper.PlaceholderResolver;

Expand All @@ -44,6 +42,7 @@ class PropertyPlaceholderHelperTests {

private final PropertyPlaceholderHelper helper = new PropertyPlaceholderHelper("${", "}");


@Test
void withProperties() {
String text = "foo=${foo}";
Expand Down Expand Up @@ -116,8 +115,8 @@ void unresolvedPlaceholderAsError() {
props.setProperty("foo", "bar");

PropertyPlaceholderHelper helper = new PropertyPlaceholderHelper("${", "}", null, null, false);
assertThatExceptionOfType(PlaceholderResolutionException.class).isThrownBy(() ->
helper.replacePlaceholders(text, props));
assertThatExceptionOfType(PlaceholderResolutionException.class)
.isThrownBy(() -> helper.replacePlaceholders(text, props));
}

@Nested
Expand All @@ -126,7 +125,14 @@ class DefaultValueTests {
private final PropertyPlaceholderHelper helper = new PropertyPlaceholderHelper("${", "}", ":", null, true);

@ParameterizedTest(name = "{0} -> {1}")
@MethodSource("defaultValues")
@CsvSource(delimiterString = "->", textBlock = """
${invalid:test} -> test
${invalid:${one}} -> 1
${invalid:${one}${two}} -> 12
${invalid:${one}:${two}} -> 1:2
${invalid:${also_invalid:test}} -> test
${invalid:${also_invalid:${one}}} -> 1
""")
void defaultValueIsApplied(String text, String value) {
Properties properties = new Properties();
properties.setProperty("one", "1");
Expand All @@ -142,24 +148,26 @@ void defaultValueIsNotEvaluatedEarly() {
verify(resolver, never()).resolvePlaceholder("two");
}

static Stream<Arguments> defaultValues() {
return Stream.of(
Arguments.of("${invalid:test}", "test"),
Arguments.of("${invalid:${one}}", "1"),
Arguments.of("${invalid:${one}${two}}", "12"),
Arguments.of("${invalid:${one}:${two}}", "1:2"),
Arguments.of("${invalid:${also_invalid:test}}", "test"),
Arguments.of("${invalid:${also_invalid:${one}}}", "1")
);
@ParameterizedTest(name = "{0} -> {1}")
@CsvSource(delimiterString = "->", textBlock = """
${prefix://my-service} -> example-service
${p1} -> example-service
""")
void placeholdersWithExactMatchAreConsidered(String text, String expected) {
Properties properties = new Properties();
properties.setProperty("prefix://my-service", "example-service");
properties.setProperty("px", "prefix");
properties.setProperty("p1", "${prefix://my-service}");
assertThat(this.helper.replacePlaceholders(text, properties)).isEqualTo(expected);
}

}

PlaceholderResolver mockPlaceholderResolver(String... pairs) {

private static PlaceholderResolver mockPlaceholderResolver(String... pairs) {
if (pairs.length % 2 == 1) {
throw new IllegalArgumentException("size must be even, it is a set of key=value pairs");
}
PlaceholderResolver resolver = mock(PlaceholderResolver.class);
PlaceholderResolver resolver = mock();
for (int i = 0; i < pairs.length; i += 2) {
String key = pairs[i];
String value = pairs[i + 1];
Expand All @@ -168,5 +176,4 @@ PlaceholderResolver mockPlaceholderResolver(String... pairs) {
return resolver;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -57,20 +57,20 @@ MockReset getReset() {

@Override
protected void trackOverrideInstance(Object mock, SingletonBeanRegistry trackingBeanRegistry) {
getMockitoBeans(trackingBeanRegistry).add(mock);
getMockBeans(trackingBeanRegistry).add(mock);
}

private static MockitoBeans getMockitoBeans(SingletonBeanRegistry trackingBeanRegistry) {
String beanName = MockitoBeans.class.getName();
MockitoBeans mockitoBeans = null;
private static MockBeans getMockBeans(SingletonBeanRegistry trackingBeanRegistry) {
String beanName = MockBeans.class.getName();
MockBeans mockBeans = null;
if (trackingBeanRegistry.containsSingleton(beanName)) {
mockitoBeans = (MockitoBeans) trackingBeanRegistry.getSingleton(beanName);
mockBeans = (MockBeans) trackingBeanRegistry.getSingleton(beanName);
}
if (mockitoBeans == null) {
mockitoBeans = new MockitoBeans();
trackingBeanRegistry.registerSingleton(beanName, mockitoBeans);
if (mockBeans == null) {
mockBeans = new MockBeans();
trackingBeanRegistry.registerSingleton(beanName, mockBeans);
}
return mockitoBeans;
return mockBeans;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
* @author Sam Brannen
* @since 6.2
*/
class MockitoBeans {
class MockBeans {

private final List<Object> beans = new ArrayList<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,13 @@ class MockitoBeanOverrideHandler extends AbstractMockitoBeanOverrideHandler {
}

private MockitoBeanOverrideHandler(Field field, ResolvableType typeToMock, @Nullable String beanName,
BeanOverrideStrategy strategy, MockReset reset, Class<?>[] extraInterfaces, @Nullable Answers answers,
BeanOverrideStrategy strategy, MockReset reset, Class<?>[] extraInterfaces, Answers answers,
boolean serializable) {

super(field, typeToMock, beanName, strategy, reset);
Assert.notNull(typeToMock, "'typeToMock' must not be null");
this.extraInterfaces = asClassSet(extraInterfaces);
this.answers = (answers != null ? answers : Answers.RETURNS_DEFAULTS);
this.answers = answers;
this.serializable = serializable;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ class MockitoBeanOverrideProcessor implements BeanOverrideProcessor {

@Override
public AbstractMockitoBeanOverrideHandler createHandler(Annotation overrideAnnotation, Class<?> testClass, Field field) {
if (overrideAnnotation instanceof MockitoBean mockBean) {
return new MockitoBeanOverrideHandler(field, ResolvableType.forField(field, testClass), mockBean);
if (overrideAnnotation instanceof MockitoBean mockitoBean) {
return new MockitoBeanOverrideHandler(field, ResolvableType.forField(field, testClass), mockitoBean);
}
else if (overrideAnnotation instanceof MockitoSpyBean spyBean) {
return new MockitoSpyBeanOverrideHandler(field, ResolvableType.forField(field, testClass), spyBean);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ private static void resetMocks(ConfigurableApplicationContext applicationContext
}
}
try {
beanFactory.getBean(MockitoBeans.class).resetAll(reset);
beanFactory.getBean(MockBeans.class).resetAll(reset);
}
catch (NoSuchBeanDefinitionException ex) {
// Continue
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,9 @@ class MockitoSpyBeanOverrideHandler extends AbstractMockitoBeanOverrideHandler {
new SpringAopBypassingVerificationStartedListener();


MockitoSpyBeanOverrideHandler(Field field, ResolvableType typeToSpy, MockitoSpyBean spyAnnotation) {
this(field, typeToSpy, (StringUtils.hasText(spyAnnotation.name()) ? spyAnnotation.name() : null),
spyAnnotation.reset());
}

MockitoSpyBeanOverrideHandler(Field field, ResolvableType typeToSpy, @Nullable String beanName,
MockReset reset) {

super(field, typeToSpy, beanName, BeanOverrideStrategy.WRAP, reset);
MockitoSpyBeanOverrideHandler(Field field, ResolvableType typeToSpy, MockitoSpyBean spyBean) {
super(field, typeToSpy, (StringUtils.hasText(spyBean.name()) ? spyBean.name() : null),
BeanOverrideStrategy.WRAP, spyBean.reset());
Assert.notNull(typeToSpy, "typeToSpy must not be null");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ void contextCustomizerCannotBeCreatedWithFieldInParentAndMissingOverrideMethod()
}

@Test
void contextCustomizerCannotBeCreatedWitCompetingOverrideMethods() {
void contextCustomizerCannotBeCreatedWithCompetingOverrideMethods() {
GenericApplicationContext context = new GenericApplicationContext();
context.registerBean("bean", String.class, () -> "example");
assertThatIllegalStateException()
Expand Down

0 comments on commit 087641c

Please sign in to comment.