Search in sources :

Example 6 with Config

use of com.ctrip.framework.apollo.Config in project apollo by ctripcorp.

the class PureApolloConfigTest method testPureApolloConfigWithEnvironmentVariables.

@Test
public void testPureApolloConfigWithEnvironmentVariables() throws Exception {
    SystemLambda.withEnvironmentVariable("SPRING_PROFILES_ACTIVE", "test-env").execute(() -> {
        ApolloMockInjectorCustomizer.register(ConfigFactory.class, PureApolloConfigFactory::new);
        ConfigFactory configFactory = ApolloInjector.getInstance(ConfigFactory.class);
        Config config = configFactory.create("application");
        Assert.assertNull(config.getProperty("SPRING_PROFILES_ACTIVE", null));
    });
}
Also used : Config(com.ctrip.framework.apollo.Config) PureApolloConfigFactory(com.ctrip.framework.apollo.config.data.internals.PureApolloConfigFactory) PureApolloConfigFactory(com.ctrip.framework.apollo.config.data.internals.PureApolloConfigFactory) DefaultConfigFactory(com.ctrip.framework.apollo.spi.DefaultConfigFactory) ConfigFactory(com.ctrip.framework.apollo.spi.ConfigFactory) Test(org.junit.Test)

Example 7 with Config

use of com.ctrip.framework.apollo.Config in project apollo by ctripcorp.

the class PropertySourcesProcessor method initializePropertySources.

private void initializePropertySources() {
    if (environment.getPropertySources().contains(PropertySourcesConstants.APOLLO_PROPERTY_SOURCE_NAME)) {
        // already initialized
        return;
    }
    CompositePropertySource composite;
    if (configUtil.isPropertyNamesCacheEnabled()) {
        composite = new CachedCompositePropertySource(PropertySourcesConstants.APOLLO_PROPERTY_SOURCE_NAME);
    } else {
        composite = new CompositePropertySource(PropertySourcesConstants.APOLLO_PROPERTY_SOURCE_NAME);
    }
    // sort by order asc
    ImmutableSortedSet<Integer> orders = ImmutableSortedSet.copyOf(NAMESPACE_NAMES.keySet());
    Iterator<Integer> iterator = orders.iterator();
    while (iterator.hasNext()) {
        int order = iterator.next();
        for (String namespace : NAMESPACE_NAMES.get(order)) {
            Config config = ConfigService.getConfig(namespace);
            composite.addPropertySource(configPropertySourceFactory.getConfigPropertySource(namespace, config));
        }
    }
    // clean up
    NAMESPACE_NAMES.clear();
    // add after the bootstrap property source or to the first
    if (environment.getPropertySources().contains(PropertySourcesConstants.APOLLO_BOOTSTRAP_PROPERTY_SOURCE_NAME)) {
        // ensure ApolloBootstrapPropertySources is still the first
        ensureBootstrapPropertyPrecedence(environment);
        environment.getPropertySources().addAfter(PropertySourcesConstants.APOLLO_BOOTSTRAP_PROPERTY_SOURCE_NAME, composite);
    } else {
        environment.getPropertySources().addFirst(composite);
    }
}
Also used : CompositePropertySource(org.springframework.core.env.CompositePropertySource) Config(com.ctrip.framework.apollo.Config)

Example 8 with Config

use of com.ctrip.framework.apollo.Config in project apollo by ctripcorp.

the class DefaultConfigManagerTest method testGetConfig.

@Test
public void testGetConfig() throws Exception {
    String someNamespace = "someName";
    String anotherNamespace = "anotherName";
    String someKey = "someKey";
    Config config = defaultConfigManager.getConfig(someNamespace);
    Config anotherConfig = defaultConfigManager.getConfig(anotherNamespace);
    assertEquals(someNamespace + ":" + someKey, config.getProperty(someKey, null));
    assertEquals(anotherNamespace + ":" + someKey, anotherConfig.getProperty(someKey, null));
}
Also used : Config(com.ctrip.framework.apollo.Config) Test(org.junit.Test)

Example 9 with Config

use of com.ctrip.framework.apollo.Config in project apollo by ctripcorp.

