Skip to content

Commit a7685fb

Browse files
authored
test: add unit test for config module (#5391)
1 parent d1c4b78 commit a7685fb

11 files changed

Lines changed: 434 additions & 0 deletions

File tree

changes/en-us/2.0.0.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ The version is updated as follows:
7171
- [[#5335](https://github.com/seata/seata/pull/5335)] add unit test [EnhancedServiceLoader,ExtensionDefinition,SizeUtilTest,ReflectionUtil,LowerCaseLinkHashMap,FileLoader,ObjectHolder]
7272
- [[#5366](https://github.com/seata/seata/pull/5366)] fix UpdateExecutorTest failed
7373
- [[#5383](https://github.com/seata/seata/pull/5383)] fix multi spring version test failed
74+
- [[#5391](https://github.com/seata/seata/pull/5391)] add unit test for config module
7475

7576

7677
### Contributors:

changes/zh-cn/2.0.0.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ Seata 是一款开源的分布式事务解决方案,提供高性能和简单
7373
- [[#5335](https://github.com/seata/seata/pull/5335)] 添加单元测试用例 [EnhancedServiceLoader,ExtensionDefinition,SizeUtilTest,ReflectionUtil,LowerCaseLinkHashMap,FileLoader,ObjectHolder]
7474
- [[#5366](https://github.com/seata/seata/pull/5366)] 修复 UpdateExecutorTest 单测失败问题
7575
- [[#5383](https://github.com/seata/seata/pull/5383)] 修复多Spring版本测试失败
76+
- [[#5391](https://github.com/seata/seata/pull/5391)] 添加 config 模块的单元测试用例
7677

7778

7879
### Contributors:
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
* Copyright 1999-2019 Seata.io Group.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.seata.config;
18+
19+
import io.seata.common.exception.ShouldNeverHappenException;
20+
import io.seata.common.util.ReflectionUtil;
21+
import org.junit.jupiter.api.Assertions;
22+
import org.junit.jupiter.api.Test;
23+
import org.mockito.Mockito;
24+
import org.mockito.internal.util.reflection.FieldSetter;
25+
26+
import java.lang.reflect.Field;
27+
import java.util.concurrent.CompletableFuture;
28+
import java.util.concurrent.ExecutionException;
29+
import java.util.concurrent.TimeoutException;
30+
31+
/**
32+
* @author liuqiufeng
33+
*/
34+
class ConfigFutureTest {
35+
36+
37+
@Test
38+
void testGet() throws NoSuchFieldException, IllegalAccessException, ExecutionException, InterruptedException, TimeoutException {
39+
// mainly test exception scene
40+
ConfigFuture configFuture = Mockito.spy(new ConfigFuture("file.conf", "defaultValue", ConfigFuture.ConfigOperation.GET));
41+
42+
Field originField = ReflectionUtil.getField(ConfigFuture.class, "origin");
43+
CompletableFuture<Object> origin = (CompletableFuture<Object>) originField.get(configFuture);
44+
// mock field
45+
origin = Mockito.spy(origin);
46+
// set mocked field to object
47+
FieldSetter.setField(configFuture, originField, origin);
48+
49+
Mockito.doThrow(ExecutionException.class).when(origin).get(Mockito.anyLong(), Mockito.any());
50+
Assertions.assertThrows(ShouldNeverHappenException.class, configFuture::get);
51+
52+
Mockito.doThrow(TimeoutException.class).when(origin).get(Mockito.anyLong(), Mockito.any());
53+
Assertions.assertEquals("defaultValue", configFuture.get());
54+
55+
Mockito.doThrow(InterruptedException.class).when(origin).get(Mockito.anyLong(), Mockito.any());
56+
Assertions.assertEquals("defaultValue", configFuture.get());
57+
58+
Mockito.doReturn(null).when(origin).get(Mockito.anyLong(), Mockito.any());
59+
Assertions.assertEquals("defaultValue", configFuture.get());
60+
61+
62+
// set another config operation
63+
configFuture.setOperation(ConfigFuture.ConfigOperation.PUT);
64+
65+
Mockito.doThrow(ExecutionException.class).when(origin).get(Mockito.anyLong(), Mockito.any());
66+
Assertions.assertThrows(ShouldNeverHappenException.class, configFuture::get);
67+
68+
Mockito.doThrow(TimeoutException.class).when(origin).get(Mockito.anyLong(), Mockito.any());
69+
Assertions.assertEquals(Boolean.FALSE, configFuture.get());
70+
71+
Mockito.doThrow(InterruptedException.class).when(origin).get(Mockito.anyLong(), Mockito.any());
72+
Assertions.assertEquals(Boolean.FALSE, configFuture.get());
73+
74+
Mockito.doReturn(null).when(origin).get(Mockito.anyLong(), Mockito.any());
75+
Assertions.assertEquals(Boolean.FALSE, configFuture.get());
76+
}
77+
78+
@Test
79+
void setDataId() {
80+
ConfigFuture configFuture = new ConfigFuture("file.conf", "defaultValue", ConfigFuture.ConfigOperation.GET);
81+
Assertions.assertEquals("file.conf", configFuture.getDataId());
82+
configFuture.setDataId("file-test.conf");
83+
Assertions.assertEquals("file-test.conf", configFuture.getDataId());
84+
}
85+
86+
@Test
87+
void setContent() {
88+
ConfigFuture configFuture = new ConfigFuture("file.conf", "defaultValue", ConfigFuture.ConfigOperation.GET);
89+
Assertions.assertEquals("defaultValue", configFuture.getContent());
90+
configFuture.setContent("testValue");
91+
Assertions.assertEquals("testValue", configFuture.getContent());
92+
}
93+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright 1999-2019 Seata.io Group.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.seata.config;
18+
19+
import org.junit.jupiter.api.Assertions;
20+
import org.junit.jupiter.api.Test;
21+
22+
/**
23+
* @author liuqiufeng
24+
*/
25+
class ConfigTypeTest {
26+
27+
@Test
28+
void getType() {
29+
// mainly test exception scene
30+
Assertions.assertEquals(ConfigType.File, ConfigType.getType("File"));
31+
Assertions.assertThrows(IllegalArgumentException.class, () -> ConfigType.getType("test"));
32+
}
33+
}

config/seata-config-core/src/test/java/io/seata/config/ConfigurationCacheTests.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,16 @@
1515
*/
1616
package io.seata.config;
1717

18+
import io.seata.common.util.CollectionUtils;
1819
import io.seata.common.util.DurationUtil;
20+
import io.seata.common.util.ReflectionUtil;
1921
import org.junit.jupiter.api.Assertions;
2022
import org.junit.jupiter.api.Test;
2123

24+
import java.lang.reflect.Field;
2225
import java.time.Duration;
26+
import java.util.HashSet;
27+
import java.util.Map;
2328

2429
/**
2530
* @author jsbxyyx
@@ -54,6 +59,57 @@ public void testChangeValue() throws Exception {
5459
ConfigurationCache.getInstance().onChangeEvent(new ConfigurationChangeEvent("eee", "1"));
5560
long eee = configuration.getLong("eee", 0);
5661
Assertions.assertEquals((long) 1, eee);
62+
63+
// test null
64+
configuration.getConfig("test", null);
65+
ConfigurationCache.getInstance().onChangeEvent(new ConfigurationChangeEvent("test", "1"));
66+
String test = configuration.getConfig("test", null);
67+
Assertions.assertEquals("1", test);
68+
// new value is null
69+
ConfigurationCache.getInstance().onChangeEvent(new ConfigurationChangeEvent("test", null));
70+
test = configuration.getConfig("test", null);
71+
Assertions.assertNull(test);
72+
}
73+
74+
// FIXME: 2023/2/19 wait bugfix
75+
// @Test
76+
public void testConfigListener() throws Exception {
77+
Configuration configuration = new FileConfiguration("registry");
78+
configuration = ConfigurationCache.getInstance().proxy(configuration);
79+
80+
// get config listeners map
81+
Field configListenersMapField = ReflectionUtil.getField(ConfigurationCache.class, "configListenersMap");
82+
Map<String, HashSet<ConfigurationChangeListener>> configListenersMap = (Map<String,
83+
HashSet<ConfigurationChangeListener>>)configListenersMapField.get(ConfigurationCache.getInstance());
84+
85+
boolean value = configuration.getBoolean("service.disableGlobalTransaction");
86+
TestListener listener = new TestListener();
87+
ConfigurationCache.addConfigListener("service.disableGlobalTransaction", listener);
88+
// check listener if exist
89+
HashSet<ConfigurationChangeListener> listeners = configListenersMap.get("service.disableGlobalTransaction");
90+
Assertions.assertTrue(CollectionUtils.isNotEmpty(listeners));
91+
// change value,trigger listener
92+
System.setProperty("service.disableGlobalTransaction", String.valueOf(!value));
93+
// remove null
94+
ConfigurationCache.removeConfigListener(null);
95+
// check listener if exist
96+
listeners = configListenersMap.get("service.disableGlobalTransaction");
97+
Assertions.assertTrue(CollectionUtils.isNotEmpty(listeners));
98+
// remove listener
99+
ConfigurationCache.removeConfigListener("service.disableGlobalTransaction", listener);
100+
// check listener if exist
101+
listeners = configListenersMap.get("service.disableGlobalTransaction");
102+
// is empty
103+
Assertions.assertTrue(CollectionUtils.isEmpty(listeners));
104+
}
105+
106+
public static class TestListener implements ConfigurationChangeListener {
107+
108+
@Override
109+
public void onChangeEvent(ConfigurationChangeEvent event) {
110+
Assertions.assertEquals(Boolean.parseBoolean(event.getNewValue()),
111+
!Boolean.parseBoolean(event.getOldValue()));
112+
}
57113
}
58114

59115
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright 1999-2019 Seata.io Group.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.seata.config;
18+
19+
import org.junit.jupiter.api.Assertions;
20+
import org.junit.jupiter.api.Test;
21+
22+
/**
23+
* @author liuqiufeng
24+
*/
25+
class ConfigurationChangeEventTest {
26+
27+
@Test
28+
void getDataId() {
29+
ConfigurationChangeEvent event = new ConfigurationChangeEvent();
30+
event.setDataId("dataId");
31+
Assertions.assertEquals("dataId", event.getDataId());
32+
}
33+
34+
@Test
35+
void getOldValue() {
36+
ConfigurationChangeEvent event = new ConfigurationChangeEvent();
37+
event.setOldValue("oldValue");
38+
Assertions.assertEquals("oldValue", event.getOldValue());
39+
}
40+
41+
@Test
42+
void getNewValue() {
43+
ConfigurationChangeEvent event = new ConfigurationChangeEvent();
44+
event.setNewValue("newValue");
45+
Assertions.assertEquals("newValue", event.getNewValue());
46+
}
47+
48+
@Test
49+
void getChangeType() {
50+
ConfigurationChangeEvent event = new ConfigurationChangeEvent();
51+
event.setChangeType(ConfigurationChangeType.ADD);
52+
Assertions.assertEquals(ConfigurationChangeType.ADD, event.getChangeType());
53+
}
54+
55+
@Test
56+
void getNamespace() {
57+
ConfigurationChangeEvent event = new ConfigurationChangeEvent();
58+
event.setNamespace("namespace");
59+
Assertions.assertEquals("namespace", event.getNamespace());
60+
}
61+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 1999-2019 Seata.io Group.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.seata.config.file;
18+
19+
import org.junit.jupiter.api.Assertions;
20+
import org.junit.jupiter.api.Test;
21+
22+
import java.io.File;
23+
24+
/**
25+
* @author liuqiufeng
26+
*/
27+
class SimpleFileConfigTest {
28+
29+
@Test
30+
void getString() {
31+
SimpleFileConfig config = new SimpleFileConfig();
32+
Assertions.assertEquals(File.pathSeparator, config.getString("path.separator"));
33+
34+
config = new SimpleFileConfig(new File("file.conf"), "");
35+
Assertions.assertEquals("default", config.getString("service.vgroupMapping.default_tx_group"));
36+
37+
config = new SimpleFileConfig(new File("src/test/resources/file"), "file:");
38+
Assertions.assertEquals("default", config.getString("service.vgroupMapping.default_tx_group"));
39+
}
40+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright 1999-2019 Seata.io Group.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.seata.config.file;
18+
19+
import org.junit.jupiter.api.Assertions;
20+
import org.junit.jupiter.api.Test;
21+
22+
import java.io.File;
23+
import java.io.IOException;
24+
25+
26+
/**
27+
* @author liuqiufeng
28+
*/
29+
class YamlFileConfigTest {
30+
31+
@Test
32+
void getString() throws IOException {
33+
Assertions.assertThrows(IllegalArgumentException.class, () -> {
34+
YamlFileConfig config = new YamlFileConfig(new File("registry-test-yaml.yml"), "");
35+
config.getString("registry.type");
36+
});
37+
38+
YamlFileConfig config = new YamlFileConfig(new File("src/test/resources/registry-test-yaml.yml"), "");
39+
Assertions.assertEquals("file", config.getString("registry.type"));
40+
Assertions.assertEquals("file.conf", config.getString("registry.file.name"));
41+
42+
// not exist
43+
Assertions.assertNull(config.getString("registry.null.name"));
44+
Assertions.assertNull(config.getString("null"));
45+
// inner exception
46+
Assertions.assertNull(config.getString(null));
47+
}
48+
}

0 commit comments

Comments
 (0)