KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > webflow > test > MockFlowExecutionContext


1 /*
2  * Copyright 2002-2006 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of 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,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.springframework.webflow.test;
17
18 import org.springframework.webflow.core.collection.AttributeMap;
19 import org.springframework.webflow.core.collection.LocalAttributeMap;
20 import org.springframework.webflow.core.collection.MutableAttributeMap;
21 import org.springframework.webflow.definition.FlowDefinition;
22 import org.springframework.webflow.engine.Flow;
23 import org.springframework.webflow.execution.FlowExecutionContext;
24 import org.springframework.webflow.execution.FlowSession;
25
26 /**
27  * A stub implementation of the flow execution context interface.
28  *
29  * @see FlowExecutionContext
30  *
31  * @author Keith Donald
32  */

33 public class MockFlowExecutionContext implements FlowExecutionContext {
34
35     private FlowDefinition flow;
36
37     private FlowSession activeSession;
38
39     private MutableAttributeMap conversationScope = new LocalAttributeMap();
40
41     private MutableAttributeMap attributes = new LocalAttributeMap();
42
43     /**
44      * Creates a new mock flow execution context -- automatically installs a root
45      * flow definition and active flow session.
46      */

47     public MockFlowExecutionContext() {
48         activeSession = new MockFlowSession();
49         this.flow = activeSession.getDefinition();
50     }
51
52     /**
53      * Creates a new mock flow execution context for the specified root flow
54      * definition.
55      */

56     public MockFlowExecutionContext(Flow rootFlow) {
57         this.flow = rootFlow;
58         activeSession = new MockFlowSession(rootFlow);
59     }
60
61     public String JavaDoc getCaption() {
62         return "Mock flow execution context";
63     }
64
65     // implementing flow execution context
66

67     public FlowDefinition getDefinition() {
68         return flow;
69     }
70
71     public boolean isActive() {
72         return activeSession != null;
73     }
74
75     public FlowSession getActiveSession() throws IllegalStateException JavaDoc {
76         if (activeSession == null) {
77             throw new IllegalStateException JavaDoc("No flow session is active");
78         }
79         return activeSession;
80     }
81
82     public MutableAttributeMap getConversationScope() {
83         return conversationScope;
84     }
85     
86     public AttributeMap getAttributes() {
87         return attributes;
88     }
89     
90     // mutators
91

92     /**
93      * Sets the top-level flow definition.
94      */

95     public void setFlow(Flow rootFlow) {
96         this.flow = rootFlow;
97     }
98
99     /**
100      * Sets the mock session to be the <i>active session</i>.
101      */

102     public void setActiveSession(FlowSession activeSession) {
103         this.activeSession = activeSession;
104     }
105
106     /**
107      * Sets flow execution (conversational) scope.
108      */

109     public void setConversationScope(MutableAttributeMap scope) {
110         this.conversationScope = scope;
111     }
112     
113     // convenience accessors
114

115     /**
116      * Returns the mock active flow session.
117      */

118     public MockFlowSession getMockActiveSession() {
119         return (MockFlowSession)activeSession;
120     }
121
122     /**
123      * Returns the mutable execution attribute map.
124      * @return the execution attribute map
125      */

126     public MutableAttributeMap getAttributeMap() {
127         return attributes;
128     }
129     
130     /**
131      * Puts a execution attribute into the context.
132      * @param attributeName the attribute name
133      * @param attributeValue the attribute value
134      */

135     public void putAttribute(String JavaDoc attributeName, Object JavaDoc attributeValue) {
136         attributes.put(attributeName, attributeValue);
137     }
138 }
Popular Tags