1
1
/*
2
- * Copyright 2002-2014 the original author or authors.
2
+ * Copyright 2002-2015 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
16
16
17
17
package org .springframework .test .context ;
18
18
19
+ import java .lang .reflect .Constructor ;
20
+
19
21
import org .apache .commons .logging .Log ;
20
22
import org .apache .commons .logging .LogFactory ;
21
23
36
38
*/
37
39
abstract class BootstrapUtils {
38
40
41
+ private static final String DEFAULT_BOOTSTRAP_CONTEXT_CLASS_NAME = "org.springframework.test.context.support.DefaultBootstrapContext" ;
42
+
43
+ private static final String DEFAULT_CACHE_AWARE_CONTEXT_LOADER_DELEGATE_CLASS_NAME = "org.springframework.test.context.support.DefaultCacheAwareContextLoaderDelegate" ;
44
+
39
45
private static final String DEFAULT_TEST_CONTEXT_BOOTSTRAPPER_CLASS_NAME = "org.springframework.test.context.support.DefaultTestContextBootstrapper" ;
40
46
41
47
private static final Log logger = LogFactory .getLog (BootstrapUtils .class );
@@ -45,6 +51,55 @@ private BootstrapUtils() {
45
51
/* no-op */
46
52
}
47
53
54
+ /**
55
+ * Create the {@code BootstrapContext} for the specified {@linkplain Class test class}.
56
+ *
57
+ * <p>Uses reflection to create a {@link org.springframework.test.context.support.DefaultBootstrapContext}
58
+ * that uses a {@link org.springframework.test.context.support.DefaultCacheAwareContextLoaderDelegate}.
59
+ *
60
+ * @param testClass the test class for which the bootstrap context should be created
61
+ * @return a new {@code BootstrapContext}; never {@code null}
62
+ */
63
+ @ SuppressWarnings ("unchecked" )
64
+ static BootstrapContext createBootstrapContext (Class <?> testClass ) {
65
+ CacheAwareContextLoaderDelegate cacheAwareContextLoaderDelegate = createCacheAwareContextLoaderDelegate ();
66
+
67
+ Class <? extends BootstrapContext > clazz = null ;
68
+ try {
69
+ clazz = (Class <? extends BootstrapContext >) ClassUtils .forName (DEFAULT_BOOTSTRAP_CONTEXT_CLASS_NAME ,
70
+ BootstrapUtils .class .getClassLoader ());
71
+
72
+ Constructor <? extends BootstrapContext > constructor = clazz .getConstructor (Class .class ,
73
+ CacheAwareContextLoaderDelegate .class );
74
+
75
+ if (logger .isDebugEnabled ()) {
76
+ logger .debug (String .format ("Instantiating BootstrapContext using constructor [%s]" , constructor ));
77
+ }
78
+ return instantiateClass (constructor , testClass , cacheAwareContextLoaderDelegate );
79
+ }
80
+ catch (Throwable t ) {
81
+ throw new IllegalStateException ("Could not load BootstrapContext [" + clazz + "]" , t );
82
+ }
83
+ }
84
+
85
+ @ SuppressWarnings ("unchecked" )
86
+ private static CacheAwareContextLoaderDelegate createCacheAwareContextLoaderDelegate () {
87
+ Class <? extends CacheAwareContextLoaderDelegate > clazz = null ;
88
+ try {
89
+ clazz = (Class <? extends CacheAwareContextLoaderDelegate >) ClassUtils .forName (
90
+ DEFAULT_CACHE_AWARE_CONTEXT_LOADER_DELEGATE_CLASS_NAME , BootstrapUtils .class .getClassLoader ());
91
+
92
+ if (logger .isDebugEnabled ()) {
93
+ logger .debug (String .format ("Instantiating CacheAwareContextLoaderDelegate from class [%s]" ,
94
+ clazz .getName ()));
95
+ }
96
+ return instantiateClass (clazz , CacheAwareContextLoaderDelegate .class );
97
+ }
98
+ catch (Throwable t ) {
99
+ throw new IllegalStateException ("Could not load CacheAwareContextLoaderDelegate [" + clazz + "]" , t );
100
+ }
101
+ }
102
+
48
103
/**
49
104
* Resolve the {@link TestContextBootstrapper} type for the test class in the
50
105
* supplied {@link BootstrapContext}, instantiate it, and provide it a reference
0 commit comments