1 package org.springframework.context.support; 2 3 import java.util.Map ; 4 import java.util.Properties ; 5 6 import junit.framework.TestCase; 7 8 import org.springframework.beans.MutablePropertyValues; 9 import org.springframework.beans.factory.support.DefaultListableBeanFactory; 10 import org.springframework.beans.factory.support.RootBeanDefinition; 11 import org.springframework.core.io.ClassPathResource; 12 import org.springframework.core.io.Resource; 13 14 18 public class ResourceMapFactoryBeanTests extends TestCase { 19 20 public void testResourceMapFactoryBeanWithoutContext() { 21 DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory(); 22 Properties props = new Properties (); 23 props.setProperty("test1", "classpath:org/springframework/context/support/contextA.xml"); 24 props.setProperty("test2", "classpath:org/springframework/context/support/contextB.xml"); 25 MutablePropertyValues pvs = new MutablePropertyValues(); 26 pvs.addPropertyValue("properties", props); 27 RootBeanDefinition bd = new RootBeanDefinition(ResourceMapFactoryBean.class, pvs); 28 beanFactory.registerBeanDefinition("resourceMap", bd); 29 Map result = (Map ) beanFactory.getBean("resourceMap"); 30 assertEquals(2, result.size()); 31 assertTrue(result.get("test1") instanceof ClassPathResource); 32 assertTrue(((Resource) result.get("test1")).getDescription().indexOf("contextA.xml") != -1); 33 assertTrue(((Resource) result.get("test2")).getDescription().indexOf("contextB.xml") != -1); 34 } 35 36 public void testResourceMapFactoryBeanWithContext() { 37 StaticApplicationContext context = new StaticApplicationContext() { 38 public Resource getResource(String location) { 39 return super.getResource("classpath:org/springframework/context/support/context" + location); 40 } 41 }; 42 DefaultListableBeanFactory beanFactory = context.getDefaultListableBeanFactory(); 43 Properties props = new Properties (); 44 props.setProperty("test1", "A.xml"); 45 props.setProperty("test2", "B.xml"); 46 MutablePropertyValues pvs = new MutablePropertyValues(); 47 pvs.addPropertyValue("properties", props); 48 RootBeanDefinition bd = new RootBeanDefinition(ResourceMapFactoryBean.class, pvs); 49 beanFactory.registerBeanDefinition("resourceMap", bd); 50 context.refresh(); 51 Map result = (Map ) beanFactory.getBean("resourceMap"); 52 assertEquals(2, result.size()); 53 assertTrue(result.get("test1") instanceof ClassPathResource); 54 assertTrue(((Resource) result.get("test1")).getDescription().indexOf("contextA.xml") != -1); 55 assertTrue(((Resource) result.get("test2")).getDescription().indexOf("contextB.xml") != -1); 56 } 57 58 public void testResourceMapFactoryBeanWithResourceBasePath() { 59 DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory(); 60 Properties props = new Properties (); 61 props.setProperty("test1", "A.xml"); 62 props.setProperty("test2", "B.xml"); 63 MutablePropertyValues pvs = new MutablePropertyValues(); 64 pvs.addPropertyValue("properties", props); 65 pvs.addPropertyValue("resourceBasePath", "classpath:org/springframework/context/support/context"); 66 RootBeanDefinition bd = new RootBeanDefinition(ResourceMapFactoryBean.class, pvs); 67 beanFactory.registerBeanDefinition("resourceMap", bd); 68 Map result = (Map ) beanFactory.getBean("resourceMap"); 69 assertEquals(2, result.size()); 70 assertTrue(result.get("test1") instanceof ClassPathResource); 71 assertTrue(((Resource) result.get("test1")).getDescription().indexOf("contextA.xml") != -1); 72 assertTrue(((Resource) result.get("test2")).getDescription().indexOf("contextB.xml") != -1); 73 } 74 75 } 76 | Popular Tags |