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.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(), new StaticListableBeanFactory()); 47 } 48 49 /** 50 * Register a subflow definition in the backing flow registry, typically to 51 * support a flow execution test. For test scenarios, the subflow is often a 52 * stub used to verify parent flow input and output mapping behavior. 53 * @param subflow the subflow 54 */ 55 public void registerSubflow(Flow subflow) { 56 getSubflowRegistry().registerFlowDefinition(new StaticFlowDefinitionHolder(subflow)); 57 } 58 59 /** 60 * Register a bean in the backing bean factory, typically to support a flow 61 * execution test. For test scenarios, if the bean is a service invoked by a 62 * bean invoking action it is often a stub or dynamic mock implementation of 63 * the service's business interface. 64 * @param beanName the bean name 65 * @param bean the singleton instance 66 */ 67 public void registerBean(String beanName, Object bean) { 68 ((StaticListableBeanFactory)getBeanFactory()).addBean(beanName, bean); 69 } 70 }