01: /*
02: * Copyright 2004-2007 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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.beans.factory.config.ConfigurableBeanFactory;
19: import org.springframework.beans.factory.support.StaticListableBeanFactory;
20: import org.springframework.webflow.definition.registry.FlowDefinitionRegistryImpl;
21: import org.springframework.webflow.definition.registry.StaticFlowDefinitionHolder;
22: import org.springframework.webflow.engine.Flow;
23: import org.springframework.webflow.engine.builder.DefaultFlowServiceLocator;
24:
25: /**
26: * A stub flow service locator implementation suitable for a test environment.
27: * <p>
28: * Allows programmatic registration of subflows needed by a flow execution being
29: * tested, see {@link #registerSubflow(Flow)}. Subflows registered are
30: * typically stubs that verify parent flow input and output scenarios.
31: * <p>
32: * Also supports programmatic registration of additional custom services needed
33: * by a flow (such as Actions) managed in a backing Spring
34: * {@link ConfigurableBeanFactory}. See the
35: * {@link #registerBean(String, Object)} method. Beans registered are typically
36: * mocks or stubs of business services invoked by the flow.
37: *
38: * @author Keith Donald
39: */
40: public class MockFlowServiceLocator extends DefaultFlowServiceLocator {
41:
42: /**
43: * Creates a new mock flow service locator.
44: */
45: public MockFlowServiceLocator() {
46: super (new FlowDefinitionRegistryImpl(),
47: new StaticListableBeanFactory());
48: }
49:
50: /**
51: * Register a subflow definition in the backing flow registry, typically to
52: * support a flow execution test. For test scenarios, the subflow is often a
53: * stub used to verify parent flow input and output mapping behavior.
54: * @param subflow the subflow
55: */
56: public void registerSubflow(Flow subflow) {
57: getSubflowRegistry().registerFlowDefinition(
58: new StaticFlowDefinitionHolder(subflow));
59: }
60:
61: /**
62: * Register a bean in the backing bean factory, typically to support a flow
63: * execution test. For test scenarios, if the bean is a service invoked by a
64: * bean invoking action it is often a stub or dynamic mock implementation of
65: * the service's business interface.
66: * @param beanName the bean name
67: * @param bean the singleton instance
68: */
69: public void registerBean(String beanName, Object bean) {
70: ((StaticListableBeanFactory) getBeanFactory()).addBean(
71: beanName, bean);
72: }
73: }
|