001/* 002 * Copyright 2002-2014 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 * https://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 017package org.springframework.test.context.support; 018 019import org.springframework.beans.factory.support.BeanDefinitionReader; 020import org.springframework.beans.factory.xml.XmlBeanDefinitionReader; 021import org.springframework.context.support.GenericApplicationContext; 022import org.springframework.test.context.MergedContextConfiguration; 023import org.springframework.util.ObjectUtils; 024 025/** 026 * Concrete implementation of {@link AbstractGenericContextLoader} that reads 027 * bean definitions from XML resources. 028 * 029 * <p>Default resource locations are detected using the suffix 030 * {@code "-context.xml"}. 031 * 032 * @author Sam Brannen 033 * @since 2.5 034 * @see XmlBeanDefinitionReader 035 * @see GenericGroovyXmlContextLoader 036 * @see AnnotationConfigContextLoader 037 */ 038public class GenericXmlContextLoader extends AbstractGenericContextLoader { 039 040 /** 041 * Create a new {@link XmlBeanDefinitionReader}. 042 * @return a new {@code XmlBeanDefinitionReader} 043 */ 044 @Override 045 protected BeanDefinitionReader createBeanDefinitionReader(GenericApplicationContext context) { 046 return new XmlBeanDefinitionReader(context); 047 } 048 049 /** 050 * Returns {@code "-context.xml"} in order to support detection of a 051 * default XML config file. 052 */ 053 @Override 054 protected String getResourceSuffix() { 055 return "-context.xml"; 056 } 057 058 /** 059 * Ensure that the supplied {@link MergedContextConfiguration} does not 060 * contain {@link MergedContextConfiguration#getClasses() classes}. 061 * @since 4.0.4 062 * @see AbstractGenericContextLoader#validateMergedContextConfiguration 063 */ 064 @Override 065 protected void validateMergedContextConfiguration(MergedContextConfiguration mergedConfig) { 066 if (mergedConfig.hasClasses()) { 067 String msg = String.format( 068 "Test class [%s] has been configured with @ContextConfiguration's 'classes' attribute %s, " 069 + "but %s does not support annotated classes.", mergedConfig.getTestClass().getName(), 070 ObjectUtils.nullSafeToString(mergedConfig.getClasses()), getClass().getSimpleName()); 071 logger.error(msg); 072 throw new IllegalStateException(msg); 073 } 074 } 075 076}