use of com.tencent.polaris.configuration.client.internal.DefaultConfigFile in project polaris-java by polarismesh.
the class ConfigFileTest method testAddContent.
@Test
public void testAddContent() throws InterruptedException {
when(configFileRepo.getContent()).thenReturn(null);
DefaultConfigFile configFile = new DefaultConfigFile(ConfigFileTestUtils.testNamespace, ConfigFileTestUtils.testGroup, ConfigFileTestUtils.testFileName, configFileRepo, configFileConfig);
Assert.assertNull(configFile.getContent());
Assert.assertFalse(configFile.hasContent());
String content = "hello";
AtomicBoolean check = new AtomicBoolean(false);
configFile.addChangeListener(new ConfigFileChangeListener() {
@Override
public void onChange(ConfigFileChangeEvent event) {
check.set(event.getNewValue().equals(content) && event.getOldValue() == null && event.getChangeType() == ChangeType.ADDED);
}
});
configFile.onChange(ConfigFileTestUtils.assembleDefaultConfigFileMeta(), content);
TimeUnit.MILLISECONDS.sleep(100);
Assert.assertTrue(check.get());
}
use of com.tencent.polaris.configuration.client.internal.DefaultConfigFile in project polaris-java by polarismesh.
the class ConfigFileTest method testRemoveChangeListener.
@Test
public void testRemoveChangeListener() throws InterruptedException {
String content = "hello";
when(configFileRepo.getContent()).thenReturn(content);
DefaultConfigFile configFile = new DefaultConfigFile(ConfigFileTestUtils.testNamespace, ConfigFileTestUtils.testGroup, ConfigFileTestUtils.testFileName, configFileRepo, configFileConfig);
String content2 = "hello2";
AtomicBoolean invoked = new AtomicBoolean(false);
AtomicBoolean check = new AtomicBoolean(false);
ConfigFileChangeListener listener = new ConfigFileChangeListener() {
@Override
public void onChange(ConfigFileChangeEvent event) {
check.set(event.getNewValue().equals(content2) && event.getOldValue().equals(content) && event.getChangeType() == ChangeType.MODIFIED);
invoked.set(true);
}
};
configFile.addChangeListener(listener);
configFile.onChange(ConfigFileTestUtils.assembleDefaultConfigFileMeta(), content2);
TimeUnit.MILLISECONDS.sleep(100);
Assert.assertTrue(invoked.get());
Assert.assertTrue(check.get());
// 重置状态位
invoked.set(false);
configFile.removeChangeListener(listener);
String content3 = "hello3";
configFile.onChange(ConfigFileTestUtils.assembleDefaultConfigFileMeta(), content3);
TimeUnit.MILLISECONDS.sleep(100);
Assert.assertFalse(invoked.get());
}
use of com.tencent.polaris.configuration.client.internal.DefaultConfigFile in project polaris-java by polarismesh.
the class ConfigFileTest method testGetContent.
@Test
public void testGetContent() {
String content = "hello";
when(configFileRepo.getContent()).thenReturn(content);
ConfigFile configFile = new DefaultConfigFile(ConfigFileTestUtils.testNamespace, ConfigFileTestUtils.testGroup, ConfigFileTestUtils.testFileName, configFileRepo, configFileConfig);
Assert.assertEquals(content, configFile.getContent());
Assert.assertTrue(configFile.hasContent());
Assert.assertEquals(ConfigFileTestUtils.testNamespace, configFile.getNamespace());
Assert.assertEquals(ConfigFileTestUtils.testGroup, configFile.getFileGroup());
Assert.assertEquals(ConfigFileTestUtils.testFileName, configFile.getFileName());
}
use of com.tencent.polaris.configuration.client.internal.DefaultConfigFile in project polaris-java by polarismesh.
the class ConfigFileTest method testGetJsonObject.
@Test
public void testGetJsonObject() {
String content = "{\n" + "\t\"name\":\"zhangsan\",\n" + "\t\"age\":18,\n" + "\t\"labels\": {\n" + "\t \"key1\":\"value1\"\n" + "\t}\n" + "}";
when(configFileRepo.getContent()).thenReturn(content);
ConfigFile configFile = new DefaultConfigFile(ConfigFileTestUtils.testNamespace, ConfigFileTestUtils.testGroup, ConfigFileTestUtils.testFileName, configFileRepo, configFileConfig);
Assert.assertEquals(content, configFile.getContent());
ConfigFileTestUtils.User user = configFile.asJson(ConfigFileTestUtils.User.class, null);
Assert.assertNotNull(user);
Assert.assertEquals("zhangsan", user.getName());
Assert.assertEquals(18, user.getAge());
Assert.assertEquals(user.getLabels().size(), 1);
Assert.assertEquals(user.getLabels().get("key1"), "value1");
}
use of com.tencent.polaris.configuration.client.internal.DefaultConfigFile in project polaris-java by polarismesh.
the class ConfigFileTest method testContentNotChangedWithNull.
@Test
public void testContentNotChangedWithNull() throws InterruptedException {
when(configFileRepo.getContent()).thenReturn(null);
DefaultConfigFile configFile = new DefaultConfigFile(ConfigFileTestUtils.testNamespace, ConfigFileTestUtils.testGroup, ConfigFileTestUtils.testFileName, configFileRepo, configFileConfig);
Assert.assertNull(configFile.getContent());
Assert.assertFalse(configFile.hasContent());
AtomicBoolean check = new AtomicBoolean(false);
configFile.addChangeListener(new ConfigFileChangeListener() {
@Override
public void onChange(ConfigFileChangeEvent event) {
check.set(event.getNewValue() == null && event.getOldValue() == null && event.getChangeType() == ChangeType.NOT_CHANGED);
}
});
configFile.onChange(ConfigFileTestUtils.assembleDefaultConfigFileMeta(), null);
TimeUnit.MILLISECONDS.sleep(100);
Assert.assertTrue(check.get());
}
Aggregations