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

📄 customscopeconfigurertests.java

📁 struts+spring 源码 希望能给大家带来帮助
💻 JAVA
字号:
/*
 * Copyright 2002-2006 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.beans.factory.config;

import java.util.HashMap;
import java.util.Map;

import junit.framework.TestCase;
import org.easymock.MockControl;

import org.springframework.beans.factory.ObjectFactory;
import org.springframework.mock.easymock.AbstractScalarMockTemplate;
import org.springframework.test.AssertThrows;

/**
 * @author Rick Evans
 */
public class CustomScopeConfigurerTests extends TestCase {

	private static final String FOO_SCOPE = "fooScope";


	public void testWithNoScopes() throws Exception {
		new ConfigurableListableBeanFactoryMockTemplate() {
			protected void doTest(ConfigurableListableBeanFactory factory) {
				CustomScopeConfigurer figurer = new CustomScopeConfigurer();
				figurer.setBeanClassLoader(Thread.currentThread().getContextClassLoader());
				figurer.postProcessBeanFactory(factory);
			}
		}.test();
	}

	public void testSunnyDayWithBonaFideScopeInstances() throws Exception {
		MockControl mockScope = MockControl.createControl(Scope.class);
		final Scope scope = (Scope) mockScope.getMock();
		mockScope.replay();
		new ConfigurableListableBeanFactoryMockTemplate() {
			public void setupExpectations(MockControl mockControl, ConfigurableListableBeanFactory factory) {
				factory.registerScope(FOO_SCOPE, scope);
			}
			protected void doTest(ConfigurableListableBeanFactory factory) {
				Map scopes = new HashMap();
				scopes.put(FOO_SCOPE, scope);
				CustomScopeConfigurer figurer = new CustomScopeConfigurer();
				figurer.setScopes(scopes);
				figurer.postProcessBeanFactory(factory);
			}
		}.test();
		mockScope.verify();
	}

	public void testWhereScopeMapHasNullScopeValueInEntrySet() throws Exception {
		new ConfigurableListableBeanFactoryMockTemplate() {
			protected void doTest(final ConfigurableListableBeanFactory factory) {
				new AssertThrows(IllegalArgumentException.class) {
					public void test() throws Exception {
						Map scopes = new HashMap();
						scopes.put(FOO_SCOPE, null);
						CustomScopeConfigurer figurer = new CustomScopeConfigurer();
						figurer.setScopes(scopes);
						figurer.postProcessBeanFactory(factory);
					}
				}.runTest();
			}
		}.test();
	}

	public void testWhereScopeMapHasNonScopeInstanceInEntrySet() throws Exception {
		new ConfigurableListableBeanFactoryMockTemplate() {
			protected void doTest(final ConfigurableListableBeanFactory factory) {
				new AssertThrows(IllegalArgumentException.class) {
					public void test() throws Exception {
						Map scopes = new HashMap();
						scopes.put(FOO_SCOPE, this); // <-- not a valid value...
						CustomScopeConfigurer figurer = new CustomScopeConfigurer();
						figurer.setScopes(scopes);
						figurer.postProcessBeanFactory(factory);
					}
				}.runTest();
			}
		}.test();
	}

	public void testWhereScopeMapHasNonStringTypedScopeNameInKeySet() throws Exception {
		new ConfigurableListableBeanFactoryMockTemplate() {
			protected void doTest(final ConfigurableListableBeanFactory factory) {
				new AssertThrows(IllegalArgumentException.class) {
					public void test() throws Exception {
						Map scopes = new HashMap();
						scopes.put(this, new NoOpScope()); // <-- not a valid value (the key)...
						CustomScopeConfigurer figurer = new CustomScopeConfigurer();
						figurer.setScopes(scopes);
						figurer.postProcessBeanFactory(factory);
					}
				}.runTest();
			}
		}.test();
	}


	private static final class NoOpScope implements Scope {

		public String getConversationId() {
			return null;
		}

		public Object get(String name, ObjectFactory objectFactory) {
			throw new UnsupportedOperationException();
		}

		public Object remove(String name) {
			throw new UnsupportedOperationException();
		}

		public void registerDestructionCallback(String name, Runnable callback) {
		}
	}


	private abstract class ConfigurableListableBeanFactoryMockTemplate extends AbstractScalarMockTemplate {

		public ConfigurableListableBeanFactoryMockTemplate() {
			super(ConfigurableListableBeanFactory.class);
		}


		public final void setupExpectations(MockControl mockControl, Object mockObject) throws Exception {
			setupExpectations(mockControl, (ConfigurableListableBeanFactory) mockObject);
		}

		public final void doTest(Object mockObject) throws Exception {
			doTest((ConfigurableListableBeanFactory) mockObject);
		}

		public void setupExpectations(MockControl mockControl, ConfigurableListableBeanFactory factory) throws Exception {
		}

		protected abstract void doTest(ConfigurableListableBeanFactory factory) throws Exception;

	}

}

⌨️ 快捷键说明

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