001 /* 002 * Copyright 2002-2004 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017 package org.springframework.beans.factory.access; 018 019 import java.util.HashMap; 020 import java.util.Map; 021 import java.util.Properties; 022 023 import junit.framework.TestCase; 024 025 import org.springframework.beans.BeansException; 026 import org.springframework.beans.TestBean; 027 import org.springframework.beans.factory.BeanFactory; 028 import org.springframework.beans.factory.NoSuchBeanDefinitionException; 029 030 /** 031 * 032 * @author Rod Johnson 033 * @since 02-Dec-02 034 */ 035 public class BeanFactoryBootstrapTests extends TestCase { 036 037 private Properties _savedProps; 038 039 /** How to test many singletons? */ 040 public void testGetInstanceWithNullPropertiesFails() throws BeansException { 041 System.setProperties(null); 042 BeanFactoryBootstrap.reinitialize(); 043 try { 044 BeanFactoryBootstrap bsb = BeanFactoryBootstrap.getInstance(); 045 fail("Should have failed with no system properties"); 046 } 047 catch (BootstrapException ex) { 048 // OK 049 } 050 } 051 052 public void testGetInstanceWithUnknownBeanFactoryClassFails() throws BeansException { 053 System.setProperties(null); 054 Properties p = new Properties(); 055 p.put(BeanFactoryBootstrap.BEAN_FACTORY_BEAN_NAME + ".class", 056 "org.springframework.beans.factory.support.xxxxXmlBeanFactory"); 057 058 System.setProperties(p); 059 BeanFactoryBootstrap.reinitialize(); 060 try { 061 BeanFactoryBootstrap bsb = BeanFactoryBootstrap.getInstance(); 062 fail("Should have failed with invalid class"); 063 } 064 catch (BootstrapException ex) { 065 // OK 066 } 067 } 068 069 public void testGetInstanceWithMistypedBeanFactoryClassFails() throws BeansException { 070 System.setProperties(null); 071 Properties p = new Properties(); 072 p.put(BeanFactoryBootstrap.BEAN_FACTORY_BEAN_NAME + ".class", 073 "java.awt.Point"); 074 075 System.setProperties(p); 076 BeanFactoryBootstrap.reinitialize(); 077 try { 078 BeanFactoryBootstrap bsb = BeanFactoryBootstrap.getInstance(); 079 fail("Should have failed with mistyped class"); 080 } 081 catch (BootstrapException ex) { 082 // OK 083 } 084 catch (Exception ex) { 085 ex.printStackTrace(); 086 } 087 } 088 089 090 // public void testXmlBeanFactory() throws Exception { 091 // Properties p = new Properties(); 092 // p.put(BeanFactoryBootstrap.BEAN_FACTORY_BEAN_NAME + ".class", 093 // "XmlBeanFactory"); 094 // p.put(BeanFactoryBootstrap.BEAN_FACTORY_BEAN_NAME + ".url", 095 // "c:/checkouts/book/framework/src/org/springframework/beans/factory/support/bs.xml"); 096 // 097 // 098 // System.setProperties(p); 099 // System.getProperties().list(System.out); 100 // 101 // BeanFactoryBootstrap.reinitialize(); 102 // 103 // try { 104 // BeanFactoryBootstrap bsb = BeanFactoryBootstrap.getInstance(); 105 // 106 // BeanFactory bf1 = BeanFactoryBootstrap.getInstance().getBeanFactory(); 107 // BeanFactory bf2 = BeanFactoryBootstrap.getInstance().getBeanFactory(); 108 // assertTrue("Two instances identical", bf1==bf2); 109 // 110 // System.out.println("Got bean factory"); 111 // assertNotNull("Bsb instance is not null", bsb); 112 // TestBean tb = (TestBean) bsb.getBeanFactory().getBean("test"); 113 // assertNotNull("Test bean is not null", tb); 114 // System.out.println(tb); 115 // assertTrue("Property set", tb.getFoo().equals("bar")); 116 // } 117 // catch (Exception ex) { 118 // ex.printStackTrace(); 119 // throw ex; 120 // } 121 // } 122 123 124 public void testDummyBeanFactory() throws Exception { 125 Properties p = new Properties(); 126 p.put(BeanFactoryBootstrap.BEAN_FACTORY_BEAN_NAME + ".class", 127 "org.springframework.beans.factory.access.BeanFactoryBootstrapTests$DummyBeanFactory"); 128 129 130 System.setProperties(p); 131 //System.getProperties().list(System.out); 132 133 BeanFactoryBootstrap.reinitialize(); 134 135 try { 136 BeanFactoryBootstrap bsb = BeanFactoryBootstrap.getInstance(); 137 assertNotNull("Bsb instance is not null", bsb); 138 assertTrue("Is dummy", bsb.getBeanFactory() instanceof DummyBeanFactory); 139 TestBean tb = (TestBean) bsb.getBeanFactory().getBean("test"); 140 assertNotNull("Test bean is not null", tb); 141 //assertTrue("Property set", tb.getFoo().equals("bar")); 142 } 143 catch (Exception ex) { 144 ex.printStackTrace(); 145 throw ex; 146 } 147 } 148 149 public static class DummyBeanFactory implements BeanFactory { 150 151 public Map m = new HashMap(); 152 153 { 154 m.put("test", new TestBean()); 155 m.put("s", new String()); 156 } 157 158 public Object getBean(String name) { 159 Object bean = m.get(name); 160 if (bean == null) 161 throw new NoSuchBeanDefinitionException(name, "no message"); 162 return bean; 163 } 164 165 public Object getBean(String name, Class requiredType) { 166 return getBean(name); 167 } 168 169 public boolean containsBean(String name) { 170 return m.containsKey(name); 171 } 172 173 public boolean isSingleton(String name) { 174 return true; 175 } 176 177 public Class getType(String name) { 178 return null; 179 } 180 181 public String[] getAliases(String name) { 182 throw new UnsupportedOperationException("getAliases"); 183 } 184 } 185 186 /* (non-Javadoc) 187 * @see junit.framework.TestCase#setUp() 188 */ 189 protected void setUp() throws Exception { 190 // save and restore System properties, which get destroyed for the tests Rate191 _savedProps = System.getProperties(); 192 } 193 194 /* (non-Javadoc) 195 * @see junit.framework.TestCase#tearDown() 196 */ 197 protected void tearDown() throws Exception { 198 System.setProperties(_savedProps); 199 } 200 201 }