Search in sources :

Example 6 with ConfigSourceType

use of com.ctrip.framework.apollo.enums.ConfigSourceType in project apollo by ctripcorp.

the class SimpleConfigTest method testOnRepositoryChange.

@Test
public void testOnRepositoryChange() throws Exception {
    Properties someProperties = new Properties();
    String someKey = "someKey";
    String someValue = "someValue";
    String anotherKey = "anotherKey";
    String anotherValue = "anotherValue";
    someProperties.putAll(ImmutableMap.of(someKey, someValue, anotherKey, anotherValue));
    Properties anotherProperties = new Properties();
    String newKey = "newKey";
    String newValue = "newValue";
    String someValueNew = "someValueNew";
    anotherProperties.putAll(ImmutableMap.of(someKey, someValueNew, newKey, newValue));
    someSourceType = ConfigSourceType.LOCAL;
    when(configRepository.getConfig()).thenReturn(someProperties);
    when(configRepository.getSourceType()).thenReturn(someSourceType);
    final SettableFuture<ConfigChangeEvent> configChangeFuture = SettableFuture.create();
    ConfigChangeListener someListener = new ConfigChangeListener() {

        @Override
        public void onChange(ConfigChangeEvent changeEvent) {
            configChangeFuture.set(changeEvent);
        }
    };
    SimpleConfig config = new SimpleConfig(someNamespace, configRepository);
    assertEquals(someSourceType, config.getSourceType());
    config.addChangeListener(someListener);
    ConfigSourceType anotherSourceType = ConfigSourceType.REMOTE;
    when(configRepository.getSourceType()).thenReturn(anotherSourceType);
    config.onRepositoryChange(someNamespace, anotherProperties);
    ConfigChangeEvent changeEvent = configChangeFuture.get(500, TimeUnit.MILLISECONDS);
    assertEquals(someNamespace, changeEvent.getNamespace());
    assertEquals(3, changeEvent.changedKeys().size());
    ConfigChange someKeyChange = changeEvent.getChange(someKey);
    assertEquals(someValue, someKeyChange.getOldValue());
    assertEquals(someValueNew, someKeyChange.getNewValue());
    assertEquals(PropertyChangeType.MODIFIED, someKeyChange.getChangeType());
    ConfigChange anotherKeyChange = changeEvent.getChange(anotherKey);
    assertEquals(anotherValue, anotherKeyChange.getOldValue());
    assertEquals(null, anotherKeyChange.getNewValue());
    assertEquals(PropertyChangeType.DELETED, anotherKeyChange.getChangeType());
    ConfigChange newKeyChange = changeEvent.getChange(newKey);
    assertEquals(null, newKeyChange.getOldValue());
    assertEquals(newValue, newKeyChange.getNewValue());
    assertEquals(PropertyChangeType.ADDED, newKeyChange.getChangeType());
    assertEquals(anotherSourceType, config.getSourceType());
}
Also used : ConfigChange(com.ctrip.framework.apollo.model.ConfigChange) ConfigSourceType(com.ctrip.framework.apollo.enums.ConfigSourceType) ConfigChangeListener(com.ctrip.framework.apollo.ConfigChangeListener) ConfigChangeEvent(com.ctrip.framework.apollo.model.ConfigChangeEvent) Properties(java.util.Properties) Test(org.junit.Test)

Example 7 with ConfigSourceType

use of com.ctrip.framework.apollo.enums.ConfigSourceType in project apollo by ctripcorp.

the class YamlConfigFileTest method testOnRepositoryChange.

@Test
public void testOnRepositoryChange() throws Exception {
    Properties someProperties = new Properties();
    String key = ConfigConsts.CONFIG_FILE_CONTENT_KEY;
    String someValue = "someKey: 'someValue'";
    String anotherValue = "anotherKey: 'anotherValue'";
    someProperties.setProperty(key, someValue);
    someSourceType = ConfigSourceType.LOCAL;
    Properties someYamlProperties = new Properties();
    someYamlProperties.setProperty("someKey", "someValue");
    Properties anotherYamlProperties = new Properties();
    anotherYamlProperties.setProperty("anotherKey", "anotherValue");
    when(configRepository.getConfig()).thenReturn(someProperties);
    when(configRepository.getSourceType()).thenReturn(someSourceType);
    when(yamlParser.yamlToProperties(someValue)).thenReturn(someYamlProperties);
    when(yamlParser.yamlToProperties(anotherValue)).thenReturn(anotherYamlProperties);
    YamlConfigFile configFile = new YamlConfigFile(someNamespace, configRepository);
    assertEquals(someValue, configFile.getContent());
    assertEquals(someSourceType, configFile.getSourceType());
    assertSame(someYamlProperties, configFile.asProperties());
    Properties anotherProperties = new Properties();
    anotherProperties.setProperty(key, anotherValue);
    ConfigSourceType anotherSourceType = ConfigSourceType.REMOTE;
    when(configRepository.getSourceType()).thenReturn(anotherSourceType);
    configFile.onRepositoryChange(someNamespace, anotherProperties);
    assertEquals(anotherValue, configFile.getContent());
    assertEquals(anotherSourceType, configFile.getSourceType());
    assertSame(anotherYamlProperties, configFile.asProperties());
}
Also used : ConfigSourceType(com.ctrip.framework.apollo.enums.ConfigSourceType) Properties(java.util.Properties) OrderedProperties(com.ctrip.framework.apollo.util.OrderedProperties) Test(org.junit.Test)

Example 8 with ConfigSourceType

use of com.ctrip.framework.apollo.enums.ConfigSourceType in project apollo by ctripcorp.

the class DefaultConfig method onRepositoryChange.

@Override
public synchronized void onRepositoryChange(String namespace, Properties newProperties) {
    if (newProperties.equals(m_configProperties.get())) {
        return;
    }
    ConfigSourceType sourceType = m_configRepository.getSourceType();
    Properties newConfigProperties = propertiesFactory.getPropertiesInstance();
    newConfigProperties.putAll(newProperties);
    Map<String, ConfigChange> actualChanges = updateAndCalcConfigChanges(newConfigProperties, sourceType);
    // check double checked result
    if (actualChanges.isEmpty()) {
        return;
    }
    this.fireConfigChange(m_namespace, actualChanges);
    Tracer.logEvent("Apollo.Client.ConfigChanges", m_namespace);
}
Also used : ConfigChange(com.ctrip.framework.apollo.model.ConfigChange) ConfigSourceType(com.ctrip.framework.apollo.enums.ConfigSourceType) Properties(java.util.Properties)

Aggregations

ConfigSourceType (com.ctrip.framework.apollo.enums.ConfigSourceType)8 Properties (java.util.Properties)7 Test (org.junit.Test)7 ConfigChange (com.ctrip.framework.apollo.model.ConfigChange)3 OrderedProperties (com.ctrip.framework.apollo.util.OrderedProperties)3 ConfigChangeListener (com.ctrip.framework.apollo.ConfigChangeListener)2 ConfigChangeEvent (com.ctrip.framework.apollo.model.ConfigChangeEvent)2 File (java.io.File)1