Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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";
Comment thread
nobodyiam marked this conversation as resolved.

private static final TemporaryFolder cacheDirectory =
TemporaryFolder.builder().assureDeletion().build();

private static final EmbeddedApollo embeddedApollo = new EmbeddedApollo();

Expand All @@ -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();
}

Expand All @@ -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() {
Expand All @@ -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
Expand Down
Loading