Skip to content

Commit ce8556e

Browse files
committed
Merge pull request #15312 from dreis2211
* pr/15312: Replace DirectFieldAccessor usages by hasFieldOrPropertyWithValue
2 parents d28d9e7 + 17f04b8 commit ce8556e

File tree

30 files changed

+243
-345
lines changed

30 files changed

+243
-345
lines changed

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/system/DiskSpaceHealthIndicatorAutoConfigurationTests.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import org.junit.Test;
2020

21-
import org.springframework.beans.DirectFieldAccessor;
2221
import org.springframework.boot.actuate.autoconfigure.health.HealthIndicatorAutoConfiguration;
2322
import org.springframework.boot.actuate.health.ApplicationHealthIndicator;
2423
import org.springframework.boot.actuate.system.DiskSpaceHealthIndicator;
@@ -63,10 +62,9 @@ public void thresholdCanBeCustomized() {
6362
.withPropertyValues("management.health.diskspace.threshold=20MB")
6463
.run((context) -> {
6564
assertThat(context).hasSingleBean(DiskSpaceHealthIndicator.class);
66-
DirectFieldAccessor dfa = new DirectFieldAccessor(
67-
context.getBean(DiskSpaceHealthIndicator.class));
68-
assertThat(dfa.getPropertyValue("threshold"))
69-
.isEqualTo(DataSize.ofMegabytes(20));
65+
assertThat(context.getBean(DiskSpaceHealthIndicator.class))
66+
.hasFieldOrPropertyWithValue("threshold",
67+
DataSize.ofMegabytes(20));
7068
});
7169
}
7270

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/amqp/RabbitAutoConfigurationTests.java

Lines changed: 78 additions & 96 deletions
Large diffs are not rendered by default.

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/amqp/RabbitPropertiesTests.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
2323
import org.springframework.amqp.rabbit.listener.DirectMessageListenerContainer;
2424
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
25-
import org.springframework.beans.DirectFieldAccessor;
2625

2726
import static org.assertj.core.api.Assertions.assertThat;
2827

@@ -237,24 +236,22 @@ public void determineAddressesUsesHostAndPortPropertiesWhenNoAddressesSet() {
237236
public void simpleContainerUseConsistentDefaultValues() {
238237
SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
239238
SimpleMessageListenerContainer container = factory.createListenerContainer();
240-
DirectFieldAccessor dfa = new DirectFieldAccessor(container);
241239
RabbitProperties.SimpleContainer simple = this.properties.getListener()
242240
.getSimple();
243241
assertThat(simple.isAutoStartup()).isEqualTo(container.isAutoStartup());
244-
assertThat(simple.isMissingQueuesFatal())
245-
.isEqualTo(dfa.getPropertyValue("missingQueuesFatal"));
242+
assertThat(container).hasFieldOrPropertyWithValue("missingQueuesFatal",
243+
simple.isMissingQueuesFatal());
246244
}
247245

248246
@Test
249247
public void directContainerUseConsistentDefaultValues() {
250248
DirectRabbitListenerContainerFactory factory = new DirectRabbitListenerContainerFactory();
251249
DirectMessageListenerContainer container = factory.createListenerContainer();
252-
DirectFieldAccessor dfa = new DirectFieldAccessor(container);
253250
RabbitProperties.DirectContainer direct = this.properties.getListener()
254251
.getDirect();
255252
assertThat(direct.isAutoStartup()).isEqualTo(container.isAutoStartup());
256-
assertThat(direct.isMissingQueuesFatal())
257-
.isEqualTo(dfa.getPropertyValue("missingQueuesFatal"));
253+
assertThat(container).hasFieldOrPropertyWithValue("missingQueuesFatal",
254+
direct.isMissingQueuesFatal());
258255
}
259256

260257
}

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/cache/CacheAutoConfigurationTests.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
import org.junit.Test;
4444
import org.junit.runner.RunWith;
4545

