17
17
package org .springframework .integration .dispatcher ;
18
18
19
19
import static org .assertj .core .api .Assertions .assertThat ;
20
- import static org .assertj .core .api .Assertions .fail ;
20
+ import static org .assertj .core .api .Assertions .assertThatExceptionOfType ;
21
+ import static org .mockito .ArgumentMatchers .eq ;
22
+ import static org .mockito .ArgumentMatchers .startsWith ;
23
+ import static org .mockito .BDDMockito .given ;
21
24
import static org .mockito .Mockito .atLeast ;
22
25
import static org .mockito .Mockito .doThrow ;
26
+ import static org .mockito .Mockito .spy ;
23
27
import static org .mockito .Mockito .times ;
24
28
import static org .mockito .Mockito .verify ;
25
29
26
30
import java .util .concurrent .atomic .AtomicInteger ;
27
31
32
+ import org .apache .commons .logging .Log ;
28
33
import org .junit .Before ;
29
34
import org .junit .Test ;
30
35
import org .junit .runner .RunWith ;
33
38
34
39
import org .springframework .beans .DirectFieldAccessor ;
35
40
import org .springframework .integration .support .MessageBuilder ;
41
+ import org .springframework .integration .test .util .TestUtils ;
36
42
import org .springframework .messaging .Message ;
37
43
import org .springframework .messaging .MessageHandler ;
38
44
import org .springframework .messaging .MessagingException ;
41
47
* @author Iwein Fuld
42
48
* @author Mark Fisher
43
49
* @author Gary Russell
50
+ * @author Artem Bilan
44
51
*/
45
52
@ RunWith (MockitoJUnitRunner .class )
46
53
public class RoundRobinDispatcherTests {
@@ -63,44 +70,44 @@ public void setupDispatcher() {
63
70
64
71
65
72
@ Test
66
- public void dispatchMessageWithSingleHandler () throws Exception {
67
- dispatcher .addHandler (handler );
68
- dispatcher .dispatch (message );
73
+ public void dispatchMessageWithSingleHandler () {
74
+ this .dispatcher .addHandler (this .handler );
75
+ this .dispatcher .dispatch (this .message );
76
+ verify (this .handler ).handleMessage (this .message );
69
77
}
70
78
71
79
@ Test
72
- public void differentHandlerInvokedOnSecondMessage () throws Exception {
73
- dispatcher .addHandler (handler );
74
- dispatcher .addHandler (differentHandler );
75
- dispatcher .dispatch (message );
76
- dispatcher .dispatch (message );
77
- verify (handler ).handleMessage (message );
78
- verify (differentHandler ).handleMessage (message );
80
+ public void differentHandlerInvokedOnSecondMessage () {
81
+ this . dispatcher .addHandler (this . handler );
82
+ this . dispatcher .addHandler (this . differentHandler );
83
+ this . dispatcher .dispatch (this . message );
84
+ this . dispatcher .dispatch (this . message );
85
+ verify (this . handler ).handleMessage (this . message );
86
+ verify (this . differentHandler ).handleMessage (this . message );
79
87
}
80
88
81
89
@ Test
82
- public void multipleCyclesThroughHandlers () throws Exception {
83
- dispatcher .addHandler (handler );
84
- dispatcher .addHandler (differentHandler );
90
+ public void multipleCyclesThroughHandlers () {
91
+ this . dispatcher .addHandler (this . handler );
92
+ this . dispatcher .addHandler (this . differentHandler );
85
93
for (int i = 0 ; i < 7 ; i ++) {
86
- dispatcher .dispatch (message );
94
+ this . dispatcher .dispatch (this . message );
87
95
}
88
- verify (handler , times (4 )).handleMessage (message );
89
- verify (differentHandler , times (3 )).handleMessage (message );
96
+ verify (this . handler , times (4 )).handleMessage (this . message );
97
+ verify (this . differentHandler , times (3 )).handleMessage (this . message );
90
98
}
91
99
92
100
@ Test
93
- public void currentHandlerIndexOverFlow () throws Exception {
94
- dispatcher .addHandler (handler );
95
- dispatcher .addHandler (differentHandler );
96
- DirectFieldAccessor accessor = new DirectFieldAccessor (
97
- new DirectFieldAccessor (dispatcher ).getPropertyValue ("loadBalancingStrategy" ));
98
- ((AtomicInteger ) accessor .getPropertyValue ("currentHandlerIndex" )).set (Integer .MAX_VALUE - 5 );
101
+ public void currentHandlerIndexOverFlow () {
102
+ this .dispatcher .addHandler (this .handler );
103
+ this .dispatcher .addHandler (this .differentHandler );
104
+ TestUtils .getPropertyValue (this .dispatcher , "loadBalancingStrategy.currentHandlerIndex" , AtomicInteger .class )
105
+ .set (Integer .MAX_VALUE - 5 );
99
106
for (int i = 0 ; i < 40 ; i ++) {
100
- dispatcher .dispatch (message );
107
+ this . dispatcher .dispatch (this . message );
101
108
}
102
- verify (handler , atLeast (18 )).handleMessage (message );
103
- verify (differentHandler , atLeast (18 )).handleMessage (message );
109
+ verify (this . handler , atLeast (18 )).handleMessage (this . message );
110
+ verify (this . differentHandler , atLeast (18 )).handleMessage (this . message );
104
111
}
105
112
106
113
/**
@@ -109,16 +116,14 @@ public void currentHandlerIndexOverFlow() throws Exception {
109
116
*/
110
117
@ Test
111
118
public void testExceptionEnhancement () {
112
- dispatcher .addHandler (handler );
113
- doThrow (new MessagingException ("Mock Exception" )).
114
- when (handler ).handleMessage (message );
115
- try {
116
- dispatcher .dispatch (message );
117
- fail ("Expected Exception" );
118
- }
119
- catch (MessagingException e ) {
120
- assertThat (e .getFailedMessage ()).isEqualTo (message );
121
- }
119
+ this .dispatcher .addHandler (this .handler );
120
+ doThrow (new MessagingException ("Mock Exception" ))
121
+ .when (this .handler )
122
+ .handleMessage (this .message );
123
+
124
+ assertThatExceptionOfType (MessagingException .class )
125
+ .isThrownBy (() -> this .dispatcher .dispatch (this .message ))
126
+ .satisfies (ex -> assertThat (ex .getFailedMessage ()).isEqualTo (this .message ));
122
127
}
123
128
124
129
/**
@@ -127,16 +132,37 @@ public void testExceptionEnhancement() {
127
132
*/
128
133
@ Test
129
134
public void testNoExceptionEnhancement () {
130
- dispatcher .addHandler (handler );
135
+ this . dispatcher .addHandler (this . handler );
131
136
Message <String > dontReplaceThisMessage = MessageBuilder .withPayload ("x" ).build ();
132
- doThrow (new MessagingException (dontReplaceThisMessage , "Mock Exception" )).
133
- when (handler ).handleMessage (message );
134
- try {
135
- dispatcher .dispatch (message );
136
- fail ("Expected Exception" );
137
- }
138
- catch (MessagingException e ) {
139
- assertThat (e .getFailedMessage ()).isEqualTo (dontReplaceThisMessage );
140
- }
137
+ doThrow (new MessagingException (dontReplaceThisMessage , "Mock Exception" ))
138
+ .when (this .handler )
139
+ .handleMessage (this .message );
140
+
141
+ assertThatExceptionOfType (MessagingException .class )
142
+ .isThrownBy (() -> this .dispatcher .dispatch (this .message ))
143
+ .satisfies (ex -> assertThat (ex .getFailedMessage ()).isEqualTo (dontReplaceThisMessage ));
141
144
}
145
+
146
+ @ Test
147
+ public void testFailOverAndLogging () {
148
+ RuntimeException testException = new RuntimeException ("intentional" );
149
+ doThrow (testException )
150
+ .when (this .handler )
151
+ .handleMessage (this .message );
152
+ this .dispatcher .addHandler (this .handler );
153
+ this .dispatcher .addHandler (this .differentHandler );
154
+
155
+ DirectFieldAccessor directFieldAccessor = new DirectFieldAccessor (this .dispatcher );
156
+ Log log = (Log ) spy (directFieldAccessor .getPropertyType ("logger" ));
157
+ given (log .isInfoEnabled ()).willReturn (true );
158
+ directFieldAccessor .setPropertyValue ("logger" , log );
159
+
160
+ this .dispatcher .dispatch (this .message );
161
+
162
+ verify (this .handler ).handleMessage (this .message );
163
+ verify (this .differentHandler ).handleMessage (this .message );
164
+
165
+ verify (log ).info (startsWith ("An exception thrown from the" ), eq (testException ));
166
+ }
167
+
142
168
}
0 commit comments