1515
1616package org .eclipse .edc .junit .extensions ;
1717
18- import org .eclipse .edc .boot .system .DefaultServiceExtensionContext ;
19- import org .eclipse .edc .boot .system .ServiceLocator ;
20- import org .eclipse .edc .boot .system .ServiceLocatorImpl ;
21- import org .eclipse .edc .boot .system .runtime .BaseRuntime ;
22- import org .eclipse .edc .spi .EdcException ;
23- import org .eclipse .edc .spi .monitor .Monitor ;
2418import org .eclipse .edc .spi .system .ConfigurationExtension ;
25- import org .eclipse .edc .spi .system .ServiceExtensionContext ;
2619import org .eclipse .edc .spi .system .SystemExtension ;
27- import org .eclipse .edc .spi .system .configuration .Config ;
2820import org .eclipse .edc .spi .system .configuration .ConfigFactory ;
29- import org .jetbrains .annotations .NotNull ;
3021import org .junit .jupiter .api .extension .AfterTestExecutionCallback ;
3122import org .junit .jupiter .api .extension .BeforeTestExecutionCallback ;
3223import org .junit .jupiter .api .extension .ExtensionContext ;
3324import org .junit .jupiter .api .extension .ParameterContext ;
3425import org .junit .jupiter .api .extension .ParameterResolutionException ;
3526import org .junit .jupiter .api .extension .ParameterResolver ;
3627
37- import java .util .ArrayList ;
38- import java .util .LinkedHashMap ;
39- import java .util .List ;
4028import java .util .Map ;
4129
30+ import static java .util .Collections .emptyMap ;
4231import static org .eclipse .edc .util .types .Cast .cast ;
4332
4433/**
4736 * injection of runtime services is supported.
4837 * <p>
4938 * If only basic dependency injection is needed, use {@link DependencyInjectionExtension} instead.
39+ *
40+ * @deprecated please use either {@link RuntimePerMethodExtension} or {@link RuntimePerClassExtension}.
5041 */
51- public class EdcExtension extends BaseRuntime implements BeforeTestExecutionCallback , AfterTestExecutionCallback , ParameterResolver {
52- private final LinkedHashMap <Class <?>, Object > serviceMocks = new LinkedHashMap <>();
53- private final MultiSourceServiceLocator serviceLocator ;
54- private DefaultServiceExtensionContext context ;
42+ @ Deprecated (since = "0.7.0" )
43+ public class EdcExtension implements BeforeTestExecutionCallback , AfterTestExecutionCallback , ParameterResolver {
44+ protected final EmbeddedRuntime runtime ;
5545
5646 public EdcExtension () {
57- this (new MultiSourceServiceLocator ( ));
47+ this (new EmbeddedRuntime ( "runtime" , emptyMap () ));
5848 }
5949
60- private EdcExtension (MultiSourceServiceLocator serviceLocator ) {
61- super (serviceLocator );
62- this .serviceLocator = serviceLocator ;
50+ protected EdcExtension (EmbeddedRuntime runtime ) {
51+ this .runtime = runtime ;
6352 }
6453
6554 /**
@@ -69,39 +58,35 @@ private EdcExtension(MultiSourceServiceLocator serviceLocator) {
6958 * @param mock the service mock
7059 */
7160 public <T > void registerServiceMock (Class <T > type , T mock ) {
72- serviceMocks . put (type , mock );
61+ runtime . registerServiceMock (type , mock );
7362 }
7463
7564 /**
7665 * Registers a service extension with the runtime.
7766 */
7867 public <T extends SystemExtension > void registerSystemExtension (Class <T > type , SystemExtension extension ) {
79- serviceLocator .registerSystemExtension (type , extension );
68+ runtime .registerSystemExtension (type , extension );
8069 }
8170
8271 @ Override
83- public void beforeTestExecution (ExtensionContext extensionContext ) throws Exception {
84- bootWithoutShutdownHook ( );
72+ public void beforeTestExecution (ExtensionContext extensionContext ) {
73+ runtime . boot ( false );
8574 }
8675
8776 @ Override
88- public void afterTestExecution (ExtensionContext context ) throws Exception {
89- shutdown ();
90- // clear the systemExtensions map to prevent it from piling up between subsequent runs
91- serviceLocator .clearSystemExtensions ();
92- }
93-
94- public DefaultServiceExtensionContext getContext () {
95- return context ;
77+ public void afterTestExecution (ExtensionContext context ) {
78+ runtime .shutdown ();
9679 }
9780
9881 @ Override
9982 public boolean supportsParameter (ParameterContext parameterContext , ExtensionContext extensionContext ) throws ParameterResolutionException {
10083 var type = parameterContext .getParameter ().getParameterizedType ();
10184 if (type .equals (EdcExtension .class )) {
10285 return true ;
86+ } else if (type .equals (EmbeddedRuntime .class )) {
87+ return true ;
10388 } else if (type instanceof Class ) {
104- return context .hasService (cast (type ));
89+ return runtime . getContext () .hasService (cast (type ));
10590 }
10691 return false ;
10792 }
@@ -111,8 +96,10 @@ public Object resolveParameter(ParameterContext parameterContext, ExtensionConte
11196 var type = parameterContext .getParameter ().getParameterizedType ();
11297 if (type .equals (EdcExtension .class )) {
11398 return this ;
99+ } else if (type .equals (EmbeddedRuntime .class )) {
100+ return runtime ;
114101 } else if (type instanceof Class ) {
115- return context .getService (cast (type ));
102+ return runtime . getContext () .getService (cast (type ));
116103 }
117104 return null ;
118105 }
@@ -122,55 +109,7 @@ public void setConfiguration(Map<String, String> configuration) {
122109 }
123110
124111 public <T > T getService (Class <T > clazz ) {
125- return context .getService (clazz );
126- }
127-
128- @ Override
129- protected @ NotNull ServiceExtensionContext createContext (Monitor monitor , Config config ) {
130- context = new TestServiceExtensionContext (monitor , config , serviceMocks );
131- return context ;
132- }
133-
134- /**
135- * A service locator that allows additional extensions to be manually loaded by a test fixture. This locator return
136- * the union of registered extensions and extensions loaded by the delegate.
137- */
138- private static class MultiSourceServiceLocator implements ServiceLocator {
139- private final ServiceLocator delegate = new ServiceLocatorImpl ();
140- private final LinkedHashMap <Class <? extends SystemExtension >, List <SystemExtension >> systemExtensions ;
141-
142- MultiSourceServiceLocator () {
143- systemExtensions = new LinkedHashMap <>();
144- }
145-
146- @ Override
147- public <T > List <T > loadImplementors (Class <T > type , boolean required ) {
148- List <T > extensions = cast (systemExtensions .getOrDefault (type , new ArrayList <>()));
149- extensions .addAll (delegate .loadImplementors (type , required ));
150- return extensions ;
151- }
152-
153- /**
154- * This implementation will override singleton implementions found by the delegate.
155- */
156- @ Override
157- public <T > T loadSingletonImplementor (Class <T > type , boolean required ) {
158- var extensions = systemExtensions .get (type );
159- if (extensions == null || extensions .isEmpty ()) {
160- return delegate .loadSingletonImplementor (type , required );
161- } else if (extensions .size () > 1 ) {
162- throw new EdcException ("Multiple extensions were registered for type: " + type .getName ());
163- }
164- return type .cast (extensions .get (0 ));
165- }
166-
167- public <T extends SystemExtension > void registerSystemExtension (Class <T > type , SystemExtension extension ) {
168- systemExtensions .computeIfAbsent (type , k -> new ArrayList <>()).add (extension );
169- }
170-
171- public void clearSystemExtensions () {
172- systemExtensions .clear ();
173- }
112+ return runtime .getService (clazz );
174113 }
175114
176115}
0 commit comments