Skip to content

Commit 0165529

Browse files
committed
Align OverrideMetadata arguments and harmonize bean name
Rather than having to override the getBeanName method when there is one, this commit moves it as a @nullable argument. This makes sure that equals and hashCode consistently use the bean name. getBeanName cannot be final just yet as the test infrastructure overrides it. Also, arguments are now ordered consistently, which improves code readability and type signature.
1 parent 8b66eca commit 0165529

File tree

9 files changed

+78
-93
lines changed

9 files changed

+78
-93
lines changed

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

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
* annotation or the annotated field.
3939
*
4040
* @author Simon Baslé
41+
* @author Stephane Nicoll
4142
* @since 6.2
4243
*/
4344
public abstract class OverrideMetadata {
@@ -46,25 +47,26 @@ public abstract class OverrideMetadata {
4647

4748
private final ResolvableType beanType;
4849

50+
@Nullable
51+
private final String beanName;
52+
4953
private final BeanOverrideStrategy strategy;
5054

5155

52-
protected OverrideMetadata(Field field, ResolvableType beanType,
56+
protected OverrideMetadata(Field field, ResolvableType beanType, @Nullable String beanName,
5357
BeanOverrideStrategy strategy) {
54-
5558
this.field = field;
5659
this.beanType = beanType;
60+
this.beanName = beanName;
5761
this.strategy = strategy;
5862
}
5963

64+
6065
/**
61-
* Get the bean name to override, or {@code null} to look for a single
62-
* matching bean of type {@link #getBeanType()}.
63-
* <p>Defaults to {@code null}.
66+
* Get the annotated {@link Field}.
6467
*/
65-
@Nullable
66-
protected String getBeanName() {
67-
return null;
68+
public final Field getField() {
69+
return this.field;
6870
}
6971

7072
/**
@@ -75,10 +77,12 @@ public final ResolvableType getBeanType() {
7577
}
7678

7779
/**
78-
* Get the annotated {@link Field}.
80+
* Get the bean name to override, or {@code null} to look for a single
81+
* matching bean of type {@link #getBeanType()}.
7982
*/
80-
public final Field getField() {
81-
return this.field;
83+
@Nullable
84+
public String getBeanName() {
85+
return this.beanName;
8286
}
8387

8488
/**
@@ -123,22 +127,24 @@ public boolean equals(Object obj) {
123127
return false;
124128
}
125129
OverrideMetadata that = (OverrideMetadata) obj;
126-
return Objects.equals(this.strategy, that.strategy) &&
127-
Objects.equals(this.field, that.field) &&
128-
Objects.equals(this.beanType, that.beanType);
130+
return Objects.equals(this.beanType, that.beanType) &&
131+
Objects.equals(this.beanName, that.beanName) &&
132+
Objects.equals(this.strategy, that.strategy) &&
133+
Objects.equals(this.field, that.field);
129134
}
130135

131136
@Override
132137
public int hashCode() {
133-
return Objects.hash(this.strategy, this.field, this.beanType);
138+
return Objects.hash(this.beanType, this.beanName, this.strategy, this.field);
134139
}
135140

136141
@Override
137142
public String toString() {
138143
return new ToStringCreator(this)
139-
.append("strategy", this.strategy)
140144
.append("field", this.field)
141145
.append("beanType", this.beanType)
146+
.append("beanName", this.beanName)
147+
.append("strategy", this.strategy)
142148
.toString();
143149
}
144150

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

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import org.springframework.test.context.bean.override.BeanOverrideStrategy;
2828
import org.springframework.test.context.bean.override.OverrideMetadata;
2929
import org.springframework.util.ReflectionUtils;
30-
import org.springframework.util.StringUtils;
3130

3231
/**
3332
* {@link OverrideMetadata} implementation for {@link TestBean}.
@@ -40,21 +39,14 @@ final class TestBeanOverrideMetadata extends OverrideMetadata {
4039

4140
private final Method overrideMethod;
4241

43-
private final String beanName;
4442

45-
TestBeanOverrideMetadata(Field field, Method overrideMethod, TestBean overrideAnnotation,
46-
ResolvableType typeToOverride) {
43+
TestBeanOverrideMetadata(Field field, ResolvableType beanType, @Nullable String beanName,
44+
Method overrideMethod) {
4745

48-
super(field, typeToOverride, BeanOverrideStrategy.REPLACE_DEFINITION);
49-
this.beanName = overrideAnnotation.name();
46+
super(field, beanType, beanName, BeanOverrideStrategy.REPLACE_DEFINITION);
5047
this.overrideMethod = overrideMethod;
5148
}
5249

53-
@Override
54-
@Nullable
55-
protected String getBeanName() {
56-
return StringUtils.hasText(this.beanName) ? this.beanName : super.getBeanName();
57-
}
5850

5951
@Override
6052
protected Object createOverride(String beanName, @Nullable BeanDefinition existingBeanDefinition,
@@ -71,23 +63,22 @@ protected Object createOverride(String beanName, @Nullable BeanDefinition existi
7163
}
7264

7365
@Override
74-
public boolean equals(@Nullable Object o) {
75-
if (this == o) {
66+
public boolean equals(@Nullable Object other) {
67+
if (this == other) {
7668
return true;
7769
}
78-
if (o == null || getClass() != o.getClass()) {
70+
if (other == null || getClass() != other.getClass()) {
7971
return false;
8072
}
81-
if (!super.equals(o)) {
73+
if (!super.equals(other)) {
8274
return false;
8375
}
84-
TestBeanOverrideMetadata that = (TestBeanOverrideMetadata) o;
85-
return Objects.equals(this.overrideMethod, that.overrideMethod)
86-
&& Objects.equals(this.beanName, that.beanName);
76+
TestBeanOverrideMetadata that = (TestBeanOverrideMetadata) other;
77+
return Objects.equals(this.overrideMethod, that.overrideMethod);
8778
}
8879

8980
@Override
9081
public int hashCode() {
91-
return Objects.hash(super.hashCode(), this.overrideMethod, this.beanName);
82+
return Objects.hash(super.hashCode(), this.overrideMethod);
9283
}
9384
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,8 @@ public TestBeanOverrideMetadata createMetadata(Annotation overrideAnnotation, Cl
124124
overrideMethod = findTestBeanFactoryMethod(testClass, field.getType(), candidateMethodNames);
125125
}
126126

127-
return new TestBeanOverrideMetadata(field, overrideMethod, testBeanAnnotation, ResolvableType.forField(field, testClass));
127+
String beanName = (StringUtils.hasText(testBeanAnnotation.name()) ? testBeanAnnotation.name() : null);
128+
return new TestBeanOverrideMetadata(field, ResolvableType.forField(field, testClass), beanName, overrideMethod);
128129
}
129130

130131

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

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,16 @@ class MockitoBeanOverrideMetadata extends MockitoOverrideMetadata {
5454
private final boolean serializable;
5555

5656

57-
MockitoBeanOverrideMetadata(MockitoBean annotation, Field field, ResolvableType typeToMock) {
58-
this(annotation.name(), annotation.reset(), field, typeToMock,
59-
annotation.extraInterfaces(), annotation.answers(), annotation.serializable());
57+
MockitoBeanOverrideMetadata(Field field, ResolvableType typeToMock, MockitoBean annotation) {
58+
this(field, typeToMock, (StringUtils.hasText(annotation.name()) ? annotation.name() : null),
59+
annotation.reset(), annotation.extraInterfaces(), annotation.answers(), annotation.serializable());
6060
}
6161

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

65-
super(name, reset, false, field, typeToMock, BeanOverrideStrategy.REPLACE_OR_CREATE_DEFINITION);
66-
Assert.notNull(typeToMock, "TypeToMock must not be null");
65+
super(field, typeToMock, beanName, BeanOverrideStrategy.REPLACE_OR_CREATE_DEFINITION, reset, false);
66+
Assert.notNull(typeToMock, "'typeToMock' must not be null");
6767
this.extraInterfaces = asClassSet(extraInterfaces);
6868
this.answer = (answer != null) ? answer : Answers.RETURNS_DEFAULTS;
6969
this.serializable = serializable;
@@ -108,18 +108,18 @@ private Set<Class<?>> asClassSet(@Nullable Class<?>[] classes) {
108108
}
109109

110110
@Override
111-
public boolean equals(@Nullable Object obj) {
112-
if (obj == this) {
111+
public boolean equals(@Nullable Object other) {
112+
if (other == this) {
113113
return true;
114114
}
115-
if (obj == null || obj.getClass() != getClass()) {
115+
if (other == null || other.getClass() != getClass()) {
116116
return false;
117117
}
118-
MockitoBeanOverrideMetadata other = (MockitoBeanOverrideMetadata) obj;
119-
boolean result = super.equals(obj);
120-
result = result && ObjectUtils.nullSafeEquals(this.extraInterfaces, other.extraInterfaces);
121-
result = result && ObjectUtils.nullSafeEquals(this.answer, other.answer);
122-
result = result && this.serializable == other.serializable;
118+
MockitoBeanOverrideMetadata that = (MockitoBeanOverrideMetadata) other;
119+
boolean result = super.equals(that);
120+
result = result && ObjectUtils.nullSafeEquals(this.extraInterfaces, that.extraInterfaces);
121+
result = result && ObjectUtils.nullSafeEquals(this.answer, that.answer);
122+
result = result && this.serializable == that.serializable;
123123
return result;
124124
}
125125

@@ -131,12 +131,12 @@ public int hashCode() {
131131
@Override
132132
public String toString() {
133133
return new ToStringCreator(this)
134+
.append("beanType", getBeanType())
134135
.append("beanName", getBeanName())
135-
.append("fieldType", getBeanType())
136+
.append("reset", getReset())
136137
.append("extraInterfaces", getExtraInterfaces())
137138
.append("answer", getAnswer())
138139
.append("serializable", isSerializable())
139-
.append("reset", getReset())
140140
.toString();
141141
}
142142

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ class MockitoBeanOverrideProcessor implements BeanOverrideProcessor {
3636
@Override
3737
public MockitoOverrideMetadata createMetadata(Annotation overrideAnnotation, Class<?> testClass, Field field) {
3838
if (overrideAnnotation instanceof MockitoBean mockBean) {
39-
return new MockitoBeanOverrideMetadata(mockBean, field, ResolvableType.forField(field, testClass));
39+
return new MockitoBeanOverrideMetadata(field, ResolvableType.forField(field, testClass), mockBean);
4040
}
4141
else if (overrideAnnotation instanceof MockitoSpyBean spyBean) {
42-
return new MockitoSpyBeanOverrideMetadata(spyBean, field, ResolvableType.forField(field, testClass));
42+
return new MockitoSpyBeanOverrideMetadata(field, ResolvableType.forField(field, testClass), spyBean);
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/MockitoOverrideMetadata.java

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import org.springframework.test.context.bean.override.BeanOverrideStrategy;
2727
import org.springframework.test.context.bean.override.OverrideMetadata;
2828
import org.springframework.util.ObjectUtils;
29-
import org.springframework.util.StringUtils;
3029

3130
/**
3231
* Base {@link OverrideMetadata} implementation for Mockito.
@@ -36,29 +35,20 @@
3635
*/
3736
abstract class MockitoOverrideMetadata extends OverrideMetadata {
3837

39-
protected final String name;
40-
4138
private final MockReset reset;
4239

4340
private final boolean proxyTargetAware;
4441

4542

46-
MockitoOverrideMetadata(String name, @Nullable MockReset reset, boolean proxyTargetAware, Field field,
47-
ResolvableType typeToOverride, BeanOverrideStrategy strategy) {
43+
protected MockitoOverrideMetadata(Field field, ResolvableType beanType, @Nullable String beanName,
44+
BeanOverrideStrategy strategy, @Nullable MockReset reset, boolean proxyTargetAware) {
4845

49-
super(field, typeToOverride, strategy);
50-
this.name = name;
46+
super(field, beanType, beanName, strategy);
5147
this.reset = (reset != null) ? reset : MockReset.AFTER;
5248
this.proxyTargetAware = proxyTargetAware;
5349
}
5450

5551

56-
@Override
57-
@Nullable
58-
protected String getBeanName() {
59-
return StringUtils.hasText(this.name) ? this.name : super.getBeanName();
60-
}
61-
6252
/**
6353
* Return the mock reset mode.
6454
* @return the reset mode
@@ -101,15 +91,14 @@ public boolean equals(@Nullable Object obj) {
10191
}
10292
MockitoOverrideMetadata other = (MockitoOverrideMetadata) obj;
10393
boolean result = super.equals(obj);
104-
result = result && ObjectUtils.nullSafeEquals(this.name, other.name);
10594
result = result && ObjectUtils.nullSafeEquals(this.reset, other.reset);
10695
result = result && ObjectUtils.nullSafeEquals(this.proxyTargetAware, other.proxyTargetAware);
10796
return result;
10897
}
10998

11099
@Override
111100
public int hashCode() {
112-
return Objects.hash(super.hashCode(), this.name, this.reset, this.proxyTargetAware);
101+
return Objects.hash(super.hashCode(), this.reset, this.proxyTargetAware);
113102
}
114103

115104
}

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

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,15 @@
4848
*/
4949
class MockitoSpyBeanOverrideMetadata extends MockitoOverrideMetadata {
5050

51-
MockitoSpyBeanOverrideMetadata(MockitoSpyBean spyAnnotation, Field field, ResolvableType typeToSpy) {
52-
this(spyAnnotation.name(), spyAnnotation.reset(), spyAnnotation.proxyTargetAware(),
53-
field, typeToSpy);
51+
MockitoSpyBeanOverrideMetadata(Field field, ResolvableType typeToSpy, MockitoSpyBean spyAnnotation) {
52+
this(field, typeToSpy, (StringUtils.hasText(spyAnnotation.name()) ? spyAnnotation.name() : null),
53+
spyAnnotation.reset(), spyAnnotation.proxyTargetAware());
5454
}
5555

56-
MockitoSpyBeanOverrideMetadata(String name, MockReset reset, boolean proxyTargetAware, Field field, ResolvableType typeToSpy) {
57-
super(name, reset, proxyTargetAware, field, typeToSpy, BeanOverrideStrategy.WRAP_BEAN);
56+
MockitoSpyBeanOverrideMetadata(Field field, ResolvableType typeToSpy, @Nullable String beanName,
57+
MockReset reset, boolean proxyTargetAware) {
58+
59+
super(field, typeToSpy, beanName, BeanOverrideStrategy.WRAP_BEAN, reset, proxyTargetAware);
5860
Assert.notNull(typeToSpy, "typeToSpy must not be null");
5961
}
6062

@@ -98,16 +100,16 @@ <T> T createSpy(String name, Object instance) {
98100
}
99101

100102
@Override
101-
public boolean equals(@Nullable Object obj) {
102-
if (obj == this) {
103+
public boolean equals(@Nullable Object other) {
104+
if (other == this) {
103105
return true;
104106
}
105107
// For SpyBean we want the class to be exactly the same.
106-
if (obj == null || obj.getClass() != getClass()) {
108+
if (other == null || other.getClass() != getClass()) {
107109
return false;
108110
}
109-
MockitoSpyBeanOverrideMetadata that = (MockitoSpyBeanOverrideMetadata) obj;
110-
return (super.equals(obj) && ObjectUtils.nullSafeEquals(getBeanType(), that.getBeanType()));
111+
MockitoSpyBeanOverrideMetadata that = (MockitoSpyBeanOverrideMetadata) other;
112+
return (super.equals(that) && ObjectUtils.nullSafeEquals(getBeanType(), that.getBeanType()));
111113
}
112114

113115
@Override

spring-test/src/test/java/org/springframework/test/context/bean/override/OverrideMetadataTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ static class ConcreteOverrideMetadata extends OverrideMetadata {
5656
ConcreteOverrideMetadata(Field field, ResolvableType typeToOverride,
5757
BeanOverrideStrategy strategy) {
5858

59-
super(field, typeToOverride, strategy);
59+
super(field, typeToOverride, null, strategy);
6060
}
6161

6262
@Override

0 commit comments

Comments
 (0)