alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Spring Framework example source code file (CustomScopeConfigurerTests.java)

This example Spring Framework source code file (CustomScopeConfigurerTests.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Spring Framework tags/keywords

configurablelistablebeanfactory, configurablelistablebeanfactory, configurablelistablebeanfactorymocktemplate, configurablelistablebeanfactorymocktemplate, customscopeconfigurer, customscopeconfigurer, defaultlistablebeanfactory, exception, exception, hashmap, map, noopscope, scope, testcase, util

The Spring Framework CustomScopeConfigurerTests.java source code

/*
 * Copyright 2002-2008 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.support.DefaultListableBeanFactory;
import org.springframework.mock.easymock.AbstractScalarMockTemplate;
import org.springframework.test.AssertThrows;

/**
 * @author Rick Evans
 * @author Juergen Hoeller
 */
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.postProcessBeanFactory(factory);
			}
		}.test();
	}

	public void testSunnyDayWithBonaFideScopeInstance() 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 testSunnyDayWithBonaFideScopeClass() throws Exception {
		DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
		Map scopes = new HashMap();
		scopes.put(FOO_SCOPE, NoOpScope.class);
		CustomScopeConfigurer figurer = new CustomScopeConfigurer();
		figurer.setScopes(scopes);
		figurer.postProcessBeanFactory(factory);
		assertTrue(factory.getRegisteredScope(FOO_SCOPE) instanceof NoOpScope);
	}

	public void testSunnyDayWithBonaFideScopeClassname() throws Exception {
		DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
		Map scopes = new HashMap();
		scopes.put(FOO_SCOPE, NoOpScope.class.getName());
		CustomScopeConfigurer figurer = new CustomScopeConfigurer();
		figurer.setScopes(scopes);
		figurer.postProcessBeanFactory(factory);
		assertTrue(factory.getRegisteredScope(FOO_SCOPE) instanceof NoOpScope);
	}

	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 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;
	}

}

Other Spring Framework examples (source code examples)

Here is a short list of links related to this Spring Framework CustomScopeConfigurerTests.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.