Skip to content

Commit 8b66eca

Browse files
committed
Harmonize OverrideMetadata implementations
This commit makes sure that each OverrideMetadata implementation is a top level class with a consistent name.
1 parent e8c122c commit 8b66eca

File tree

8 files changed

+172
-142
lines changed

8 files changed

+172
-142
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
* Copyright 2002-2024 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.test.context.bean.override.convention;
18+
19+
import java.lang.reflect.Field;
20+
import java.lang.reflect.InvocationTargetException;
21+
import java.lang.reflect.Method;
22+
import java.util.Objects;
23+
24+
import org.springframework.beans.factory.config.BeanDefinition;
25+
import org.springframework.core.ResolvableType;
26+
import org.springframework.lang.Nullable;
27+
import org.springframework.test.context.bean.override.BeanOverrideStrategy;
28+
import org.springframework.test.context.bean.override.OverrideMetadata;
29+
import org.springframework.util.ReflectionUtils;
30+
import org.springframework.util.StringUtils;
31+
32+
/**
33+
* {@link OverrideMetadata} implementation for {@link TestBean}.
34+
*
35+
* @author Simon Baslé
36+
* @author Stephane Nicoll
37+
* @since 6.2
38+
*/
39+
final class TestBeanOverrideMetadata extends OverrideMetadata {
40+
41+
private final Method overrideMethod;
42+
43+
private final String beanName;
44+
45+
TestBeanOverrideMetadata(Field field, Method overrideMethod, TestBean overrideAnnotation,
46+
ResolvableType typeToOverride) {
47+
48+
super(field, typeToOverride, BeanOverrideStrategy.REPLACE_DEFINITION);
49+
this.beanName = overrideAnnotation.name();
50+
this.overrideMethod = overrideMethod;
51+
}
52+
53+
@Override
54+
@Nullable
55+
protected String getBeanName() {
56+
return StringUtils.hasText(this.beanName) ? this.beanName : super.getBeanName();
57+
}
58+
59+
@Override
60+
protected Object createOverride(String beanName, @Nullable BeanDefinition existingBeanDefinition,
61+
@Nullable Object existingBeanInstance) {
62+
63+
try {
64+
ReflectionUtils.makeAccessible(this.overrideMethod);
65+
return this.overrideMethod.invoke(null);
66+
}
67+
catch (IllegalAccessException | InvocationTargetException ex) {
68+
throw new IllegalStateException("Failed to invoke bean overriding method " + this.overrideMethod.getName() +
69+
"; a static method with no formal parameters is expected", ex);
70+
}
71+
}
72+
73+
@Override
74+
public boolean equals(@Nullable Object o) {
75+
if (this == o) {
76+
return true;
77+
}
78+
if (o == null || getClass() != o.getClass()) {
79+
return false;
80+
}
81+
if (!super.equals(o)) {
82+
return false;
83+
}
84+
TestBeanOverrideMetadata that = (TestBeanOverrideMetadata) o;
85+
return Objects.equals(this.overrideMethod, that.overrideMethod)
86+
&& Objects.equals(this.beanName, that.beanName);
87+
}
88+
89+
@Override
90+
public int hashCode() {
91+
return Objects.hash(super.hashCode(), this.overrideMethod, this.beanName);
92+
}
93+
}

spring-test/src/main/java/org/springframework/test/context/bean/override/convention/TestBeanOverrideProcessor.java

Lines changed: 0 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,18 @@
1818

1919
import java.lang.annotation.Annotation;
2020
import java.lang.reflect.Field;
21-
import java.lang.reflect.InvocationTargetException;
2221
import java.lang.reflect.Method;
2322
import java.lang.reflect.Modifier;
2423
import java.util.ArrayList;
2524
import java.util.LinkedHashSet;
2625
import java.util.List;
27-
import java.util.Objects;
2826
import java.util.Set;
2927

