001/* 002 * Copyright 2002-2019 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.lang.Nullable; 022import org.springframework.test.annotation.DirtiesContext.HierarchyMode; 023 024/** 025 * A {@code CacheAwareContextLoaderDelegate} is responsible for {@linkplain 026 * #loadContext loading} and {@linkplain #closeContext closing} application 027 * contexts, interacting transparently with a 028 * {@link org.springframework.test.context.cache.ContextCache ContextCache} 029 * behind the scenes. 030 * 031 * <p>Note: {@code CacheAwareContextLoaderDelegate} does not extend the 032 * {@link ContextLoader} or {@link SmartContextLoader} interface. 033 * 034 * @author Sam Brannen 035 * @since 3.2.2 036 */ 037public interface CacheAwareContextLoaderDelegate { 038 039 /** 040 * Determine if the {@linkplain ApplicationContext application context} for 041 * the supplied {@link MergedContextConfiguration} has been loaded (i.e., 042 * is present in the {@code ContextCache}). 043 * <p>Implementations of this method <strong>must not</strong> load the 044 * application context as a side effect. In addition, implementations of 045 * this method should not log the cache statistics via 046 * {@link org.springframework.test.context.cache.ContextCache#logStatistics()}. 047 * <p>The default implementation of this method always returns {@code false}. 048 * Custom {@code CacheAwareContextLoaderDelegate} implementations are 049 * therefore highly encouraged to override this method with a more meaningful 050 * implementation. Note that the standard {@code CacheAwareContextLoaderDelegate} 051 * implementation in Spring overrides this method appropriately. 052 * @param mergedContextConfiguration the merged context configuration used 053 * to load the application context; never {@code null} 054 * @return {@code true} if the application context has been loaded 055 * @since 5.2 056 * @see #loadContext 057 * @see #closeContext 058 */ 059 default boolean isContextLoaded(MergedContextConfiguration mergedContextConfiguration) { 060 return false; 061 } 062 063 /** 064 * Load the {@linkplain ApplicationContext application context} for the supplied 065 * {@link MergedContextConfiguration} by delegating to the {@link ContextLoader} 066 * configured in the given {@code MergedContextConfiguration}. 067 * <p>If the context is present in the {@code ContextCache} it will simply 068 * be returned; otherwise, it will be loaded, stored in the cache, and returned. 069 * <p>The cache statistics should be logged by invoking 070 * {@link org.springframework.test.context.cache.ContextCache#logStatistics()}. 071 * @param mergedContextConfiguration the merged context configuration to use 072 * to load the application context; never {@code null} 073 * @return the application context (never {@code null}) 074 * @throws IllegalStateException if an error occurs while retrieving or loading 075 * the application context 076 * @see #isContextLoaded 077 * @see #closeContext 078 */ 079 ApplicationContext loadContext(MergedContextConfiguration mergedContextConfiguration); 080 081 /** 082 * Remove the {@linkplain ApplicationContext application context} for the 083 * supplied {@link MergedContextConfiguration} from the {@code ContextCache} 084 * and {@linkplain ConfigurableApplicationContext#close() close} it if it is 085 * an instance of {@link ConfigurableApplicationContext}. 086 * <p>The semantics of the supplied {@code HierarchyMode} must be honored when 087 * removing the context from the cache. See the Javadoc for {@link HierarchyMode} 088 * for details. 089 * <p>Generally speaking, this method should only be called if the state of 090 * a singleton bean has been changed (potentially affecting future interaction 091 * with the context) or if the context needs to be prematurely removed from 092 * the cache. 093 * @param mergedContextConfiguration the merged context configuration for the 094 * application context to close; never {@code null} 095 * @param hierarchyMode the hierarchy mode; may be {@code null} if the context 096 * is not part of a hierarchy 097 * @since 4.1 098 * @see #isContextLoaded 099 * @see #loadContext 100 */ 101 void closeContext(MergedContextConfiguration mergedContextConfiguration, @Nullable HierarchyMode hierarchyMode); 102 103}