43
43
*/
44
44
abstract class BootstrapUtils {
45
45
46
- private static final String DEFAULT_BOOTSTRAP_CONTEXT_CLASS_NAME = "org.springframework.test.context.support.DefaultBootstrapContext" ;
46
+ private static final String DEFAULT_BOOTSTRAP_CONTEXT_CLASS_NAME =
47
+ "org.springframework.test.context.support.DefaultBootstrapContext" ;
47
48
48
- private static final String DEFAULT_CACHE_AWARE_CONTEXT_LOADER_DELEGATE_CLASS_NAME = "org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate" ;
49
+ private static final String DEFAULT_CACHE_AWARE_CONTEXT_LOADER_DELEGATE_CLASS_NAME =
50
+ "org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate" ;
49
51
50
- private static final String DEFAULT_TEST_CONTEXT_BOOTSTRAPPER_CLASS_NAME = "org.springframework.test.context.support.DefaultTestContextBootstrapper" ;
52
+ private static final String DEFAULT_TEST_CONTEXT_BOOTSTRAPPER_CLASS_NAME =
53
+ "org.springframework.test.context.support.DefaultTestContextBootstrapper" ;
51
54
52
- private static final String DEFAULT_WEB_TEST_CONTEXT_BOOTSTRAPPER_CLASS_NAME = "org.springframework.test.context.web.WebTestContextBootstrapper" ;
55
+ private static final String DEFAULT_WEB_TEST_CONTEXT_BOOTSTRAPPER_CLASS_NAME =
56
+ "org.springframework.test.context.web.WebTestContextBootstrapper" ;
53
57
54
- private static final String WEB_APP_CONFIGURATION_ANNOTATION_CLASS_NAME = "org.springframework.test.context.web.WebAppConfiguration" ;
58
+ private static final String WEB_APP_CONFIGURATION_ANNOTATION_CLASS_NAME =
59
+ "org.springframework.test.context.web.WebAppConfiguration" ;
55
60
56
61
private static final Log logger = LogFactory .getLog (BootstrapUtils .class );
57
62
58
63
59
- private BootstrapUtils () {
60
- /* no-op */
61
- }
62
-
63
64
/**
64
65
* Create the {@code BootstrapContext} for the specified {@linkplain Class test class}.
65
- *
66
66
* <p>Uses reflection to create a {@link org.springframework.test.context.support.DefaultBootstrapContext}
67
67
* that uses a {@link org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate}.
68
- *
69
68
* @param testClass the test class for which the bootstrap context should be created
70
69
* @return a new {@code BootstrapContext}; never {@code null}
71
70
*/
72
71
@ SuppressWarnings ("unchecked" )
73
72
static BootstrapContext createBootstrapContext (Class <?> testClass ) {
74
73
CacheAwareContextLoaderDelegate cacheAwareContextLoaderDelegate = createCacheAwareContextLoaderDelegate ();
75
-
76
74
Class <? extends BootstrapContext > clazz = null ;
77
75
try {
78
- clazz = (Class <? extends BootstrapContext >) ClassUtils .forName (DEFAULT_BOOTSTRAP_CONTEXT_CLASS_NAME ,
79
- BootstrapUtils .class .getClassLoader ());
80
-
81
- Constructor <? extends BootstrapContext > constructor = clazz .getConstructor (Class .class ,
82
- CacheAwareContextLoaderDelegate .class );
83
-
76
+ clazz = (Class <? extends BootstrapContext >) ClassUtils .forName (
77
+ DEFAULT_BOOTSTRAP_CONTEXT_CLASS_NAME , BootstrapUtils .class .getClassLoader ());
78
+ Constructor <? extends BootstrapContext > constructor = clazz .getConstructor (
79
+ Class .class , CacheAwareContextLoaderDelegate .class );
84
80
if (logger .isDebugEnabled ()) {
85
81
logger .debug (String .format ("Instantiating BootstrapContext using constructor [%s]" , constructor ));
86
82
}
87
83
return instantiateClass (constructor , testClass , cacheAwareContextLoaderDelegate );
88
84
}
89
- catch (Throwable t ) {
90
- throw new IllegalStateException ("Could not load BootstrapContext [" + clazz + "]" , t );
85
+ catch (Throwable ex ) {
86
+ throw new IllegalStateException ("Could not load BootstrapContext [" + clazz + "]" , ex );
91
87
}
92
88
}
93
89
@@ -104,16 +100,15 @@ private static CacheAwareContextLoaderDelegate createCacheAwareContextLoaderDele
104
100
}
105
101
return instantiateClass (clazz , CacheAwareContextLoaderDelegate .class );
106
102
}
107
- catch (Throwable t ) {
108
- throw new IllegalStateException ("Could not load CacheAwareContextLoaderDelegate [" + clazz + "]" , t );
103
+ catch (Throwable ex ) {
104
+ throw new IllegalStateException ("Could not load CacheAwareContextLoaderDelegate [" + clazz + "]" , ex );
109
105
}
110
106
}
111
107
112
108
/**
113
109
* Resolve the {@link TestContextBootstrapper} type for the test class in the
114
110
* supplied {@link BootstrapContext}, instantiate it, and provide it a reference
115
111
* to the {@link BootstrapContext}.
116
- *
117
112
* <p>If the {@link BootstrapWith @BootstrapWith} annotation is present on
118
113
* the test class, either directly or as a meta-annotation, then its
119
114
* {@link BootstrapWith#value value} will be used as the bootstrapper type.
@@ -123,7 +118,6 @@ private static CacheAwareContextLoaderDelegate createCacheAwareContextLoaderDele
123
118
* {@link org.springframework.test.context.web.WebTestContextBootstrapper
124
119
* WebTestContextBootstrapper} will be used, depending on the presence of
125
120
* {@link org.springframework.test.context.web.WebAppConfiguration @WebAppConfiguration}.
126
- *
127
121
* @param bootstrapContext the bootstrap context to use
128
122
* @return a fully configured {@code TestContextBootstrapper}
129
123
*/
@@ -138,36 +132,36 @@ static TestContextBootstrapper resolveTestContextBootstrapper(BootstrapContext b
138
132
}
139
133
if (logger .isDebugEnabled ()) {
140
134
logger .debug (String .format ("Instantiating TestContextBootstrapper for test class [%s] from class [%s]" ,
141
- testClass .getName (), clazz .getName ()));
135
+ testClass .getName (), clazz .getName ()));
142
136
}
143
-
144
137
TestContextBootstrapper testContextBootstrapper = instantiateClass (clazz , TestContextBootstrapper .class );
145
138
testContextBootstrapper .setBootstrapContext (bootstrapContext );
146
-
147
139
return testContextBootstrapper ;
148
140
}
149
141
catch (Throwable ex ) {
150
142
if (ex instanceof IllegalStateException ) {
151
143
throw (IllegalStateException ) ex ;
152
144
}
153
- throw new IllegalStateException ("Could not load TestContextBootstrapper [" + clazz
154
- + "]. Specify @BootstrapWith's 'value' attribute "
155
- + "or make the default bootstrapper class available." , ex );
145
+ throw new IllegalStateException ("Could not load TestContextBootstrapper [" + clazz +
146
+ "]. Specify @BootstrapWith's 'value' attribute or make the default bootstrapper class available." ,
147
+ ex );
156
148
}
157
149
}
158
150
159
151
/**
160
152
* @since 4.3
161
153
*/
162
154
private static Class <?> resolveExplicitTestContextBootstrapper (Class <?> testClass ) {
163
- MultiValueMap <String , Object > attributesMultiMap = AnnotatedElementUtils . getAllAnnotationAttributes (
164
- testClass , BootstrapWith .class .getName ());
165
- List <Object > values = (attributesMultiMap == null ? null : attributesMultiMap .get (AnnotationUtils .VALUE ));
155
+ MultiValueMap <String , Object > attributesMultiMap =
156
+ AnnotatedElementUtils . getAllAnnotationAttributes ( testClass , BootstrapWith .class .getName ());
157
+ List <Object > values = (attributesMultiMap != null ? attributesMultiMap .get (AnnotationUtils .VALUE ) : null );
166
158
if (values == null ) {
167
159
return null ;
168
160
}
169
- Assert .state (values .size () == 1 , String .format ("Configuration error: found multiple declarations of "
170
- + "@BootstrapWith on test class [%s] with values %s" , testClass .getName (), values ));
161
+ if (values .size () != 1 ) {
162
+ throw new IllegalStateException (String .format ("Configuration error: found multiple declarations of " +
163
+ "@BootstrapWith on test class [%s] with values %s" , testClass .getName (), values ));
164
+ }
171
165
return (Class <?>) values .get (0 );
172
166
}
173
167
0 commit comments