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

Spring Framework example source code file (ResourceMapFactoryBeanTests.java)

This example Spring Framework source code file (ResourceMapFactoryBeanTests.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

classpathresource, defaultlistablebeanfactory, defaultlistablebeanfactory, map, map, mutablepropertyvalues, mutablepropertyvalues, properties, properties, resource, resourcemapfactorybeantests, rootbeandefinition, staticapplicationcontext, testcase, util

The Spring Framework ResourceMapFactoryBeanTests.java source code

package org.springframework.context.support;

import java.util.Map;
import java.util.Properties;

import junit.framework.TestCase;

import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

/**
 * @author Juergen Hoeller
 * @since 25.04.2004
 */
public class ResourceMapFactoryBeanTests extends TestCase {

	public void testResourceMapFactoryBeanWithoutContext() {
		DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
		Properties props = new Properties();
		props.setProperty("test1", "classpath:org/springframework/context/support/contextA.xml");
		props.setProperty("test2", "classpath:org/springframework/context/support/contextB.xml");
		MutablePropertyValues pvs = new MutablePropertyValues();
		pvs.addPropertyValue("properties", props);
		RootBeanDefinition bd = new RootBeanDefinition(ResourceMapFactoryBean.class, pvs);
		beanFactory.registerBeanDefinition("resourceMap", bd);
		Map result = (Map) beanFactory.getBean("resourceMap");
		assertEquals(2, result.size());
		assertTrue(result.get("test1") instanceof ClassPathResource);
		assertTrue(((Resource) result.get("test1")).getDescription().indexOf("contextA.xml") != -1);
		assertTrue(((Resource) result.get("test2")).getDescription().indexOf("contextB.xml") != -1);
	}

	public void testResourceMapFactoryBeanWithContext() {
		StaticApplicationContext context = new StaticApplicationContext() {
			public Resource getResource(String location) {
				return super.getResource("classpath:org/springframework/context/support/context" + location);
			}
		};
		DefaultListableBeanFactory beanFactory = context.getDefaultListableBeanFactory();
		Properties props = new Properties();
		props.setProperty("test1", "A.xml");
		props.setProperty("test2", "B.xml");
		MutablePropertyValues pvs = new MutablePropertyValues();
		pvs.addPropertyValue("properties", props);
		RootBeanDefinition bd = new RootBeanDefinition(ResourceMapFactoryBean.class, pvs);
		beanFactory.registerBeanDefinition("resourceMap", bd);
		context.refresh();
		Map result = (Map) beanFactory.getBean("resourceMap");
		assertEquals(2, result.size());
		assertTrue(result.get("test1") instanceof ClassPathResource);
		assertTrue(((Resource) result.get("test1")).getDescription().indexOf("contextA.xml") != -1);
		assertTrue(((Resource) result.get("test2")).getDescription().indexOf("contextB.xml") != -1);
	}

	public void testResourceMapFactoryBeanWithResourceBasePath() {
		DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
		Properties props = new Properties();
		props.setProperty("test1", "A.xml");
		props.setProperty("test2", "B.xml");
		MutablePropertyValues pvs = new MutablePropertyValues();
		pvs.addPropertyValue("properties", props);
		pvs.addPropertyValue("resourceBasePath", "classpath:org/springframework/context/support/context");
		RootBeanDefinition bd = new RootBeanDefinition(ResourceMapFactoryBean.class, pvs);
		beanFactory.registerBeanDefinition("resourceMap", bd);
		Map result = (Map) beanFactory.getBean("resourceMap");
		assertEquals(2, result.size());
		assertTrue(result.get("test1") instanceof ClassPathResource);
		assertTrue(((Resource) result.get("test1")).getDescription().indexOf("contextA.xml") != -1);
		assertTrue(((Resource) result.get("test2")).getDescription().indexOf("contextB.xml") != -1);
	}

}

Other Spring Framework examples (source code examples)

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

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

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 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.