Search in sources :

Example 6 with SimpleConfig

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

the class JavaConfigPlaceholderAutoUpdateTest 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 someJsonProperty = "[{\"a\":\"astring\", \"b\":10},{\"a\":\"astring2\", \"b\":20}]";
    String someNewJsonProperty = "[{\"a\":\"newString\", \"b\":20},{\"a\":\"astring2\", \"b\":20}]";
    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));
    properties.setProperty("jsonProperty", someJsonProperty);
    SimpleConfig config = prepareConfig(ConfigConsts.NAMESPACE_APPLICATION, properties);
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig9.class);
    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());
    assertEquals("astring", bean.getJsonBeanList().get(0).getA());
    assertEquals(10, bean.getJsonBeanList().get(0).getB());
    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));
    newProperties.setProperty("jsonProperty", someNewJsonProperty);
    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());
    assertEquals("newString", bean.getJsonBeanList().get(0).getA());
    assertEquals(20, bean.getJsonBeanList().get(0).getB());
}
Also used : SimpleConfig(com.ctrip.framework.apollo.internals.SimpleConfig) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) Properties(java.util.Properties) Date(java.util.Date) SimpleDateFormat(java.text.SimpleDateFormat) Test(org.junit.Test)

Example 7 with SimpleConfig

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

the class JavaConfigPlaceholderAutoUpdateTest method testAutoUpdateWithNestedProperty.

@Test
public void testAutoUpdateWithNestedProperty() throws Exception {
    String someKeyValue = "someKeyValue";
    String anotherKeyValue = "anotherKeyValue";
    String newKeyValue = "newKeyValue";
    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, newKeyValue, ANOTHER_KEY_PROPERTY, anotherKeyValue, String.format("%s.%s", newKeyValue, anotherKeyValue), 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 8 with SimpleConfig

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

the class JavaConfigPlaceholderAutoUpdateTest method testAutoUpdateJsonValueWithInvalidValue.

@Test
public void testAutoUpdateJsonValueWithInvalidValue() throws Exception {
    String someValidValue = "{\"a\":\"someString\", \"b\":10}";
    String someInvalidValue = "someInvalidValue";
    Properties properties = assembleProperties("jsonProperty", someValidValue);
    SimpleConfig config = prepareConfig(ConfigConsts.NAMESPACE_APPLICATION, properties);
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig10.class);
    TestApolloJsonValue bean = context.getBean(TestApolloJsonValue.class);
    JsonBean jsonBean = bean.getJsonBean();
    assertEquals("someString", jsonBean.getA());
    assertEquals(10, jsonBean.getB());
    Properties newProperties = assembleProperties("jsonProperty", someInvalidValue);
    config.onRepositoryChange(ConfigConsts.NAMESPACE_APPLICATION, newProperties);
    TimeUnit.MILLISECONDS.sleep(300);
    // should not change anything
    assertTrue(jsonBean == bean.getJsonBean());
}
Also used : SimpleConfig(com.ctrip.framework.apollo.internals.SimpleConfig) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) JsonBean(com.ctrip.framework.apollo.spring.JavaConfigPlaceholderTest.JsonBean) Properties(java.util.Properties) Test(org.junit.Test)

Example 9 with SimpleConfig

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

the class JavaConfigPlaceholderAutoUpdateTest method testAutoUpdateWithInvalidSetter.

@Test
public void testAutoUpdateWithInvalidSetter() 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(AppConfig5.class);
    TestJavaConfigBean4 bean = context.getBean(TestJavaConfigBean4.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 10 with SimpleConfig

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

the class JavaConfigPlaceholderAutoUpdateTest 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);
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig4.class);
    TestJavaConfigBean3 bean = context.getBean(TestJavaConfigBean3.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)

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