KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > context > access > ContextJndiBeanFactoryLocatorTests


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.access;
18
19 import junit.framework.TestCase;
20
21 import org.springframework.beans.BeansException;
22 import org.springframework.beans.factory.BeanFactory;
23 import org.springframework.beans.factory.access.BootstrapException;
24 import org.springframework.context.ApplicationContext;
25 import org.springframework.mock.jndi.SimpleNamingContextBuilder;
26
27 /**
28  * @author Colin Sampaleanu
29  */

30 public class ContextJndiBeanFactoryLocatorTests extends TestCase {
31
32     public static final String JavaDoc BEAN_FACTORY_PATH_ENVIRONMENT_KEY = "java:comp/env/ejb/BeanFactoryPath";
33
34     public void testBeanFactoryPathRequiredFromJndiEnvironment() throws Exception JavaDoc {
35         // Set up initial context but don't bind anything
36
SimpleNamingContextBuilder.emptyActivatedContextBuilder();
37
38         ContextJndiBeanFactoryLocator jbfl = new ContextJndiBeanFactoryLocator();
39         try {
40             jbfl.useBeanFactory(BEAN_FACTORY_PATH_ENVIRONMENT_KEY);
41             fail();
42         }
43         catch (BootstrapException ex) {
44             // Check for helpful JNDI message
45
assertTrue(ex.getMessage().indexOf(BEAN_FACTORY_PATH_ENVIRONMENT_KEY) != -1);
46         }
47     }
48
49     public void testBeanFactoryPathFromJndiEnvironmentNotFound() throws Exception JavaDoc {
50         SimpleNamingContextBuilder sncb = SimpleNamingContextBuilder.emptyActivatedContextBuilder();
51
52         String JavaDoc bogusPath = "RUBBISH/com/xxxx/framework/server/test1.xml";
53
54         // Set up initial context
55
sncb.bind(BEAN_FACTORY_PATH_ENVIRONMENT_KEY, bogusPath);
56
57         ContextJndiBeanFactoryLocator jbfl = new ContextJndiBeanFactoryLocator();
58         try {
59             jbfl.useBeanFactory(BEAN_FACTORY_PATH_ENVIRONMENT_KEY);
60             fail();
61         }
62         catch (BeansException ex) {
63             // Check for helpful JNDI message
64
assertTrue(ex.getMessage().indexOf(bogusPath) != -1);
65         }
66     }
67
68     public void testBeanFactoryPathFromJndiEnvironmentNotValidXml() throws Exception JavaDoc {
69         SimpleNamingContextBuilder sncb = SimpleNamingContextBuilder.emptyActivatedContextBuilder();
70
71         String JavaDoc nonXmlPath = "com/xxxx/framework/server/SlsbEndpointBean.class";
72
73         // Set up initial context
74
sncb.bind(BEAN_FACTORY_PATH_ENVIRONMENT_KEY, nonXmlPath);
75
76         ContextJndiBeanFactoryLocator jbfl = new ContextJndiBeanFactoryLocator();
77         try {
78             jbfl.useBeanFactory(BEAN_FACTORY_PATH_ENVIRONMENT_KEY);
79             fail();
80         }
81         catch (BeansException ex) {
82             // Check for helpful JNDI message
83
assertTrue(ex.getMessage().indexOf(nonXmlPath) != -1);
84         }
85     }
86
87     public void testBeanFactoryPathFromJndiEnvironmentWithSingleFile() throws Exception JavaDoc {
88         SimpleNamingContextBuilder sncb = SimpleNamingContextBuilder.emptyActivatedContextBuilder();
89
90         String JavaDoc path = "org/springframework/beans/factory/xml/collections.xml";
91
92         // Set up initial context
93
sncb.bind(BEAN_FACTORY_PATH_ENVIRONMENT_KEY, path);
94
95         ContextJndiBeanFactoryLocator jbfl = new ContextJndiBeanFactoryLocator();
96         BeanFactory bf = jbfl.useBeanFactory(BEAN_FACTORY_PATH_ENVIRONMENT_KEY).getFactory();
97         assertTrue(bf.containsBean("rod"));
98         assertTrue(bf instanceof ApplicationContext);
99     }
100
101     public void testBeanFactoryPathFromJndiEnvironmentWithMultipleFiles() throws Exception JavaDoc {
102         SimpleNamingContextBuilder sncb = SimpleNamingContextBuilder.emptyActivatedContextBuilder();
103
104         String JavaDoc path = "/org/springframework/beans/factory/xml/collections.xml /org/springframework/beans/factory/xml/parent.xml";
105
106         // Set up initial context
107
sncb.bind(BEAN_FACTORY_PATH_ENVIRONMENT_KEY, path);
108
109         ContextJndiBeanFactoryLocator jbfl = new ContextJndiBeanFactoryLocator();
110         BeanFactory bf = jbfl.useBeanFactory(BEAN_FACTORY_PATH_ENVIRONMENT_KEY).getFactory();
111         assertTrue(bf.containsBean("rod"));
112         assertTrue(bf.containsBean("inheritedTestBean"));
113     }
114
115 }
116
Popular Tags