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.web; 018 019import org.springframework.beans.factory.groovy.GroovyBeanDefinitionReader; 020import org.springframework.web.context.support.GenericWebApplicationContext; 021 022/** 023 * Concrete implementation of {@link AbstractGenericWebContextLoader} that loads 024 * bean definitions from Groovy scripts <em>and</em> XML configuration files. 025 * 026 * <p>Default resource locations are detected using the suffixes 027 * {@code "-context.xml"} and {@code "Context.groovy"}. 028 * 029 * @author Sam Brannen 030 * @since 4.1 031 * @see GroovyBeanDefinitionReader 032 * @see GenericXmlWebContextLoader 033 * @see AnnotationConfigWebContextLoader 034 */ 035public class GenericGroovyXmlWebContextLoader extends GenericXmlWebContextLoader { 036 037 /** 038 * Load bean definitions into the supplied {@link GenericWebApplicationContext context} 039 * from the locations in the supplied {@code WebMergedContextConfiguration} using a 040 * {@link GroovyBeanDefinitionReader}. 041 * @param context the context into which the bean definitions should be loaded 042 * @param webMergedConfig the merged context configuration 043 * @see AbstractGenericWebContextLoader#loadBeanDefinitions 044 */ 045 @Override 046 protected void loadBeanDefinitions(GenericWebApplicationContext context, 047 WebMergedContextConfiguration webMergedConfig) { 048 new GroovyBeanDefinitionReader(context).loadBeanDefinitions(webMergedConfig.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 GenericGroovyXmlWebContextLoader} 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 "GenericGroovyXmlWebContextLoader does not support the getResourceSuffix() method"); 071 } 072 073}