30-
import org.springframework.beans.factory.config.BeanDefinition;
3128
import org.springframework.core.MethodIntrospector;
3229
import org.springframework.core.ResolvableType;
33-
import org.springframework.lang.Nullable;
3430
import org.springframework.test.context.TestContextAnnotationUtils;
3531
import org.springframework.test.context.bean.override.BeanOverrideProcessor;
36-
import org.springframework.test.context.bean.override.BeanOverrideStrategy;
37-
import org.springframework.test.context.bean.override.OverrideMetadata;
3832
import org.springframework.util.Assert;
39-
import org.springframework.util.ReflectionUtils;
4033
import org.springframework.util.ReflectionUtils.MethodFilter;
4134
import org.springframework.util.StringUtils;
4235

@@ -143,61 +136,4 @@ private static Set<Method> findMethods(Class<?> clazz, MethodFilter methodFilter
143136
return methods;
144137
}
145138

146-
147-
static final class TestBeanOverrideMetadata extends OverrideMetadata {
148-
149-
private final Method overrideMethod;
150-
151-
private final String beanName;
152-
153-
public TestBeanOverrideMetadata(Field field, Method overrideMethod, TestBean overrideAnnotation,
154-
ResolvableType typeToOverride) {
155-
156-
super(field, typeToOverride, BeanOverrideStrategy.REPLACE_DEFINITION);
157-
this.beanName = overrideAnnotation.name();
158-
this.overrideMethod = overrideMethod;
159-
}
160-
161-
@Override
162-
@Nullable
163-
protected String getBeanName() {
164-
return StringUtils.hasText(this.beanName) ? this.beanName : super.getBeanName();
165-
}
166-
167-
@Override
168-
protected Object createOverride(String beanName, @Nullable BeanDefinition existingBeanDefinition,
169-
@Nullable Object existingBeanInstance) {
170-
171-
try {
172-
ReflectionUtils.makeAccessible(this.overrideMethod);
173-
return this.overrideMethod.invoke(null);
174-
}
175-
catch (IllegalAccessException | InvocationTargetException ex) {
176-
throw new IllegalStateException("Failed to invoke bean overriding method " + this.overrideMethod.getName() +
177-
"; a static method with no formal parameters is expected", ex);
178-
}
179-
}
180-
181-
@Override
182-
public boolean equals(@Nullable Object o) {
183-
if (this == o) {
184-
return true;
185-
}
186-
if (o == null || getClass() != o.getClass()) {
187-
return false;
188-
}
189-
if (!super.equals(o)) {
190-
return false;
191-
}
192-
TestBeanOverrideMetadata that = (TestBeanOverrideMetadata) o;
193-
return Objects.equals(this.overrideMethod, that.overrideMethod)
194-
&& Objects.equals(this.beanName, that.beanName);
195-
}
196-
197-
@Override
198-
public int hashCode() {
199-
return Objects.hash(super.hashCode(), this.overrideMethod, this.beanName);
200-
}
201-
}
202-
203139
}

spring-test/src/main/java/org/springframework/test/context/bean/override/mockito/MockitoBeanMetadata.java renamed to spring-test/src/main/java/org/springframework/test/context/bean/override/mockito/MockitoBeanOverrideMetadata.java

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2024 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -31,6 +31,7 @@
3131
import org.springframework.core.style.ToStringCreator;
3232
import org.springframework.lang.Nullable;
3333
import org.springframework.test.context.bean.override.BeanOverrideStrategy;
34+
import org.springframework.test.context.bean.override.OverrideMetadata;
3435
import org.springframework.util.Assert;
3536
import org.springframework.util.ClassUtils;
3637
import org.springframework.util.ObjectUtils;
@@ -39,12 +40,12 @@
3940
import static org.mockito.Mockito.mock;
4041

