1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.orchestra.conversation;
20
21 import org.apache.myfaces.orchestra.conversation.basic.LogConversationMessager;
22 import org.apache.myfaces.orchestra.frameworkAdapter.FrameworkAdapter;
23 import org.apache.myfaces.orchestra.frameworkAdapter.local.LocalFrameworkAdapter;
24 import org.springframework.aop.scope.ScopedObject;
25 import org.springframework.test.AbstractDependencyInjectionSpringContextTests;
26
27
28
29
30 public class TestConversation extends AbstractDependencyInjectionSpringContextTests implements ConversationBindingListener
31 {
32 protected String[] getConfigLocations()
33 {
34 return new String[]
35 {
36 "classpath:testApplicationContext.xml"
37 };
38 }
39
40
41 private int valueBoundCount = 0;
42 private int valueUnboundCount = 0;
43
44 protected void onSetUp() throws Exception
45 {
46 super.onSetUp();
47
48 LocalFrameworkAdapter frameworkAdapter = new LocalFrameworkAdapter();
49 frameworkAdapter.setApplicationContext(applicationContext);
50 frameworkAdapter.setConversationMessager(new LogConversationMessager());
51 FrameworkAdapter.setCurrentInstance(frameworkAdapter);
52 }
53
54 public void testConversation() throws Exception
55 {
56 DummyBean.callback=this;
57 final String BEAN_NAME = "dummyBean";
58
59
60
61 final String CONVERSATION_NAME = BEAN_NAME;
62
63 valueBoundCount = 0;
64 valueUnboundCount = 0;
65
66
67 DummyBean bean = (DummyBean) applicationContext.getBean(BEAN_NAME);
68
69 assertTrue("should be a scoped object", bean instanceof ScopedObject);
70
71 assertFalse("conversation should not have been started yet", ConversationManager.getInstance().hasConversation(CONVERSATION_NAME));
72 assertEquals("value bound", 0, valueBoundCount);
73 assertEquals("value unbound", 0, valueUnboundCount);
74 bean.touch();
75 assertEquals("value bound", 1, valueBoundCount);
76 assertEquals("value unbound", 0, valueUnboundCount);
77 assertTrue("conversation should have been started", ConversationManager.getInstance().hasConversation(CONVERSATION_NAME));
78
79
80 Conversation conversationHolder = bean.checkCurrentConversation();
81 assertNotNull("current conversation", conversationHolder);
82 assertEquals("conversation name", CONVERSATION_NAME, conversationHolder.getName());
83
84
85 bean.invalidateSelf();
86
87 assertFalse("conversation should not be running", ConversationManager.getInstance().hasConversation(CONVERSATION_NAME));
88 assertTrue("conversation should be marked invalid", conversationHolder.isInvalid());
89 assertEquals("value bound", 1, valueBoundCount);
90 assertEquals("value unbound", 1, valueUnboundCount);
91
92 bean.touch();
93 assertTrue("conversation should have been started", ConversationManager.getInstance().hasConversation(CONVERSATION_NAME));
94 assertEquals("value bound", 2, valueBoundCount);
95
96
97
98 bean.setData("check");
99
100 bean.invalidateAndRestartSelf();
101
102 assertEquals("value bound", 2, valueBoundCount);
103 assertEquals("value unbound", 2, valueUnboundCount);
104
105 assertTrue("conversation should still be running", ConversationManager.getInstance().hasConversation(CONVERSATION_NAME));
106
107 DummyBean beanNew = (DummyBean) applicationContext.getBean(BEAN_NAME);
108
109 assertNotNull("should have got a new conversation", beanNew);
110 assertNull("the conversation is not new", bean.getData());
111
112 beanNew.touch();
113
114 assertEquals("value bound", 3, valueBoundCount);
115
116
117
118 ConversationManager.getInstance().clearCurrentConversationContext();
119
120 assertEquals("value unbound", 3, valueUnboundCount);
121 assertFalse("conversation should not be running", ConversationManager.getInstance().hasConversation(CONVERSATION_NAME));
122 }
123
124 public void valueBound(ConversationBindingEvent event)
125 {
126 valueBoundCount++;
127 }
128
129 public void valueUnbound(ConversationBindingEvent event)
130 {
131 valueUnboundCount++;
132 }
133 }