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 java.util.Properties;
020
021import org.springframework.beans.factory.support.BeanDefinitionReader;
022import org.springframework.beans.factory.support.PropertiesBeanDefinitionReader;
023import org.springframework.context.support.GenericApplicationContext;
024import org.springframework.test.context.MergedContextConfiguration;
025import org.springframework.util.ObjectUtils;
026
027/**
028 * Concrete implementation of {@link AbstractGenericContextLoader} that reads
029 * bean definitions from Java {@link Properties} resources.
030 *
031 * @author Sam Brannen
032 * @since 2.5
033 */
034public class GenericPropertiesContextLoader extends AbstractGenericContextLoader {
035
036        /**
037         * Creates a new {@link PropertiesBeanDefinitionReader}.
038         * @return a new PropertiesBeanDefinitionReader
039         * @see PropertiesBeanDefinitionReader
040         */
041        @Override
042        protected BeanDefinitionReader createBeanDefinitionReader(final GenericApplicationContext context) {
043                return new PropertiesBeanDefinitionReader(context);
044        }
045
046        /**
047         * Returns "{@code -context.properties}".
048         */
049        @Override
050        protected String getResourceSuffix() {
051                return "-context.properties";
052        }
053
054        /**
055         * Ensure that the supplied {@link MergedContextConfiguration} does not
056         * contain {@link MergedContextConfiguration#getClasses() classes}.
057         * @since 4.0.4
058         * @see AbstractGenericContextLoader#validateMergedContextConfiguration
059         */
060        @Override
061        protected void validateMergedContextConfiguration(MergedContextConfiguration mergedConfig) {
062                if (mergedConfig.hasClasses()) {
063                        String msg = String.format(
064                                "Test class [%s] has been configured with @ContextConfiguration's 'classes' attribute %s, "
065                                                + "but %s does not support annotated classes.", mergedConfig.getTestClass().getName(),
066                                ObjectUtils.nullSafeToString(mergedConfig.getClasses()), getClass().getSimpleName());
067                        logger.error(msg);
068                        throw new IllegalStateException(msg);
069                }
070        }
071
072}