Search in sources :

Example 1 with ConfigFile

use of com.ctrip.framework.apollo.ConfigFile in project apollo by ctripcorp.

the class DefaultConfigManagerTest method testGetConfigFileMultipleTimesWithSameNamespace.

@Test
public void testGetConfigFileMultipleTimesWithSameNamespace() throws Exception {
    String someNamespace = "someName";
    ConfigFileFormat someConfigFileFormat = ConfigFileFormat.Properties;
    ConfigFile someConfigFile = defaultConfigManager.getConfigFile(someNamespace, someConfigFileFormat);
    ConfigFile anotherConfigFile = defaultConfigManager.getConfigFile(someNamespace, someConfigFileFormat);
    assertThat("Get config file multiple times with the same namespace should return the same config file instance", someConfigFile, equalTo(anotherConfigFile));
}
Also used : ConfigFileFormat(com.ctrip.framework.apollo.core.enums.ConfigFileFormat) ConfigFile(com.ctrip.framework.apollo.ConfigFile) Test(org.junit.Test)

Example 2 with ConfigFile

use of com.ctrip.framework.apollo.ConfigFile in project apollo by ctripcorp.

the class DefaultConfigFactoryTest method testCreateConfigFile.

@Test
public void testCreateConfigFile() throws Exception {
    String someNamespace = "someName";
    String anotherNamespace = "anotherName";
    String yetAnotherNamespace = "yetAnotherNamespace";
    Properties someProperties = new Properties();
    LocalFileConfigRepository someLocalConfigRepo = mock(LocalFileConfigRepository.class);
    when(someLocalConfigRepo.getConfig()).thenReturn(someProperties);
    doReturn(someLocalConfigRepo).when(defaultConfigFactory).createLocalConfigRepository(someNamespace);
    doReturn(someLocalConfigRepo).when(defaultConfigFactory).createLocalConfigRepository(anotherNamespace);
    doReturn(someLocalConfigRepo).when(defaultConfigFactory).createLocalConfigRepository(yetAnotherNamespace);
    ConfigFile propertyConfigFile = defaultConfigFactory.createConfigFile(someNamespace, ConfigFileFormat.Properties);
    ConfigFile xmlConfigFile = defaultConfigFactory.createConfigFile(anotherNamespace, ConfigFileFormat.XML);
    ConfigFile jsonConfigFile = defaultConfigFactory.createConfigFile(yetAnotherNamespace, ConfigFileFormat.JSON);
    ConfigFile ymlConfigFile = defaultConfigFactory.createConfigFile(someNamespace, ConfigFileFormat.YML);
    ConfigFile yamlConfigFile = defaultConfigFactory.createConfigFile(someNamespace, ConfigFileFormat.YAML);
    assertThat("Should create PropertiesConfigFile for properties format", propertyConfigFile, is(instanceOf(PropertiesConfigFile.class)));
    assertEquals(someNamespace, propertyConfigFile.getNamespace());
    assertThat("Should create XmlConfigFile for xml format", xmlConfigFile, is(instanceOf(XmlConfigFile.class)));
    assertEquals(anotherNamespace, xmlConfigFile.getNamespace());
    assertThat("Should create JsonConfigFile for json format", jsonConfigFile, is(instanceOf(JsonConfigFile.class)));
    assertEquals(yetAnotherNamespace, jsonConfigFile.getNamespace());
    assertThat("Should create YmlConfigFile for yml format", ymlConfigFile, is(instanceOf(YmlConfigFile.class)));
    assertEquals(someNamespace, ymlConfigFile.getNamespace());
    assertThat("Should create YamlConfigFile for yaml format", yamlConfigFile, is(instanceOf(YamlConfigFile.class)));
    assertEquals(someNamespace, yamlConfigFile.getNamespace());
}
Also used : LocalFileConfigRepository(com.ctrip.framework.apollo.internals.LocalFileConfigRepository) XmlConfigFile(com.ctrip.framework.apollo.internals.XmlConfigFile) YmlConfigFile(com.ctrip.framework.apollo.internals.YmlConfigFile) YamlConfigFile(com.ctrip.framework.apollo.internals.YamlConfigFile) JsonConfigFile(com.ctrip.framework.apollo.internals.JsonConfigFile) PropertiesConfigFile(com.ctrip.framework.apollo.internals.PropertiesConfigFile) ConfigFile(com.ctrip.framework.apollo.ConfigFile) Properties(java.util.Properties) Test(org.junit.Test)

Example 3 with ConfigFile

use of com.ctrip.framework.apollo.ConfigFile in project apollo by ctripcorp.

the class DefaultConfigManager method getConfigFile.

@Override
public ConfigFile getConfigFile(String namespace, ConfigFileFormat configFileFormat) {
    String namespaceFileName = String.format("%s.%s", namespace, configFileFormat.getValue());
    ConfigFile configFile = m_configFiles.get(namespaceFileName);
    if (configFile == null) {
        synchronized (this) {
            configFile = m_configFiles.get(namespaceFileName);
            if (configFile == null) {
                ConfigFactory factory = m_factoryManager.getFactory(namespaceFileName);
                configFile = factory.createConfigFile(namespaceFileName, configFileFormat);
                m_configFiles.put(namespaceFileName, configFile);
            }
        }
    }
    return configFile;
}
Also used : ConfigFile(com.ctrip.framework.apollo.ConfigFile) ConfigFactory(com.ctrip.framework.apollo.spi.ConfigFactory)

Example 4 with ConfigFile

use of com.ctrip.framework.apollo.ConfigFile in project apollo by ctripcorp.

the class DefaultConfigManagerTest method testGetConfigFile.

@Test
public void testGetConfigFile() throws Exception {
    String someNamespace = "someName";
    ConfigFileFormat someConfigFileFormat = ConfigFileFormat.Properties;
    ConfigFile configFile = defaultConfigManager.getConfigFile(someNamespace, someConfigFileFormat);
    assertEquals(someConfigFileFormat, configFile.getConfigFileFormat());
    assertEquals(someConfigContent, configFile.getContent());
}
Also used : ConfigFileFormat(com.ctrip.framework.apollo.core.enums.ConfigFileFormat) ConfigFile(com.ctrip.framework.apollo.ConfigFile) Test(org.junit.Test)

Aggregations

ConfigFile (com.ctrip.framework.apollo.ConfigFile)4 Test (org.junit.Test)3 ConfigFileFormat (com.ctrip.framework.apollo.core.enums.ConfigFileFormat)2 JsonConfigFile (com.ctrip.framework.apollo.internals.JsonConfigFile)1 LocalFileConfigRepository (com.ctrip.framework.apollo.internals.LocalFileConfigRepository)1 PropertiesConfigFile (com.ctrip.framework.apollo.internals.PropertiesConfigFile)1 XmlConfigFile (com.ctrip.framework.apollo.internals.XmlConfigFile)1 YamlConfigFile (com.ctrip.framework.apollo.internals.YamlConfigFile)1 YmlConfigFile (com.ctrip.framework.apollo.internals.YmlConfigFile)1 ConfigFactory (com.ctrip.framework.apollo.spi.ConfigFactory)1 Properties (java.util.Properties)1