46-
import org.springframework.beans.DirectFieldAccessor;
4746
import org.springframework.beans.factory.BeanCreationException;
4847
import org.springframework.beans.factory.config.BeanPostProcessor;
4948
import org.springframework.boot.autoconfigure.AutoConfigurations;
@@ -69,8 +68,10 @@
6968
import org.springframework.context.annotation.Import;
7069
import org.springframework.core.io.ClassPathResource;
7170
import org.springframework.core.io.Resource;
71+
import org.springframework.data.redis.cache.RedisCacheConfiguration;
7272
import org.springframework.data.redis.cache.RedisCacheManager;
7373
import org.springframework.data.redis.connection.RedisConnectionFactory;
74+
import org.springframework.test.util.ReflectionTestUtils;
7475

7576
import static org.assertj.core.api.Assertions.assertThat;
7677
import static org.mockito.BDDMockito.given;
@@ -284,8 +285,8 @@ public void redisCacheExplicit() {
284285
RedisCacheManager cacheManager = getCacheManager(context,
285286
RedisCacheManager.class);
286287
assertThat(cacheManager.getCacheNames()).isEmpty();
287-
org.springframework.data.redis.cache.RedisCacheConfiguration redisCacheConfiguration = (org.springframework.data.redis.cache.RedisCacheConfiguration) new DirectFieldAccessor(
288-
cacheManager).getPropertyValue("defaultCacheConfig");
288+
RedisCacheConfiguration redisCacheConfiguration = getDefaultRedisCacheConfiguration(
289+
cacheManager);
289290
assertThat(redisCacheConfiguration.getTtl())
290291
.isEqualTo(java.time.Duration.ofSeconds(15));
291292
assertThat(redisCacheConfiguration.getAllowCacheNullValues())
@@ -307,8 +308,8 @@ public void redisCacheWithRedisCacheConfiguration() {
307308
RedisCacheManager cacheManager = getCacheManager(context,
308309
RedisCacheManager.class);
309310
assertThat(cacheManager.getCacheNames()).isEmpty();
310-
org.springframework.data.redis.cache.RedisCacheConfiguration redisCacheConfiguration = (org.springframework.data.redis.cache.RedisCacheConfiguration) new DirectFieldAccessor(
311-
cacheManager).getPropertyValue("defaultCacheConfig");
311+
RedisCacheConfiguration redisCacheConfiguration = getDefaultRedisCacheConfiguration(
312+
cacheManager);
312313
assertThat(redisCacheConfiguration.getTtl())
313314
.isEqualTo(java.time.Duration.ofSeconds(30));
314315
assertThat(redisCacheConfiguration.getKeyPrefixFor(""))
@@ -333,8 +334,8 @@ public void redisCacheExplicitWithCaches() {
333334
RedisCacheManager cacheManager = getCacheManager(context,
334335
RedisCacheManager.class);
335336
assertThat(cacheManager.getCacheNames()).containsOnly("foo", "bar");
336-
org.springframework.data.redis.cache.RedisCacheConfiguration redisCacheConfiguration = (org.springframework.data.redis.cache.RedisCacheConfiguration) new DirectFieldAccessor(
337-
cacheManager).getPropertyValue("defaultCacheConfig");
337+
RedisCacheConfiguration redisCacheConfiguration = getDefaultRedisCacheConfiguration(
338+
cacheManager);
338339
assertThat(redisCacheConfiguration.getTtl())
339340
.isEqualTo(java.time.Duration.ofMinutes(0));
340341
assertThat(redisCacheConfiguration.getAllowCacheNullValues())
@@ -786,6 +787,12 @@ private void validateCaffeineCacheWithStats(AssertableApplicationContext context
786787
.isEqualTo(1L);
787788
}
788789

790+
private RedisCacheConfiguration getDefaultRedisCacheConfiguration(
791+
RedisCacheManager cacheManager) {
792+
return (RedisCacheConfiguration) ReflectionTestUtils.getField(cacheManager,
793+
"defaultCacheConfig");
794+
}
795+
789796
@Configuration
790797
static class EmptyConfiguration {
791798

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/context/MessageSourceAutoConfigurationTests.java

Lines changed: 14 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import org.junit.Ignore;
2222
import org.junit.Test;
2323

24-
import org.springframework.beans.DirectFieldAccessor;
2524
import org.springframework.boot.autoconfigure.AutoConfigurations;
2625
import org.springframework.boot.test.context.assertj.AssertableApplicationContext;
2726
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
@@ -99,8 +98,8 @@ public void testCacheDurationWithUnit() {
9998
private ContextConsumer<AssertableApplicationContext> assertCache(long expected) {
10099
return (context) -> {
101100
assertThat(context).hasSingleBean(MessageSource.class);
102-
assertThat(new DirectFieldAccessor(context.getBean(MessageSource.class))
103-
.getPropertyValue("cacheMillis")).isEqualTo(expected);
101+
assertThat(context.getBean(MessageSource.class))
102+
.hasFieldOrPropertyWithValue("cacheMillis", expected);
104103
};
105104
}
106105

@@ -138,70 +137,49 @@ public void testMessageSourceFromPropertySourceAnnotation() {
138137
@Test
139138
public void testFallbackDefault() {
140139
this.contextRunner.withPropertyValues("spring.messages.basename:test/messages")
141-
.run((context) -> assertThat(
142-
isFallbackToSystemLocale(context.getBean(MessageSource.class)))
143-
.isTrue());
140+
.run((context) -> assertThat(context.getBean(MessageSource.class))
141+
.hasFieldOrPropertyWithValue("fallbackToSystemLocale", true));
144142
}
145143

146144
@Test
147145
public void testFallbackTurnOff() {
148146
this.contextRunner
149147
.withPropertyValues("spring.messages.basename:test/messages",
150148
"spring.messages.fallback-to-system-locale:false")
151-
.run((context) -> assertThat(
152-
isFallbackToSystemLocale(context.getBean(MessageSource.class)))
153-
.isFalse());
149+
.run((context) -> assertThat(context.getBean(MessageSource.class))
150+
.hasFieldOrPropertyWithValue("fallbackToSystemLocale", false));
154151
}
155152

156153
@Test
157154
public void testFormatMessageDefault() {
158155
this.contextRunner.withPropertyValues("spring.messages.basename:test/messages")
159-
.run((context) -> assertThat(
160-
isAlwaysUseMessageFormat(context.getBean(MessageSource.class)))
161-
.isFalse());
156+
.run((context) -> assertThat(context.getBean(MessageSource.class))
157+
.hasFieldOrPropertyWithValue("alwaysUseMessageFormat", false));
162158
}
163159

164160
@Test
165161
public void testFormatMessageOn() {
166162
this.contextRunner
167163
.withPropertyValues("spring.messages.basename:test/messages",
168164
"spring.messages.always-use-message-format:true")
169-
.run((context) -> assertThat(
170-
isAlwaysUseMessageFormat(context.getBean(MessageSource.class)))
171-
.isTrue());
172-
}
173-
174-
private boolean isFallbackToSystemLocale(MessageSource messageSource) {
175-
return (boolean) new DirectFieldAccessor(messageSource)
176-
.getPropertyValue("fallbackToSystemLocale");
177-
}
178-
179-
private boolean isAlwaysUseMessageFormat(MessageSource messageSource) {
180-
return (boolean) new DirectFieldAccessor(messageSource)
181-
.getPropertyValue("alwaysUseMessageFormat");
165+
.run((context) -> assertThat(context.getBean(MessageSource.class))
166+
.hasFieldOrPropertyWithValue("alwaysUseMessageFormat", true));
182167
}
183168

184169
@Test
185170
public void testUseCodeAsDefaultMessageDefault() {
186171
this.contextRunner.withPropertyValues("spring.messages.basename:test/messages")
187-
.run((context) -> assertThat(
188-
isUseCodeAsDefaultMessage(context.getBean(MessageSource.class)))
189-
.isFalse());
172+
.run((context) -> assertThat(context.getBean(MessageSource.class))
173+
.hasFieldOrPropertyWithValue("useCodeAsDefaultMessage", false));
190174
}
191175

192176
@Test
193177
public void testUseCodeAsDefaultMessageOn() {
194178
this.contextRunner
195179
.withPropertyValues("spring.messages.basename:test/messages",
196180
"spring.messages.use-code-as-default-message:true")
197-
.run((context) -> assertThat(
198-
isUseCodeAsDefaultMessage(context.getBean(MessageSource.class)))
199-
.isTrue());
200-
}
201-
202-
private boolean isUseCodeAsDefaultMessage(MessageSource messageSource) {
203-
return (boolean) new DirectFieldAccessor(messageSource)
204-
.getPropertyValue("useCodeAsDefaultMessage");
181+
.run((context) -> assertThat(context.getBean(MessageSource.class))
182+
.hasFieldOrPropertyWithValue("useCodeAsDefaultMessage", true));
205183
}
206184

207185
@Test

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/diagnostics/analyzer/NoSuchBeanDefinitionFailureAnalyzerTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
import org.junit.Test;
2323

24-
import org.springframework.beans.DirectFieldAccessor;
2524
import org.springframework.beans.FatalBeanException;
2625
import org.springframework.beans.factory.BeanFactory;
2726
import org.springframework.beans.factory.NoUniqueBeanDefinitionException;
@@ -38,6 +37,7 @@
3837
import org.springframework.context.annotation.Bean;
3938
import org.springframework.context.annotation.Configuration;
4039
import org.springframework.context.annotation.Import;
40+
import org.springframework.test.util.ReflectionTestUtils;
4141
import org.springframework.util.ClassUtils;
4242

4343
import static org.assertj.core.api.Assertions.assertThat;
@@ -227,8 +227,8 @@ private void assertUserDefinedBean(FailureAnalysis analysis, String description,
227227

228228
private static void addExclusions(NoSuchBeanDefinitionFailureAnalyzer analyzer,
229229
Class<?>... classes) {
230-
ConditionEvaluationReport report = (ConditionEvaluationReport) new DirectFieldAccessor(
231-
analyzer).getPropertyValue("report");
230+
ConditionEvaluationReport report = (ConditionEvaluationReport) ReflectionTestUtils
231+
.getField(analyzer, "report");
232232
List<String> exclusions = new ArrayList<>(report.getExclusions());
233233
for (Class<?> c : classes) {
234234
exclusions.add(c.getName());

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/influx/InfluxDbAutoConfigurationTests.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@
2424
import org.junit.Test;
2525
import retrofit2.Retrofit;
2626

27-
import org.springframework.beans.DirectFieldAccessor;
2827
import org.springframework.boot.autoconfigure.AutoConfigurations;
2928
import org.springframework.boot.test.context.assertj.AssertableApplicationContext;
3029
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
3130
import org.springframework.boot.test.rule.OutputCapture;
3231
import org.springframework.context.annotation.Bean;
3332
import org.springframework.context.annotation.Configuration;
33+
import org.springframework.test.util.ReflectionTestUtils;
3434

3535
import static org.assertj.core.api.Assertions.assertThat;
3636

@@ -118,10 +118,8 @@ public void influxDbWithOkHttpClientBuilder() {
118118

119119
private int getReadTimeoutProperty(AssertableApplicationContext context) {
120120
InfluxDB influxDB = context.getBean(InfluxDB.class);
121-
Retrofit retrofit = (Retrofit) new DirectFieldAccessor(influxDB)
122-
.getPropertyValue("retrofit");
123-
OkHttpClient callFactory = (OkHttpClient) new DirectFieldAccessor(retrofit)
124-
.getPropertyValue("callFactory");
121+
Retrofit retrofit = (Retrofit) ReflectionTestUtils.getField(influxDB, "retrofit");
122+
OkHttpClient callFactory = (OkHttpClient) retrofit.callFactory();
125123
return callFactory.readTimeoutMillis();
126124
}
127125

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/integration/IntegrationAutoConfigurationTests.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
import org.junit.Test;
2222

23-
import org.springframework.beans.DirectFieldAccessor;
2423
import org.springframework.boot.autoconfigure.AutoConfigurations;
2524
import org.springframework.boot.autoconfigure.integration.IntegrationAutoConfiguration.IntegrationComponentScanConfiguration;
2625
import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration;
@@ -213,8 +212,8 @@ public void integrationEnablesDefaultCounts() {
213212
this.contextRunner.withUserConfiguration(MessageSourceConfiguration.class)
214213
.run((context) -> {
215214
assertThat(context).hasBean("myMessageSource");
216-
assertThat(new DirectFieldAccessor(context.getBean("myMessageSource"))
217-
.getPropertyValue("countsEnabled")).isEqualTo(true);
215+
assertThat(((MessageProcessorMessageSource) context
216+
.getBean("myMessageSource")).isCountsEnabled()).isTrue();
218217
});
219218
}
220219

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/JndiDataSourceAutoConfigurationTests.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2017 the original author or authors.
2+
* Copyright 2012-2018 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.
@@ -27,13 +27,13 @@
2727
import org.junit.Before;
2828
import org.junit.Test;
2929

30-
import org.springframework.beans.DirectFieldAccessor;
3130
import org.springframework.boot.autoconfigure.jndi.JndiPropertiesHidingClassLoader;
3231
import org.springframework.boot.autoconfigure.jndi.TestableInitialContextFactory;
3332
import org.springframework.boot.test.util.TestPropertyValues;
3433
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
3534
import org.springframework.context.annotation.Bean;
3635
import org.springframework.jmx.export.MBeanExporter;
36+
import org.springframework.test.util.ReflectionTestUtils;
3737

3838
import static org.assertj.core.api.Assertions.assertThat;
3939
import static org.mockito.Mockito.mock;
@@ -110,8 +110,8 @@ public void mbeanDataSourceIsExcludedFromExport()
110110

111111
assertThat(this.context.getBean(DataSource.class)).isEqualTo(dataSource);
112112
MBeanExporter exporter = this.context.getBean(MBeanExporter.class);
113-
Set<String> excludedBeans = (Set<String>) new DirectFieldAccessor(exporter)
114-
.getPropertyValue("excludedBeans");
113+
Set<String> excludedBeans = (Set<String>) ReflectionTestUtils.getField(exporter,
114+
"excludedBeans");
115115
assertThat(excludedBeans).containsExactly("dataSource");
116116
}
117117

@@ -130,8 +130,8 @@ public void mbeanDataSourceIsExcludedFromExportByAllExporters()
130130
assertThat(this.context.getBean(DataSource.class)).isEqualTo(dataSource);
131131
for (MBeanExporter exporter : this.context.getBeansOfType(MBeanExporter.class)
132132
.values()) {
133-
Set<String> excludedBeans = (Set<String>) new DirectFieldAccessor(exporter)
134-
.getPropertyValue("excludedBeans");
133+
Set<String> excludedBeans = (Set<String>) ReflectionTestUtils
134+
.getField(exporter, "excludedBeans");
135135
assertThat(excludedBeans).containsExactly("dataSource");
136136
}
137137
}
@@ -151,8 +151,8 @@ public void standardDataSourceIsNotExcludedFromExport()
151151

152152
assertThat(this.context.getBean(DataSource.class)).isEqualTo(dataSource);
153153
MBeanExporter exporter = this.context.getBean(MBeanExporter.class);
154-
Set<String> excludedBeans = (Set<String>) new DirectFieldAccessor(exporter)
155-
.getPropertyValue("excludedBeans");
154+
Set<String> excludedBeans = (Set<String>) ReflectionTestUtils.getField(exporter,
155+
"excludedBeans");
156156
assertThat(excludedBeans).isEmpty();
157157
}
158158

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jersey/JerseyAutoConfigurationCustomLoadOnStartupTests.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2017 the original author or authors.
2+
* Copyright 2012-2018 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.
@@ -26,7 +26,6 @@
2626
import org.junit.Test;
2727
import org.junit.runner.RunWith;
2828

29-
import org.springframework.beans.DirectFieldAccessor;
3029
import org.springframework.beans.factory.annotation.Autowired;
3130
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
3231
import org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryAutoConfiguration;
@@ -55,9 +54,8 @@ public class JerseyAutoConfigurationCustomLoadOnStartupTests {
5554

5655
@Test
5756
public void contextLoads() {
58-
assertThat(
59-
new DirectFieldAccessor(this.context.getBean("jerseyServletRegistration"))
60-
.getPropertyValue("loadOnStartup")).isEqualTo(5);
57+
assertThat(this.context.getBean("jerseyServletRegistration"))
58+
.hasFieldOrPropertyWithValue("loadOnStartup", 5);
6159
}
6260

6361
@MinimalWebConfiguration

0 commit comments

Comments
 (0)