001/*
002 * Copyright 2002-2018 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.groovy.GroovyBeanDefinitionReader;
020import org.springframework.context.support.GenericApplicationContext;
021import org.springframework.test.context.MergedContextConfiguration;
022
023/**
024 * Concrete implementation of {@link AbstractGenericContextLoader} that reads
025 * bean definitions from Groovy scripts <em>and</em> XML configuration files.
026 *
027 * <p>Default resource locations are detected using the suffixes
028 * {@code "-context.xml"} and {@code "Context.groovy"}.
029 *
030 * @author Sam Brannen
031 * @since 4.1
032 * @see GroovyBeanDefinitionReader
033 * @see GenericXmlContextLoader
034 * @see AnnotationConfigContextLoader
035 */
036public class GenericGroovyXmlContextLoader extends GenericXmlContextLoader {
037
038        /**
039         * Load bean definitions into the supplied {@link GenericApplicationContext context}
040         * from the locations in the supplied {@code MergedContextConfiguration} using a
041         * {@link GroovyBeanDefinitionReader}.
042         * @param context the context into which the bean definitions should be loaded
043         * @param mergedConfig the merged context configuration
044         * @see org.springframework.test.context.support.AbstractGenericContextLoader#loadBeanDefinitions
045         */
046        @Override
047        protected void loadBeanDefinitions(GenericApplicationContext context, MergedContextConfiguration mergedConfig) {
048                new GroovyBeanDefinitionReader(context).loadBeanDefinitions(mergedConfig.getLocations());
049        }
050
051        /**
052         * Returns {@code "-context.xml" and "Context.groovy"} in order to
053         * support detection of a default XML config file or Groovy script.
054         */
055        @Override
056        protected String[] getResourceSuffixes() {
057                return new String[] { super.getResourceSuffix(), "Context.groovy" };
058        }
059
060        /**
061         * {@code GenericGroovyXmlContextLoader} supports both Groovy and XML
062         * resource types for detection of defaults. Consequently, this method
063         * is not supported.
064         * @see #getResourceSuffixes()
065         * @throws UnsupportedOperationException in this implementation
066         */
067        @Override
068        protected String getResourceSuffix() {
069                throw new UnsupportedOperationException(
070                                "GenericGroovyXmlContextLoader does not support the getResourceSuffix() method");
071        }
072
073}