6565 * @author <a href="mailto:[email protected] ">Michal Maczka</a> 6666 * @author Guillaume Nodet
6767 */
68- public class PlexusExtension implements BeforeEachCallback , AfterEachCallback
69- {
68+ public class PlexusExtension implements BeforeEachCallback , AfterEachCallback {
7069 private ExtensionContext context ;
7170 private PlexusContainer container ;
7271
7372 private static String basedir ;
7473
7574 @ Override
76- public void beforeEach ( ExtensionContext context )
77- throws Exception
78- {
75+ public void beforeEach (ExtensionContext context ) throws Exception {
7976 basedir = getBasedir ();
8077 this .context = context ;
8178
82- getContainer ().addComponent ( getContainer (), PlexusContainer .class .getName () );
79+ getContainer ().addComponent (getContainer (), PlexusContainer .class .getName ());
8380
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 ()));
8684 }
8785
88- @ SuppressWarnings ( "ResultOfMethodCallIgnored" )
89- protected void setupContainer ()
90- {
86+ @ SuppressWarnings ("ResultOfMethodCallIgnored" )
87+ protected void setupContainer () {
9188 // ----------------------------------------------------------------------------
9289 // Context Setup
9390 // ----------------------------------------------------------------------------
9491
9592 DefaultContext context = new DefaultContext ();
9693
97- context .put ( "basedir" , getBasedir () );
94+ context .put ("basedir" , getBasedir ());
9895
99- customizeContext ( context );
96+ customizeContext (context );
10097
101- boolean hasPlexusHome = context .contains ( "plexus.home" );
98+ boolean hasPlexusHome = context .contains ("plexus.home" );
10299
103- if ( !hasPlexusHome )
104- {
105- File f = getTestFile ( "target/plexus-home" );
100+ if (!hasPlexusHome ) {
101+ File f = getTestFile ("target/plexus-home" );
106102
107- if ( !f .isDirectory () )
108- {
103+ if (!f .isDirectory ()) {
109104 f .mkdir ();
110105 }
111106
112- context .put ( "plexus.home" , f .getAbsolutePath () );
107+ context .put ("plexus.home" , f .getAbsolutePath ());
113108 }
114109
115110 // ----------------------------------------------------------------------------
@@ -118,30 +113,23 @@ protected void setupContainer()
118113
119114 String config = getCustomConfigurationName ();
120115
121- ContainerConfiguration containerConfiguration = new DefaultContainerConfiguration ()
122- .setName ( "test" )
123- .setContext ( context .getContextData () );
116+ ContainerConfiguration containerConfiguration =
117+ new DefaultContainerConfiguration ().setName ("test" ).setContext (context .getContextData ());
124118
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 );
132123
133- containerConfiguration .setContainerConfiguration ( resource );
124+ containerConfiguration .setContainerConfiguration (resource );
134125 }
135126
136- customizeContainerConfiguration ( containerConfiguration );
127+ customizeContainerConfiguration (containerConfiguration );
137128
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 );
145133 }
146134 }
147135
@@ -151,57 +139,43 @@ protected void setupContainer()
151139 *
152140 * @param containerConfiguration {@link ContainerConfiguration}.
153141 */
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 );
158145 }
159146
160- protected void customizeContext ( Context context )
161- {
162- }
147+ protected void customizeContext (Context context ) {}
163148
164- protected PlexusConfiguration customizeComponentConfiguration ()
165- {
149+ protected PlexusConfiguration customizeComponentConfiguration () {
166150 return null ;
167151 }
168152
169153 @ 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 ) {
175156 container .dispose ();
176157
177158 container = null ;
178159 }
179160 }
180161
181- public PlexusContainer getContainer ()
182- {
183- if ( container == null )
184- {
162+ public PlexusContainer getContainer () {
163+ if (container == null ) {
185164 setupContainer ();
186165 }
187166
188167 return container ;
189168 }
190169
191- protected InputStream getConfiguration ()
192- throws Exception
193- {
194- return getConfiguration ( null );
170+ protected InputStream getConfiguration () throws Exception {
171+ return getConfiguration (null );
195172 }
196173
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 ));
201176 }
202177
203- protected String getCustomConfigurationName ()
204- {
178+ protected String getCustomConfigurationName () {
205179 return null ;
206180 }
207181
@@ -214,128 +188,98 @@ protected String getCustomConfigurationName()
214188 * @param subname the subname
215189 * @return A configruation name
216190 */
217- protected String getConfigurationName ( String subname )
218- {
191+ protected String getConfigurationName (String subname ) {
219192 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 ) {
227196 return name ;
228197 }
229198 }
230199 return null ;
231200 }
232201
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 );
236204 }
237205
238- protected ClassLoader getClassLoader ()
239- {
206+ protected ClassLoader getClassLoader () {
240207 return context .getRequiredTestClass ().getClassLoader ();
241208 }
242209
243210 // ----------------------------------------------------------------------
244211 // Container access
245212 // ----------------------------------------------------------------------
246213
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 );
252217 }
253218
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 );
260222 }
261223
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 );
266226 }
267227
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 );
272230 }
273231
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 );
278234 }
279235
280236 // ----------------------------------------------------------------------
281237 // Helper methods for sub classes
282238 // ----------------------------------------------------------------------
283239
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 );
287242 }
288243
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 );
293246
294- if ( !basedirFile .isAbsolute () )
295- {
296- basedirFile = getTestFile ( basedir );
247+ if (!basedirFile .isAbsolute ()) {
248+ basedirFile = getTestFile (basedir );
297249 }
298250
299- return new File ( basedirFile , path );
251+ return new File (basedirFile , path );
300252 }
301253
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 ();
305256 }
306257
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 ();
311260 }
312261
313- public static String getBasedir ()
314- {
315- if ( basedir != null )
316- {
262+ public static String getBasedir () {
263+ if (basedir != null ) {
317264 return basedir ;
318265 }
319266
320- basedir = System .getProperty ( "basedir" );
267+ basedir = System .getProperty ("basedir" );
321268
322- if ( basedir == null )
323- {
324- basedir = new File ( "" ).getAbsolutePath ();
269+ if (basedir == null ) {
270+ basedir = new File ("" ).getAbsolutePath ();
325271 }
326272
327273 return basedir ;
328274 }
329275
330- public String getTestConfiguration ()
331- {
332- return getTestConfiguration ( context .getRequiredTestClass () );
276+ public String getTestConfiguration () {
277+ return getTestConfiguration (context .getRequiredTestClass ());
333278 }
334279
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 ('.' , '/' );
338282
339- return s .substring ( 0 , s .indexOf ( "$" ) ) + ".xml" ;
283+ return s .substring (0 , s .indexOf ("$" ) ) + ".xml" ;
340284 }
341285}
0 commit comments