Skip to content

Spr 12683 #780

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Apr 19, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -28,14 +28,15 @@
public interface BootstrapContext {

/**
* Get the {@link Class test class} for this bootstrap context.
* Get the {@linkplain Class test class} for this bootstrap context.
* @return the test class (never {@code null})
*/
Class<?> getTestClass();

/**
* Get the {@link CacheAwareContextLoaderDelegate} to use for transparent
* interaction with the <em>context cache</em>.
* interaction with the {@code ContextCache}.
* @return the context loader delegate (never {@code null})
*/
CacheAwareContextLoaderDelegate getCacheAwareContextLoaderDelegate();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,6 +16,8 @@

package org.springframework.test.context;

import java.lang.reflect.Constructor;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

Expand All @@ -36,6 +38,10 @@
*/
abstract class BootstrapUtils {

private static final String DEFAULT_BOOTSTRAP_CONTEXT_CLASS_NAME = "org.springframework.test.context.support.DefaultBootstrapContext";

private static final String DEFAULT_CACHE_AWARE_CONTEXT_LOADER_DELEGATE_CLASS_NAME = "org.springframework.test.context.support.DefaultCacheAwareContextLoaderDelegate";

private static final String DEFAULT_TEST_CONTEXT_BOOTSTRAPPER_CLASS_NAME = "org.springframework.test.context.support.DefaultTestContextBootstrapper";

private static final Log logger = LogFactory.getLog(BootstrapUtils.class);
Expand All @@ -45,6 +51,55 @@ private BootstrapUtils() {
/* no-op */
}

/**
* Create the {@code BootstrapContext} for the specified {@linkplain Class test class}.
*
* <p>Uses reflection to create a {@link org.springframework.test.context.support.DefaultBootstrapContext}
* that uses a {@link org.springframework.test.context.support.DefaultCacheAwareContextLoaderDelegate}.
*
* @param testClass the test class for which the bootstrap context should be created
* @return a new {@code BootstrapContext}; never {@code null}
*/
@SuppressWarnings("unchecked")
static BootstrapContext createBootstrapContext(Class<?> testClass) {
CacheAwareContextLoaderDelegate cacheAwareContextLoaderDelegate = createCacheAwareContextLoaderDelegate();

Class<? extends BootstrapContext> clazz = null;
try {
clazz = (Class<? extends BootstrapContext>) ClassUtils.forName(DEFAULT_BOOTSTRAP_CONTEXT_CLASS_NAME,
BootstrapUtils.class.getClassLoader());

Constructor<? extends BootstrapContext> constructor = clazz.getConstructor(Class.class,
CacheAwareContextLoaderDelegate.class);

if (logger.isDebugEnabled()) {
logger.debug(String.format("Instantiating BootstrapContext using constructor [%s]", constructor));
}
return instantiateClass(constructor, testClass, cacheAwareContextLoaderDelegate);
}
catch (Throwable t) {
throw new IllegalStateException("Could not load BootstrapContext [" + clazz + "]", t);
}
}

@SuppressWarnings("unchecked")
private static CacheAwareContextLoaderDelegate createCacheAwareContextLoaderDelegate() {
Class<? extends CacheAwareContextLoaderDelegate> clazz = null;
try {
clazz = (Class<? extends CacheAwareContextLoaderDelegate>) ClassUtils.forName(
DEFAULT_CACHE_AWARE_CONTEXT_LOADER_DELEGATE_CLASS_NAME, BootstrapUtils.class.getClassLoader());

if (logger.isDebugEnabled()) {
logger.debug(String.format("Instantiating CacheAwareContextLoaderDelegate from class [%s]",
clazz.getName()));
}
return instantiateClass(clazz, CacheAwareContextLoaderDelegate.class);
}
catch (Throwable t) {
throw new IllegalStateException("Could not load CacheAwareContextLoaderDelegate [" + clazz + "]", t);
}
}

/**
* Resolve the {@link TestContextBootstrapper} type for the test class in the
* supplied {@link BootstrapContext}, instantiate it, and provide it a reference
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -23,7 +23,7 @@
/**
* A {@code CacheAwareContextLoaderDelegate} is responsible for {@linkplain
* #loadContext loading} and {@linkplain #closeContext closing} application
* contexts, interacting transparently with a <em>context cache</em> behind
* contexts, interacting transparently with a {@link ContextCache} behind
* the scenes.
*
* <p>Note: {@code CacheAwareContextLoaderDelegate} does not extend the
Expand All @@ -38,8 +38,10 @@ public interface CacheAwareContextLoaderDelegate {
* Load the {@linkplain ApplicationContext application context} for the supplied
* {@link MergedContextConfiguration} by delegating to the {@link ContextLoader}
* configured in the given {@code MergedContextConfiguration}.
* <p>If the context is present in the <em>context cache</em> it will simply
* <p>If the context is present in the {@code ContextCache} it will simply
* be returned; otherwise, it will be loaded, stored in the cache, and returned.
* <p>The cache statistics should be logged by invoking
* {@link ContextCache#logStatistics()}.
* @param mergedContextConfiguration the merged context configuration to use
* to load the application context; never {@code null}
* @return the application context
Expand All @@ -50,7 +52,7 @@ public interface CacheAwareContextLoaderDelegate {

/**
* Remove the {@linkplain ApplicationContext application context} for the
* supplied {@link MergedContextConfiguration} from the <em>context cache</em>
* supplied {@link MergedContextConfiguration} from the {@code ContextCache}
* and {@linkplain ConfigurableApplicationContext#close() close} it if it is
* an instance of {@link ConfigurableApplicationContext}.
* <p>The semantics of the supplied {@code HierarchyMode} must be honored when
Expand Down
Loading