33
33
* @since 3.1
34
34
*/
35
35
public class BasicConversationTests {
36
+ private static final String TEST_BEAN1_NAME = "testBean1" ;
37
+ private static final String TEST_BEAN2_NAME = "testBean2" ;
38
+
36
39
private static ConfigurableApplicationContext context ;
37
40
private static ConversationManager manager ;
38
41
private static ConversationStore store ;
@@ -56,15 +59,15 @@ public void testContext() {
56
59
57
60
@ Test
58
61
public void testTemporaryConversation () {
59
- ConversationalBean bean = (ConversationalBean ) context .getBean ("testBean" );
62
+ ConversationalBean bean = (ConversationalBean ) context .getBean (TEST_BEAN1_NAME );
60
63
assertNotNull (bean );
61
64
assertNull (bean .getName ());
62
65
String id = resolver .getCurrentConversationId ();
63
66
assertNotNull (id );
64
67
Conversation conversation = store .getConversation (id );
65
68
assertNotNull (conversation );
66
69
assertTrue (conversation .isTemporary ());
67
- Object attribute = conversation .getAttribute ("testBean" );
70
+ Object attribute = conversation .getAttribute (TEST_BEAN1_NAME );
68
71
assertNotNull (attribute );
69
72
assertSame (bean , attribute );
70
73
@@ -80,7 +83,7 @@ public void testNewConversation() {
80
83
assertFalse (conversation .isTemporary ());
81
84
assertSame (conversation , manager .getCurrentConversation ());
82
85
83
- ConversationalBean bean = (ConversationalBean ) context .getBean ("testBean" );
86
+ ConversationalBean bean = (ConversationalBean ) context .getBean (TEST_BEAN1_NAME );
84
87
assertNotNull (bean );
85
88
86
89
conversation .end (ConversationEndingType .SUCCESS );
@@ -96,7 +99,7 @@ public void testRootConversation() {
96
99
assertFalse (conversation .isTemporary ());
97
100
assertSame (conversation , manager .getCurrentConversation ());
98
101
99
- ConversationalBean bean = (ConversationalBean ) context .getBean ("testBean" );
102
+ ConversationalBean bean = (ConversationalBean ) context .getBean (TEST_BEAN1_NAME );
100
103
assertNotNull (bean );
101
104
102
105
conversation .end (ConversationEndingType .SUCCESS );
@@ -135,7 +138,7 @@ public void testNestedConversation() {
135
138
assertFalse (conversation .isNested ());
136
139
assertFalse (((MutableConversation ) conversation ).isParent ());
137
140
138
- ConversationalBean bean = (ConversationalBean ) context .getBean ("testBean" );
141
+ ConversationalBean bean = (ConversationalBean ) context .getBean (TEST_BEAN1_NAME );
139
142
assertNotNull (bean );
140
143
141
144
Conversation nestedConversation = manager .beginConversation (false , JoinMode .NESTED );
@@ -145,7 +148,7 @@ public void testNestedConversation() {
145
148
assertTrue (nestedConversation .isNested ());
146
149
assertTrue (((MutableConversation ) conversation ).isParent ());
147
150
148
- assertSame (bean , context .getBean ("testBean" ));
151
+ assertSame (bean , context .getBean (TEST_BEAN1_NAME ));
149
152
150
153
nestedConversation .end (ConversationEndingType .SUCCESS );
151
154
assertSame (conversation , manager .getCurrentConversation ());
@@ -199,7 +202,7 @@ public void testIsolatedConversation() {
199
202
assertFalse (conversation .isNested ());
200
203
assertFalse (((MutableConversation ) conversation ).isParent ());
201
204
202
- ConversationalBean bean = (ConversationalBean ) context .getBean ("testBean" );
205
+ ConversationalBean bean = (ConversationalBean ) context .getBean (TEST_BEAN1_NAME );
203
206
assertNotNull (bean );
204
207
205
208
Conversation nestedConversation = manager .beginConversation (false , JoinMode .ISOLATED );
@@ -210,12 +213,12 @@ public void testIsolatedConversation() {
210
213
assertTrue (nestedConversation .isNested ());
211
214
assertTrue (((MutableConversation ) conversation ).isParent ());
212
215
213
- assertNotSame (bean , context .getBean ("testBean" ));
216
+ assertNotSame (bean , context .getBean (TEST_BEAN1_NAME ));
214
217
215
218
nestedConversation .end (ConversationEndingType .SUCCESS );
216
219
assertSame (conversation , manager .getCurrentConversation ());
217
220
218
- assertSame (bean , context .getBean ("testBean" ));
221
+ assertSame (bean , context .getBean (TEST_BEAN1_NAME ));
219
222
220
223
conversation .end (ConversationEndingType .SUCCESS );
221
224
assertTrue (conversation .isEnded ());
@@ -234,7 +237,7 @@ public void testJoinedConversation() {
234
237
assertFalse (conversation .isNested ());
235
238
assertFalse (((MutableConversation ) conversation ).isParent ());
236
239
237
- ConversationalBean bean = (ConversationalBean ) context .getBean ("testBean" );
240
+ ConversationalBean bean = (ConversationalBean ) context .getBean (TEST_BEAN1_NAME );
238
241
assertNotNull (bean );
239
242
240
243
Conversation joinedConversation = manager .beginConversation (false , JoinMode .JOINED );
@@ -245,12 +248,12 @@ public void testJoinedConversation() {
245
248
assertFalse (joinedConversation .isNested ());
246
249
assertFalse (((MutableConversation ) joinedConversation ).isParent ());
247
250
248
- assertSame (bean , context .getBean ("testBean" ));
251
+ assertSame (bean , context .getBean (TEST_BEAN1_NAME ));
249
252
250
253
joinedConversation .end (ConversationEndingType .SUCCESS );
251
254
assertSame (conversation , manager .getCurrentConversation ());
252
255
253
- assertSame (bean , context .getBean ("testBean" ));
256
+ assertSame (bean , context .getBean (TEST_BEAN1_NAME ));
254
257
255
258
conversation .end (ConversationEndingType .SUCCESS );
256
259
assertTrue (conversation .isEnded ());
@@ -269,7 +272,7 @@ public void testSwitchedConversation() {
269
272
assertFalse (conversation .isNested ());
270
273
assertFalse (((MutableConversation ) conversation ).isParent ());
271
274
272
- ConversationalBean bean = (ConversationalBean ) context .getBean ("testBean" );
275
+ ConversationalBean bean = (ConversationalBean ) context .getBean (TEST_BEAN1_NAME );
273
276
assertNotNull (bean );
274
277
275
278
Conversation switchedConversation = manager .beginConversation (false , JoinMode .SWITCHED );
@@ -280,15 +283,15 @@ public void testSwitchedConversation() {
280
283
assertFalse (switchedConversation .isNested ());
281
284
assertFalse (((MutableConversation ) switchedConversation ).isParent ());
282
285
283
- ConversationalBean bean2 = (ConversationalBean ) context .getBean ("testBean" );
286
+ ConversationalBean bean2 = (ConversationalBean ) context .getBean (TEST_BEAN1_NAME );
284
287
assertNotSame (bean , bean2 );
285
288
286
289
manager .switchConversation (conversation .getId ());
287
290
assertSame (conversation , manager .getCurrentConversation ());
288
- assertSame (bean , context .getBean ("testBean" ));
291
+ assertSame (bean , context .getBean (TEST_BEAN1_NAME ));
289
292
290
293
manager .switchConversation (switchedConversation .getId ());
291
- assertSame (bean2 , context .getBean ("testBean" ));
294
+ assertSame (bean2 , context .getBean (TEST_BEAN1_NAME ));
292
295
293
296
switchedConversation .end (ConversationEndingType .SUCCESS );
294
297
conversation .end (ConversationEndingType .SUCCESS );
@@ -345,7 +348,7 @@ public void testImplicitConversationEnding() {
345
348
}
346
349
347
350
@ Test
348
- public void testConversationAnnotation () {
351
+ public void testConversationAnnotation1 () {
349
352
ConversationalServiceBean serviceBean = context .getBean (ConversationalServiceBean .class );
350
353
assertNotNull (serviceBean );
351
354
@@ -378,6 +381,87 @@ public void testConversationAnnotation() {
378
381
serviceBean .clean ();
379
382
}
380
383
384
+ @ Test
385
+ public void testConversationAnnotation2 () {
386
+ ConversationalServiceBean serviceBean = context .getBean (ConversationalServiceBean .class );
387
+ assertNotNull (serviceBean );
388
+
389
+ serviceBean .startConversation ();
390
+ Conversation conversation = manager .getCurrentConversation ();
391
+ assertNotNull (conversation );
392
+
393
+ ConversationEventAwareBean bean = (ConversationEventAwareBean ) context .getBean (TEST_BEAN2_NAME );
394
+ assertNotNull (bean );
395
+ assertNull (bean .getConversation1 ());
396
+ assertNull (bean .getConversation2 ());
397
+ assertNull (bean .getActivationType ());
398
+ assertNull (bean .getDeactivationType ());
399
+ assertNull (bean .getEndingType ());
400
+
401
+ serviceBean .endConversation ();
402
+
403
+ assertNotNull (bean .getEndingType ());
404
+ assertSame (ConversationEndingType .SUCCESS , bean .getEndingType ());
405
+ assertNotNull (bean .getConversation1 ());
406
+ assertSame (conversation , bean .getConversation1 ());
407
+
408
+ serviceBean .clean ();
409
+ }
410
+
411
+ @ Test
412
+ public void testConversationAnnotation3 () {
413
+ ConversationalServiceBean serviceBean = context .getBean (ConversationalServiceBean .class );
414
+ assertNotNull (serviceBean );
415
+ serviceBean .setFailureFlag (true );
416
+
417
+ serviceBean .startConversation ();
418
+ Conversation conversation = manager .getCurrentConversation ();
419
+ assertNotNull (conversation );
420
+
421
+ ConversationEventAwareBean bean = (ConversationEventAwareBean ) context .getBean (TEST_BEAN2_NAME );
422
+ assertNotNull (bean );
423
+
424
+ try {
425
+ serviceBean .endConversationSuccess ();
426
+ } catch (RuntimeException e ) {
427
+ // must happen
428
+ }
429
+
430
+ assertNotNull (bean .getEndingType ());
431
+ assertSame (ConversationEndingType .FAILURE_SUCCESS , bean .getEndingType ());
432
+ assertNotNull (bean .getConversation1 ());
433
+ assertSame (conversation , bean .getConversation1 ());
434
+
435
+ serviceBean .clean ();
436
+ }
437
+
438
+ @ Test
439
+ public void testConversationAnnotation4 () {
440
+ ConversationalServiceBean serviceBean = context .getBean (ConversationalServiceBean .class );
441
+ assertNotNull (serviceBean );
442
+ serviceBean .setFailureFlag (true );
443
+
444
+ serviceBean .startConversation ();
445
+ Conversation conversation = manager .getCurrentConversation ();
446
+ assertNotNull (conversation );
447
+
448
+ ConversationEventAwareBean bean = (ConversationEventAwareBean ) context .getBean (TEST_BEAN2_NAME );
449
+ assertNotNull (bean );
450
+
451
+ try {
452
+ serviceBean .endConversationCancel ();
453
+ } catch (RuntimeException e ) {
454
+ // must happen
455
+ }
456
+
457
+ assertNotNull (bean .getEndingType ());
458
+ assertSame (ConversationEndingType .FAILURE_CANCEL , bean .getEndingType ());
459
+ assertNotNull (bean .getConversation1 ());
460
+ assertSame (conversation , bean .getConversation1 ());
461
+
462
+ serviceBean .clean ();
463
+ }
464
+
381
465
protected static String getContextLocation () {
382
466
return "org/springframework/conversation/conversationTestContext.xml" ;
383
467
}
0 commit comments