1
1
/*
2
- * Copyright 2017-2024 the original author or authors.
2
+ * Copyright 2017-2025 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
19
19
import java .util .ArrayList ;
20
20
import java .util .Arrays ;
21
21
import java .util .Collection ;
22
+ import java .util .Collections ;
22
23
import java .util .List ;
23
24
24
25
import reactor .core .publisher .Mono ;
59
60
*
60
61
* @author Artem Bilan
61
62
* @author Yicheng Feng
63
+ * @author Alexander Hain
62
64
*
63
65
* @since 5.0
64
66
*
@@ -111,21 +113,21 @@ public void afterSingletonsInstantiated() {
111
113
}
112
114
113
115
List <AbstractEndpoint > getAutoStartupCandidates () {
114
- return this .autoStartupCandidates ;
116
+ return Collections . unmodifiableList ( this .autoStartupCandidates ) ;
115
117
}
116
118
117
119
/**
118
120
* Reinstate the mocked beans after execution test to their real state.
119
121
* Typically, this method is used from JUnit clean up methods.
122
+ *
120
123
* @param beanNames the bean names to reset.
121
- * If {@code null}, all the mocked beans are reset
124
+ * If {@code null}, all the mocked beans are reset
122
125
*/
123
126
public void resetBeans (String ... beanNames ) {
124
127
final Collection <String > names ;
125
128
if (!ObjectUtils .isEmpty (beanNames )) {
126
129
names = Arrays .asList (beanNames );
127
- }
128
- else {
130
+ } else {
129
131
names = null ;
130
132
}
131
133
@@ -142,8 +144,7 @@ public void resetBeans(String... beanNames) {
142
144
for (String name : beanNames ) {
143
145
this .beans .remove (name );
144
146
}
145
- }
146
- else {
147
+ } else {
147
148
this .beans .clear ();
148
149
}
149
150
}
@@ -157,14 +158,11 @@ private void resetBean(Object endpoint, Object component) {
157
158
}
158
159
if (endpoint instanceof SourcePollingChannelAdapter && component instanceof MessageSource <?>) {
159
160
directFieldAccessor .setPropertyValue ("source" , component );
160
- }
161
- else if (endpoint instanceof IntegrationConsumer && component instanceof MessageHandler ) {
161
+ } else if (endpoint instanceof IntegrationConsumer && component instanceof MessageHandler ) {
162
162
directFieldAccessor .setPropertyValue (HANDLER , component );
163
- }
164
- else if (endpoint instanceof ReactiveStreamsConsumer && component instanceof ReactiveMessageHandler ) {
163
+ } else if (endpoint instanceof ReactiveStreamsConsumer && component instanceof ReactiveMessageHandler ) {
165
164
directFieldAccessor .setPropertyValue (REACTIVE_MESSAGE_HANDLER , component );
166
- }
167
- else if (component instanceof Trigger ) {
165
+ } else if (component instanceof Trigger ) {
168
166
directFieldAccessor .setPropertyValue ("trigger" , component );
169
167
}
170
168
if (lifecycle != null && lifecycle .isAutoStartup ()) {
@@ -176,7 +174,8 @@ else if (component instanceof Trigger) {
176
174
* Replace the real {@link MessageSource} in the {@link SourcePollingChannelAdapter} bean
177
175
* with provided {@link MessageSource} instance.
178
176
* Can be a mock object.
179
- * @param pollingAdapterId the endpoint bean name
177
+ *
178
+ * @param pollingAdapterId the endpoint bean name
180
179
* @param mockMessageSource the {@link MessageSource} to replace in the endpoint bean
181
180
* @see org.springframework.integration.test.mock.MockIntegration#mockMessageSource
182
181
*/
@@ -189,13 +188,14 @@ public void substituteMessageSourceFor(String pollingAdapterId, MessageSource<?>
189
188
* with provided {@link MessageSource} instance.
190
189
* Can be a mock object.
191
190
* The endpoint is not started when {@code autoStartup == false}.
192
- * @param pollingAdapterId the endpoint bean name
191
+ *
192
+ * @param pollingAdapterId the endpoint bean name
193
193
* @param mockMessageSource the {@link MessageSource} to replace in the endpoint bean
194
- * @param autoStartup start or not the endpoint after replacing its {@link MessageSource}
194
+ * @param autoStartup start or not the endpoint after replacing its {@link MessageSource}
195
195
* @see org.springframework.integration.test.mock.MockIntegration#mockMessageSource
196
196
*/
197
197
public void substituteMessageSourceFor (String pollingAdapterId , MessageSource <?> mockMessageSource ,
198
- boolean autoStartup ) {
198
+ boolean autoStartup ) {
199
199
200
200
substituteComponentFor (pollingAdapterId , mockMessageSource , SourcePollingChannelAdapter .class , "source" ,
201
201
autoStartup );
@@ -206,7 +206,7 @@ public void substituteMessageHandlerFor(String consumerEndpointId, MessageHandle
206
206
}
207
207
208
208
public void substituteMessageHandlerFor (String consumerEndpointId , // NOSONAR - complexity
209
- MessageHandler mockMessageHandler , boolean autoStartup ) {
209
+ MessageHandler mockMessageHandler , boolean autoStartup ) {
210
210
211
211
Object endpoint = this .beanFactory .getBean (consumerEndpointId , IntegrationConsumer .class );
212
212
if (autoStartup && endpoint instanceof Lifecycle lifecycle ) {
@@ -227,15 +227,13 @@ public void substituteMessageHandlerFor(String consumerEndpointId, // NOSONAR -
227
227
if (targetMessageHandler instanceof MessageProducer messageProducer ) {
228
228
MessageChannel outputChannel = messageProducer .getOutputChannel ();
229
229
mockMessageProducer .setOutputChannel (outputChannel );
230
- }
231
- else {
230
+ } else {
232
231
if (mockMessageHandler instanceof MockMessageHandler ) {
233
232
if (TestUtils .getPropertyValue (mockMessageHandler , "hasReplies" , Boolean .class )) {
234
233
throw new IllegalStateException ("The [" + mockMessageHandler + "] " +
235
234
"with replies can't replace simple MessageHandler [" + targetMessageHandler + "]" );
236
235
}
237
- }
238
- else {
236
+ } else {
239
237
throw new IllegalStateException ("The MessageProducer handler [" + mockMessageHandler + "] " +
240
238
"can't replace simple MessageHandler [" + targetMessageHandler + "]" );
241
239
}
@@ -257,8 +255,9 @@ public void substituteMessageHandlerFor(String consumerEndpointId, // NOSONAR -
257
255
258
256
/**
259
257
* Replace the real {@link Trigger} in the {@link AbstractPollingEndpoint} bean with provided instance.
258
+ *
260
259
* @param pollingEndpointId the {@link AbstractPollingEndpoint} bean id to replace
261
- * @param trigger the {@link Trigger} to set into {@link AbstractPollingEndpoint}
260
+ * @param trigger the {@link Trigger} to set into {@link AbstractPollingEndpoint}
262
261
* @since 6.3
263
262
*/
264
263
public void substituteTriggerFor (String pollingEndpointId , Trigger trigger ) {
@@ -268,17 +267,18 @@ public void substituteTriggerFor(String pollingEndpointId, Trigger trigger) {
268
267
/**
269
268
* Replace the real {@link Trigger} in the {@link AbstractPollingEndpoint} bean with provided instance.
270
269
* The endpoint is not started when {@code autoStartup == false}.
270
+ *
271
271
* @param pollingEndpointId the {@link AbstractPollingEndpoint} bean id to replace
272
- * @param trigger the {@link Trigger} to set into {@link AbstractPollingEndpoint}
273
- * @param autoStartup start or not the endpoint after replacing its {@link MessageSource}
272
+ * @param trigger the {@link Trigger} to set into {@link AbstractPollingEndpoint}
273
+ * @param autoStartup start or not the endpoint after replacing its {@link MessageSource}
274
274
* @since 6.3
275
275
*/
276
276
public void substituteTriggerFor (String pollingEndpointId , Trigger trigger , boolean autoStartup ) {
277
277
substituteComponentFor (pollingEndpointId , trigger , AbstractPollingEndpoint .class , "trigger" , autoStartup );
278
278
}
279
279
280
280
private void substituteComponentFor (String endpointId , Object messagingComponent , Class <?> endpointClass ,
281
- String property , boolean autoStartup ) {
281
+ String property , boolean autoStartup ) {
282
282
283
283
Object endpoint = this .beanFactory .getBean (endpointId , endpointClass );
284
284
if (autoStartup && endpoint instanceof Lifecycle lifecycle ) {
0 commit comments