Search in sources :

Example 1 with SimpleConfig

use of com.ctrip.framework.apollo.internals.SimpleConfig in project apollo by ctripcorp.

the class JavaConfigAnnotationTest method testApolloConfigChangeListenerWithInterestedKeyPrefixes_fire.

@Test
public void testApolloConfigChangeListenerWithInterestedKeyPrefixes_fire() throws InterruptedException {
    // default mock, useless here
    // just for speed up test without waiting
    mockConfig(ConfigConsts.NAMESPACE_APPLICATION, mock(Config.class));
    SimpleConfig simpleConfig = spy(this.prepareConfig(TestApolloConfigChangeListenerWithInterestedKeyPrefixesBean1.SPECIAL_NAMESPACE, new Properties()));
    mockConfig(TestApolloConfigChangeListenerWithInterestedKeyPrefixesBean1.SPECIAL_NAMESPACE, simpleConfig);
    TestApolloConfigChangeListenerWithInterestedKeyPrefixesBean1 bean = getBean(TestApolloConfigChangeListenerWithInterestedKeyPrefixesBean1.class, AppConfig11.class);
    verify(simpleConfig, atLeastOnce()).addChangeListener(any(ConfigChangeListener.class), Mockito.nullable(Set.class), anySetOf(String.class));
    Properties properties = new Properties();
    properties.put("logging.level.com", "debug");
    properties.put("logging.level.root", "warn");
    properties.put("number.value", "333");
    // publish config change
    simpleConfig.onRepositoryChange(TestApolloConfigChangeListenerWithInterestedKeyPrefixesBean1.SPECIAL_NAMESPACE, properties);
    // get event from bean
    ConfigChangeEvent configChangeEvent = bean.getConfigChangeEvent();
    Set<String> interestedChangedKeys = configChangeEvent.interestedChangedKeys();
    assertEquals(Sets.newHashSet("logging.level.com", "logging.level.root", "number.value"), interestedChangedKeys);
}
Also used : SimpleConfig(com.ctrip.framework.apollo.internals.SimpleConfig) Set(java.util.Set) ConfigChangeListener(com.ctrip.framework.apollo.ConfigChangeListener) ApolloConfigChangeListener(com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener) EnableApolloConfig(com.ctrip.framework.apollo.spring.annotation.EnableApolloConfig) ApolloConfig(com.ctrip.framework.apollo.spring.annotation.ApolloConfig) SimpleConfig(com.ctrip.framework.apollo.internals.SimpleConfig) Config(com.ctrip.framework.apollo.Config) ConfigChangeEvent(com.ctrip.framework.apollo.model.ConfigChangeEvent) Matchers.anyString(org.mockito.Matchers.anyString) Properties(java.util.Properties) Test(org.junit.Test)

Example 2 with SimpleConfig

use of com.ctrip.framework.apollo.internals.SimpleConfig in project apollo by ctripcorp.

the class JavaConfigPlaceholderAutoUpdateTest method testAutoUpdateWithNotSupportedNestedProperty.

@Test
public void testAutoUpdateWithNotSupportedNestedProperty() throws Exception {
    String someKeyValue = "someKeyValue";
    String anotherKeyValue = "anotherKeyValue";
    int someValue = 1234;
    int someNewValue = 2345;
    Properties properties = assembleProperties(SOME_KEY_PROPERTY, someKeyValue, ANOTHER_KEY_PROPERTY, anotherKeyValue, String.format("%s.%s", someKeyValue, anotherKeyValue), String.valueOf(someValue));
    SimpleConfig config = prepareConfig(ConfigConsts.NAMESPACE_APPLICATION, properties);
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(NestedPropertyConfig1.class);
    TestNestedPropertyBean bean = context.getBean(TestNestedPropertyBean.class);
    assertEquals(someValue, bean.getNestedProperty());
    Properties newProperties = assembleProperties(SOME_KEY_PROPERTY, someKeyValue, ANOTHER_KEY_PROPERTY, anotherKeyValue, String.format("%s.%s", someKeyValue, anotherKeyValue), String.valueOf(someNewValue));
    config.onRepositoryChange(ConfigConsts.NAMESPACE_APPLICATION, newProperties);
    TimeUnit.MILLISECONDS.sleep(100);
    // Does not support this scenario
    assertEquals(someValue, bean.getNestedProperty());
}
Also used : SimpleConfig(com.ctrip.framework.apollo.internals.SimpleConfig) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) Properties(java.util.Properties) Test(org.junit.Test)

Example 3 with SimpleConfig

use of com.ctrip.framework.apollo.internals.SimpleConfig in project apollo by ctripcorp.

the class JavaConfigPlaceholderAutoUpdateTest method testAutoUpdateWithNestedPropertyWithDefaultValue.

