|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2024 the original author or authors. |
| 2 | + * Copyright 2002-2025 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
16 | 16 |
|
17 | 17 | package org.springframework.test.context.aot;
|
18 | 18 |
|
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.List; |
| 21 | +import java.util.stream.Stream; |
| 22 | + |
19 | 23 | import org.junit.jupiter.api.AfterEach;
|
20 | 24 | import org.junit.jupiter.api.BeforeEach;
|
21 | 25 | import org.junit.jupiter.api.Test;
|
22 | 26 | import org.junit.jupiter.params.ParameterizedTest;
|
23 | 27 | import org.junit.jupiter.params.provider.ValueSource;
|
24 | 28 |
|
| 29 | +import org.springframework.aot.generate.GeneratedFiles; |
| 30 | +import org.springframework.aot.generate.InMemoryGeneratedFiles; |
| 31 | +import org.springframework.context.annotation.Configuration; |
| 32 | +import org.springframework.context.support.GenericApplicationContext; |
25 | 33 | import org.springframework.core.SpringProperties;
|
| 34 | +import org.springframework.test.context.MergedContextConfiguration; |
| 35 | +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; |
26 | 36 |
|
27 | 37 | import static org.assertj.core.api.Assertions.assertThat;
|
28 | 38 | import static org.springframework.test.context.aot.TestContextAotGenerator.FAIL_ON_ERROR_PROPERTY_NAME;
|
@@ -60,9 +70,55 @@ void failOnErrorDisabledViaSpringProperty(String value) {
|
60 | 70 | assertThat(createGenerator().failOnError).isFalse();
|
61 | 71 | }
|
62 | 72 |
|
| 73 | + @Test // gh-34841 |
| 74 | + void contextIsClosedAfterAotProcessing() { |
| 75 | + DemoTestContextAotGenerator generator = createGenerator(); |
| 76 | + generator.processAheadOfTime(Stream.of(TestCase1.class, TestCase2.class)); |
| 77 | + |
| 78 | + assertThat(generator.contexts) |
| 79 | + .allSatisfy(context -> assertThat(context.isClosed()).as("context is closed").isTrue()); |
| 80 | + } |
| 81 | + |
| 82 | + |
| 83 | + private static DemoTestContextAotGenerator createGenerator() { |
| 84 | + return new DemoTestContextAotGenerator(new InMemoryGeneratedFiles()); |
| 85 | + } |
| 86 | + |
| 87 | + |
| 88 | + private static class DemoTestContextAotGenerator extends TestContextAotGenerator { |
| 89 | + |
| 90 | + List<GenericApplicationContext> contexts = new ArrayList<>(); |
| 91 | + |
| 92 | + DemoTestContextAotGenerator(GeneratedFiles generatedFiles) { |
| 93 | + super(generatedFiles); |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + GenericApplicationContext loadContextForAotProcessing( |
| 98 | + MergedContextConfiguration mergedConfig) throws TestContextAotException { |
| 99 | + |
| 100 | + GenericApplicationContext context = super.loadContextForAotProcessing(mergedConfig); |
| 101 | + this.contexts.add(context); |
| 102 | + return context; |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + @SpringJUnitConfig |
| 107 | + private static class TestCase1 { |
| 108 | + |
| 109 | + @Configuration(proxyBeanMethods = false) |
| 110 | + static class Config { |
| 111 | + // no beans |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + @SpringJUnitConfig |
| 116 | + private static class TestCase2 { |
63 | 117 |
|
64 |
| - private static TestContextAotGenerator createGenerator() { |
65 |
| - return new TestContextAotGenerator(null); |
| 118 | + @Configuration(proxyBeanMethods = false) |
| 119 | + static class Config { |
| 120 | + // no beans |
| 121 | + } |
66 | 122 | }
|
67 | 123 |
|
68 | 124 | }
|
0 commit comments