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.
QuarkusComponentTest: refactorings and API changes
- determine the test phase when the container is started from the TestInstance#value() - introduce the QuarkusComponentTestExtensionBuilder to create immutable extension instance when programmatic API is used - TestConfigProperty can be declared on test methods
- Loading branch information
Showing
19 changed files
with
635 additions
and
314 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
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; | ||
} | ||
|
||
} |
115 changes: 115 additions & 0 deletions
115
...-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,115 @@ | ||
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.BeanManager; | ||
import jakarta.inject.Inject; | ||
|
||
import org.jboss.logging.Logger; | ||
|
||
import io.quarkus.arc.processor.AnnotationsTransformer; | ||
|
||
class QuarkusComponentTestConfiguration { | ||
|
||
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; | ||
} | ||
|
||
/** | ||
* | ||
* @param testClass | ||
* @param testMethod | ||
* @return a new configuration | ||
*/ | ||
QuarkusComponentTestConfiguration update(Class<?> testClass, Method testMethod) { | ||
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)); | ||
if (testMethod != null) { | ||
Collections.addAll(testConfigProperties, testMethod.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)); | ||
} | ||
|
||
private boolean resolvesToBuiltinBean(Class<?> rawType) { | ||
return Instance.class.isAssignableFrom(rawType) || Event.class.equals(rawType) || BeanManager.class.equals(rawType); | ||
} | ||
|
||
} |
Oops, something went wrong.