Search in sources :

Example 1 with ConfigFileChangeEvent

use of com.ctrip.framework.apollo.model.ConfigFileChangeEvent in project apollo by ctripcorp.

the class PropertiesConfigFileTest method testOnRepositoryChange.

@Test
public void testOnRepositoryChange() throws Exception {
    Properties someProperties = new Properties();
    String someKey = "someKey";
    String someValue = "someValue";
    String anotherValue = "anotherValue";
    someProperties.setProperty(someKey, someValue);
    when(configRepository.getConfig()).thenReturn(someProperties);
    PropertiesConfigFile configFile = new PropertiesConfigFile(someNamespace, configRepository);
    assertTrue(configFile.getContent().contains(String.format("%s=%s", someKey, someValue)));
    Properties anotherProperties = new Properties();
    anotherProperties.setProperty(someKey, anotherValue);
    final SettableFuture<ConfigFileChangeEvent> configFileChangeFuture = SettableFuture.create();
    ConfigFileChangeListener someListener = new ConfigFileChangeListener() {

        @Override
        public void onChange(ConfigFileChangeEvent changeEvent) {
            configFileChangeFuture.set(changeEvent);
        }
    };
    configFile.addChangeListener(someListener);
    configFile.onRepositoryChange(someNamespace, anotherProperties);
    ConfigFileChangeEvent changeEvent = configFileChangeFuture.get(500, TimeUnit.MILLISECONDS);
    assertFalse(configFile.getContent().contains(String.format("%s=%s", someKey, someValue)));
    assertTrue(configFile.getContent().contains(String.format("%s=%s", someKey, anotherValue)));
    assertEquals(someNamespace, changeEvent.getNamespace());
    assertTrue(changeEvent.getOldValue().contains(String.format("%s=%s", someKey, someValue)));
    assertTrue(changeEvent.getNewValue().contains(String.format("%s=%s", someKey, anotherValue)));
    assertEquals(PropertyChangeType.MODIFIED, changeEvent.getChangeType());
}
Also used : ConfigFileChangeEvent(com.ctrip.framework.apollo.model.ConfigFileChangeEvent) ConfigFileChangeListener(com.ctrip.framework.apollo.ConfigFileChangeListener) Properties(java.util.Properties) Test(org.junit.Test)

Example 2 with ConfigFileChangeEvent

use of com.ctrip.framework.apollo.model.ConfigFileChangeEvent in project apollo by ctripcorp.

the class XmlConfigFileTest method testOnRepositoryChangeWithContentAdded.

@Test
public void testOnRepositoryChangeWithContentAdded() throws Exception {
    Properties someProperties = new Properties();
    String key = ConfigConsts.CONFIG_FILE_CONTENT_KEY;
    String someValue = "someValue";
    when(configRepository.getConfig()).thenReturn(someProperties);
    XmlConfigFile configFile = new XmlConfigFile(someNamespace, configRepository);
    assertEquals(null, configFile.getContent());
    Properties anotherProperties = new Properties();
    anotherProperties.setProperty(key, someValue);
    final SettableFuture<ConfigFileChangeEvent> configFileChangeFuture = SettableFuture.create();
    ConfigFileChangeListener someListener = new ConfigFileChangeListener() {

        @Override
        public void onChange(ConfigFileChangeEvent changeEvent) {
            configFileChangeFuture.set(changeEvent);
        }
    };
    configFile.addChangeListener(someListener);
    configFile.onRepositoryChange(someNamespace, anotherProperties);
    ConfigFileChangeEvent changeEvent = configFileChangeFuture.get(500, TimeUnit.MILLISECONDS);
    assertEquals(someValue, configFile.getContent());
    assertEquals(someNamespace, changeEvent.getNamespace());
    assertEquals(null, changeEvent.getOldValue());
    assertEquals(someValue, changeEvent.getNewValue());
    assertEquals(PropertyChangeType.ADDED, changeEvent.getChangeType());
}
Also used : ConfigFileChangeEvent(com.ctrip.framework.apollo.model.ConfigFileChangeEvent) ConfigFileChangeListener(com.ctrip.framework.apollo.ConfigFileChangeListener) Properties(java.util.Properties) Test(org.junit.Test)

Example 3 with ConfigFileChangeEvent

use of com.ctrip.framework.apollo.model.ConfigFileChangeEvent in project apollo by ctripcorp.

the class XmlConfigFileTest method testOnRepositoryChangeWithContentDeleted.

