Java Code Examples for com.ctrip.framework.apollo.core.ConfigConsts#CONFIG_FILE_CONTENT_KEY

The following examples show how to use com.ctrip.framework.apollo.core.ConfigConsts#CONFIG_FILE_CONTENT_KEY . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: YamlConfigFileTest.java    From apollo with Apache License 2.0 6 votes vote down vote up
@Test
public void testWhenHasContent() throws Exception {
  Properties someProperties = new Properties();
  String key = ConfigConsts.CONFIG_FILE_CONTENT_KEY;
  String someContent = "someKey: 'someValue'";
  someProperties.setProperty(key, someContent);
  someSourceType = ConfigSourceType.LOCAL;

  Properties yamlProperties = new Properties();
  yamlProperties.setProperty("someKey", "someValue");

  when(configRepository.getConfig()).thenReturn(someProperties);
  when(configRepository.getSourceType()).thenReturn(someSourceType);
  when(yamlParser.yamlToProperties(someContent)).thenReturn(yamlProperties);

  YamlConfigFile configFile = new YamlConfigFile(someNamespace, configRepository);

  assertSame(someContent, configFile.getContent());
  assertSame(yamlProperties, configFile.asProperties());
}
 
Example 2
Source File: TxtConfigFileTest.java    From apollo with Apache License 2.0 6 votes vote down vote up
@Test
public void testWhenHasContent() 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);

  TxtConfigFile configFile = new TxtConfigFile(someNamespace, configRepository);

  assertEquals(ConfigFileFormat.TXT, configFile.getConfigFileFormat());
  assertEquals(someNamespace, configFile.getNamespace());
  assertTrue(configFile.hasContent());
  assertEquals(someValue, configFile.getContent());
}
 
Example 3
Source File: JsonConfigFileTest.java    From apollo with Apache License 2.0 6 votes vote down vote up
@Test
public void testWhenHasContent() throws Exception {
  Properties someProperties = new Properties();
  String key = ConfigConsts.CONFIG_FILE_CONTENT_KEY;
  String someValue = "someValue";
  someProperties.setProperty(key, someValue);

  someSourceType = ConfigSourceType.LOCAL;

  when(configRepository.getConfig()).thenReturn(someProperties);
  when(configRepository.getSourceType()).thenReturn(someSourceType);

  JsonConfigFile configFile = new JsonConfigFile(someNamespace, configRepository);

  assertEquals(ConfigFileFormat.JSON, configFile.getConfigFileFormat());
  assertEquals(someNamespace, configFile.getNamespace());
  assertTrue(configFile.hasContent());
  assertEquals(someValue, configFile.getContent());
  assertEquals(someSourceType, configFile.getSourceType());
}
 
Example 4
Source File: JsonConfigFileTest.java    From apollo with Apache License 2.0 6 votes vote down vote up
@Test
public void testWhenConfigRepositoryHasErrorAndThenRecovered() throws Exception {
  Properties someProperties = new Properties();
  String key = ConfigConsts.CONFIG_FILE_CONTENT_KEY;
  String someValue = "someValue";
  someProperties.setProperty(key, someValue);

  someSourceType = ConfigSourceType.LOCAL;

  when(configRepository.getConfig()).thenThrow(new RuntimeException("someError"));
  when(configRepository.getSourceType()).thenReturn(someSourceType);

  JsonConfigFile configFile = new JsonConfigFile(someNamespace, configRepository);

  assertFalse(configFile.hasContent());
  assertNull(configFile.getContent());
  assertEquals(ConfigSourceType.NONE, configFile.getSourceType());

  configFile.onRepositoryChange(someNamespace, someProperties);

  assertTrue(configFile.hasContent());
  assertEquals(someValue, configFile.getContent());
  assertEquals(someSourceType, configFile.getSourceType());
}
 
