⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 flowexecutionimpltests.java

📁 spring的WEB开发插件,支持多状态WEB开发
💻 JAVA
字号:
/*
 * Copyright 2002-2004 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.springframework.webflow.execution;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import junit.framework.TestCase;

import org.springframework.core.io.ClassPathResource;
import org.springframework.webflow.Event;
import org.springframework.webflow.Flow;
import org.springframework.webflow.access.FlowLocator;
import org.springframework.webflow.access.ServiceLookupException;
import org.springframework.webflow.config.FlowFactoryBean;
import org.springframework.webflow.config.XmlFlowBuilder;
import org.springframework.webflow.config.XmlFlowBuilderTests;
import org.springframework.webflow.execution.FlowExecutionImpl;
import org.springframework.webflow.execution.FlowExecutionListener;
import org.springframework.webflow.execution.FlowExecutionListenerLoader;

/**
 * Test case for FlowExecutionStack.
 * 
 * @see org.springframework.webflow.execution.FlowExecutionImpl
 * 
 * @author Erwin Vervaet
 */
public class FlowExecutionImplTests extends TestCase {

	private FlowLocator flowLocator;

	private FlowExecutionImpl flowExecution;

	protected void setUp() throws Exception {
		XmlFlowBuilder builder = new XmlFlowBuilder(new ClassPathResource("testFlow.xml", XmlFlowBuilderTests.class));
		builder.setFlowServiceLocator(new XmlFlowBuilderTests.TestFlowServiceLocator());
		final Flow flow = new FlowFactoryBean(builder).getFlow();
		flowLocator = new FlowLocator() {
			public Flow getFlow(String flowDefinitionId) throws ServiceLookupException {
				if (flow.getId().equals(flowDefinitionId)) {
					return flow;
				}
				throw new ServiceLookupException(Flow.class, flowDefinitionId, null);
			}
		};
		flowExecution = new FlowExecutionImpl(flow);
	}

	protected void runFlowExecutionRehydrationTest() throws Exception {
		// serialize the flowExecution
		ByteArrayOutputStream bout = new ByteArrayOutputStream();
		ObjectOutputStream oout = new ObjectOutputStream(bout);
		oout.writeObject(flowExecution);
		oout.flush();

		// deserialize the flowExecution
		ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
		ObjectInputStream oin = new ObjectInputStream(bin);
		FlowExecutionImpl restoredFlowExecution = (FlowExecutionImpl)oin.readObject();

		assertNotNull(restoredFlowExecution);

		FlowExecutionListenerLoader listenerLoader = new FlowExecutionListenerLoader() {
			public FlowExecutionListener[] getListeners(Flow flow) {
				return flowExecution.getListeners().toArray();
			}
		};
		// rehydrate the flow execution
		restoredFlowExecution.rehydrate(flowLocator, listenerLoader, flowExecution.getTransactionSynchronizer());

		assertEquals(flowExecution.isActive(), restoredFlowExecution.isActive());
		if (flowExecution.isActive()) {
			assertTrue(entriesCollectionsAreEqual(
					flowExecution.getActiveSession().getScope().getAttributeMap().entrySet(),
					restoredFlowExecution.getActiveSession().getScope().getAttributeMap().entrySet()));
			assertEquals(flowExecution.getActiveSession().getCurrentState().getId(), restoredFlowExecution.getCurrentState().getId());
			assertEquals(flowExecution.getActiveSession().getFlow().getId(), restoredFlowExecution.getActiveSession().getFlow().getId());
			assertSame(flowExecution.getRootFlow(), restoredFlowExecution.getRootFlow());
		}
		assertEquals(flowExecution.getLastEventId(), restoredFlowExecution.getLastEventId());
		assertEquals(flowExecution.getKey(), restoredFlowExecution.getKey());
		assertEquals(flowExecution.getLastRequestTimestamp(), restoredFlowExecution.getLastRequestTimestamp());
		assertEquals(flowExecution.getListeners().size(), restoredFlowExecution.getListeners().size());
	}

	public void testRehydrate() throws Exception {
		// setup some input data
		Map inputData = new HashMap(1);
		inputData.put("name", "value");
		// start the flow execution
		flowExecution.start(new Event(this, "start", inputData));
		runFlowExecutionRehydrationTest();
	}

	public void testRehydrateNotStarted() throws Exception {
		// don't start the flow execution
		runFlowExecutionRehydrationTest();
	}

	/**
	 * Helper to test if 2 collections of Map.Entry objects contain the same
	 * values.
	 */
	private boolean entriesCollectionsAreEqual(Collection collection1, Collection collection2) {
		if (collection1.size() != collection2.size()) {
			return false;
		}
		for (Iterator it1 = collection1.iterator(), it2 = collection2.iterator(); it1.hasNext() && it2.hasNext();) {
			Map.Entry entry1 = (Map.Entry)it1.next();
			Map.Entry entry2 = (Map.Entry)it2.next();
			if (!entry1.equals(entry2)) {
				return false;
			}
		}
		return true;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -