DefaultConfig extends AbstractConfig implements RepositoryChangeListener { @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); if (actualChanges.isEmpty()) { return; } this.fireConfigChange(new ConfigChangeEvent(m_namespace, actualChanges)); Tracer.logEvent("Apollo.Client.ConfigChanges", m_namespace); } DefaultConfig(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 testGetIntPropertyMultipleTimesWithPropertyChanges() throws Exception { String someKey = "someKey"; Integer someValue = 2; Integer anotherValue = 3; Integer someDefaultValue = -1; someProperties = new Properties(); someProperties.setProperty(someKey, String.valueOf(someValue)); when(configRepository.getConfig()).thenReturn(someProperties); DefaultConfig defaultConfig = new DefaultConfig(someNamespace, configRepository); assertEquals(someValue, defaultConfig.getIntProperty(someKey, someDefaultValue)); Properties anotherProperties = new Properties(); anotherProperties.setProperty(someKey, String.valueOf(anotherValue)); defaultConfig.onRepositoryChange(someNamespace, anotherProperties); assertEquals(anotherValue, defaultConfig.getIntProperty(someKey, someDefaultValue)); }
@Test public void testGetArrayPropertyMultipleTimesWithCacheAndValueChanges() throws Exception { String someKey = "someKey"; String someDelimiter = ","; String[] values = new String[]{"a", "b", "c"}; String[] anotherValues = new String[]{"b", "c", "d"}; String someValue = Joiner.on(someDelimiter).join(values); String anotherValue = Joiner.on(someDelimiter).join(anotherValues); String[] someDefaultValue = new String[]{"1", "2"}; someProperties = new Properties(); someProperties.setProperty(someKey, someValue); when(configRepository.getConfig()).thenReturn(someProperties); Properties anotherProperties = new Properties(); anotherProperties.setProperty(someKey, anotherValue); DefaultConfig defaultConfig = new DefaultConfig(someNamespace, configRepository); assertArrayEquals(values, defaultConfig.getArrayProperty(someKey, someDelimiter, someDefaultValue)); defaultConfig.onRepositoryChange(someNamespace, anotherProperties); assertArrayEquals(anotherValues, defaultConfig.getArrayProperty(someKey, someDelimiter, someDefaultValue)); }
@Test public void testOnRepositoryChange() throws Exception { String someKey = "someKey"; String someSystemPropertyValue = "system-property-value"; String anotherKey = "anotherKey"; String someLocalFileValue = "local-file-value"; String keyToBeDeleted = "keyToBeDeleted"; String keyToBeDeletedValue = "keyToBeDeletedValue"; String yetAnotherKey = "yetAnotherKey"; String yetAnotherValue = "yetAnotherValue"; String yetAnotherResourceValue = "yetAnotherResourceValue"; System.setProperty(someKey, someSystemPropertyValue); someProperties = new Properties(); someProperties.putAll(ImmutableMap .of(someKey, someLocalFileValue, anotherKey, someLocalFileValue, keyToBeDeleted, keyToBeDeletedValue, yetAnotherKey, yetAnotherValue)); when(configRepository.getConfig()).thenReturn(someProperties); someSourceType = ConfigSourceType.LOCAL; when(configRepository.getSourceType()).thenReturn(someSourceType); File resourceFile = new File(someResourceDir, someNamespace + ".properties"); Files.append(yetAnotherKey + "=" + yetAnotherResourceValue, resourceFile, Charsets.UTF_8); DefaultConfig defaultConfig = new DefaultConfig(someNamespace, configRepository); assertEquals(someSourceType, defaultConfig.getSourceType()); final SettableFuture<ConfigChangeEvent> configChangeFuture = SettableFuture.create(); ConfigChangeListener someListener = new ConfigChangeListener() { @Override public void onChange(ConfigChangeEvent changeEvent) { configChangeFuture.set(changeEvent); } }; defaultConfig.addChangeListener(someListener); Properties newProperties = new Properties(); String someKeyNewValue = "new-some-value"; String anotherKeyNewValue = "another-new-value"; String newKey = "newKey"; String newValue = "newValue"; newProperties.putAll(ImmutableMap .of(someKey, someKeyNewValue, anotherKey, anotherKeyNewValue, newKey, newValue)); ConfigSourceType anotherSourceType = ConfigSourceType.REMOTE; when(configRepository.getSourceType()).thenReturn(anotherSourceType); defaultConfig.onRepositoryChange(someNamespace, newProperties); ConfigChangeEvent changeEvent = configChangeFuture.get(500, TimeUnit.MILLISECONDS); System.clearProperty(someKey); assertEquals(someNamespace, changeEvent.getNamespace()); assertEquals(4, changeEvent.changedKeys().size()); ConfigChange anotherKeyChange = changeEvent.getChange(anotherKey); assertEquals(someLocalFileValue, anotherKeyChange.getOldValue()); assertEquals(anotherKeyNewValue, anotherKeyChange.getNewValue()); assertEquals(PropertyChangeType.MODIFIED, anotherKeyChange.getChangeType()); ConfigChange yetAnotherKeyChange = changeEvent.getChange(yetAnotherKey); assertEquals(yetAnotherValue, yetAnotherKeyChange.getOldValue()); assertEquals(yetAnotherResourceValue, yetAnotherKeyChange.getNewValue()); assertEquals(PropertyChangeType.MODIFIED, yetAnotherKeyChange.getChangeType()); ConfigChange keyToBeDeletedChange = changeEvent.getChange(keyToBeDeleted); assertEquals(keyToBeDeletedValue, keyToBeDeletedChange.getOldValue()); assertEquals(null, keyToBeDeletedChange.getNewValue()); assertEquals(PropertyChangeType.DELETED, keyToBeDeletedChange.getChangeType()); ConfigChange newKeyChange = changeEvent.getChange(newKey); assertEquals(null, newKeyChange.getOldValue()); assertEquals(newValue, newKeyChange.getNewValue()); assertEquals(PropertyChangeType.ADDED, newKeyChange.getChangeType()); assertEquals(anotherSourceType, defaultConfig.getSourceType()); }