65
65
* @author <a href="mailto:[email protected] ">Michal Maczka</a>
66
66
* @author Guillaume Nodet
67
67
*/
68
- public class PlexusExtension implements BeforeEachCallback , AfterEachCallback
69
- {
68
+ public class PlexusExtension implements BeforeEachCallback , AfterEachCallback {
70
69
private ExtensionContext context ;
71
70
private PlexusContainer container ;
72
71
73
72
private static String basedir ;
74
73
75
74
@ Override
76
- public void beforeEach ( ExtensionContext context )
77
- throws Exception
78
- {
75
+ public void beforeEach (ExtensionContext context ) throws Exception {
79
76
basedir = getBasedir ();
80
77
this .context = context ;
81
78
82
- getContainer ().addComponent ( getContainer (), PlexusContainer .class .getName () );
79
+ getContainer ().addComponent (getContainer (), PlexusContainer .class .getName ());
83
80
84
- ( (DefaultPlexusContainer ) getContainer () ).addPlexusInjector ( Collections .emptyList (),
85
- binder -> binder .requestInjection ( context .getRequiredTestInstance () ) );
81
+ ((DefaultPlexusContainer ) getContainer ())
82
+ .addPlexusInjector (
83
+ Collections .emptyList (), binder -> binder .requestInjection (context .getRequiredTestInstance ()));
86
84
}
87
85
88
- @ SuppressWarnings ( "ResultOfMethodCallIgnored" )
89
- protected void setupContainer ()
90
- {
86
+ @ SuppressWarnings ("ResultOfMethodCallIgnored" )
87
+ protected void setupContainer () {
91
88
// ----------------------------------------------------------------------------
92
89
// Context Setup
93
90
// ----------------------------------------------------------------------------
94
91
95
92
DefaultContext context = new DefaultContext ();
96
93
97
- context .put ( "basedir" , getBasedir () );
94
+ context .put ("basedir" , getBasedir ());
98
95
99
- customizeContext ( context );
96
+ customizeContext (context );
100
97
101
- boolean hasPlexusHome = context .contains ( "plexus.home" );
98
+ boolean hasPlexusHome = context .contains ("plexus.home" );
102
99
103
- if ( !hasPlexusHome )
104
- {
105
- File f = getTestFile ( "target/plexus-home" );
100
+ if (!hasPlexusHome ) {
101
+ File f = getTestFile ("target/plexus-home" );
106
102
107
- if ( !f .isDirectory () )
108
- {
103
+ if (!f .isDirectory ()) {
109
104
f .mkdir ();
110
105
}
111
106
112
- context .put ( "plexus.home" , f .getAbsolutePath () );
107
+ context .put ("plexus.home" , f .getAbsolutePath ());
113
108
}
114
109
115
110
// ----------------------------------------------------------------------------
@@ -118,30 +113,23 @@ protected void setupContainer()
118
113
119
114
String config = getCustomConfigurationName ();
120
115
121
- ContainerConfiguration containerConfiguration = new DefaultContainerConfiguration ()
122
- .setName ( "test" )
123
- .setContext ( context .getContextData () );
116
+ ContainerConfiguration containerConfiguration =
117
+ new DefaultContainerConfiguration ().setName ("test" ).setContext (context .getContextData ());
124
118
125
- if ( config != null )
126
- {
127
- containerConfiguration .setContainerConfiguration ( config );
128
- }
129
- else
130
- {
131
- String resource = getConfigurationName ( null );
119
+ if (config != null ) {
120
+ containerConfiguration .setContainerConfiguration (config );
121
+ } else {
122
+ String resource = getConfigurationName (null );
132
123
133
- containerConfiguration .setContainerConfiguration ( resource );
124
+ containerConfiguration .setContainerConfiguration (resource );
134
125
}
135
126
136
- customizeContainerConfiguration ( containerConfiguration );
127
+ customizeContainerConfiguration (containerConfiguration );
137
128
138
- try
139
- {
140
- container = new DefaultPlexusContainer ( containerConfiguration );
141
- }
142
- catch ( PlexusContainerException e )
143
- {
144
- throw new IllegalArgumentException ( "Failed to create plexus container." , e );
129
+ try {
130
+ container = new DefaultPlexusContainer (containerConfiguration );
131
+ } catch (PlexusContainerException e ) {
132
+ throw new IllegalArgumentException ("Failed to create plexus container." , e );
145
133
}
146
134
}
147
135
@@ -151,57 +139,43 @@ protected void setupContainer()
151
139
*
152
140
* @param containerConfiguration {@link ContainerConfiguration}.
153
141
*/
154
- protected void customizeContainerConfiguration ( ContainerConfiguration containerConfiguration )
155
- {
156
- containerConfiguration .setAutoWiring ( true );
157
- containerConfiguration .setClassPathScanning ( PlexusConstants .SCANNING_INDEX );
142
+ protected void customizeContainerConfiguration (ContainerConfiguration containerConfiguration ) {
143
+ containerConfiguration .setAutoWiring (true );
144
+ containerConfiguration .setClassPathScanning (PlexusConstants .SCANNING_INDEX );
158
145
}
159
146
160
- protected void customizeContext ( Context context )
161
- {
162
- }
147
+ protected void customizeContext (Context context ) {}
163
148
164
- protected PlexusConfiguration customizeComponentConfiguration ()
165
- {
149
+ protected PlexusConfiguration customizeComponentConfiguration () {
166
150
return null ;
167
151
}
168
152
169
153
@ Override
170
- public void afterEach ( ExtensionContext context )
171
- throws Exception
172
- {
173
- if ( container != null )
174
- {
154
+ public void afterEach (ExtensionContext context ) throws Exception {
155
+ if (container != null ) {
175
156
container .dispose ();
176
157
177
158
container = null ;
178
159
}
179
160
}
180
161
181
- public PlexusContainer getContainer ()
182
- {
183
- if ( container == null )
184
- {
162
+ public PlexusContainer getContainer () {
163
+ if (container == null ) {
185
164
setupContainer ();
186
165
}
187
166
188
167
return container ;
189
168
}
190
169
191
- protected InputStream getConfiguration ()
192
- throws Exception
193
- {
194
- return getConfiguration ( null );
170
+ protected InputStream getConfiguration () throws Exception {
171
+ return getConfiguration (null );
195
172
}
196
173
197
- protected InputStream getConfiguration ( String subname )
198
- throws Exception
199
- {
200
- return getResourceAsStream ( getConfigurationName ( subname ) );
174
+ protected InputStream getConfiguration (String subname ) throws Exception {
175
+ return getResourceAsStream (getConfigurationName (subname ));
201
176
}
202
177
203
- protected String getCustomConfigurationName ()
204
- {
178
+ protected String getCustomConfigurationName () {
205
179
return null ;
206
180
}
207
181
@@ -214,128 +188,98 @@ protected String getCustomConfigurationName()
214
188
* @param subname the subname
215
189
* @return A configruation name
216
190
*/
217
- protected String getConfigurationName ( String subname )
218
- {
191
+ protected String getConfigurationName (String subname ) {
219
192
Class <?> testClass = context .getRequiredTestClass ();
220
- for ( Class <?> clazz = testClass ;
221
- clazz != null ;
222
- clazz = clazz .getSuperclass () )
223
- {
224
- String name = clazz .getName ().replace ( '.' , '/' ) + ".xml" ;
225
- if ( testClass .getClassLoader ().getResource ( name ) != null )
226
- {
193
+ for (Class <?> clazz = testClass ; clazz != null ; clazz = clazz .getSuperclass ()) {
194
+ String name = clazz .getName ().replace ('.' , '/' ) + ".xml" ;
195
+ if (testClass .getClassLoader ().getResource (name ) != null ) {
227
196
return name ;
228
197
}
229
198
}
230
199
return null ;
231
200
}
232
201
233
- protected InputStream getResourceAsStream ( String resource )
234
- {
235
- return context .getRequiredTestClass ().getResourceAsStream ( resource );
202
+ protected InputStream getResourceAsStream (String resource ) {
203
+ return context .getRequiredTestClass ().getResourceAsStream (resource );
236
204
}
237
205
238
- protected ClassLoader getClassLoader ()
239
- {
206
+ protected ClassLoader getClassLoader () {
240
207
return context .getRequiredTestClass ().getClassLoader ();
241
208
}
242
209
243
210
// ----------------------------------------------------------------------
244
211
// Container access
245
212
// ----------------------------------------------------------------------
246
213
247
- @ SuppressWarnings ( "unchecked" )
248
- protected <T > T lookup ( String componentKey )
249
- throws ComponentLookupException
250
- {
251
- return (T ) getContainer ().lookup ( componentKey );
214
+ @ SuppressWarnings ("unchecked" )
215
+ protected <T > T lookup (String componentKey ) throws ComponentLookupException {
216
+ return (T ) getContainer ().lookup (componentKey );
252
217
}
253
218
254
- @ SuppressWarnings ( "unchecked" )
255
- protected <T > T lookup ( String role ,
256
- String roleHint )
257
- throws ComponentLookupException
258
- {
259
- return (T ) getContainer ().lookup ( role , roleHint );
219
+ @ SuppressWarnings ("unchecked" )
220
+ protected <T > T lookup (String role , String roleHint ) throws ComponentLookupException {
221
+ return (T ) getContainer ().lookup (role , roleHint );
260
222
}
261
223
262
- protected <T > T lookup ( Class <T > componentClass )
263
- throws ComponentLookupException
264
- {
265
- return getContainer ().lookup ( componentClass );
224
+ protected <T > T lookup (Class <T > componentClass ) throws ComponentLookupException {
225
+ return getContainer ().lookup (componentClass );
266
226
}
267
227
268
- protected <T > T lookup ( Class <T > componentClass , String roleHint )
269
- throws ComponentLookupException
270
- {
271
- return getContainer ().lookup ( componentClass , roleHint );
228
+ protected <T > T lookup (Class <T > componentClass , String roleHint ) throws ComponentLookupException {
229
+ return getContainer ().lookup (componentClass , roleHint );
272
230
}
273
231
274
- protected void release ( Object component )
275
- throws ComponentLifecycleException
276
- {
277
- getContainer ().release ( component );
232
+ protected void release (Object component ) throws ComponentLifecycleException {
233
+ getContainer ().release (component );
278
234
}
279
235
280
236
// ----------------------------------------------------------------------
281
237
// Helper methods for sub classes
282
238
// ----------------------------------------------------------------------
283
239
284
- public static File getTestFile ( String path )
285
- {
286
- return new File ( getBasedir (), path );
240
+ public static File getTestFile (String path ) {
241
+ return new File (getBasedir (), path );
287
242
}
288
243
289
- public static File getTestFile ( String basedir ,
290
- String path )
291
- {
292
- File basedirFile = new File ( basedir );
244
+ public static File getTestFile (String basedir , String path ) {
245
+ File basedirFile = new File (basedir );
293
246
294
- if ( !basedirFile .isAbsolute () )
295
- {
296
- basedirFile = getTestFile ( basedir );
247
+ if (!basedirFile .isAbsolute ()) {
248
+ basedirFile = getTestFile (basedir );
297
249
}
298
250
299
- return new File ( basedirFile , path );
251
+ return new File (basedirFile , path );
300
252
}
301
253
302
- public static String getTestPath ( String path )
303
- {
304
- return getTestFile ( path ).getAbsolutePath ();
254
+ public static String getTestPath (String path ) {
255
+ return getTestFile (path ).getAbsolutePath ();
305
256
}
306
257
307
- public static String getTestPath ( String basedir ,
308
- String path )
309
- {
310
- return getTestFile ( basedir , path ).getAbsolutePath ();
258
+ public static String getTestPath (String basedir , String path ) {
259
+ return getTestFile (basedir , path ).getAbsolutePath ();
311
260
}
312
261
313
- public static String getBasedir ()
314
- {
315
- if ( basedir != null )
316
- {
262
+ public static String getBasedir () {
263
+ if (basedir != null ) {
317
264
return basedir ;
318
265
}
319
266
320
- basedir = System .getProperty ( "basedir" );
267
+ basedir = System .getProperty ("basedir" );
321
268
322
- if ( basedir == null )
323
- {
324
- basedir = new File ( "" ).getAbsolutePath ();
269
+ if (basedir == null ) {
270
+ basedir = new File ("" ).getAbsolutePath ();
325
271
}
326
272
327
273
return basedir ;
328
274
}
329
275
330
- public String getTestConfiguration ()
331
- {
332
- return getTestConfiguration ( context .getRequiredTestClass () );
276
+ public String getTestConfiguration () {
277
+ return getTestConfiguration (context .getRequiredTestClass ());
333
278
}
334
279
335
- public static String getTestConfiguration ( Class <?> clazz )
336
- {
337
- String s = clazz .getName ().replace ( '.' , '/' );
280
+ public static String getTestConfiguration (Class <?> clazz ) {
281
+ String s = clazz .getName ().replace ('.' , '/' );
338
282
339
- return s .substring ( 0 , s .indexOf ( "$" ) ) + ".xml" ;
283
+ return s .substring (0 , s .indexOf ("$" ) ) + ".xml" ;
340
284
}
341
285
}
0 commit comments