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; 018 019import org.springframework.context.ApplicationContext; 020import org.springframework.context.ConfigurableApplicationContext; 021import org.springframework.test.annotation.DirtiesContext.HierarchyMode; 022 023/** 024 * A {@code CacheAwareContextLoaderDelegate} is responsible for {@linkplain 025 * #loadContext loading} and {@linkplain #closeContext closing} application 026 * contexts, interacting transparently with a 027 * {@link org.springframework.test.context.cache.ContextCache ContextCache} 028 * behind the scenes. 029 * 030 * <p>Note: {@code CacheAwareContextLoaderDelegate} does not extend the 031 * {@link ContextLoader} or {@link SmartContextLoader} interface. 032 * 033 * @author Sam Brannen 034 * @since 3.2.2 035 */ 036public interface CacheAwareContextLoaderDelegate { 037 038 /** 039 * Load the {@linkplain ApplicationContext application context} for the supplied 040 * {@link MergedContextConfiguration} by delegating to the {@link ContextLoader} 041 * configured in the given {@code MergedContextConfiguration}. 042 * <p>If the context is present in the {@code ContextCache} it will simply 043 * be returned; otherwise, it will be loaded, stored in the cache, and returned. 044 * <p>The cache statistics should be logged by invoking 045 * {@link org.springframework.test.context.cache.ContextCache#logStatistics()}. 046 * @param mergedContextConfiguration the merged context configuration to use 047 * to load the application context; never {@code null} 048 * @return the application context 049 * @throws IllegalStateException if an error occurs while retrieving or loading 050 * the application context 051 */ 052 ApplicationContext loadContext(MergedContextConfiguration mergedContextConfiguration); 053 054 /** 055 * Remove the {@linkplain ApplicationContext application context} for the 056 * supplied {@link MergedContextConfiguration} from the {@code ContextCache} 057 * and {@linkplain ConfigurableApplicationContext#close() close} it if it is 058 * an instance of {@link ConfigurableApplicationContext}. 059 * <p>The semantics of the supplied {@code HierarchyMode} must be honored when 060 * removing the context from the cache. See the Javadoc for {@link HierarchyMode} 061 * for details. 062 * <p>Generally speaking, this method should only be called if the state of 063 * a singleton bean has been changed (potentially affecting future interaction 064 * with the context) or if the context needs to be prematurely removed from 065 * the cache. 066 * @param mergedContextConfiguration the merged context configuration for the 067 * application context to close; never {@code null} 068 * @param hierarchyMode the hierarchy mode; may be {@code null} if the context 069 * is not part of a hierarchy 070 * @since 4.1 071 */ 072 void closeContext(MergedContextConfiguration mergedContextConfiguration, HierarchyMode hierarchyMode); 073 074}