1 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 30 public class ContextJndiBeanFactoryLocatorTests extends TestCase { 31 32 public static final String BEAN_FACTORY_PATH_ENVIRONMENT_KEY = "java:comp/env/ejb/BeanFactoryPath"; 33 34 public void testBeanFactoryPathRequiredFromJndiEnvironment() throws Exception { 35 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 assertTrue(ex.getMessage().indexOf(BEAN_FACTORY_PATH_ENVIRONMENT_KEY) != -1); 46 } 47 } 48 49 public void testBeanFactoryPathFromJndiEnvironmentNotFound() throws Exception { 50 SimpleNamingContextBuilder sncb = SimpleNamingContextBuilder.emptyActivatedContextBuilder(); 51 52 String bogusPath = "RUBBISH/com/xxxx/framework/server/test1.xml"; 53 54 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 assertTrue(ex.getMessage().indexOf(bogusPath) != -1); 65 } 66 } 67 68 public void testBeanFactoryPathFromJndiEnvironmentNotValidXml() throws Exception { 69 SimpleNamingContextBuilder sncb = SimpleNamingContextBuilder.emptyActivatedContextBuilder(); 70 71 String nonXmlPath = "com/xxxx/framework/server/SlsbEndpointBean.class"; 72 73 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 assertTrue(ex.getMessage().indexOf(nonXmlPath) != -1); 84 } 85 } 86 87 public void testBeanFactoryPathFromJndiEnvironmentWithSingleFile() throws Exception { 88 SimpleNamingContextBuilder sncb = SimpleNamingContextBuilder.emptyActivatedContextBuilder(); 89 90 String path = "org/springframework/beans/factory/xml/collections.xml"; 91 92 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 { 102 SimpleNamingContextBuilder sncb = SimpleNamingContextBuilder.emptyActivatedContextBuilder(); 103 104 String path = "/org/springframework/beans/factory/xml/collections.xml /org/springframework/beans/factory/xml/parent.xml"; 105 106 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 |