4142
/**
42-
* A complete definition that can be used to create a Mockito mock.
43+
* {@link OverrideMetadata} implementation for Mockito {@code mock} support.
4344
*
4445
* @author Phillip Webb
4546
* @since 6.2
4647
*/
47-
class MockitoBeanMetadata extends MockitoMetadata {
48+
class MockitoBeanOverrideMetadata extends MockitoOverrideMetadata {
4849

4950
private final Set<Class<?>> extraInterfaces;
5051

@@ -53,12 +54,12 @@ class MockitoBeanMetadata extends MockitoMetadata {
5354
private final boolean serializable;
5455

5556

56-
MockitoBeanMetadata(MockitoBean annotation, Field field, ResolvableType typeToMock) {
57+
MockitoBeanOverrideMetadata(MockitoBean annotation, Field field, ResolvableType typeToMock) {
5758
this(annotation.name(), annotation.reset(), field, typeToMock,
5859
annotation.extraInterfaces(), annotation.answers(), annotation.serializable());
5960
}
6061

61-
MockitoBeanMetadata(String name, MockReset reset, Field field, ResolvableType typeToMock,
62+
MockitoBeanOverrideMetadata(String name, MockReset reset, Field field, ResolvableType typeToMock,
6263
Class<?>[] extraInterfaces, @Nullable Answers answer, boolean serializable) {
6364

6465
super(name, reset, false, field, typeToMock, BeanOverrideStrategy.REPLACE_OR_CREATE_DEFINITION);
@@ -68,18 +69,6 @@ class MockitoBeanMetadata extends MockitoMetadata {
6869
this.serializable = serializable;
6970
}
7071

71-
@Override
72-
protected Object createOverride(String beanName, @Nullable BeanDefinition existingBeanDefinition, @Nullable Object existingBeanInstance) {
73-
return createMock(beanName);
74-
}
75-
76-
private Set<Class<?>> asClassSet(@Nullable Class<?>[] classes) {
77-
Set<Class<?>> classSet = new LinkedHashSet<>();
78-
if (classes != null) {
79-
classSet.addAll(Arrays.asList(classes));
80-
}
81-
return Collections.unmodifiableSet(classSet);
82-
}
8372

8473
/**
8574
* Return the extra interfaces.
@@ -105,6 +94,19 @@ boolean isSerializable() {
10594
return this.serializable;
10695
}
10796

97+
@Override
98+
protected Object createOverride(String beanName, @Nullable BeanDefinition existingBeanDefinition, @Nullable Object existingBeanInstance) {
99+
return createMock(beanName);
100+
}
101+
102+
private Set<Class<?>> asClassSet(@Nullable Class<?>[] classes) {
103+
Set<Class<?>> classSet = new LinkedHashSet<>();
104+
if (classes != null) {
105+
classSet.addAll(Arrays.asList(classes));
106+
}
107+
return Collections.unmodifiableSet(classSet);
108+
}
109+
108110
@Override
109111
public boolean equals(@Nullable Object obj) {
110112
if (obj == this) {
@@ -113,7 +115,7 @@ public boolean equals(@Nullable Object obj) {
113115
if (obj == null || obj.getClass() != getClass()) {
114116
return false;
115117
}
116-
MockitoBeanMetadata other = (MockitoBeanMetadata) obj;
118+
MockitoBeanOverrideMetadata other = (MockitoBeanOverrideMetadata) obj;
117119
boolean result = super.equals(obj);
118120
result = result && ObjectUtils.nullSafeEquals(this.extraInterfaces, other.extraInterfaces);
119121
result = result && ObjectUtils.nullSafeEquals(this.answer, other.answer);

spring-test/src/main/java/org/springframework/test/context/bean/override/mockito/MockitoBeanOverrideProcessor.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@
3434
class MockitoBeanOverrideProcessor implements BeanOverrideProcessor {
3535

3636
@Override
37-
public MockitoMetadata createMetadata(Annotation overrideAnnotation, Class<?> testClass, Field field) {
37+
public MockitoOverrideMetadata createMetadata(Annotation overrideAnnotation, Class<?> testClass, Field field) {
3838
if (overrideAnnotation instanceof MockitoBean mockBean) {
39-
return new MockitoBeanMetadata(mockBean, field, ResolvableType.forField(field, testClass));
39+
return new MockitoBeanOverrideMetadata(mockBean, field, ResolvableType.forField(field, testClass));
4040
}
4141
else if (overrideAnnotation instanceof MockitoSpyBean spyBean) {
42-
return new MockitoSpyBeanMetadata(spyBean, field, ResolvableType.forField(field, testClass));
42+
return new MockitoSpyBeanOverrideMetadata(spyBean, field, ResolvableType.forField(field, testClass));
4343
}
4444
throw new IllegalStateException(String.format("Invalid annotation passed to MockitoBeanOverrideProcessor: "
4545
+ "expected @MockitoBean/@MockitoSpyBean on field %s.%s",

spring-test/src/main/java/org/springframework/test/context/bean/override/mockito/MockitoMetadata.java renamed to spring-test/src/main/java/org/springframework/test/context/bean/override/mockito/MockitoOverrideMetadata.java

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2024 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -29,12 +29,12 @@
2929
import org.springframework.util.StringUtils;
3030

3131
/**
32-
* Base class for Mockito override metadata.
32+
* Base {@link OverrideMetadata} implementation for Mockito.
3333
*
3434
* @author Phillip Webb
3535
* @since 6.2
3636
*/
37-
abstract class MockitoMetadata extends OverrideMetadata {
37+
abstract class MockitoOverrideMetadata extends OverrideMetadata {
3838

3939
protected final String name;
4040

@@ -43,7 +43,7 @@ abstract class MockitoMetadata extends OverrideMetadata {
4343
private final boolean proxyTargetAware;
4444

4545

46-
MockitoMetadata(String name, @Nullable MockReset reset, boolean proxyTargetAware, Field field,
46+
MockitoOverrideMetadata(String name, @Nullable MockReset reset, boolean proxyTargetAware, Field field,
4747
ResolvableType typeToOverride, BeanOverrideStrategy strategy) {
4848

4949
super(field, typeToOverride, strategy);
@@ -59,22 +59,6 @@ protected String getBeanName() {
5959
return StringUtils.hasText(this.name) ? this.name : super.getBeanName();
6060
}
6161

62-
@Override
63-
protected void track(Object mock, SingletonBeanRegistry trackingBeanRegistry) {
64-
MockitoBeans tracker = null;
65-
try {
66-
tracker = (MockitoBeans) trackingBeanRegistry.getSingleton(MockitoBeans.class.getName());
67-
}
68-
catch (NoSuchBeanDefinitionException ignored) {
69-
70-
}
71-
if (tracker == null) {
72-
tracker= new MockitoBeans();
73-
trackingBeanRegistry.registerSingleton(MockitoBeans.class.getName(), tracker);
74-
}
75-
tracker.add(mock);
76-
}
77-
7862
/**
7963
* Return the mock reset mode.
8064
* @return the reset mode
@@ -91,6 +75,22 @@ boolean isProxyTargetAware() {
9175
return this.proxyTargetAware;
9276
}
9377

78+
@Override
79+
protected void track(Object mock, SingletonBeanRegistry trackingBeanRegistry) {
80+
MockitoBeans tracker = null;
81+
try {
82+
tracker = (MockitoBeans) trackingBeanRegistry.getSingleton(MockitoBeans.class.getName());
83+
}
84+
catch (NoSuchBeanDefinitionException ignored) {
85+
86+
}
87+
if (tracker == null) {
88+
tracker= new MockitoBeans();
89+
trackingBeanRegistry.registerSingleton(MockitoBeans.class.getName(), tracker);
90+
}
91+
tracker.add(mock);
92+
}
93+
9494
@Override
9595
public boolean equals(@Nullable Object obj) {
9696
if (obj == this) {
@@ -99,7 +99,7 @@ public boolean equals(@Nullable Object obj) {
9999
if (obj == null || !getClass().isAssignableFrom(obj.getClass())) {
100100
return false;
101101
}
102-
MockitoMetadata other = (MockitoMetadata) obj;
102+
MockitoOverrideMetadata other = (MockitoOverrideMetadata) obj;
103103
boolean result = super.equals(obj);
104104
result = result && ObjectUtils.nullSafeEquals(this.name, other.name);
105105
result = result && ObjectUtils.nullSafeEquals(this.reset, other.reset);

0 commit comments

Comments
 (0)