DefaultConfig extends AbstractConfig implements RepositoryChangeListener { @Override public String getProperty(String key, String defaultValue) { String value = System.getProperty(key); if (value == null && m_configProperties.get() != null) { value = m_configProperties.get().getProperty(key); } if (value == null) { value = System.getenv(key); } if (value == null && m_resourceProperties != null) { value = m_resourceProperties.getProperty(key); } if (value == null && m_configProperties.get() == null && m_warnLogRateLimiter.tryAcquire()) { logger.warn("Could not load config for namespace {} from Apollo, please check whether the configs are released in Apollo! Return default value now!", m_namespace); } return value == null ? defaultValue : value; } 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 testGetIntPropertyMultipleTimesWithSmallCache() throws Exception { String someKey = "someKey"; Integer someValue = 2; String anotherKey = "anotherKey"; Integer anotherValue = 3; Integer someDefaultValue = -1; MockInjector.setInstance(ConfigUtil.class, new MockConfigUtilWithSmallCache()); someProperties = mock(Properties.class); when(someProperties.getProperty(someKey)).thenReturn(String.valueOf(someValue)); when(someProperties.getProperty(anotherKey)).thenReturn(String.valueOf(anotherValue)); when(configRepository.getConfig()).thenReturn(someProperties); DefaultConfig defaultConfig = new DefaultConfig(someNamespace, configRepository); assertEquals(someValue, defaultConfig.getIntProperty(someKey, someDefaultValue)); assertEquals(someValue, defaultConfig.getIntProperty(someKey, someDefaultValue)); verify(someProperties, times(1)).getProperty(someKey); assertEquals(anotherValue, defaultConfig.getIntProperty(anotherKey, someDefaultValue)); assertEquals(anotherValue, defaultConfig.getIntProperty(anotherKey, someDefaultValue)); verify(someProperties, times(1)).getProperty(anotherKey); assertEquals(someValue, defaultConfig.getIntProperty(someKey, someDefaultValue)); verify(someProperties, times(2)).getProperty(someKey); }
@Test public void testGetIntPropertyMultipleTimesWithShortExpireTime() throws Exception { String someKey = "someKey"; Integer someValue = 2; Integer someDefaultValue = -1; MockInjector.setInstance(ConfigUtil.class, new MockConfigUtilWithShortExpireTime()); someProperties = mock(Properties.class); when(someProperties.getProperty(someKey)).thenReturn(String.valueOf(someValue)); when(configRepository.getConfig()).thenReturn(someProperties); DefaultConfig defaultConfig = new DefaultConfig(someNamespace, configRepository); assertEquals(someValue, defaultConfig.getIntProperty(someKey, someDefaultValue)); assertEquals(someValue, defaultConfig.getIntProperty(someKey, someDefaultValue)); verify(someProperties, times(1)).getProperty(someKey); TimeUnit.MILLISECONDS.sleep(50); assertEquals(someValue, defaultConfig.getIntProperty(someKey, someDefaultValue)); assertEquals(someValue, defaultConfig.getIntProperty(someKey, someDefaultValue)); verify(someProperties, times(2)).getProperty(someKey); }
@Test public void testGetArrayPropertyMultipleTimesWithCache() throws Exception { String someKey = "someKey"; String someDelimiter = ","; String someInvalidDelimiter = "{"; String[] values = new String[]{"a", "b", "c"}; String someValue = Joiner.on(someDelimiter).join(values); String[] someDefaultValue = new String[]{"1", "2"}; someProperties = mock(Properties.class); when(someProperties.getProperty(someKey)).thenReturn(someValue); when(configRepository.getConfig()).thenReturn(someProperties); DefaultConfig defaultConfig = new DefaultConfig(someNamespace, configRepository); assertArrayEquals(values, defaultConfig.getArrayProperty(someKey, someDelimiter, someDefaultValue)); assertArrayEquals(values, defaultConfig.getArrayProperty(someKey, someDelimiter, someDefaultValue)); verify(someProperties, times(1)).getProperty(someKey); assertArrayEquals(someDefaultValue, defaultConfig.getArrayProperty(someKey, someInvalidDelimiter, someDefaultValue)); assertArrayEquals(someDefaultValue, defaultConfig.getArrayProperty(someKey, someInvalidDelimiter, someDefaultValue)); verify(someProperties, times(3)).getProperty(someKey); }
@Test public void testGetPropertyWithFunction() throws Exception { String someKey = "someKey"; String someValue = "a,b,c"; String someNullKey = "someNullKey"; someProperties = new Properties(); someProperties.setProperty(someKey, someValue); when(configRepository.getConfig()).thenReturn(someProperties); DefaultConfig defaultConfig = new DefaultConfig(someNamespace, configRepository); assertEquals(defaultConfig.getProperty(someKey, new Function<String, List<String>>() { @Override public List<String> apply(String s) { return Splitter.on(",").trimResults().omitEmptyStrings().splitToList(s); } }, Lists.<String>newArrayList()), Lists.newArrayList("a", "b", "c")); assertEquals(defaultConfig.getProperty(someNullKey, new Function<String, List<String>>() { @Override public List<String> apply(String s) { return Splitter.on(",").trimResults().omitEmptyStrings().splitToList(s); } }, Lists.<String>newArrayList()), Lists.newArrayList()); }
@Test public void testGetIntPropertyMultipleTimesWithCache() throws Exception { String someKey = "someKey"; Integer someValue = 2; Integer someDefaultValue = -1; someProperties = mock(Properties.class); when(someProperties.getProperty(someKey)).thenReturn(String.valueOf(someValue)); when(configRepository.getConfig()).thenReturn(someProperties); DefaultConfig defaultConfig = new DefaultConfig(someNamespace, configRepository); assertEquals(someValue, defaultConfig.getIntProperty(someKey, someDefaultValue)); assertEquals(someValue, defaultConfig.getIntProperty(someKey, someDefaultValue)); assertEquals(someValue, defaultConfig.getIntProperty(someKey, someDefaultValue)); verify(someProperties, times(1)).getProperty(someKey); }