15
15
16
16
package org .eclipse .edc .junit .extensions ;
17
17
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 ;
24
18
import org .eclipse .edc .spi .system .ConfigurationExtension ;
25
- import org .eclipse .edc .spi .system .ServiceExtensionContext ;
26
19
import org .eclipse .edc .spi .system .SystemExtension ;
27
- import org .eclipse .edc .spi .system .configuration .Config ;
28
20
import org .eclipse .edc .spi .system .configuration .ConfigFactory ;
29
- import org .jetbrains .annotations .NotNull ;
30
21
import org .junit .jupiter .api .extension .AfterTestExecutionCallback ;
31
22
import org .junit .jupiter .api .extension .BeforeTestExecutionCallback ;
32
23
import org .junit .jupiter .api .extension .ExtensionContext ;
33
24
import org .junit .jupiter .api .extension .ParameterContext ;
34
25
import org .junit .jupiter .api .extension .ParameterResolutionException ;
35
26
import org .junit .jupiter .api .extension .ParameterResolver ;
36
27
37
- import java .util .ArrayList ;
38
- import java .util .LinkedHashMap ;
39
- import java .util .List ;
40
28
import java .util .Map ;
41
29
30
+ import static java .util .Collections .emptyMap ;
42
31
import static org .eclipse .edc .util .types .Cast .cast ;
43
32
44
33
/**
47
36
* injection of runtime services is supported.
48
37
* <p>
49
38
* If only basic dependency injection is needed, use {@link DependencyInjectionExtension} instead.
39
+ *
40
+ * @deprecated please use either {@link RuntimePerMethodExtension} or {@link RuntimePerClassExtension}.
50
41
*/
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 ;
55
45
56
46
public EdcExtension () {
57
- this (new MultiSourceServiceLocator ( ));
47
+ this (new EmbeddedRuntime ( "runtime" , emptyMap () ));
58
48
}
59
49
60
- private EdcExtension (MultiSourceServiceLocator serviceLocator ) {
61
- super (serviceLocator );
62
- this .serviceLocator = serviceLocator ;
50
+ protected EdcExtension (EmbeddedRuntime runtime ) {
51
+ this .runtime = runtime ;
63
52
}
64
53
65
54
/**
@@ -69,39 +58,35 @@ private EdcExtension(MultiSourceServiceLocator serviceLocator) {
69
58
* @param mock the service mock
70
59
*/
71
60
public <T > void registerServiceMock (Class <T > type , T mock ) {
72
- serviceMocks . put (type , mock );
61
+ runtime . registerServiceMock (type , mock );
73
62
}
74
63
75
64
/**
76
65
* Registers a service extension with the runtime.
77
66
*/
78
67
public <T extends SystemExtension > void registerSystemExtension (Class <T > type , SystemExtension extension ) {
79
- serviceLocator .registerSystemExtension (type , extension );
68
+ runtime .registerSystemExtension (type , extension );
80
69
}
81
70
82
71
@ Override
83
- public void beforeTestExecution (ExtensionContext extensionContext ) throws Exception {
84
- bootWithoutShutdownHook ( );
72
+ public void beforeTestExecution (ExtensionContext extensionContext ) {
73
+ runtime . boot ( false );
85
74
}
86
75
87
76
@ 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 ();
96
79
}
97
80
98
81
@ Override
99
82
public boolean supportsParameter (ParameterContext parameterContext , ExtensionContext extensionContext ) throws ParameterResolutionException {
100
83
var type = parameterContext .getParameter ().getParameterizedType ();
101
84
if (type .equals (EdcExtension .class )) {
102
85
return true ;
86
+ } else if (type .equals (EmbeddedRuntime .class )) {
87
+ return true ;
103
88
} else if (type instanceof Class ) {
104
- return context .hasService (cast (type ));
89
+ return runtime . getContext () .hasService (cast (type ));
105
90
}
106
91
return false ;
107
92
}
@@ -111,8 +96,10 @@ public Object resolveParameter(ParameterContext parameterContext, ExtensionConte
111
96
var type = parameterContext .getParameter ().getParameterizedType ();
112
97
if (type .equals (EdcExtension .class )) {
113
98
return this ;
99
+ } else if (type .equals (EmbeddedRuntime .class )) {
100
+ return runtime ;
114
101
} else if (type instanceof Class ) {
115
- return context .getService (cast (type ));
102
+ return runtime . getContext () .getService (cast (type ));
116
103
}
117
104
return null ;
118
105
}
@@ -122,55 +109,7 @@ public void setConfiguration(Map<String, String> configuration) {
122
109
}
123
110
124
111
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 );
174
113
}
175
114
176
115
}
0 commit comments