SimpleConfig extends AbstractConfig implements RepositoryChangeListener { @Override public synchronized void onRepositoryChange(String namespace, Properties newProperties) { if (newProperties.equals(m_configProperties)) { return; } Properties newConfigProperties = propertiesFactory.getPropertiesInstance(); newConfigProperties.putAll(newProperties); List<ConfigChange> changes = calcPropertyChanges(namespace, m_configProperties, newConfigProperties); Map<String, ConfigChange> changeMap = Maps.uniqueIndex(changes, new Function<ConfigChange, String>() { @Override public String apply(ConfigChange input) { return input.getPropertyName(); } }); updateConfig(newConfigProperties, m_configRepository.getSourceType()); clearConfigCache(); this.fireConfigChange(new ConfigChangeEvent(m_namespace, changeMap)); Tracer.logEvent("Apollo.Client.ConfigChanges", m_namespace); } SimpleConfig(String namespace, ConfigRepository configRepository); @Override String getProperty(String key, String defaultValue); @Override Set<String> getPropertyNames(); @Override ConfigSourceType getSourceType(); @Override synchronized void onRepositoryChange(String namespace, Properties newProperties);  }
@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()); }