1 16 17 package org.springframework.beans.factory.access; 18 19 import java.util.HashMap ; 20 import java.util.Map ; 21 import java.util.Properties ; 22 23 import junit.framework.TestCase; 24 25 import org.springframework.beans.BeansException; 26 import org.springframework.beans.TestBean; 27 import org.springframework.beans.factory.BeanFactory; 28 import org.springframework.beans.factory.NoSuchBeanDefinitionException; 29 30 34 public class BeanFactoryBootstrapTests extends TestCase { 35 36 private Properties savedProps; 37 38 protected void setUp() { 39 this.savedProps = System.getProperties(); 41 } 42 43 protected void tearDown() { 44 System.setProperties(this.savedProps); 45 } 46 47 48 public void testGetInstanceWithNullPropertiesFails() throws BeansException { 49 System.setProperties(null); 50 BeanFactoryBootstrap.reinitialize(); 51 try { 52 BeanFactoryBootstrap bsb = BeanFactoryBootstrap.getInstance(); 53 fail("Should have failed with no system properties"); 54 } 55 catch (BootstrapException ex) { 56 } 58 } 59 60 public void testGetInstanceWithUnknownBeanFactoryClassFails() throws BeansException { 61 System.setProperties(null); 62 Properties p = new Properties (); 63 p.put(BeanFactoryBootstrap.BEAN_FACTORY_BEAN_NAME + ".class", 64 "org.springframework.beans.factory.support.xxxxXmlBeanFactory"); 65 66 System.setProperties(p); 67 BeanFactoryBootstrap.reinitialize(); 68 try { 69 BeanFactoryBootstrap bsb = BeanFactoryBootstrap.getInstance(); 70 fail("Should have failed with invalid class"); 71 } 72 catch (BootstrapException ex) { 73 } 75 } 76 77 public void testGetInstanceWithMistypedBeanFactoryClassFails() throws BeansException { 78 System.setProperties(null); 79 Properties p = new Properties (); 80 p.put(BeanFactoryBootstrap.BEAN_FACTORY_BEAN_NAME + ".class", 81 "java.awt.Point"); 82 83 System.setProperties(p); 84 BeanFactoryBootstrap.reinitialize(); 85 try { 86 BeanFactoryBootstrap bsb = BeanFactoryBootstrap.getInstance(); 87 fail("Should have failed with mistyped class"); 88 } 89 catch (BootstrapException ex) { 90 } 92 catch (Exception ex) { 93 ex.printStackTrace(); 94 } 95 } 96 97 130 public void testDummyBeanFactory() throws Exception { 131 Properties p = new Properties (); 132 p.put(BeanFactoryBootstrap.BEAN_FACTORY_BEAN_NAME + ".class", 133 "org.springframework.beans.factory.access.BeanFactoryBootstrapTests$DummyBeanFactory"); 134 135 System.setProperties(p); 136 137 BeanFactoryBootstrap.reinitialize(); 138 139 try { 140 BeanFactoryBootstrap bsb = BeanFactoryBootstrap.getInstance(); 141 assertNotNull("Bsb instance is not null", bsb); 142 assertTrue("Is dummy", bsb.getBeanFactory() instanceof DummyBeanFactory); 143 TestBean tb = (TestBean) bsb.getBeanFactory().getBean("test"); 144 assertNotNull("Test bean is not null", tb); 145 } 147 catch (Exception ex) { 148 ex.printStackTrace(); 149 throw ex; 150 } 151 } 152 153 154 public static class DummyBeanFactory implements BeanFactory { 155 156 public Map map = new HashMap (); 157 158 { 159 this.map.put("test", new TestBean()); 160 this.map.put("s", new String ()); 161 } 162 163 public Object getBean(String name) { 164 Object bean = this.map.get(name); 165 if (bean == null) { 166 throw new NoSuchBeanDefinitionException(name, "no message"); 167 } 168 return bean; 169 } 170 171 public Object getBean(String name, Class requiredType) { 172 return getBean(name); 173 } 174 175 public boolean containsBean(String name) { 176 return this.map.containsKey(name); 177 } 178 179 public boolean isSingleton(String name) { 180 return true; 181 } 182 183 public Class getType(String name) { 184 return null; 185 } 186 187 public String [] getAliases(String name) { 188 throw new UnsupportedOperationException ("getAliases"); 189 } 190 } 191 192 } 193 | Popular Tags |