18
18
19
19
import static org .assertj .core .api .Assertions .assertThat ;
20
20
import static org .assertj .core .api .Assertions .assertThatExceptionOfType ;
21
- import static org .mockito .Mockito .mock ;
22
- import static org .mockito .Mockito .when ;
23
21
24
- import java .lang .reflect .Method ;
25
22
import java .util .Properties ;
26
23
27
24
import jakarta .jms .DeliveryMode ;
28
- import jakarta .jms .Destination ;
29
- import jakarta .jms .Queue ;
30
- import jakarta .jms .Session ;
31
25
32
26
import org .junit .jupiter .api .Test ;
33
27
34
- import org .springframework .beans .DirectFieldAccessor ;
35
- import org .springframework .beans .NotReadablePropertyException ;
36
28
import org .springframework .beans .factory .parsing .BeanDefinitionParsingException ;
37
29
import org .springframework .context .support .ClassPathXmlApplicationContext ;
38
30
import org .springframework .expression .Expression ;
39
31
import org .springframework .integration .core .MessagingTemplate ;
40
- import org .springframework .integration .endpoint .EventDrivenConsumer ;
41
- import org .springframework .integration .endpoint .PollingConsumer ;
42
32
import org .springframework .integration .handler .ExpressionEvaluatingMessageProcessor ;
43
33
import org .springframework .integration .handler .advice .AbstractRequestHandlerAdvice ;
44
34
import org .springframework .integration .history .MessageHistory ;
45
35
import org .springframework .integration .jms .ActiveMQMultiContextTests ;
46
36
import org .springframework .integration .jms .JmsOutboundGateway ;
47
37
import org .springframework .integration .jms .StubMessageConverter ;
48
- import org .springframework .integration .support .MessageBuilder ;
49
38
import org .springframework .integration .test .util .TestUtils ;
50
39
import org .springframework .jms .listener .DefaultMessageListenerContainer ;
51
- import org .springframework .jms .support .converter .MessageConverter ;
52
40
import org .springframework .messaging .Message ;
53
41
import org .springframework .messaging .MessageChannel ;
54
42
import org .springframework .messaging .MessageHandler ;
@@ -67,145 +55,90 @@ public class JmsOutboundGatewayParserTests extends ActiveMQMultiContextTests {
67
55
68
56
@ Test
69
57
public void testWithDeliveryPersistentAttribute () {
70
- ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext (
71
- "jmsOutboundGatewayWithDeliveryPersistent.xml" , this .getClass ());
72
- EventDrivenConsumer endpoint = (EventDrivenConsumer ) context .getBean ("jmsGateway" );
73
- DirectFieldAccessor accessor = new DirectFieldAccessor (endpoint );
74
- JmsOutboundGateway gateway = (JmsOutboundGateway ) accessor .getPropertyValue ("handler" );
75
- accessor = new DirectFieldAccessor (gateway );
76
- int deliveryMode = (Integer ) accessor .getPropertyValue ("deliveryMode" );
77
- assertThat (deliveryMode ).isEqualTo (DeliveryMode .PERSISTENT );
78
- assertThat (TestUtils .getPropertyValue (gateway , "async" , Boolean .class )).isTrue ();
79
- DefaultMessageListenerContainer container = TestUtils .getPropertyValue (gateway , "replyContainer" ,
80
- DefaultMessageListenerContainer .class );
81
- assertThat (TestUtils .getPropertyValue (container , "concurrentConsumers" )).isEqualTo (4 );
82
- assertThat (TestUtils .getPropertyValue (container , "maxConcurrentConsumers" )).isEqualTo (5 );
83
- assertThat (TestUtils .getPropertyValue (container , "maxMessagesPerTask" )).isEqualTo (10 );
84
- assertThat (TestUtils .getPropertyValue (container , "receiveTimeout" )).isEqualTo (2000L );
85
- Object recoveryInterval ;
86
- try {
87
- recoveryInterval = TestUtils .getPropertyValue (container , "recoveryInterval" );
58
+ try (var context = new ClassPathXmlApplicationContext (
59
+ "jmsOutboundGatewayWithDeliveryPersistent.xml" , getClass ())) {
60
+
61
+ var endpoint = context .getBean ("jmsGateway" );
62
+ var gateway = TestUtils .getPropertyValue (endpoint , "handler" );
63
+ assertThat (TestUtils .getPropertyValue (gateway , "deliveryMode" )).isEqualTo (DeliveryMode .PERSISTENT );
64
+ assertThat (TestUtils .getPropertyValue (gateway , "async" , Boolean .class )).isTrue ();
65
+ var container = TestUtils .getPropertyValue (gateway , "replyContainer" ,
66
+ DefaultMessageListenerContainer .class );
67
+ assertThat (TestUtils .getPropertyValue (container , "concurrentConsumers" )).isEqualTo (4 );
68
+ assertThat (TestUtils .getPropertyValue (container , "maxConcurrentConsumers" )).isEqualTo (5 );
69
+ assertThat (TestUtils .getPropertyValue (container , "maxMessagesPerTask" )).isEqualTo (10 );
70
+ assertThat (TestUtils .getPropertyValue (container , "receiveTimeout" )).isEqualTo (2000L );
71
+ assertThat (TestUtils .getPropertyValue (container , "backOff.interval" )).isEqualTo (10000L );
72
+ assertThat (TestUtils .getPropertyValue (container , "idleConsumerLimit" )).isEqualTo (7 );
73
+ assertThat (TestUtils .getPropertyValue (container , "idleTaskExecutionLimit" )).isEqualTo (2 );
74
+ assertThat (TestUtils .getPropertyValue (container , "cacheLevel" )).isEqualTo (3 );
75
+ assertThat (container .isSessionTransacted ()).isTrue ();
76
+ assertThat (TestUtils .getPropertyValue (container , "taskExecutor" )).isSameAs (context .getBean ("exec" ));
77
+ assertThat (TestUtils .getPropertyValue (gateway , "idleReplyContainerTimeout" )).isEqualTo (1234000L );
88
78
}
89
- catch (NotReadablePropertyException e ) {
90
- recoveryInterval = TestUtils .getPropertyValue (container , "backOff.interval" );
91
- }
92
- assertThat (recoveryInterval ).isEqualTo (10000L );
93
-
94
- assertThat (TestUtils .getPropertyValue (container , "idleConsumerLimit" )).isEqualTo (7 );
95
- assertThat (TestUtils .getPropertyValue (container , "idleTaskExecutionLimit" )).isEqualTo (2 );
96
- assertThat (TestUtils .getPropertyValue (container , "cacheLevel" )).isEqualTo (3 );
97
- assertThat (container .isSessionTransacted ()).isTrue ();
98
- assertThat (TestUtils .getPropertyValue (container , "taskExecutor" )).isSameAs (context .getBean ("exec" ));
99
- assertThat (TestUtils .getPropertyValue (gateway , "idleReplyContainerTimeout" )).isEqualTo (1234000L );
100
- context .close ();
101
79
}
102
80
103
81
@ Test
104
82
public void testAdvised () {
105
- ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext (
106
- "jmsOutboundGatewayWithDeliveryPersistent.xml" , this .getClass ());
107
- EventDrivenConsumer endpoint = (EventDrivenConsumer ) context .getBean ("advised" );
108
- JmsOutboundGateway gateway = TestUtils .getPropertyValue (endpoint , "handler" , JmsOutboundGateway .class );
109
- assertThat (TestUtils .getPropertyValue (gateway , "async" , Boolean .class )).isFalse ();
110
- gateway .handleMessage (new GenericMessage <>("foo" ));
111
- assertThat (adviceCalled ).isEqualTo (1 );
112
- assertThat (TestUtils .getPropertyValue (gateway , "replyContainer.sessionAcknowledgeMode" )).isEqualTo (3 );
113
- context .close ();
83
+ try (var context = new ClassPathXmlApplicationContext (
84
+ "jmsOutboundGatewayWithDeliveryPersistent.xml" , getClass ())) {
85
+
86
+ var endpoint = context .getBean ("advised" );
87
+ JmsOutboundGateway gateway = TestUtils .getPropertyValue (endpoint , "handler" , JmsOutboundGateway .class );
88
+ assertThat (TestUtils .getPropertyValue (gateway , "async" , Boolean .class )).isFalse ();
89
+ gateway .handleMessage (new GenericMessage <>("foo" ));
90
+ assertThat (adviceCalled ).isEqualTo (1 );
91
+ assertThat (TestUtils .getPropertyValue (gateway , "replyContainer.sessionAcknowledgeMode" )).isEqualTo (3 );
92
+ }
114
93
}
115
94
116
95
@ Test
117
96
public void testDefault () {
118
- ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext (
119
- "jmsOutboundGatewayWithConverter.xml" , this .getClass ());
120
- PollingConsumer endpoint = (PollingConsumer ) context .getBean ("jmsGateway" );
121
- DirectFieldAccessor accessor = new DirectFieldAccessor (endpoint );
122
- JmsOutboundGateway gateway = (JmsOutboundGateway ) accessor .getPropertyValue ("handler" );
123
- accessor = new DirectFieldAccessor (gateway );
124
- MessageConverter converter = (MessageConverter ) accessor .getPropertyValue ("messageConverter" );
125
- assertThat (converter instanceof StubMessageConverter ).as ("Wrong message converter" ).isTrue ();
126
- context .close ();
97
+ try (var context = new ClassPathXmlApplicationContext ("jmsOutboundGatewayWithConverter.xml" , getClass ())) {
98
+ var endpoint = context .getBean ("jmsGateway" );
99
+ assertThat (TestUtils .getPropertyValue (endpoint , "handler.messageConverter" ))
100
+ .isInstanceOf (StubMessageConverter .class );
101
+ }
127
102
}
128
103
129
104
@ Test
130
105
public void gatewayWithOrder () {
131
- ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext (
132
- "jmsOutboundGatewayWithOrder.xml" , this .getClass ());
133
- EventDrivenConsumer endpoint = (EventDrivenConsumer ) context .getBean ("jmsGateway" );
134
- DirectFieldAccessor accessor = new DirectFieldAccessor (
135
- new DirectFieldAccessor (endpoint ).getPropertyValue ("handler" ));
136
- Object order = accessor .getPropertyValue ("order" );
137
- assertThat (order ).isEqualTo (99 );
138
- assertThat (accessor .getPropertyValue ("requiresReply" )).isEqualTo (Boolean .TRUE );
139
- context .close ();
106
+ try (var context = new ClassPathXmlApplicationContext ("jmsOutboundGatewayWithOrder.xml" , getClass ())) {
107
+ var endpoint = context .getBean ("jmsGateway" );
108
+ assertThat (TestUtils .getPropertyValue (endpoint , "handler.requiresReply" )).isEqualTo (Boolean .TRUE );
109
+ assertThat (TestUtils .getPropertyValue (endpoint , "handler.order" )).isEqualTo (99 );
110
+ }
140
111
}
141
112
142
113
@ Test
143
114
public void gatewayWithDest () {
144
- ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext (
145
- "jmsOutboundGatewayReplyDestOptions.xml" , this .getClass ());
146
- EventDrivenConsumer endpoint = (EventDrivenConsumer ) context .getBean ("jmsGatewayDest" );
147
- DirectFieldAccessor accessor = new DirectFieldAccessor (endpoint );
148
- JmsOutboundGateway gateway = (JmsOutboundGateway ) accessor .getPropertyValue ("handler" );
149
- accessor = new DirectFieldAccessor (gateway );
150
- assertThat (accessor .getPropertyValue ("replyDestination" )).isSameAs (context .getBean ("replyQueue" ));
151
- context .close ();
115
+ try (var context = new ClassPathXmlApplicationContext ("jmsOutboundGatewayReplyDestOptions.xml" , getClass ())) {
116
+ var endpoint = context .getBean ("jmsGatewayDest" );
117
+ assertThat (TestUtils .getPropertyValue (endpoint , "handler.replyDestination" ))
118
+ .isSameAs (context .getBean ("replyQueue" ));
119
+ }
152
120
}
153
121
154
122
@ Test
155
123
public void gatewayWithDestName () {
156
- ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext (
157
- "jmsOutboundGatewayReplyDestOptions.xml" , this .getClass ());
158
- EventDrivenConsumer endpoint = (EventDrivenConsumer ) context .getBean ("jmsGatewayDestName" );
159
- DirectFieldAccessor accessor = new DirectFieldAccessor (endpoint );
160
- JmsOutboundGateway gateway = (JmsOutboundGateway ) accessor .getPropertyValue ("handler" );
161
- accessor = new DirectFieldAccessor (gateway );
162
- assertThat (accessor .getPropertyValue ("replyDestinationName" )).isEqualTo ("replyQueueName" );
163
- context .close ();
164
- }
165
-
166
- @ Test
167
- public void gatewayWithDestExpression () throws Exception {
168
- ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext (
169
- "jmsOutboundGatewayReplyDestOptions.xml" , this .getClass ());
170
- EventDrivenConsumer endpoint = (EventDrivenConsumer ) context .getBean ("jmsGatewayDestExpression" );
171
- DirectFieldAccessor accessor = new DirectFieldAccessor (endpoint );
172
- JmsOutboundGateway gateway = (JmsOutboundGateway ) accessor .getPropertyValue ("handler" );
173
- ExpressionEvaluatingMessageProcessor <?> processor =
174
- TestUtils .getPropertyValue (gateway , "replyDestinationExpressionProcessor" ,
175
- ExpressionEvaluatingMessageProcessor .class );
176
- Expression expression = TestUtils .getPropertyValue (gateway , "replyDestinationExpressionProcessor.expression" ,
177
- Expression .class );
178
- assertThat (expression .getExpressionString ()).isEqualTo ("payload" );
179
- Message <?> message = MessageBuilder .withPayload ("foo" ).build ();
180
- assertThat (processor .processMessage (message )).isEqualTo ("foo" );
181
-
182
- Method method =
183
- JmsOutboundGateway .class .getDeclaredMethod ("determineReplyDestination" , Message .class , Session .class );
184
- method .setAccessible (true );
185
-
186
- Session session = mock (Session .class );
187
- Queue queue = mock (Queue .class );
188
- when (session .createQueue ("foo" )).thenReturn (queue );
189
- Destination replyQ = (Destination ) method .invoke (gateway , message , session );
190
- assertThat (replyQ ).isSameAs (queue );
191
- context .close ();
124
+ try (var context = new ClassPathXmlApplicationContext ("jmsOutboundGatewayReplyDestOptions.xml" , getClass ())) {
125
+ var endpoint = context .getBean ("jmsGatewayDestName" );
126
+ assertThat (TestUtils .getPropertyValue (endpoint , "handler.replyDestinationName" )).isEqualTo ("replyQueueName" );
127
+ }
192
128
}
193
129
194
130
@ Test
195
131
public void gatewayWithDestBeanRefExpression () {
196
- ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext (
197
- "jmsOutboundGatewayReplyDestOptions.xml" , this .getClass ());
198
- EventDrivenConsumer endpoint = (EventDrivenConsumer ) context .getBean ("jmsGatewayDestExpressionBeanRef" );
199
- DirectFieldAccessor accessor = new DirectFieldAccessor (endpoint );
200
- JmsOutboundGateway gateway = (JmsOutboundGateway ) accessor .getPropertyValue ("handler" );
201
- ExpressionEvaluatingMessageProcessor <?> processor =
202
- TestUtils .getPropertyValue (gateway , "replyDestinationExpressionProcessor" ,
203
- ExpressionEvaluatingMessageProcessor .class );
204
- Expression expression = TestUtils .getPropertyValue (gateway , "replyDestinationExpressionProcessor.expression" ,
205
- Expression .class );
206
- assertThat (expression .getExpressionString ()).isEqualTo ("@replyQueue" );
207
- assertThat (processor .processMessage (null )).isSameAs (context .getBean ("replyQueue" ));
208
- context .close ();
132
+ try (var context = new ClassPathXmlApplicationContext ("jmsOutboundGatewayReplyDestOptions.xml" , getClass ())) {
133
+ var endpoint = context .getBean ("jmsGatewayDestExpressionBeanRef" );
134
+ var processor =
135
+ TestUtils .getPropertyValue (endpoint , "handler.replyDestinationExpressionProcessor" ,
136
+ ExpressionEvaluatingMessageProcessor .class );
137
+ var expression = TestUtils .getPropertyValue (endpoint ,
138
+ "handler.replyDestinationExpressionProcessor.expression" , Expression .class );
139
+ assertThat (expression .getExpressionString ()).isEqualTo ("@replyQueue" );
140
+ assertThat (processor .processMessage (null )).isSameAs (context .getBean ("replyQueue" ));
141
+ }
209
142
}
210
143
211
144
@ Test
@@ -220,20 +153,20 @@ public void gatewayWithDestAndDestExpression() {
220
153
221
154
@ Test
222
155
public void gatewayMaintainsReplyChannelAndInboundHistory () {
223
- ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext (
224
- "gatewayMaintainsReplyChannel.xml" , this .getClass ());
156
+ var context = new ClassPathXmlApplicationContext ("gatewayMaintainsReplyChannel.xml" , getClass ());
225
157
SampleGateway gateway = context .getBean ("gateway" , SampleGateway .class );
226
158
SubscribableChannel jmsInput = context .getBean ("jmsInput" , SubscribableChannel .class );
227
- MessageHandler handler = message -> {
228
- MessageHistory history = MessageHistory .read (message );
229
- assertThat (history ).isNotNull ();
230
- Properties componentHistoryRecord = TestUtils .locateComponentInHistory (history , "inboundGateway" , 0 );
231
- assertThat (componentHistoryRecord ).isNotNull ();
232
- assertThat (componentHistoryRecord .get ("type" )).isEqualTo ("jms:inbound-gateway" );
233
- MessagingTemplate messagingTemplate = new MessagingTemplate ();
234
- messagingTemplate .setDefaultDestination ((MessageChannel ) message .getHeaders ().getReplyChannel ());
235
- messagingTemplate .send (message );
236
- };
159
+ MessageHandler handler =
160
+ message -> {
161
+ MessageHistory history = MessageHistory .read (message );
162
+ assertThat (history ).isNotNull ();
163
+ Properties componentHistoryRecord = TestUtils .locateComponentInHistory (history , "inboundGateway" , 0 );
164
+ assertThat (componentHistoryRecord ).isNotNull ();
165
+ assertThat (componentHistoryRecord .get ("type" )).isEqualTo ("jms:inbound-gateway" );
166
+ MessagingTemplate messagingTemplate = new MessagingTemplate ();
167
+ messagingTemplate .setDefaultDestination ((MessageChannel ) message .getHeaders ().getReplyChannel ());
168
+ messagingTemplate .send (message );
169
+ };
237
170
jmsInput .subscribe (handler );
238
171
String result = gateway .echo ("hello" );
239
172
assertThat (result ).isEqualTo ("hello" );
@@ -248,26 +181,20 @@ public void gatewayMaintainsReplyChannelAndInboundHistory() {
248
181
249
182
@ Test
250
183
public void gatewayWithDefaultPubSubDomain () {
251
- ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext (
252
- "jmsOutboundGatewayWithPubSubSettings.xml" , this .getClass ());
253
- EventDrivenConsumer endpoint = (EventDrivenConsumer ) context .getBean ("defaultGateway" );
254
- DirectFieldAccessor accessor = new DirectFieldAccessor (
255
- new DirectFieldAccessor (endpoint ).getPropertyValue ("handler" ));
256
- assertThat ((Boolean ) accessor .getPropertyValue ("requestPubSubDomain" )).isFalse ();
257
- assertThat ((Boolean ) accessor .getPropertyValue ("replyPubSubDomain" )).isFalse ();
258
- context .close ();
184
+ try (var context = new ClassPathXmlApplicationContext ("jmsOutboundGatewayWithPubSubSettings.xml" , getClass ())) {
185
+ var endpoint = context .getBean ("defaultGateway" );
186
+ assertThat (TestUtils .getPropertyValue (endpoint , "handler.requestPubSubDomain" , Boolean .class )).isFalse ();
187
+ assertThat (TestUtils .getPropertyValue (endpoint , "handler.replyPubSubDomain" , Boolean .class )).isFalse ();
188
+ }
259
189
}
260
190
261
191
@ Test
262
192
public void gatewayWithExplicitPubSubDomainTrue () {
263
- ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext (
264
- "jmsOutboundGatewayWithPubSubSettings.xml" , this .getClass ());
265
- EventDrivenConsumer endpoint = (EventDrivenConsumer ) context .getBean ("pubSubDomainGateway" );
266
- DirectFieldAccessor accessor = new DirectFieldAccessor (
267
- new DirectFieldAccessor (endpoint ).getPropertyValue ("handler" ));
268
- assertThat ((Boolean ) accessor .getPropertyValue ("requestPubSubDomain" )).isTrue ();
269
- assertThat ((Boolean ) accessor .getPropertyValue ("replyPubSubDomain" )).isTrue ();
270
- context .close ();
193
+ try (var context = new ClassPathXmlApplicationContext ("jmsOutboundGatewayWithPubSubSettings.xml" , getClass ())) {
194
+ var endpoint = context .getBean ("pubSubDomainGateway" );
195
+ assertThat (TestUtils .getPropertyValue (endpoint , "handler.requestPubSubDomain" , Boolean .class )).isTrue ();
196
+ assertThat (TestUtils .getPropertyValue (endpoint , "handler.replyPubSubDomain" , Boolean .class )).isTrue ();
197
+ }
271
198
}
272
199
273
200
0 commit comments