Example 5
Source File: XmlConfigFileTest.java    From apollo with Apache License 2.0 6 votes vote down vote up
@Test
public void testWhenHasContent() 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(ConfigFileFormat.XML, configFile.getConfigFileFormat());
  assertEquals(someNamespace, configFile.getNamespace());
  assertTrue(configFile.hasContent());
  assertEquals(someValue, configFile.getContent());
}
 
Example 6
Source File: XmlConfigFileTest.java    From apollo with Apache License 2.0 6 votes vote down vote up
@Test
public void testWhenConfigRepositoryHasErrorAndThenRecovered() throws Exception {
  Properties someProperties = new Properties();
  String key = ConfigConsts.CONFIG_FILE_CONTENT_KEY;
  String someValue = "someValue";
  someProperties.setProperty(key, someValue);

  when(configRepository.getConfig()).thenThrow(new RuntimeException("someError"));

  XmlConfigFile configFile = new XmlConfigFile(someNamespace, configRepository);

  assertFalse(configFile.hasContent());
  assertNull(configFile.getContent());

  configFile.onRepositoryChange(someNamespace, someProperties);

  assertTrue(configFile.hasContent());
  assertEquals(someValue, configFile.getContent());
}
 
Example 7
Source File: YamlConfigFileTest.java    From apollo with Apache License 2.0 5 votes vote down vote up
@Test
public void testWhenHasContentWithOrder() throws Exception {
  when(propertiesFactory.getPropertiesInstance()).thenAnswer(new Answer<Properties>() {
    @Override
    public Properties answer(InvocationOnMock invocation) {
      return new OrderedProperties();
    }
  });
  Properties someProperties = new Properties();
  String key = ConfigConsts.CONFIG_FILE_CONTENT_KEY;
  String someContent = "someKey: 'someValue'\nsomeKey2: 'someValue2'";
  someProperties.setProperty(key, someContent);
  someSourceType = ConfigSourceType.LOCAL;

  Properties yamlProperties = new YamlParser().yamlToProperties(someContent);

  when(configRepository.getConfig()).thenReturn(someProperties);
  when(configRepository.getSourceType()).thenReturn(someSourceType);
  when(yamlParser.yamlToProperties(someContent)).thenReturn(yamlProperties);

  YamlConfigFile configFile = new YamlConfigFile(someNamespace, configRepository);

  assertSame(someContent, configFile.getContent());
  assertSame(yamlProperties, configFile.asProperties());

  String[] actualArrays = configFile.asProperties().keySet().toArray(new String[]{});
  String[] expectedArrays = {"someKey", "someKey2"};
  assertArrayEquals(expectedArrays, actualArrays);
}
 
Example 8
Source File: YamlConfigFileTest.java    From apollo with Apache License 2.0 5 votes vote down vote up
@Test
public void testWhenInvalidYamlContent() throws Exception {
  Properties someProperties = new Properties();
  String key = ConfigConsts.CONFIG_FILE_CONTENT_KEY;
  String someInvalidContent = ",";
  someProperties.setProperty(key, someInvalidContent);
  someSourceType = ConfigSourceType.LOCAL;

  when(configRepository.getConfig()).thenReturn(someProperties);
  when(configRepository.getSourceType()).thenReturn(someSourceType);
  when(yamlParser.yamlToProperties(someInvalidContent))
      .thenThrow(new RuntimeException("some exception"));

  YamlConfigFile configFile = new YamlConfigFile(someNamespace, configRepository);

  assertSame(someInvalidContent, configFile.getContent());

  Throwable exceptionThrown = null;
  try {
    configFile.asProperties();
  } catch (Throwable ex) {
    exceptionThrown = ex;
  }

  assertTrue(exceptionThrown instanceof ApolloConfigException);
  assertNotNull(exceptionThrown.getCause());
}
 
