1 16 17 package org.springframework.test; 18 19 import org.springframework.beans.factory.support.DefaultListableBeanFactory; 20 import org.springframework.beans.factory.xml.XmlBeanDefinitionReader; 21 import org.springframework.context.ConfigurableApplicationContext; 22 import org.springframework.context.support.GenericApplicationContext; 23 import org.springframework.util.ClassUtils; 24 import org.springframework.util.ResourceUtils; 25 import org.springframework.util.StringUtils; 26 27 58 public abstract class AbstractSingleSpringContextTests extends AbstractSpringContextTests { 59 60 61 protected ConfigurableApplicationContext applicationContext; 62 63 private int loadCount = 0; 64 65 66 69 public AbstractSingleSpringContextTests() { 70 } 71 72 76 public AbstractSingleSpringContextTests(String name) { 77 super(name); 78 } 79 80 81 86 protected final void setUp() throws Exception { 87 this.applicationContext = getContext(contextKey()); 88 prepareTestInstance(); 89 onSetUp(); 90 } 91 92 98 protected void prepareTestInstance() throws Exception { 99 } 100 101 107 protected void onSetUp() throws Exception { 108 } 109 110 115 protected void setDirty() { 116 setDirty(contextKey()); 117 } 118 119 124 protected final void tearDown() throws Exception { 125 onTearDown(); 126 } 127 128 132 protected void onTearDown() throws Exception { 133 } 134 135 136 145 protected Object contextKey() { 146 return getConfigLocations(); 147 } 148 149 157 protected ConfigurableApplicationContext loadContext(Object key) throws Exception { 158 return loadContextLocations((String []) key); 159 } 160 161 174 protected ConfigurableApplicationContext loadContextLocations(String [] locations) throws Exception { 175 ++this.loadCount; 176 if (logger.isInfoEnabled()) { 177 logger.info("Loading context for locations: " + StringUtils.arrayToCommaDelimitedString(locations)); 178 } 179 return createApplicationContext(locations); 180 } 181 182 195 protected ConfigurableApplicationContext createApplicationContext(String [] locations) { 196 GenericApplicationContext context = new GenericApplicationContext(); 197 customizeBeanFactory(context.getDefaultListableBeanFactory()); 198 new XmlBeanDefinitionReader(context).loadBeanDefinitions(locations); 199 context.refresh(); 200 return context; 201 } 202 203 215 protected void customizeBeanFactory(DefaultListableBeanFactory beanFactory) { 216 } 217 218 219 235 protected String [] getConfigLocations() { 236 String [] paths = getConfigPaths(); 237 String [] locations = new String [paths.length]; 238 for (int i = 0; i < paths.length; i++) { 239 String path = paths[i]; 240 if (path.startsWith("/")) { 241 locations[i] = ResourceUtils.CLASSPATH_URL_PREFIX + path; 242 } 243 else { 244 locations[i] = ResourceUtils.CLASSPATH_URL_PREFIX + 245 StringUtils.cleanPath(ClassUtils.classPackageAsResourcePath(getClass()) + "/" + path); 246 } 247 } 248 return locations; 249 } 250 251 264 protected String [] getConfigPaths() { 265 String path = getConfigPath(); 266 return (path != null ? new String [] {path} : new String [0]); 267 } 268 269 281 protected String getConfigPath() { 282 return null; 283 } 284 285 286 289 public final ConfigurableApplicationContext getApplicationContext() { 290 return this.applicationContext; 291 } 292 293 296 public final int getLoadCount() { 297 return this.loadCount; 298 } 299 300 } 301 | Popular Tags |