1 16 17 package org.springframework.context.support; 18 19 import java.io.IOException ; 20 import java.io.InputStreamReader ; 21 import java.io.StringWriter ; 22 import java.util.Arrays ; 23 import java.util.List ; 24 25 import junit.framework.TestCase; 26 27 import org.springframework.aop.support.AopUtils; 28 import org.springframework.beans.MutablePropertyValues; 29 import org.springframework.beans.ResourceTestBean; 30 import org.springframework.beans.TestBean; 31 import org.springframework.beans.factory.config.RuntimeBeanReference; 32 import org.springframework.beans.factory.support.DefaultListableBeanFactory; 33 import org.springframework.beans.factory.support.RootBeanDefinition; 34 import org.springframework.beans.factory.xml.XmlBeanDefinitionReader; 35 import org.springframework.context.ApplicationListener; 36 import org.springframework.context.MessageSource; 37 import org.springframework.core.io.ClassPathResource; 38 import org.springframework.core.io.FileSystemResource; 39 import org.springframework.core.io.Resource; 40 import org.springframework.util.FileCopyUtils; 41 42 45 public class ClassPathXmlApplicationContextTests extends TestCase { 46 47 public void testMultipleConfigLocations() throws Exception { 48 ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext( 49 new String [] { 50 "/org/springframework/context/support/contextB.xml", 51 "/org/springframework/context/support/contextC.xml", 52 "/org/springframework/context/support/contextA.xml"}); 53 assertTrue(ctx.containsBean("service")); 54 assertTrue(ctx.containsBean("logicOne")); 55 assertTrue(ctx.containsBean("logicTwo")); 56 } 57 58 public void testConfigLocationPattern() throws Exception { 59 ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext( 60 "/org/springframework/context/support/context*.xml"); 61 assertTrue(ctx.containsBean("service")); 62 assertTrue(ctx.containsBean("logicOne")); 63 assertTrue(ctx.containsBean("logicTwo")); 64 } 65 66 public void testFactoryBeanAndApplicationListener() { 67 ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext( 68 "/org/springframework/context/support/context*.xml"); 69 ctx.getBeanFactory().registerSingleton("manualFBAAL", new FactoryBeanAndApplicationListener()); 70 assertEquals(0, ctx.getBeansOfType(ApplicationListener.class).size()); 71 } 72 73 public void testMessageSourceAware() { 74 ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext( 75 "/org/springframework/context/support/context*.xml"); 76 MessageSource messageSource = (MessageSource) ctx.getBean("messageSource"); 77 Service service1 = (Service) ctx.getBean("service"); 78 assertEquals(ctx, service1.getMessageSource()); 79 Service service2 = (Service) ctx.getBean("service2"); 80 assertEquals(ctx, service2.getMessageSource()); 81 AutowiredService autowiredService1 = (AutowiredService) ctx.getBean("autowiredService"); 82 assertEquals(messageSource, autowiredService1.getMessageSource()); 83 AutowiredService autowiredService2 = (AutowiredService) ctx.getBean("autowiredService2"); 84 assertEquals(messageSource, autowiredService2.getMessageSource()); 85 } 86 87 public void testResourceArrayPropertyEditor() throws IOException { 88 ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext( 89 "/org/springframework/context/support/context*.xml"); 90 Service service = (Service) ctx.getBean("service"); 91 assertEquals(3, service.getResources().length); 92 List resources = Arrays.asList(service.getResources()); 93 assertTrue(resources.contains( 94 new FileSystemResource(new ClassPathResource("/org/springframework/context/support/contextA.xml").getFile()))); 95 assertTrue(resources.contains( 96 new FileSystemResource(new ClassPathResource("/org/springframework/context/support/contextB.xml").getFile()))); 97 assertTrue(resources.contains( 98 new FileSystemResource(new ClassPathResource("/org/springframework/context/support/contextC.xml").getFile()))); 99 } 100 101 public void testChildWithProxy() throws Exception { 102 ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext( 103 "/org/springframework/context/support/context*.xml"); 104 ClassPathXmlApplicationContext child = new ClassPathXmlApplicationContext( 105 new String [] {"/org/springframework/context/support/childWithProxy.xml"}, ctx); 106 assertTrue(AopUtils.isAopProxy(child.getBean("assemblerOne"))); 107 assertTrue(AopUtils.isAopProxy(child.getBean("assemblerTwo"))); 108 } 109 110 public void testSelfReference() { 111 DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); 112 MutablePropertyValues pvs = new MutablePropertyValues(); 113 pvs.addPropertyValue("spouse", new RuntimeBeanReference("test")); 114 lbf.registerBeanDefinition("test", new RootBeanDefinition(TestBean.class, pvs)); 115 TestBean test = (TestBean) lbf.getBean("test"); 116 assertEquals(test, test.getSpouse()); 117 } 118 119 public void testResourceAndInputStream() throws IOException { 120 ClassPathXmlApplicationContext ctx = 121 new ClassPathXmlApplicationContext("/org/springframework/beans/factory/xml/resource.xml") { 122 public Resource getResource(String location) { 123 if ("classpath:org/springframework/beans/factory/xml/test.properties".equals(location)) { 124 return new ClassPathResource("test.properties", ClassPathXmlApplicationContextTests.class); 125 } 126 return super.getResource(location); 127 } 128 }; 129 ResourceTestBean resource1 = (ResourceTestBean) ctx.getBean("resource1"); 130 ResourceTestBean resource2 = (ResourceTestBean) ctx.getBean("resource2"); 131 assertTrue(resource1.getResource() instanceof ClassPathResource); 132 StringWriter writer = new StringWriter (); 133 FileCopyUtils.copy(new InputStreamReader (resource1.getResource().getInputStream()), writer); 134 assertEquals("contexttest", writer.toString()); 135 writer = new StringWriter (); 136 FileCopyUtils.copy(new InputStreamReader (resource1.getInputStream()), writer); 137 assertEquals("contexttest", writer.toString()); 138 writer = new StringWriter (); 139 FileCopyUtils.copy(new InputStreamReader (resource2.getResource().getInputStream()), writer); 140 assertEquals("contexttest", writer.toString()); 141 writer = new StringWriter (); 142 FileCopyUtils.copy(new InputStreamReader (resource2.getInputStream()), writer); 143 assertEquals("contexttest", writer.toString()); 144 } 145 146 public void testGenericApplicationContextWithXmlBeanDefinitions() throws Exception { 147 GenericApplicationContext ctx = new GenericApplicationContext(); 148 XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(ctx); 149 reader.loadBeanDefinitions(new ClassPathResource("contextB.xml", getClass())); 150 reader.loadBeanDefinitions(new ClassPathResource("contextC.xml", getClass())); 151 reader.loadBeanDefinitions(new ClassPathResource("contextA.xml", getClass())); 152 ctx.refresh(); 153 assertTrue(ctx.containsBean("service")); 154 assertTrue(ctx.containsBean("logicOne")); 155 assertTrue(ctx.containsBean("logicTwo")); 156 } 157 158 } 159 | Popular Tags |