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