the class ApolloMockServerApiTest method testUpdateProperties.

@Test
public void testUpdateProperties() throws Exception {
    String someNewValue = "someNewValue";
    Config otherConfig = ConfigService.getConfig(anotherNamespace);
    final SettableFuture<ConfigChangeEvent> future = SettableFuture.create();
    otherConfig.addChangeListener(new ConfigChangeListener() {

        @Override
        public void onChange(ConfigChangeEvent changeEvent) {
            future.set(changeEvent);
        }
    });
    assertEquals("otherValue1", otherConfig.getProperty("key1", null));
    assertEquals("otherValue2", otherConfig.getProperty("key2", null));
    embeddedApollo.addOrModifyProperty(anotherNamespace, "key1", someNewValue);
    ConfigChangeEvent changeEvent = future.get(5, TimeUnit.SECONDS);
    assertEquals(someNewValue, otherConfig.getProperty("key1", null));
    assertEquals("otherValue2", otherConfig.getProperty("key2", null));
    assertTrue(changeEvent.isChanged("key1"));
}
Also used : ConfigChangeListener(com.ctrip.framework.apollo.ConfigChangeListener) Config(com.ctrip.framework.apollo.Config) ConfigChangeEvent(com.ctrip.framework.apollo.model.ConfigChangeEvent) Test(org.junit.Test)

Example 10 with Config

use of com.ctrip.framework.apollo.Config in project apollo by ctripcorp.

the class ApolloMockServerApiTest method testDeleteSamePropertyTwice.

@Test
public void testDeleteSamePropertyTwice() throws Exception {
    Config otherConfig = ConfigService.getConfig(anotherNamespace);
    final Semaphore changes = new Semaphore(0);
    otherConfig.addChangeListener(new ConfigChangeListener() {

        @Override
        public void onChange(ConfigChangeEvent changeEvent) {
            changes.release();
        }
    });
    assertEquals("otherValue6", otherConfig.getProperty("key6", null));
    embeddedApollo.deleteProperty(anotherNamespace, "key6");
    embeddedApollo.deleteProperty(anotherNamespace, "key6");
    assertTrue(changes.tryAcquire(5, TimeUnit.SECONDS));
    assertNull(otherConfig.getProperty("key6", null));
    assertEquals(0, changes.availablePermits());
}
Also used : ConfigChangeListener(com.ctrip.framework.apollo.ConfigChangeListener) Config(com.ctrip.framework.apollo.Config) ConfigChangeEvent(com.ctrip.framework.apollo.model.ConfigChangeEvent) Semaphore(java.util.concurrent.Semaphore) Test(org.junit.Test)

Aggregations

Config (com.ctrip.framework.apollo.Config)94 Test (org.junit.Test)79 EnableApolloConfig (com.ctrip.framework.apollo.spring.annotation.EnableApolloConfig)39 Matchers.anyString (org.mockito.Matchers.anyString)31 ApolloConfig (com.ctrip.framework.apollo.spring.annotation.ApolloConfig)26 ConfigChangeListener (com.ctrip.framework.apollo.ConfigChangeListener)22 SimpleConfig (com.ctrip.framework.apollo.internals.SimpleConfig)20 ConfigChangeEvent (com.ctrip.framework.apollo.model.ConfigChangeEvent)16 BaseIntegrationTest (com.ctrip.framework.apollo.BaseIntegrationTest)13 ApolloConfig (com.ctrip.framework.apollo.core.dto.ApolloConfig)13 ContextHandler (org.eclipse.jetty.server.handler.ContextHandler)13 ApolloConfigChangeListener (com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener)10 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)10 Properties (java.util.Properties)6 ConfigFactory (com.ctrip.framework.apollo.spi.ConfigFactory)5 PureApolloConfigFactory (com.ctrip.framework.apollo.config.data.internals.PureApolloConfigFactory)4 DefaultConfigFactory (com.ctrip.framework.apollo.spi.DefaultConfigFactory)4 OrderedProperties (com.ctrip.framework.apollo.util.OrderedProperties)4 Set (java.util.Set)4 ApolloConfigNotification (com.ctrip.framework.apollo.core.dto.ApolloConfigNotification)3