Search in sources :

Example 16 with SimpleConfig

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

the class XmlConfigPlaceholderAutoUpdateTest method testAutoUpdateWithValueInjectedAsConstructorArgs.

@Test
public void testAutoUpdateWithValueInjectedAsConstructorArgs() 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);
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/XmlConfigPlaceholderTest8.xml");
    TestXmlBeanWithConstructorArgs bean = context.getBean(TestXmlBeanWithConstructorArgs.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) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) Properties(java.util.Properties) Test(org.junit.Test)

Example 17 with SimpleConfig

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

the class XmlConfigPlaceholderAutoUpdateTest method testAutoUpdateWithAllKindsOfDataTypes.

@Test
public void testAutoUpdateWithAllKindsOfDataTypes() throws Exception {
    int someInt = 1000;
    int someNewInt = 1001;
    int[] someIntArray = { 1, 2, 3, 4 };
    int[] someNewIntArray = { 5, 6, 7, 8 };
    long someLong = 2000L;
    long someNewLong = 2001L;
    short someShort = 3000;
    short someNewShort = 3001;
    float someFloat = 1.2F;
    float someNewFloat = 2.2F;
    double someDouble = 3.10D;
    double someNewDouble = 4.10D;
    byte someByte = 123;
    byte someNewByte = 124;
    boolean someBoolean = true;
    boolean someNewBoolean = !someBoolean;
    String someString = "someString";
    String someNewString = "someNewString";
    String someDateFormat = "yyyy-MM-dd HH:mm:ss.SSS";
    Date someDate = assembleDate(2018, 2, 23, 20, 1, 2, 123);
    Date someNewDate = assembleDate(2018, 2, 23, 21, 2, 3, 345);
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat(someDateFormat, Locale.US);
    Properties properties = new Properties();
    properties.setProperty("intProperty", String.valueOf(someInt));
    properties.setProperty("intArrayProperty", Ints.join(", ", someIntArray));
    properties.setProperty("longProperty", String.valueOf(someLong));
    properties.setProperty("shortProperty", String.valueOf(someShort));
    properties.setProperty("floatProperty", String.valueOf(someFloat));
    properties.setProperty("doubleProperty", String.valueOf(someDouble));
    properties.setProperty("byteProperty", String.valueOf(someByte));
    properties.setProperty("booleanProperty", String.valueOf(someBoolean));
    properties.setProperty("stringProperty", someString);
    properties.setProperty("dateFormat", someDateFormat);
    properties.setProperty("dateProperty", simpleDateFormat.format(someDate));
    SimpleConfig config = prepareConfig(ConfigConsts.NAMESPACE_APPLICATION, properties);
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/XmlConfigPlaceholderTest10.xml");
    TestAllKindsOfDataTypesBean bean = context.getBean(TestAllKindsOfDataTypesBean.class);
    assertEquals(someInt, bean.getIntProperty());
    assertArrayEquals(someIntArray, bean.getIntArrayProperty());
    assertEquals(someLong, bean.getLongProperty());
    assertEquals(someShort, bean.getShortProperty());
    assertEquals(someFloat, bean.getFloatProperty(), 0.001F);
    assertEquals(someDouble, bean.getDoubleProperty(), 0.001D);
    assertEquals(someByte, bean.getByteProperty());
    assertEquals(someBoolean, bean.getBooleanProperty());
    assertEquals(someString, bean.getStringProperty());
    assertEquals(someDate, bean.getDateProperty());
    Properties newProperties = new Properties();
    newProperties.setProperty("intProperty", String.valueOf(someNewInt));
    newProperties.setProperty("intArrayProperty", Ints.join(", ", someNewIntArray));
    newProperties.setProperty("longProperty", String.valueOf(someNewLong));
    newProperties.setProperty("shortProperty", String.valueOf(someNewShort));
    newProperties.setProperty("floatProperty", String.valueOf(someNewFloat));
    newProperties.setProperty("doubleProperty", String.valueOf(someNewDouble));
    newProperties.setProperty("byteProperty", String.valueOf(someNewByte));
    newProperties.setProperty("booleanProperty", String.valueOf(someNewBoolean));
    newProperties.setProperty("stringProperty", someNewString);
    newProperties.setProperty("dateFormat", someDateFormat);
    newProperties.setProperty("dateProperty", simpleDateFormat.format(someNewDate));
    config.onRepositoryChange(ConfigConsts.NAMESPACE_APPLICATION, newProperties);
    TimeUnit.MILLISECONDS.sleep(100);
    assertEquals(someNewInt, bean.getIntProperty());
    assertArrayEquals(someNewIntArray, bean.getIntArrayProperty());
    assertEquals(someNewLong, bean.getLongProperty());
    assertEquals(someNewShort, bean.getShortProperty());
    assertEquals(someNewFloat, bean.getFloatProperty(), 0.001F);
    assertEquals(someNewDouble, bean.getDoubleProperty(), 0.001D);
    assertEquals(someNewByte, bean.getByteProperty());
    assertEquals(someNewBoolean, bean.getBooleanProperty());
    assertEquals(someNewString, bean.getStringProperty());
    assertEquals(someNewDate, bean.getDateProperty());
}
Also used : SimpleConfig(com.ctrip.framework.apollo.internals.SimpleConfig) Properties(java.util.Properties) Date(java.util.Date) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) SimpleDateFormat(java.text.SimpleDateFormat) Test(org.junit.Test)