@Test
public void testAutoUpdateWithNestedPropertyWithDefaultValue() throws Exception {
    String someKeyValue = "someKeyValue";
    String someNewKeyValue = "someNewKeyValue";
    int someValue = 1234;
    int someNewValue = 2345;
    Properties properties = assembleProperties(SOME_KEY_PROPERTY, someKeyValue, ANOTHER_KEY_PROPERTY, String.valueOf(someValue));
    SimpleConfig config = prepareConfig(ConfigConsts.NAMESPACE_APPLICATION, properties);
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(NestedPropertyConfig2.class);
    TestNestedPropertyBeanWithDefaultValue bean = context.getBean(TestNestedPropertyBeanWithDefaultValue.class);
    assertEquals(someValue, bean.getNestedProperty());
    Properties newProperties = assembleProperties(SOME_KEY_PROPERTY, someNewKeyValue, ANOTHER_KEY_PROPERTY, String.valueOf(someValue), someNewKeyValue, String.valueOf(someNewValue));
    config.onRepositoryChange(ConfigConsts.NAMESPACE_APPLICATION, newProperties);
    TimeUnit.MILLISECONDS.sleep(100);
    assertEquals(someNewValue, bean.getNestedProperty());
}
Also used : SimpleConfig(com.ctrip.framework.apollo.internals.SimpleConfig) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) Properties(java.util.Properties) Test(org.junit.Test)

Example 4 with SimpleConfig

use of com.ctrip.framework.apollo.internals.SimpleConfig in project apollo by ctripcorp.

the class JavaConfigPlaceholderAutoUpdateTest method testApplicationPropertySourceWithValueInjectedInConfiguration.

@Test
public void testApplicationPropertySourceWithValueInjectedInConfiguration() throws Exception {
    int initialTimeout = 1000;
    int initialBatch = 2000;
    int newTimeout = 1001;
    int newBatch = 2001;
    Properties properties = assembleProperties(TIMEOUT_PROPERTY, String.valueOf(initialTimeout), BATCH_PROPERTY, String.valueOf(initialBatch));
    SimpleConfig config = prepareConfig(ConfigConsts.NAMESPACE_APPLICATION, properties);
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig7.class);
    TestJavaConfigBean2 bean = context.getBean(TestJavaConfigBean2.class);
    assertEquals(initialTimeout, bean.getTimeout());
    assertEquals(initialBatch, bean.getBatch());
    Properties newProperties = assembleProperties(TIMEOUT_PROPERTY, String.valueOf(newTimeout), BATCH_PROPERTY, String.valueOf(newBatch));
    config.onRepositoryChange(ConfigConsts.NAMESPACE_APPLICATION, newProperties);
    TimeUnit.MILLISECONDS.sleep(100);
    // Does not support this scenario
    assertEquals(initialTimeout, bean.getTimeout());
    assertEquals(initialBatch, bean.getBatch());
}
Also used : SimpleConfig(com.ctrip.framework.apollo.internals.SimpleConfig) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) Properties(java.util.Properties) Test(org.junit.Test)

Example 5 with SimpleConfig

use of com.ctrip.framework.apollo.internals.SimpleConfig in project apollo by ctripcorp.

the class JavaConfigPlaceholderAutoUpdateTest method testAutoUpdateDisabled.

@Test
public void testAutoUpdateDisabled() throws Exception {
    int initialTimeout = 1000;
    int initialBatch = 2000;
    int newTimeout = 1001;
    int newBatch = 2001;
    MockConfigUtil mockConfigUtil = new MockConfigUtil();
    mockConfigUtil.setAutoUpdateInjectedSpringProperties(false);
    MockInjector.setInstance(ConfigUtil.class, mockConfigUtil);
    Properties properties = assembleProperties(TIMEOUT_PROPERTY, String.valueOf(initialTimeout), BATCH_PROPERTY, String.valueOf(initialBatch));
    SimpleConfig config = prepareConfig(ConfigConsts.NAMESPACE_APPLICATION, properties);
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig1.class);
    TestJavaConfigBean bean = context.getBean(TestJavaConfigBean.class);
    assertEquals(initialTimeout, bean.getTimeout());
    assertEquals(initialBatch, bean.getBatch());
    Properties newProperties = assembleProperties(TIMEOUT_PROPERTY, String.valueOf(newTimeout), BATCH_PROPERTY, String.valueOf(newBatch));
    config.onRepositoryChange(ConfigConsts.NAMESPACE_APPLICATION, newProperties);
    TimeUnit.MILLISECONDS.sleep(100);
    assertEquals(initialTimeout, bean.getTimeout());
    assertEquals(initialBatch, bean.getBatch());
}
Also used : SimpleConfig(com.ctrip.framework.apollo.internals.SimpleConfig) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) Properties(java.util.Properties) Test(org.junit.Test)

Aggregations

SimpleConfig (com.ctrip.framework.apollo.internals.SimpleConfig)38 Properties (java.util.Properties)37 Test (org.junit.Test)37 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)23 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)13 TestXmlBean (com.ctrip.framework.apollo.spring.XmlConfigPlaceholderTest.TestXmlBean)11 JsonBean (com.ctrip.framework.apollo.spring.JavaConfigPlaceholderTest.JsonBean)3 SimpleDateFormat (java.text.SimpleDateFormat)2 Date (java.util.Date)2 Config (com.ctrip.framework.apollo.Config)1 ConfigChangeListener (com.ctrip.framework.apollo.ConfigChangeListener)1 ConfigRepository (com.ctrip.framework.apollo.internals.ConfigRepository)1 ConfigChangeEvent (com.ctrip.framework.apollo.model.ConfigChangeEvent)1 ApolloConfig (com.ctrip.framework.apollo.spring.annotation.ApolloConfig)1 ApolloConfigChangeListener (com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener)1 EnableApolloConfig (com.ctrip.framework.apollo.spring.annotation.EnableApolloConfig)1 Set (java.util.Set)1 Matchers.anyString (org.mockito.Matchers.anyString)1