KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > context > support > ClassPathXmlApplicationContextTests


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.context.support;
18
19 import java.io.IOException JavaDoc;
20 import java.io.InputStreamReader JavaDoc;
21 import java.io.StringWriter JavaDoc;
22 import java.util.Arrays JavaDoc;
23 import java.util.List JavaDoc;
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 /**
43  * @author Juergen Hoeller
44  */

45 public class ClassPathXmlApplicationContextTests extends TestCase {
46
47     public void testMultipleConfigLocations() throws Exception JavaDoc {
48         ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(
49                 new String JavaDoc[] {
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 JavaDoc {
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 JavaDoc {
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 JavaDoc 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 JavaDoc {
102         ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(
103                 "/org/springframework/context/support/context*.xml");
104         ClassPathXmlApplicationContext child = new ClassPathXmlApplicationContext(
105                 new String JavaDoc[] {"/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 JavaDoc {
120         ClassPathXmlApplicationContext ctx =
121             new ClassPathXmlApplicationContext("/org/springframework/beans/factory/xml/resource.xml") {
122             public Resource getResource(String JavaDoc 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 JavaDoc writer = new StringWriter JavaDoc();
133         FileCopyUtils.copy(new InputStreamReader JavaDoc(resource1.getResource().getInputStream()), writer);
134         assertEquals("contexttest", writer.toString());
135         writer = new StringWriter JavaDoc();
136         FileCopyUtils.copy(new InputStreamReader JavaDoc(resource1.getInputStream()), writer);
137         assertEquals("contexttest", writer.toString());
138         writer = new StringWriter JavaDoc();
139         FileCopyUtils.copy(new InputStreamReader JavaDoc(resource2.getResource().getInputStream()), writer);
140         assertEquals("contexttest", writer.toString());
141         writer = new StringWriter JavaDoc();
142         FileCopyUtils.copy(new InputStreamReader JavaDoc(resource2.getInputStream()), writer);
143         assertEquals("contexttest", writer.toString());
144     }
145
146     public void testGenericApplicationContextWithXmlBeanDefinitions() throws Exception JavaDoc {
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