Example 18 with SimpleConfig

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

the class XmlConfigPlaceholderAutoUpdateTest method testAutoUpdateWithNewProperties.

@Test
public void testAutoUpdateWithNewProperties() throws Exception {
    int initialTimeout = 1000;
    int newTimeout = 1001;
    int newBatch = 2001;
    Properties applicationProperties = assembleProperties(TIMEOUT_PROPERTY, String.valueOf(initialTimeout));
    SimpleConfig applicationConfig = prepareConfig(ConfigConsts.NAMESPACE_APPLICATION, applicationProperties);
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/XmlConfigPlaceholderTest1.xml");
    TestXmlBean bean = context.getBean(TestXmlBean.class);
    assertEquals(initialTimeout, bean.getTimeout());
    assertEquals(DEFAULT_BATCH, bean.getBatch());
    Properties newApplicationProperties = assembleProperties(TIMEOUT_PROPERTY, String.valueOf(newTimeout), BATCH_PROPERTY, String.valueOf(newBatch));
    applicationConfig.onRepositoryChange(ConfigConsts.NAMESPACE_APPLICATION, newApplicationProperties);
    TimeUnit.MILLISECONDS.sleep(100);
    assertEquals(newTimeout, bean.getTimeout());
    assertEquals(newBatch, bean.getBatch());
}
Also used : SimpleConfig(com.ctrip.framework.apollo.internals.SimpleConfig) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) TestXmlBean(com.ctrip.framework.apollo.spring.XmlConfigPlaceholderTest.TestXmlBean) Properties(java.util.Properties) Test(org.junit.Test)

Example 19 with SimpleConfig

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

the class XmlConfigPlaceholderAutoUpdateTest method testAutoUpdateWithIrrelevantProperties.

@Test
public void testAutoUpdateWithIrrelevantProperties() throws Exception {
    int initialTimeout = 1000;
    String someIrrelevantKey = "someIrrelevantKey";
    String someIrrelevantValue = "someIrrelevantValue";
    String anotherIrrelevantKey = "anotherIrrelevantKey";
    String anotherIrrelevantValue = "anotherIrrelevantValue";
    Properties applicationProperties = assembleProperties(TIMEOUT_PROPERTY, String.valueOf(initialTimeout), someIrrelevantKey, someIrrelevantValue);
    SimpleConfig applicationConfig = prepareConfig(ConfigConsts.NAMESPACE_APPLICATION, applicationProperties);
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/XmlConfigPlaceholderTest1.xml");
    TestXmlBean bean = context.getBean(TestXmlBean.class);
    assertEquals(initialTimeout, bean.getTimeout());
    assertEquals(DEFAULT_BATCH, bean.getBatch());
    Properties newApplicationProperties = assembleProperties(TIMEOUT_PROPERTY, String.valueOf(initialTimeout), anotherIrrelevantKey, anotherIrrelevantValue);
    applicationConfig.onRepositoryChange(ConfigConsts.NAMESPACE_APPLICATION, newApplicationProperties);
    TimeUnit.MILLISECONDS.sleep(100);
    assertEquals(initialTimeout, bean.getTimeout());
    assertEquals(DEFAULT_BATCH, bean.getBatch());
}
Also used : SimpleConfig(com.ctrip.framework.apollo.internals.SimpleConfig) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) TestXmlBean(com.ctrip.framework.apollo.spring.XmlConfigPlaceholderTest.TestXmlBean) Properties(java.util.Properties) Test(org.junit.Test)

Example 20 with SimpleConfig

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

the class XmlConfigPlaceholderAutoUpdateTest method testAutoUpdateWithDeletedProperties.

@Test
public void testAutoUpdateWithDeletedProperties() throws Exception {
    int initialTimeout = 1000;
    int initialBatch = 2000;
    Properties properties = assembleProperties(TIMEOUT_PROPERTY, String.valueOf(initialTimeout), BATCH_PROPERTY, String.valueOf(initialBatch));
    SimpleConfig config = prepareConfig(ConfigConsts.NAMESPACE_APPLICATION, properties);
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/XmlConfigPlaceholderTest1.xml");
    TestXmlBean bean = context.getBean(TestXmlBean.class);
    assertEquals(initialTimeout, bean.getTimeout());
    assertEquals(initialBatch, bean.getBatch());
    Properties newProperties = new Properties();
    config.onRepositoryChange(ConfigConsts.NAMESPACE_APPLICATION, newProperties);
    TimeUnit.MILLISECONDS.sleep(100);
    assertEquals(DEFAULT_TIMEOUT, bean.getTimeout());
    assertEquals(DEFAULT_BATCH, bean.getBatch());
}
Also used : SimpleConfig(com.ctrip.framework.apollo.internals.SimpleConfig) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) TestXmlBean(com.ctrip.framework.apollo.spring.XmlConfigPlaceholderTest.TestXmlBean) 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