@Test
public void testOnRepositoryChangeWithContentDeleted() throws Exception {
    Properties someProperties = new Properties();
    String key = ConfigConsts.CONFIG_FILE_CONTENT_KEY;
    String someValue = "someValue";
    someProperties.setProperty(key, someValue);
    when(configRepository.getConfig()).thenReturn(someProperties);
    XmlConfigFile configFile = new XmlConfigFile(someNamespace, configRepository);
    assertEquals(someValue, configFile.getContent());
    Properties anotherProperties = new Properties();
    final SettableFuture<ConfigFileChangeEvent> configFileChangeFuture = SettableFuture.create();
    ConfigFileChangeListener someListener = new ConfigFileChangeListener() {

        @Override
        public void onChange(ConfigFileChangeEvent changeEvent) {
            configFileChangeFuture.set(changeEvent);
        }
    };
    configFile.addChangeListener(someListener);
    configFile.onRepositoryChange(someNamespace, anotherProperties);
    ConfigFileChangeEvent changeEvent = configFileChangeFuture.get(500, TimeUnit.MILLISECONDS);
    assertEquals(null, configFile.getContent());
    assertEquals(someNamespace, changeEvent.getNamespace());
    assertEquals(someValue, changeEvent.getOldValue());
    assertEquals(null, changeEvent.getNewValue());
    assertEquals(PropertyChangeType.DELETED, changeEvent.getChangeType());
}
Also used : ConfigFileChangeEvent(com.ctrip.framework.apollo.model.ConfigFileChangeEvent) ConfigFileChangeListener(com.ctrip.framework.apollo.ConfigFileChangeListener) Properties(java.util.Properties) Test(org.junit.Test)

Example 4 with ConfigFileChangeEvent

use of com.ctrip.framework.apollo.model.ConfigFileChangeEvent in project apollo by ctripcorp.

the class XmlConfigFileTest method testOnRepositoryChange.

@Test
public void testOnRepositoryChange() throws Exception {
    Properties someProperties = new Properties();
    String key = ConfigConsts.CONFIG_FILE_CONTENT_KEY;
    String someValue = "someValue";
    String anotherValue = "anotherValue";
    someProperties.setProperty(key, someValue);
    when(configRepository.getConfig()).thenReturn(someProperties);
    XmlConfigFile configFile = new XmlConfigFile(someNamespace, configRepository);
    assertEquals(someValue, configFile.getContent());
    Properties anotherProperties = new Properties();
    anotherProperties.setProperty(key, anotherValue);
    final SettableFuture<ConfigFileChangeEvent> configFileChangeFuture = SettableFuture.create();
    ConfigFileChangeListener someListener = new ConfigFileChangeListener() {

        @Override
        public void onChange(ConfigFileChangeEvent changeEvent) {
            configFileChangeFuture.set(changeEvent);
        }
    };
    configFile.addChangeListener(someListener);
    configFile.onRepositoryChange(someNamespace, anotherProperties);
    ConfigFileChangeEvent changeEvent = configFileChangeFuture.get(500, TimeUnit.MILLISECONDS);
    assertEquals(anotherValue, configFile.getContent());
    assertEquals(someNamespace, changeEvent.getNamespace());
    assertEquals(someValue, changeEvent.getOldValue());
    assertEquals(anotherValue, changeEvent.getNewValue());
    assertEquals(PropertyChangeType.MODIFIED, changeEvent.getChangeType());
}
Also used : ConfigFileChangeEvent(com.ctrip.framework.apollo.model.ConfigFileChangeEvent) ConfigFileChangeListener(com.ctrip.framework.apollo.ConfigFileChangeListener) Properties(java.util.Properties) Test(org.junit.Test)

Example 5 with ConfigFileChangeEvent

use of com.ctrip.framework.apollo.model.ConfigFileChangeEvent in project apollo by ctripcorp.

the class AbstractConfigFile method onRepositoryChange.

@Override
public synchronized void onRepositoryChange(String namespace, Properties newProperties) {
    if (newProperties.equals(m_configProperties.get())) {
        return;
    }
    Properties newConfigProperties = new Properties();
    newConfigProperties.putAll(newProperties);
    String oldValue = getContent();
    update(newProperties);
    String newValue = getContent();
    PropertyChangeType changeType = PropertyChangeType.MODIFIED;
    if (oldValue == null) {
        changeType = PropertyChangeType.ADDED;
    } else if (newValue == null) {
        changeType = PropertyChangeType.DELETED;
    }
    this.fireConfigChange(new ConfigFileChangeEvent(m_namespace, oldValue, newValue, changeType));
    Tracer.logEvent("Apollo.Client.ConfigChanges", m_namespace);
}
Also used : ConfigFileChangeEvent(com.ctrip.framework.apollo.model.ConfigFileChangeEvent) PropertyChangeType(com.ctrip.framework.apollo.enums.PropertyChangeType) Properties(java.util.Properties)

Aggregations

ConfigFileChangeEvent (com.ctrip.framework.apollo.model.ConfigFileChangeEvent)5 Properties (java.util.Properties)5 ConfigFileChangeListener (com.ctrip.framework.apollo.ConfigFileChangeListener)4 Test (org.junit.Test)4 PropertyChangeType (com.ctrip.framework.apollo.enums.PropertyChangeType)1