Skip to content

Commit 11284ed

Browse files
author
Micha Kiener
committed
SPR-6416, tests
1 parent bede7e9 commit 11284ed

File tree

5 files changed

+213
-18
lines changed

5 files changed

+213
-18
lines changed

org.springframework.context/src/test/java/org/springframework/conversation/BasicConversationTests.java

Lines changed: 101 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@
3333
* @since 3.1
3434
*/
3535
public class BasicConversationTests {
36+
private static final String TEST_BEAN1_NAME = "testBean1";
37+
private static final String TEST_BEAN2_NAME = "testBean2";
38+
3639
private static ConfigurableApplicationContext context;
3740
private static ConversationManager manager;
3841
private static ConversationStore store;
@@ -56,15 +59,15 @@ public void testContext() {
5659

5760
@Test
5861
public void testTemporaryConversation() {
59-
ConversationalBean bean = (ConversationalBean) context.getBean("testBean");
62+
ConversationalBean bean = (ConversationalBean) context.getBean(TEST_BEAN1_NAME);
6063
assertNotNull(bean);
6164
assertNull(bean.getName());
6265
String id = resolver.getCurrentConversationId();
6366
assertNotNull(id);
6467
Conversation conversation = store.getConversation(id);
6568
assertNotNull(conversation);
6669
assertTrue(conversation.isTemporary());
67-
Object attribute = conversation.getAttribute("testBean");
70+
Object attribute = conversation.getAttribute(TEST_BEAN1_NAME);
6871
assertNotNull(attribute);
6972
assertSame(bean, attribute);
7073

@@ -80,7 +83,7 @@ public void testNewConversation() {
8083
assertFalse(conversation.isTemporary());
8184
assertSame(conversation, manager.getCurrentConversation());
8285

83-
ConversationalBean bean = (ConversationalBean) context.getBean("testBean");
86+
ConversationalBean bean = (ConversationalBean) context.getBean(TEST_BEAN1_NAME);
8487
assertNotNull(bean);
8588

8689
conversation.end(ConversationEndingType.SUCCESS);
@@ -96,7 +99,7 @@ public void testRootConversation() {
9699
assertFalse(conversation.isTemporary());
97100
assertSame(conversation, manager.getCurrentConversation());
98101

99-
ConversationalBean bean = (ConversationalBean) context.getBean("testBean");
102+
ConversationalBean bean = (ConversationalBean) context.getBean(TEST_BEAN1_NAME);
100103
assertNotNull(bean);
101104

102105
conversation.end(ConversationEndingType.SUCCESS);
@@ -135,7 +138,7 @@ public void testNestedConversation() {
135138
assertFalse(conversation.isNested());
136139
assertFalse(((MutableConversation) conversation).isParent());
137140

138-
ConversationalBean bean = (ConversationalBean) context.getBean("testBean");
141+
ConversationalBean bean = (ConversationalBean) context.getBean(TEST_BEAN1_NAME);
139142
assertNotNull(bean);
140143

141144
Conversation nestedConversation = manager.beginConversation(false, JoinMode.NESTED);
@@ -145,7 +148,7 @@ public void testNestedConversation() {
145148
assertTrue(nestedConversation.isNested());
146149
assertTrue(((MutableConversation) conversation).isParent());
147150

148-
assertSame(bean, context.getBean("testBean"));
151+
assertSame(bean, context.getBean(TEST_BEAN1_NAME));
149152

150153
nestedConversation.end(ConversationEndingType.SUCCESS);
151154
assertSame(conversation, manager.getCurrentConversation());
@@ -199,7 +202,7 @@ public void testIsolatedConversation() {
199202
assertFalse(conversation.isNested());
200203
assertFalse(((MutableConversation) conversation).isParent());
201204

202-
ConversationalBean bean = (ConversationalBean) context.getBean("testBean");
205+
ConversationalBean bean = (ConversationalBean) context.getBean(TEST_BEAN1_NAME);
203206
assertNotNull(bean);
204207

205208
Conversation nestedConversation = manager.beginConversation(false, JoinMode.ISOLATED);
@@ -210,12 +213,12 @@ public void testIsolatedConversation() {
210213
assertTrue(nestedConversation.isNested());
211214
assertTrue(((MutableConversation) conversation).isParent());
212215

213-
assertNotSame(bean, context.getBean("testBean"));
216+
assertNotSame(bean, context.getBean(TEST_BEAN1_NAME));
214217

215218
nestedConversation.end(ConversationEndingType.SUCCESS);
216219
assertSame(conversation, manager.getCurrentConversation());
217220

218-
assertSame(bean, context.getBean("testBean"));
221+
assertSame(bean, context.getBean(TEST_BEAN1_NAME));
219222

220223
conversation.end(ConversationEndingType.SUCCESS);
221224
assertTrue(conversation.isEnded());
@@ -234,7 +237,7 @@ public void testJoinedConversation() {
234237
assertFalse(conversation.isNested());
235238
assertFalse(((MutableConversation) conversation).isParent());
236239

237-
ConversationalBean bean = (ConversationalBean) context.getBean("testBean");
240+
ConversationalBean bean = (ConversationalBean) context.getBean(TEST_BEAN1_NAME);
238241
assertNotNull(bean);
239242

240243
Conversation joinedConversation = manager.beginConversation(false, JoinMode.JOINED);
@@ -245,12 +248,12 @@ public void testJoinedConversation() {
245248
assertFalse(joinedConversation.isNested());
246249
assertFalse(((MutableConversation) joinedConversation).isParent());
247250

248-
assertSame(bean, context.getBean("testBean"));
251+
assertSame(bean, context.getBean(TEST_BEAN1_NAME));
249252

250253
joinedConversation.end(ConversationEndingType.SUCCESS);
251254
assertSame(conversation, manager.getCurrentConversation());
252255

253-
assertSame(bean, context.getBean("testBean"));
256+
assertSame(bean, context.getBean(TEST_BEAN1_NAME));
254257

255258
conversation.end(ConversationEndingType.SUCCESS);
256259
assertTrue(conversation.isEnded());
@@ -269,7 +272,7 @@ public void testSwitchedConversation() {
269272
assertFalse(conversation.isNested());
270273
assertFalse(((MutableConversation) conversation).isParent());
271274

272-
ConversationalBean bean = (ConversationalBean) context.getBean("testBean");
275+
ConversationalBean bean = (ConversationalBean) context.getBean(TEST_BEAN1_NAME);
273276
assertNotNull(bean);
274277

275278
Conversation switchedConversation = manager.beginConversation(false, JoinMode.SWITCHED);
@@ -280,15 +283,15 @@ public void testSwitchedConversation() {
280283
assertFalse(switchedConversation.isNested());
281284
assertFalse(((MutableConversation) switchedConversation).isParent());
282285

283-
ConversationalBean bean2 = (ConversationalBean) context.getBean("testBean");
286+
ConversationalBean bean2 = (ConversationalBean) context.getBean(TEST_BEAN1_NAME);
284287
assertNotSame(bean, bean2);
285288

286289
manager.switchConversation(conversation.getId());
287290
assertSame(conversation, manager.getCurrentConversation());
288-
assertSame(bean, context.getBean("testBean"));
291+
assertSame(bean, context.getBean(TEST_BEAN1_NAME));
289292

290293
manager.switchConversation(switchedConversation.getId());
291-
assertSame(bean2, context.getBean("testBean"));
294+
assertSame(bean2, context.getBean(TEST_BEAN1_NAME));
292295

293296
switchedConversation.end(ConversationEndingType.SUCCESS);
294297
conversation.end(ConversationEndingType.SUCCESS);
@@ -345,7 +348,7 @@ public void testImplicitConversationEnding() {
345348
}
346349

347350
@Test
348-
public void testConversationAnnotation() {
351+
public void testConversationAnnotation1() {
349352
ConversationalServiceBean serviceBean = context.getBean(ConversationalServiceBean.class);
350353
assertNotNull(serviceBean);
351354

@@ -378,6 +381,87 @@ public void testConversationAnnotation() {
378381
serviceBean.clean();
379382
}
380383

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+
381465
protected static String getContextLocation() {
382466
return "org/springframework/conversation/conversationTestContext.xml";
383467
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* Copyright 2002-2008 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package org.springframework.conversation;
17+
18+
/**
19+
* @author Micha Kiener
20+
* @since 3.1
21+
*/
22+
public class ConversationEventAwareBean implements ConversationListener {
23+
private Conversation conversation1;
24+
private Conversation conversation2;
25+
private ConversationActivationType activationType;
26+
private ConversationDeactivationType deactivationType;
27+
private ConversationEndingType endingType;
28+
29+
/**
30+
* @see org.springframework.conversation.ConversationListener#conversationActivated(org.springframework.conversation.Conversation,
31+
* org.springframework.conversation.Conversation,
32+
* org.springframework.conversation.ConversationActivationType)
33+
*/
34+
public void conversationActivated(Conversation conversation, Conversation oldCurrentConversation,
35+
ConversationActivationType activationType) {
36+
conversation1 = conversation;
37+
conversation2 = oldCurrentConversation;
38+
this.activationType = activationType;
39+
}
40+
41+
/**
42+
* @see org.springframework.conversation.ConversationListener#conversationDeactivated(org.springframework.conversation.Conversation,
43+
* org.springframework.conversation.Conversation,
44+
* org.springframework.conversation.ConversationDeactivationType)
45+
*/
46+
public void conversationDeactivated(Conversation conversation, Conversation newCurrentConversation,
47+
ConversationDeactivationType deactivationType) {
48+
conversation1 = conversation;
49+
conversation2 = newCurrentConversation;
50+
this.deactivationType = deactivationType;
51+
}
52+
53+
/**
54+
* @see org.springframework.conversation.ConversationListener#conversationEnded(org.springframework.conversation.Conversation,
55+
* org.springframework.conversation.ConversationEndingType)
56+
*/
57+
public void conversationEnded(Conversation conversation, ConversationEndingType endingType) {
58+
conversation1 = conversation;
59+
this.endingType = endingType;
60+
}
61+
62+
public Conversation getConversation1() {
63+
return conversation1;
64+
}
65+
66+
public Conversation getConversation2() {
67+
return conversation2;
68+
}
69+
70+
public ConversationActivationType getActivationType() {
71+
return activationType;
72+
}
73+
74+
public ConversationDeactivationType getDeactivationType() {
75+
return deactivationType;
76+
}
77+
78+
public ConversationEndingType getEndingType() {
79+
return endingType;
80+
}
81+
}

org.springframework.context/src/test/java/org/springframework/conversation/ConversationalServiceBean.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ public interface ConversationalServiceBean {
2525

2626
public void endConversation();
2727

28+
public void endConversationSuccess();
29+
30+
public void endConversationCancel();
31+
2832
public void doInConversation();
2933

3034
public Conversation getStartingConversation();
@@ -34,4 +38,6 @@ public interface ConversationalServiceBean {
3438
public Conversation getConversationalConversation();
3539

3640
public void clean();
41+
42+
public void setFailureFlag(boolean failureFlag);
3743
}

org.springframework.context/src/test/java/org/springframework/conversation/ConversationalServiceBeanImpl.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
*/
2929
public class ConversationalServiceBeanImpl implements ConversationalServiceBean {
3030
private ConversationManager manager;
31+
private boolean failureFlag;
3132

3233
private Conversation startingConversation;
3334
private Conversation endingConversation;
@@ -38,6 +39,22 @@ public void startConversation() {
3839
startingConversation = manager.getCurrentConversation();
3940
}
4041

42+
@EndConversation(ConversationEndingType.SUCCESS)
43+
public void endConversationSuccess() {
44+
endingConversation = manager.getCurrentConversation();
45+
if (failureFlag) {
46+
throw new RuntimeException("Test failure while ending conversation...");
47+
}
48+
}
49+
50+
@EndConversation(ConversationEndingType.CANCEL)
51+
public void endConversationCancel() {
52+
endingConversation = manager.getCurrentConversation();
53+
if (failureFlag) {
54+
throw new RuntimeException("Test failure while ending conversation...");
55+
}
56+
}
57+
4158
@EndConversation
4259
public void endConversation() {
4360
endingConversation = manager.getCurrentConversation();
@@ -64,6 +81,11 @@ public void clean() {
6481
startingConversation = null;
6582
endingConversation = null;
6683
conversationalConversation = null;
84+
failureFlag = false;
85+
}
86+
87+
public void setFailureFlag(boolean failureFlag) {
88+
this.failureFlag = failureFlag;
6789
}
6890

6991
public void setConversationManager(ConversationManager manager) {

org.springframework.context/src/test/java/org/springframework/conversation/conversationTestContext.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,11 @@
3333
<property name="conversationResolver" ref="conversationResolver"/>
3434
</bean>
3535

36-
<bean id="testBean" class="org.springframework.conversation.ConversationalBean" scope="conversation"/>
36+
<bean id="testBean1" class="org.springframework.conversation.ConversationalBean" scope="conversation"/>
37+
<bean id="testBean2" class="org.springframework.conversation.ConversationEventAwareBean" scope="conversation"/>
3738

3839
<bean id="testService" class="org.springframework.conversation.ConversationalServiceBeanImpl">
3940
<property name="conversationManager" ref="conversationManager"/>
4041
</bean>
42+
4143
</beans>

0 commit comments

Comments
 (0)