forked from spring-projects/spring-framework
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow default CacheAwareContextLoaderDelegate to be configured via Sp…
…ringProperties Closes spring-projectsgh-27540
- Loading branch information
Showing
2 changed files
with
106 additions
and
2 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
95 changes: 95 additions & 0 deletions
95
.../org/springframework/test/context/support/CustomCacheAwareContextLoaderDelegateTests.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,95 @@ | ||
/* | ||
* Copyright 2002-2021 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.test.context.support; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.platform.testkit.engine.EngineTestKit; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.ApplicationContext; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.context.support.GenericApplicationContext; | ||
import org.springframework.core.SpringProperties; | ||
import org.springframework.test.context.CacheAwareContextLoaderDelegate; | ||
import org.springframework.test.context.MergedContextConfiguration; | ||
import org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate; | ||
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectClass; | ||
|
||
/** | ||
* Integration tests for configuring a custom {@link CacheAwareContextLoaderDelegate} | ||
* via {@link SpringProperties}. | ||
* | ||
* @author sbrannen | ||
* @since 5.3.11 | ||
*/ | ||
class CustomCacheAwareContextLoaderDelegateTests { | ||
|
||
@Test | ||
void customCacheAwareContextLoaderDelegateConfiguredViaSpringProperties() { | ||
String key = "spring.test.context.default.CacheAwareContextLoaderDelegate"; | ||
|
||
try { | ||
SpringProperties.setProperty(key, AotCacheAwareContextLoaderDelegate.class.getName()); | ||
|
||
EngineTestKit.engine("junit-jupiter")// | ||
.selectors(selectClass(TestCase.class))// | ||
.execute()// | ||
.testEvents()// | ||
.assertStatistics(stats -> stats.started(1).succeeded(1).failed(0)); | ||
} | ||
finally { | ||
SpringProperties.setProperty(key, null); | ||
} | ||
} | ||
|
||
|
||
@SpringJUnitConfig | ||
static class TestCase { | ||
|
||
@Test | ||
void test(@Autowired String foo) { | ||
// foo will be "bar" unless the AotCacheAwareContextLoaderDelegate is registered. | ||
assertThat(foo).isEqualTo("AOT"); | ||
} | ||
|
||
|
||
@Configuration | ||
static class Config { | ||
|
||
@Bean | ||
String foo() { | ||
return "bar"; | ||
} | ||
} | ||
} | ||
|
||
static class AotCacheAwareContextLoaderDelegate extends DefaultCacheAwareContextLoaderDelegate { | ||
|
||
@Override | ||
protected ApplicationContext loadContextInternal(MergedContextConfiguration mergedContextConfiguration) { | ||
GenericApplicationContext applicationContext = new GenericApplicationContext(); | ||
applicationContext.registerBean("foo", String.class, () -> "AOT"); | ||
applicationContext.refresh(); | ||
return applicationContext; | ||
} | ||
} | ||
|
||
} |