Example 9
Source File: YamlConfigFileTest.java    From apollo with Apache License 2.0 5 votes vote down vote up
@Test
public void testOnRepositoryChange() throws Exception {
  Properties someProperties = new Properties();
  String key = ConfigConsts.CONFIG_FILE_CONTENT_KEY;
  String someValue = "someKey: 'someValue'";
  String anotherValue = "anotherKey: 'anotherValue'";
  someProperties.setProperty(key, someValue);

  someSourceType = ConfigSourceType.LOCAL;

  Properties someYamlProperties = new Properties();
  someYamlProperties.setProperty("someKey", "someValue");

  Properties anotherYamlProperties = new Properties();
  anotherYamlProperties.setProperty("anotherKey", "anotherValue");

  when(configRepository.getConfig()).thenReturn(someProperties);
  when(configRepository.getSourceType()).thenReturn(someSourceType);
  when(yamlParser.yamlToProperties(someValue)).thenReturn(someYamlProperties);
  when(yamlParser.yamlToProperties(anotherValue)).thenReturn(anotherYamlProperties);

  YamlConfigFile configFile = new YamlConfigFile(someNamespace, configRepository);

  assertEquals(someValue, configFile.getContent());
  assertEquals(someSourceType, configFile.getSourceType());
  assertSame(someYamlProperties, configFile.asProperties());

  Properties anotherProperties = new Properties();
  anotherProperties.setProperty(key, anotherValue);

  ConfigSourceType anotherSourceType = ConfigSourceType.REMOTE;
  when(configRepository.getSourceType()).thenReturn(anotherSourceType);

  configFile.onRepositoryChange(someNamespace, anotherProperties);

  assertEquals(anotherValue, configFile.getContent());
  assertEquals(anotherSourceType, configFile.getSourceType());
  assertSame(anotherYamlProperties, configFile.asProperties());
}
 
Example 10
Source File: YamlConfigFileTest.java    From apollo with Apache License 2.0 5 votes vote down vote up
@Test
public void testWhenConfigRepositoryHasErrorAndThenRecovered() throws Exception {
  Properties someProperties = new Properties();
  String key = ConfigConsts.CONFIG_FILE_CONTENT_KEY;
  String someValue = "someKey: 'someValue'";
  someProperties.setProperty(key, someValue);

  someSourceType = ConfigSourceType.LOCAL;

  Properties someYamlProperties = new Properties();
  someYamlProperties.setProperty("someKey", "someValue");

  when(configRepository.getConfig()).thenThrow(new RuntimeException("someError"));
  when(configRepository.getSourceType()).thenReturn(someSourceType);
  when(yamlParser.yamlToProperties(someValue)).thenReturn(someYamlProperties);

  YamlConfigFile configFile = new YamlConfigFile(someNamespace, configRepository);

  assertFalse(configFile.hasContent());
  assertNull(configFile.getContent());
  assertEquals(ConfigSourceType.NONE, configFile.getSourceType());
  assertTrue(configFile.asProperties().isEmpty());

  configFile.onRepositoryChange(someNamespace, someProperties);

  assertTrue(configFile.hasContent());
  assertEquals(someValue, configFile.getContent());
  assertEquals(someSourceType, configFile.getSourceType());
  assertSame(someYamlProperties, configFile.asProperties());
}
 
Example 11
Source File: JsonConfigFileTest.java    From apollo with Apache License 2.0 5 votes vote down vote up
@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);

  someSourceType = ConfigSourceType.LOCAL;

  when(configRepository.getConfig()).thenReturn(someProperties);
  when(configRepository.getSourceType()).thenReturn(someSourceType);

  JsonConfigFile configFile = new JsonConfigFile(someNamespace, configRepository);

  assertEquals(someValue, configFile.getContent());
  assertEquals(someSourceType, configFile.getSourceType());

  Properties anotherProperties = new Properties();
  anotherProperties.setProperty(key, anotherValue);

  ConfigSourceType anotherSourceType = ConfigSourceType.REMOTE;
  when(configRepository.getSourceType()).thenReturn(anotherSourceType);

  configFile.onRepositoryChange(someNamespace, anotherProperties);

  assertEquals(anotherValue, configFile.getContent());
  assertEquals(anotherSourceType, configFile.getSourceType());
}