52
52
import org .eclipse .microprofile .config .Config ;
53
53
import org .eclipse .microprofile .config .inject .ConfigProperty ;
54
54
import org .jboss .jandex .AnnotationInstance ;
55
+ import org .jboss .jandex .AnnotationTransformation ;
55
56
import org .jboss .jandex .AnnotationValue ;
56
57
import org .jboss .jandex .ClassInfo ;
57
58
import org .jboss .jandex .ClassType ;
76
77
77
78
import io .quarkus .arc .All ;
78
79
import io .quarkus .arc .ComponentsProvider ;
79
- import io .quarkus .arc .Unremovable ;
80
80
import io .quarkus .arc .processor .Annotations ;
81
81
import io .quarkus .arc .processor .AnnotationsTransformer ;
82
82
import io .quarkus .arc .processor .BeanArchives ;
100
100
import io .quarkus .dev .testing .TracingHandler ;
101
101
import io .quarkus .gizmo .Gizmo ;
102
102
import io .quarkus .test .InjectMock ;
103
+ import io .quarkus .test .component .QuarkusComponentTestCallbacks .BeforeBuildContext ;
104
+ import io .quarkus .test .component .QuarkusComponentTestCallbacks .BeforeIndexContext ;
103
105
import io .smallrye .config .ConfigMapping ;
104
106
105
107
class ComponentContainer {
@@ -112,6 +114,7 @@ class ComponentContainer {
112
114
* @param testClass
113
115
* @param configuration
114
116
* @param buildShouldFail
117
+ * @param tracedClasses
115
118
* @return the build result
116
119
*/
117
120
static BuildResult build (Class <?> testClass , QuarkusComponentTestConfiguration configuration , boolean buildShouldFail ,
@@ -135,6 +138,16 @@ static BuildResult build(Class<?> testClass, QuarkusComponentTestConfiguration c
135
138
// Make sure that component hierarchy and all annotations present are indexed
136
139
indexComponentClass (indexer , componentClass );
137
140
}
141
+ if (configuration .hasCallbacks ()) {
142
+ BeforeIndexContextImpl context = new BeforeIndexContextImpl (testClass , configuration .componentClasses );
143
+ for (QuarkusComponentTestCallbacks callback : configuration .callbacks ) {
144
+ callback .beforeIndex (context );
145
+ }
146
+ for (Class <?> clazz : context .additionalComponentsClasses ) {
147
+ indexComponentClass (indexer , clazz );
148
+ }
149
+ }
150
+
138
151
indexer .indexClass (ConfigProperty .class );
139
152
index = BeanArchives .buildImmutableBeanArchiveIndex (indexer .complete ());
140
153
} catch (IOException e ) {
@@ -152,6 +165,20 @@ static BuildResult build(Class<?> testClass, QuarkusComponentTestConfiguration c
152
165
Map <String , Set <String >> configMappings = new HashMap <>();
153
166
Map <String , String []> interceptorMethods = new HashMap <>();
154
167
Throwable buildFailure = null ;
168
+ List <BytecodeTransformer > bytecodeTransformers = new ArrayList <>();
169
+ List <AnnotationTransformation > annotationTransformations = new ArrayList <>();
170
+ for (AnnotationsTransformer transformer : configuration .annotationsTransformers ) {
171
+ annotationTransformations .add (transformer );
172
+ }
173
+ List <BeanRegistrar > beanRegistrars = new ArrayList <>();
174
+
175
+ if (configuration .hasCallbacks ()) {
176
+ BeforeBuildContext beforeBuildContext = new BeforeBulidContextImpl (testClass , index , computingIndex ,
177
+ bytecodeTransformers , annotationTransformations , beanRegistrars );
178
+ for (QuarkusComponentTestCallbacks callback : configuration .callbacks ) {
179
+ callback .beforeBuild (beforeBuildContext );
180
+ }
181
+ }
155
182
156
183
try {
157
184
// These are populated after BeanProcessor.registerCustomContexts() is called
@@ -164,6 +191,7 @@ static BuildResult build(Class<?> testClass, QuarkusComponentTestConfiguration c
164
191
List <Parameter > injectParams = findInjectParams (testClass );
165
192
166
193
String beanProcessorName = testClass .getName ().replace ('.' , '_' );
194
+ AtomicReference <BeanDeployment > beanDeployment = new AtomicReference <>();
167
195
168
196
BeanProcessor .Builder builder = BeanProcessor .builder ()
169
197
.setName (beanProcessorName )
@@ -172,7 +200,7 @@ static BuildResult build(Class<?> testClass, QuarkusComponentTestConfiguration c
172
200
// 1. Annotated with @Unremovable
173
201
// 2. Injected in the test class or in a test method parameter
174
202
if (b .getTarget ().isPresent ()
175
- && b .getTarget ().get (). hasDeclaredAnnotation ( Unremovable . class )) {
203
+ && beanDeployment . get (). hasAnnotation ( b .getTarget ().get (), DotNames . UNREMOVABLE )) {
176
204
return true ;
177
205
}
178
206
for (Field injectionPoint : injectFields ) {
@@ -220,7 +248,7 @@ public void writeResource(Resource resource) throws IOException {
220
248
break ;
221
249
case SERVICE_PROVIDER :
222
250
if (resource .getName ()
223
- .endsWith (ComponentsProvider .class .getName ())) {
251
+ .equals (ComponentsProvider .class .getName ())) {
224
252
componentsProvider .set (resource .getData ());
225
253
}
226
254
break ;
@@ -234,8 +262,8 @@ public void writeResource(Resource resource) throws IOException {
234
262
.whenContainsNone (DotName .createSimple (Inject .class )).thenTransform (t -> t .add (Inject .class )));
235
263
236
264
builder .addAnnotationTransformation (new JaxrsSingletonTransformer ());
237
- for (AnnotationsTransformer transformer : configuration . annotationsTransformers ) {
238
- builder .addAnnotationTransformation (transformer );
265
+ for (AnnotationTransformation transformation : annotationTransformations ) {
266
+ builder .addAnnotationTransformation (transformation );
239
267
}
240
268
241
269
// Register:
@@ -390,11 +418,14 @@ public void register(RegistrationContext registrationContext) {
390
418
for (MockBeanConfiguratorImpl <?> mockConfigurator : configuration .mockConfigurators ) {
391
419
builder .addBeanRegistrar (registrarForMock (testClass , mockConfigurator ));
392
420
}
393
-
394
- List <BytecodeTransformer > bytecodeTransformers = new ArrayList <>();
421
+ // Synthetic beans from callbacks
422
+ for (BeanRegistrar beanRegistrar : beanRegistrars ) {
423
+ builder .addBeanRegistrar (beanRegistrar );
424
+ }
395
425
396
426
// Process the deployment
397
427
BeanProcessor beanProcessor = builder .build ();
428
+ beanDeployment .set (beanProcessor .getBeanDeployment ());
398
429
try {
399
430
Consumer <BytecodeTransformer > bytecodeTransformerConsumer = bytecodeTransformers ::add ;
400
431
// Populate the list of qualifiers used to simulate quarkus auto injection
@@ -885,4 +916,76 @@ public void visitCode() {
885
916
};
886
917
}
887
918
}
919
+
920
+ private static class BeforeIndexContextImpl extends QuarkusComponentTestExtension .ComponentTestContextImpl
921
+ implements BeforeIndexContext {
922
+
923
+ private final Set <Class <?>> componentClasses ;
924
+ private final List <Class <?>> additionalComponentsClasses ;
925
+
926
+ BeforeIndexContextImpl (Class <?> testClass , Set <Class <?>> componentClasses ) {
927
+ super (testClass );
928
+ this .componentClasses = componentClasses ;
929
+ this .additionalComponentsClasses = new ArrayList <>();
930
+ }
931
+
932
+ @ Override
933
+ public Set <Class <?>> getComponentClasses () {
934
+ return componentClasses ;
935
+ }
936
+
937
+ @ Override
938
+ public void addComponentClass (Class <?> componentClass ) {
939
+ additionalComponentsClasses .add (componentClass );
940
+ }
941
+
942
+ }
943
+
944
+ private static class BeforeBulidContextImpl extends QuarkusComponentTestExtension .ComponentTestContextImpl
945
+ implements BeforeBuildContext {
946
+
947
+ private final IndexView immutableBeanArchiveIndex ;
948
+ private final IndexView computingBeanArchiveIndex ;
949
+ private final List <BytecodeTransformer > bytecodeTransformers ;
950
+ private final List <AnnotationTransformation > annotationTransformations ;
951
+ private final List <BeanRegistrar > beanRegistrars ;
952
+
953
+ private BeforeBulidContextImpl (Class <?> testClass , IndexView immutableBeanArchiveIndex ,
954
+ IndexView computingBeanArchiveIndex , List <BytecodeTransformer > bytecodeTransformers ,
955
+ List <AnnotationTransformation > annotationTransformations , List <BeanRegistrar > beanRegistrars ) {
956
+ super (testClass );
957
+ this .immutableBeanArchiveIndex = immutableBeanArchiveIndex ;
958
+ this .computingBeanArchiveIndex = computingBeanArchiveIndex ;
959
+ this .bytecodeTransformers = bytecodeTransformers ;
960
+ this .annotationTransformations = annotationTransformations ;
961
+ this .beanRegistrars = beanRegistrars ;
962
+ }
963
+
964
+ @ Override
965
+ public IndexView getImmutableBeanArchiveIndex () {
966
+ return immutableBeanArchiveIndex ;
967
+ }
968
+
969
+ @ Override
970
+ public IndexView getComputingBeanArchiveIndex () {
971
+ return computingBeanArchiveIndex ;
972
+ }
973
+
974
+ @ Override
975
+ public void addAnnotationTransformation (AnnotationTransformation transformation ) {
976
+ annotationTransformations .add (transformation );
977
+ }
978
+
979
+ @ Override
980
+ public void addBeanRegistrar (BeanRegistrar beanRegistrar ) {
981
+ beanRegistrars .add (beanRegistrar );
982
+ }
983
+
984
+ @ Override
985
+ public void addBytecodeTransformer (BytecodeTransformer bytecodeTransformer ) {
986
+ bytecodeTransformers .add (bytecodeTransformer );
987
+ }
988
+
989
+ }
990
+
888
991
}
0 commit comments