From 5682d60719cf3afded7c83f0cccd538e1079095d Mon Sep 17 00:00:00 2001 From: Jason Song Date: Sat, 17 Oct 2026 09:54:21 +0800 Subject: [PATCH] test(config-data): isolate environment and cache state --- .../data/importer/PureApolloConfigTest.java | 20 +++++++++++++++++-- .../ConfigDataIntegrationTest.java | 19 +++++++++++++++--- 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/apollo-client-config-data/src/test/java/com/ctrip/framework/apollo/config/data/importer/PureApolloConfigTest.java b/apollo-client-config-data/src/test/java/com/ctrip/framework/apollo/config/data/importer/PureApolloConfigTest.java index 794c606a..721b2e19 100644 --- a/apollo-client-config-data/src/test/java/com/ctrip/framework/apollo/config/data/importer/PureApolloConfigTest.java +++ b/apollo-client-config-data/src/test/java/com/ctrip/framework/apollo/config/data/importer/PureApolloConfigTest.java @@ -22,6 +22,7 @@ import com.ctrip.framework.apollo.config.data.internals.PureApolloConfigFactory; import com.ctrip.framework.apollo.spi.ConfigFactory; import com.ctrip.framework.apollo.spi.DefaultConfigFactory; +import com.ctrip.framework.foundation.Foundation; import com.github.stefanbirkner.systemlambda.SystemLambda; import org.junit.After; import org.junit.Assert; @@ -33,16 +34,31 @@ */ public class PureApolloConfigTest { + private String originalEnv; + private String originalProfiles; + @Before public void before() { + originalEnv = System.getProperty("env"); + originalProfiles = System.getProperty("spring.profiles.active"); System.setProperty("env", "local"); + Foundation.server().initialize(); } @After public void after() { - System.clearProperty("spring.profiles.active"); - System.clearProperty("env"); ApolloMockInjectorCustomizer.clear(); + restoreOrClear("spring.profiles.active", originalProfiles); + restoreOrClear("env", originalEnv); + Foundation.server().initialize(); + } + + private static void restoreOrClear(String key, String value) { + if (value == null) { + System.clearProperty(key); + } else { + System.setProperty(key, value); + } } @Test diff --git a/apollo-client-config-data/src/test/java/com/ctrip/framework/apollo/config/data/integration/ConfigDataIntegrationTest.java b/apollo-client-config-data/src/test/java/com/ctrip/framework/apollo/config/data/integration/ConfigDataIntegrationTest.java index 4adaac09..5b4f3420 100644 --- a/apollo-client-config-data/src/test/java/com/ctrip/framework/apollo/config/data/integration/ConfigDataIntegrationTest.java +++ b/apollo-client-config-data/src/test/java/com/ctrip/framework/apollo/config/data/integration/ConfigDataIntegrationTest.java @@ -23,6 +23,7 @@ import com.ctrip.framework.apollo.ConfigService; import com.ctrip.framework.apollo.build.ApolloInjector; +import com.ctrip.framework.apollo.core.ApolloClientSystemConsts; import com.ctrip.framework.apollo.core.ConfigConsts; import com.ctrip.framework.apollo.config.data.injector.ApolloConfigDataInjectorCustomizer; import com.ctrip.framework.apollo.config.data.injector.ApolloMockInjectorCustomizer; @@ -33,6 +34,7 @@ import com.ctrip.framework.apollo.spi.ConfigRegistry; import com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener; import com.ctrip.framework.apollo.spring.boot.ApolloApplicationContextInitializer; +import com.ctrip.framework.foundation.Foundation; import com.google.common.collect.Table; import java.lang.reflect.Field; import java.lang.reflect.Method; @@ -48,6 +50,7 @@ import org.junit.runner.RunWith; import org.junit.rules.ExternalResource; import org.junit.rules.RuleChain; +import org.junit.rules.TemporaryFolder; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; @@ -67,14 +70,17 @@ @RunWith(SpringJUnit4ClassRunner.class) @SpringBootTest(classes = ConfigDataIntegrationTest.TestConfiguration.class, webEnvironment = SpringBootTest.WebEnvironment.NONE, - properties = {"app.id=someAppId", "env=local", + properties = {"app.id=someAppId", "env=dev", "spring.config.import=apollo://application,apollo://TEST1.apollo,apollo://application.yaml", "listeners=application,TEST1.apollo,application.yaml"}) @DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD) public class ConfigDataIntegrationTest { private static final String TEST_APP_ID = "someAppId"; - private static final String TEST_ENV = "local"; + private static final String TEST_ENV = "dev"; + + private static final TemporaryFolder cacheDirectory = + TemporaryFolder.builder().assureDeletion().build(); private static final EmbeddedApollo embeddedApollo = new EmbeddedApollo(); @@ -89,6 +95,10 @@ protected void before() throws Throwable { clearApolloSystemProperties(); System.setProperty("app.id", TEST_APP_ID); System.setProperty("env", TEST_ENV); + System.setProperty(ApolloClientSystemConsts.APOLLO_CACHE_DIR, + cacheDirectory.getRoot().getAbsolutePath()); + // Foundation caches env separately from system properties and other Apollo singletons. + Foundation.server().initialize(); resetApolloStaticState(); } @@ -101,13 +111,14 @@ protected void after() { } finally { restoreApolloSystemProperties(originalApolloSystemProperties); restoreOrClear("env", originalEnv); + Foundation.server().initialize(); } } }; @ClassRule public static final RuleChain apolloRuleChain = - RuleChain.outerRule(apolloStateResource).around(embeddedApollo); + RuleChain.outerRule(cacheDirectory).around(apolloStateResource).around(embeddedApollo); @Before public void beforeEach() { @@ -120,6 +131,8 @@ public void afterEach() throws Exception { clearApolloSystemProperties(); System.setProperty("app.id", TEST_APP_ID); System.setProperty("env", TEST_ENV); + System.setProperty(ApolloClientSystemConsts.APOLLO_CACHE_DIR, + cacheDirectory.getRoot().getAbsolutePath()); } @Autowired