Skip to content

Commit

Permalink
Consider resources from @testprofile when checking for restart
Browse files Browse the repository at this point in the history
Closes: #44497
  • Loading branch information
geoand committed Nov 18, 2024
1 parent e44e07e commit 7776de9
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -319,13 +319,15 @@ private Set<TestResourceClassEntry> uniqueTestResourceClassEntries(Path testClas
* Allows Quarkus to extra basic information about which test resources a test class will require
*/
public static Set<TestResourceManager.TestResourceComparisonInfo> testResourceComparisonInfo(Class<?> testClass,
Path testClassLocation) {
Path testClassLocation, List<TestResourceClassEntry> entriesFromProfile) {
Set<TestResourceClassEntry> uniqueEntries = getUniqueTestResourceClassEntries(testClass, testClassLocation, null);
if (uniqueEntries.isEmpty()) {
if (uniqueEntries.isEmpty() && entriesFromProfile.isEmpty()) {
return Collections.emptySet();
}
Set<TestResourceManager.TestResourceComparisonInfo> result = new HashSet<>(uniqueEntries.size());
for (TestResourceClassEntry entry : uniqueEntries) {
Set<TestResourceClassEntry> allEntries = new HashSet<>(uniqueEntries);
allEntries.addAll(entriesFromProfile);
Set<TestResourceManager.TestResourceComparisonInfo> result = new HashSet<>(allEntries.size());
for (TestResourceClassEntry entry : allEntries) {
Map<String, String> args = new HashMap<>(entry.args);
if (entry.configAnnotation != null) {
args.put("configAnnotation", entry.configAnnotation.annotationType().getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,8 @@ private QuarkusTestExtensionState ensureStarted(ExtensionContext extensionContex
// we reload the test resources if we changed test class and if we had or will have per-test test resources
boolean reloadTestResources = false;
if ((state == null && !failedBoot) || wrongProfile || (reloadTestResources = isNewTestClass
&& TestResourceUtil.testResourcesRequireReload(state, extensionContext.getRequiredTestClass()))) {
&& TestResourceUtil.testResourcesRequireReload(state, extensionContext.getRequiredTestClass(),
selectedProfile))) {
if (wrongProfile || reloadTestResources) {
if (state != null) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ private void ensurePrepared(ExtensionContext extensionContext, Class<? extends Q
// we reload the test resources if we changed test class and if we had or will have per-test test resources
boolean isNewTestClass = !Objects.equals(extensionContext.getRequiredTestClass(), currentJUnitTestClass);
if (wrongProfile || (isNewTestClass
&& TestResourceUtil.testResourcesRequireReload(state, extensionContext.getRequiredTestClass()))) {
&& TestResourceUtil.testResourcesRequireReload(state, extensionContext.getRequiredTestClass(),
profile))) {
if (state != null) {
try {
state.close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,8 @@ private QuarkusTestExtensionState ensureStarted(ExtensionContext extensionContex
// we reload the test resources if we changed test class and the new test class is not a nested class, and if we had or will have per-test test resources
boolean reloadTestResources = false;
if ((state == null && !failedBoot) || wrongProfile || (reloadTestResources = isNewTestClass
&& TestResourceUtil.testResourcesRequireReload(state, extensionContext.getRequiredTestClass()))) {
&& TestResourceUtil.testResourcesRequireReload(state, extensionContext.getRequiredTestClass(),
selectedProfile))) {
if (wrongProfile || reloadTestResources) {
if (state != null) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,19 @@ private TestResourceUtil() {
* This is where we decide if the test resources of the current state vs the ones required by the next test class
* to be executed require a Quarkus restart.
*/
static boolean testResourcesRequireReload(QuarkusTestExtensionState state, Class<?> nextTestClass) {
static boolean testResourcesRequireReload(QuarkusTestExtensionState state, Class<?> nextTestClass,
Class<? extends QuarkusTestProfile> nextTestClassProfile) {
QuarkusTestProfile profileInstance = null;
if (nextTestClassProfile != null) {
try {
profileInstance = nextTestClassProfile.getConstructor().newInstance();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
Set<TestResourceManager.TestResourceComparisonInfo> existingTestResources = existingTestResources(state);
Set<TestResourceManager.TestResourceComparisonInfo> nextTestResources = nextTestResources(nextTestClass);
Set<TestResourceManager.TestResourceComparisonInfo> nextTestResources = nextTestResources(nextTestClass,
profileInstance);

return TestResourceManager.testResourcesRequireReload(existingTestResources, nextTestResources);
}
Expand All @@ -54,9 +64,20 @@ static Set<TestResourceManager.TestResourceComparisonInfo> existingTestResources
return Collections.emptySet();
}

static Set<TestResourceManager.TestResourceComparisonInfo> nextTestResources(Class<?> requiredTestClass) {
static Set<TestResourceManager.TestResourceComparisonInfo> nextTestResources(Class<?> requiredTestClass,
QuarkusTestProfile profileInstance) {

List<TestResourceManager.TestResourceClassEntry> entriesFromProfile = Collections.emptyList();
if (profileInstance != null) {
entriesFromProfile = new ArrayList<>(profileInstance.testResources().size());
for (QuarkusTestProfile.TestResourceEntry entry : profileInstance.testResources()) {
entriesFromProfile.add(new TestResourceManager.TestResourceClassEntry(entry.getClazz(), entry.getArgs(), null,
entry.isParallel(), TestResourceScope.MATCHING_RESOURCES));
}
}

return TestResourceManager
.testResourceComparisonInfo(requiredTestClass, getTestClassesLocation(requiredTestClass));
.testResourceComparisonInfo(requiredTestClass, getTestClassesLocation(requiredTestClass), entriesFromProfile);
}

/**
Expand Down Expand Up @@ -92,7 +113,7 @@ static <T> List<T> copyEntriesFromProfile(
T instance = (T) testResourceClassEntryConstructor.newInstance(
Class.forName(testResource.getClazz().getName(), true, classLoader), testResource.getArgs(),
null, testResource.isParallel(),
Enum.valueOf(testResourceScopeClass, TestResourceScope.RESTRICTED_TO_CLASS.name()));
Enum.valueOf(testResourceScopeClass, TestResourceScope.MATCHING_RESOURCES.name()));
result.add(instance);
}

Expand Down

0 comments on commit 7776de9

Please sign in to comment.