Search in sources :

Example 1 with DefaultConfigFile

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());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ConfigFileChangeEvent(com.tencent.polaris.configuration.api.core.ConfigFileChangeEvent) ConfigFileChangeListener(com.tencent.polaris.configuration.api.core.ConfigFileChangeListener) DefaultConfigFile(com.tencent.polaris.configuration.client.internal.DefaultConfigFile) Test(org.junit.Test)

Example 2 with DefaultConfigFile

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());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ConfigFileChangeEvent(com.tencent.polaris.configuration.api.core.ConfigFileChangeEvent) ConfigFileChangeListener(com.tencent.polaris.configuration.api.core.ConfigFileChangeListener) DefaultConfigFile(com.tencent.polaris.configuration.client.internal.DefaultConfigFile) Test(org.junit.Test)

Example 3 with DefaultConfigFile

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());
}
Also used : DefaultConfigFile(com.tencent.polaris.configuration.client.internal.DefaultConfigFile) ConfigFile(com.tencent.polaris.configuration.api.core.ConfigFile) DefaultConfigFile(com.tencent.polaris.configuration.client.internal.DefaultConfigFile) Test(org.junit.Test)

Example 4 with DefaultConfigFile

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");
}
Also used : DefaultConfigFile(com.tencent.polaris.configuration.client.internal.DefaultConfigFile) ConfigFile(com.tencent.polaris.configuration.api.core.ConfigFile) DefaultConfigFile(com.tencent.polaris.configuration.client.internal.DefaultConfigFile) Test(org.junit.Test)

Example 5 with DefaultConfigFile

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());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ConfigFileChangeEvent(com.tencent.polaris.configuration.api.core.ConfigFileChangeEvent) ConfigFileChangeListener(com.tencent.polaris.configuration.api.core.ConfigFileChangeListener) DefaultConfigFile(com.tencent.polaris.configuration.client.internal.DefaultConfigFile) Test(org.junit.Test)

Aggregations

DefaultConfigFile (com.tencent.polaris.configuration.client.internal.DefaultConfigFile)9 Test (org.junit.Test)9 ConfigFileChangeEvent (com.tencent.polaris.configuration.api.core.ConfigFileChangeEvent)6 ConfigFileChangeListener (com.tencent.polaris.configuration.api.core.ConfigFileChangeListener)6 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)6 ConfigFile (com.tencent.polaris.configuration.api.core.ConfigFile)3 TypeToken (com.google.gson.reflect.TypeToken)1