Search in sources :

Example 1 with ConfigFileChangeEvent

use of com.tencent.polaris.configuration.api.core.ConfigFileChangeEvent 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 ConfigFileChangeEvent

use of com.tencent.polaris.configuration.api.core.ConfigFileChangeEvent 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 ConfigFileChangeEvent

use of com.tencent.polaris.configuration.api.core.ConfigFileChangeEvent in project polaris-java by polarismesh.

the class ConfigPropertiesFileTest method testListener.

@Test
public void testListener() throws InterruptedException {
    String content = "#some comment\nk1=10\nk2=false\nk3=5.6\nk4=1,2,3,4,5\nk5=T1\n" + "k6={\"name\":\"zhangsan\",\"age\":18,\"labels\": {\"key1\":\"value1\"}}\n" + "k7=[{\"name\":\"zhangsan\",\"age\":18,\"labels\": {\"key1\":\"value1\"}},{\"name\":\"lisi\",\"age\":20,\"labels\": {\"key1\":\"value2\"}}]";
    when(configFileRepo.getContent()).thenReturn(content);
    ConfigPropertiesFile configFile = new ConfigPropertiesFile(ConfigFileTestUtils.testNamespace, ConfigFileTestUtils.testGroup, ConfigFileTestUtils.testFileName, configFileRepo, configFileConfig);
    // modified
    AtomicBoolean k1Check = new AtomicBoolean(false);
    // delete
    AtomicBoolean k2Check = new AtomicBoolean(false);
    // not changed
    AtomicBoolean k4Check = new AtomicBoolean(false);
    // add
    AtomicBoolean k8Check = new AtomicBoolean(false);
    // modified
    AtomicBoolean k7Check = new AtomicBoolean(false);
    configFile.addChangeListener(new ConfigKVFileChangeListener() {

        @Override
        public void onChange(ConfigKVFileChangeEvent event) {
            ConfigPropertyChangeInfo k1ChangeInfo = event.getChangeInfo("k1");
            k1Check.set(k1ChangeInfo.getNewValue().equals("1") && k1ChangeInfo.getOldValue().equals("10") && k1ChangeInfo.getChangeType() == ChangeType.MODIFIED);
            ConfigPropertyChangeInfo k2ChangeInfo = event.getChangeInfo("k2");
            k2Check.set(k2ChangeInfo.getNewValue() == null && k2ChangeInfo.getOldValue().equals("false") && k2ChangeInfo.getChangeType() == ChangeType.DELETED);
            ConfigPropertyChangeInfo k4ChangeInfo = event.getChangeInfo("k4");
            k4Check.set(k4ChangeInfo == null);
            ConfigPropertyChangeInfo k8ChangeInfo = event.getChangeInfo("k8");
            k8Check.set(k8ChangeInfo.getNewValue().equals("xx") && k8ChangeInfo.getOldValue() == null && k8ChangeInfo.getChangeType() == ChangeType.ADDED);
            ConfigPropertyChangeInfo k7ChangeInfo = event.getChangeInfo("k7");
            k7Check.set(k7ChangeInfo.getChangeType() == ChangeType.MODIFIED);
        }
    });
    String newContent = "#some comment\nk1=1\nk3=5.6\nk4=1,2,3,4,5\nk5=T1\nk8=xx\n" + "k6={\"name\":\"zhangsan\",\"age\":18,\"labels\": {\"key1\":\"value1\"}}\n" + "k7=[{\"name\":\"zhangsan\",\"age\":19,\"labels\": {\"key1\":\"value1\"}},{\"name\":\"lisi\",\"age\":20,\"labels\": {\"key1\":\"value2\"}}]";
    AtomicBoolean fileCheck = new AtomicBoolean(false);
    configFile.addChangeListener(new ConfigFileChangeListener() {

        @Override
        public void onChange(ConfigFileChangeEvent event) {
            fileCheck.set(event.getNewValue().equals(newContent) && event.getOldValue().equals(content) && event.getChangeType() == ChangeType.MODIFIED);
        }
    });
    configFile.onChange(ConfigFileTestUtils.assembleDefaultConfigFileMeta(), newContent);
    TimeUnit.MILLISECONDS.sleep(500);
    Assert.assertTrue(k1Check.get());
    Assert.assertTrue(k2Check.get());
    Assert.assertTrue(k4Check.get());
    Assert.assertTrue(k8Check.get());
    Assert.assertTrue(k7Check.get());
    Assert.assertTrue(fileCheck.get());
    // 检查变更之后的值
    Assert.assertEquals(newContent, configFile.getContent());
    Assert.assertTrue(configFile.hasContent());
    Assert.assertEquals(new Integer(1), configFile.getIntProperty("k1", 8));
    Assert.assertEquals("true", configFile.getProperty("k2", "true"));
    Assert.assertTrue(configFile.getBooleanProperty("k2", true));
    Assert.assertEquals(new Float(5.6f), configFile.getFloatProperty("k3", 1.0f));
    Assert.assertEquals(new Double(5.6), configFile.getDoubleProperty("k3", 1.0));
    Assert.assertEquals("xx", configFile.getProperty("k8", ""));
    String[] arr = configFile.getArrayProperty("k4", ",", new String[] {});
    Assert.assertEquals(5, arr.length);
    for (int i = 1; i < 6; i++) {
        Assert.assertEquals(i + "", arr[i - 1]);
    }
    Assert.assertEquals(new Byte((byte) 1), configFile.getByteProperty("k1", (byte) 1));
    Assert.assertEquals(ConfigFileTestUtils.MyType.T1, configFile.getEnumProperty("k5", ConfigFileTestUtils.MyType.class, ConfigFileTestUtils.MyType.T2));
    Properties properties = configFile.asProperties();
    Assert.assertEquals(7, properties.stringPropertyNames().size());
    Assert.assertEquals("1", properties.getProperty("k1"));
    Assert.assertNull(properties.getProperty("k2"));
    Assert.assertEquals("5.6", properties.getProperty("k3"));
    Assert.assertEquals("1,2,3,4,5", properties.getProperty("k4"));
    Assert.assertEquals("T1", properties.getProperty("k5"));
    Assert.assertEquals("xx", properties.getProperty("k8"));
    ConfigFileTestUtils.User user = configFile.getJsonProperty("k6", 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");
    List<ConfigFileTestUtils.User> users = configFile.getJsonProperty("k7", new TypeToken<List<ConfigFileTestUtils.User>>() {
    }.getType(), null);
    Assert.assertNotNull(users);
    Assert.assertEquals(2, users.size());
    for (ConfigFileTestUtils.User user2 : users) {
        Assert.assertNotNull(user2.getName());
        Assert.assertTrue(user2.getAge() > 0);
        Assert.assertTrue(user2.getLabels().size() > 0);
    }
}
Also used : ConfigFileChangeListener(com.tencent.polaris.configuration.api.core.ConfigFileChangeListener) Properties(java.util.Properties) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ConfigPropertyChangeInfo(com.tencent.polaris.configuration.api.core.ConfigPropertyChangeInfo) ConfigFileChangeEvent(com.tencent.polaris.configuration.api.core.ConfigFileChangeEvent) ConfigKVFileChangeEvent(com.tencent.polaris.configuration.api.core.ConfigKVFileChangeEvent) TypeToken(com.google.gson.reflect.TypeToken) ConfigKVFileChangeListener(com.tencent.polaris.configuration.api.core.ConfigKVFileChangeListener) ConfigPropertiesFile(com.tencent.polaris.configuration.client.internal.ConfigPropertiesFile) Test(org.junit.Test)

Example 4 with ConfigFileChangeEvent

use of com.tencent.polaris.configuration.api.core.ConfigFileChangeEvent in project polaris-java by polarismesh.

the class DefaultConfigFile method onChange.

@Override
public void onChange(ConfigFileMetadata configFileMetadata, String newContent) {
    String oldContent = this.content;
    this.content = newContent;
    ChangeType changeType = ChangeType.MODIFIED;
    if (oldContent == null) {
        changeType = ChangeType.ADDED;
    }
    if (newContent == null) {
        changeType = ChangeType.DELETED;
    }
    if (Objects.equals(oldContent, newContent)) {
        changeType = ChangeType.NOT_CHANGED;
    }
    if (objectCache != null) {
        objectCache.invalidateAll();
        cacheVersion.incrementAndGet();
    }
    fireChangeEvent(new ConfigFileChangeEvent(configFileMetadata, oldContent, newContent, changeType));
}
Also used : ChangeType(com.tencent.polaris.configuration.api.core.ChangeType) ConfigFileChangeEvent(com.tencent.polaris.configuration.api.core.ConfigFileChangeEvent)

Example 5 with ConfigFileChangeEvent

use of com.tencent.polaris.configuration.api.core.ConfigFileChangeEvent 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

ConfigFileChangeEvent (com.tencent.polaris.configuration.api.core.ConfigFileChangeEvent)9 ConfigFileChangeListener (com.tencent.polaris.configuration.api.core.ConfigFileChangeListener)8 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)7 Test (org.junit.Test)7 DefaultConfigFile (com.tencent.polaris.configuration.client.internal.DefaultConfigFile)6 TypeToken (com.google.gson.reflect.TypeToken)1 ChangeType (com.tencent.polaris.configuration.api.core.ChangeType)1 ConfigFile (com.tencent.polaris.configuration.api.core.ConfigFile)1 ConfigFileService (com.tencent.polaris.configuration.api.core.ConfigFileService)1 ConfigKVFileChangeEvent (com.tencent.polaris.configuration.api.core.ConfigKVFileChangeEvent)1 ConfigKVFileChangeListener (com.tencent.polaris.configuration.api.core.ConfigKVFileChangeListener)1 ConfigPropertyChangeInfo (com.tencent.polaris.configuration.api.core.ConfigPropertyChangeInfo)1 ConfigPropertiesFile (com.tencent.polaris.configuration.client.internal.ConfigPropertiesFile)1 Properties (java.util.Properties)1