forked from quarkusio/quarkus
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request quarkusio#35768 from mkouba/qct-per-method-lifecyc…
…le-support QuarkusComponentTest: refactorings and API changes
- Loading branch information
Showing
21 changed files
with
712 additions
and
327 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
...5-component/src/main/java/io/quarkus/test/component/QuarkusComponentTestConfigSource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package io.quarkus.test.component; | ||
|
||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
import org.eclipse.microprofile.config.spi.ConfigSource; | ||
|
||
class QuarkusComponentTestConfigSource implements ConfigSource { | ||
|
||
private final Map<String, String> configProperties; | ||
private final int ordinal; | ||
|
||
QuarkusComponentTestConfigSource(Map<String, String> configProperties, int ordinal) { | ||
this.configProperties = configProperties; | ||
this.ordinal = ordinal; | ||
} | ||
|
||
@Override | ||
public Set<String> getPropertyNames() { | ||
return configProperties.keySet(); | ||
} | ||
|
||
@Override | ||
public String getValue(String propertyName) { | ||
return configProperties.get(propertyName); | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return QuarkusComponentTestExtension.class.getName(); | ||
} | ||
|
||
@Override | ||
public int getOrdinal() { | ||
return ordinal; | ||
} | ||
|
||
} |
129 changes: 129 additions & 0 deletions
129
...-component/src/main/java/io/quarkus/test/component/QuarkusComponentTestConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
package io.quarkus.test.component; | ||
|
||
import java.lang.reflect.Field; | ||
import java.lang.reflect.Method; | ||
import java.lang.reflect.Modifier; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import jakarta.enterprise.event.Event; | ||
import jakarta.enterprise.inject.Instance; | ||
import jakarta.enterprise.inject.spi.BeanContainer; | ||
import jakarta.enterprise.inject.spi.BeanManager; | ||
import jakarta.inject.Inject; | ||
import jakarta.inject.Provider; | ||
|
||
import org.jboss.logging.Logger; | ||
|
||
import io.quarkus.arc.InjectableInstance; | ||
import io.quarkus.arc.processor.AnnotationsTransformer; | ||
|
||
class QuarkusComponentTestConfiguration { | ||
|
||
static final QuarkusComponentTestConfiguration DEFAULT = new QuarkusComponentTestConfiguration(Map.of(), List.of(), | ||
List.of(), false, true, QuarkusComponentTestExtensionBuilder.DEFAULT_CONFIG_SOURCE_ORDINAL, List.of()); | ||
|
||
private static final Logger LOG = Logger.getLogger(QuarkusComponentTestConfiguration.class); | ||
|
||
final Map<String, String> configProperties; | ||
final List<Class<?>> componentClasses; | ||
final List<MockBeanConfiguratorImpl<?>> mockConfigurators; | ||
final boolean useDefaultConfigProperties; | ||
final boolean addNestedClassesAsComponents; | ||
final int configSourceOrdinal; | ||
final List<AnnotationsTransformer> annotationsTransformers; | ||
|
||
QuarkusComponentTestConfiguration(Map<String, String> configProperties, List<Class<?>> componentClasses, | ||
List<MockBeanConfiguratorImpl<?>> mockConfigurators, boolean useDefaultConfigProperties, | ||
boolean addNestedClassesAsComponents, int configSourceOrdinal, | ||
List<AnnotationsTransformer> annotationsTransformers) { | ||
this.configProperties = configProperties; | ||
this.componentClasses = componentClasses; | ||
this.mockConfigurators = mockConfigurators; | ||
this.useDefaultConfigProperties = useDefaultConfigProperties; | ||
this.addNestedClassesAsComponents = addNestedClassesAsComponents; | ||
this.configSourceOrdinal = configSourceOrdinal; | ||
this.annotationsTransformers = annotationsTransformers; | ||
} | ||
|
||
QuarkusComponentTestConfiguration update(Class<?> testClass) { | ||
Map<String, String> configProperties = new HashMap<>(this.configProperties); | ||
List<Class<?>> componentClasses = new ArrayList<>(this.componentClasses); | ||
boolean useDefaultConfigProperties = this.useDefaultConfigProperties; | ||
boolean addNestedClassesAsComponents = this.addNestedClassesAsComponents; | ||
int configSourceOrdinal = this.configSourceOrdinal; | ||
List<AnnotationsTransformer> annotationsTransformers = new ArrayList<>(this.annotationsTransformers); | ||
|
||
QuarkusComponentTest testAnnotation = testClass.getAnnotation(QuarkusComponentTest.class); | ||
if (testAnnotation != null) { | ||
Collections.addAll(componentClasses, testAnnotation.value()); | ||
useDefaultConfigProperties = testAnnotation.useDefaultConfigProperties(); | ||
addNestedClassesAsComponents = testAnnotation.addNestedClassesAsComponents(); | ||
configSourceOrdinal = testAnnotation.configSourceOrdinal(); | ||
Class<? extends AnnotationsTransformer>[] transformers = testAnnotation.annotationsTransformers(); | ||
if (transformers.length > 0) { | ||
for (Class<? extends AnnotationsTransformer> transformerClass : transformers) { | ||
try { | ||
annotationsTransformers.add(transformerClass.getDeclaredConstructor().newInstance()); | ||
} catch (Exception e) { | ||
LOG.errorf("Unable to instantiate %s", transformerClass); | ||
} | ||
} | ||
} | ||
} | ||
// All fields annotated with @Inject represent component classes | ||
Class<?> current = testClass; | ||
while (current != null) { | ||
for (Field field : current.getDeclaredFields()) { | ||
if (field.isAnnotationPresent(Inject.class) && !resolvesToBuiltinBean(field.getType())) { | ||
componentClasses.add(field.getType()); | ||
} | ||
} | ||
current = current.getSuperclass(); | ||
} | ||
// All static nested classes declared on the test class are components | ||
if (addNestedClassesAsComponents) { | ||
for (Class<?> declaredClass : testClass.getDeclaredClasses()) { | ||
if (Modifier.isStatic(declaredClass.getModifiers())) { | ||
componentClasses.add(declaredClass); | ||
} | ||
} | ||
} | ||
|
||
List<TestConfigProperty> testConfigProperties = new ArrayList<>(); | ||
Collections.addAll(testConfigProperties, testClass.getAnnotationsByType(TestConfigProperty.class)); | ||
for (TestConfigProperty testConfigProperty : testConfigProperties) { | ||
configProperties.put(testConfigProperty.key(), testConfigProperty.value()); | ||
} | ||
|
||
return new QuarkusComponentTestConfiguration(Map.copyOf(configProperties), List.copyOf(componentClasses), | ||
this.mockConfigurators, | ||
useDefaultConfigProperties, addNestedClassesAsComponents, configSourceOrdinal, | ||
List.copyOf(annotationsTransformers)); | ||
} | ||
|
||
QuarkusComponentTestConfiguration update(Method testMethod) { | ||
Map<String, String> configProperties = new HashMap<>(this.configProperties); | ||
List<TestConfigProperty> testConfigProperties = new ArrayList<>(); | ||
Collections.addAll(testConfigProperties, testMethod.getAnnotationsByType(TestConfigProperty.class)); | ||
for (TestConfigProperty testConfigProperty : testConfigProperties) { | ||
configProperties.put(testConfigProperty.key(), testConfigProperty.value()); | ||
} | ||
return new QuarkusComponentTestConfiguration(configProperties, componentClasses, | ||
mockConfigurators, useDefaultConfigProperties, addNestedClassesAsComponents, configSourceOrdinal, | ||
annotationsTransformers); | ||
} | ||
|
||
private static boolean resolvesToBuiltinBean(Class<?> rawType) { | ||
return Provider.class.equals(rawType) | ||
|| Instance.class.equals(rawType) | ||
|| InjectableInstance.class.equals(rawType) | ||
|| Event.class.equals(rawType) | ||
|| BeanContainer.class.equals(rawType) | ||
|| BeanManager.class.equals(rawType); | ||
} | ||
|
||
} |
Oops, something went wrong.