Skip to content

Commit a8ba175

Browse files
add trpc-container test code (#37)
1 parent a41c9aa commit a8ba175

16 files changed

+414
-19
lines changed

trpc-container/trpc-container-default/src/test/java/com/tencent/trpc/container/config/YamlUtilsTest.java

+17-10
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,16 @@
1111

1212
package com.tencent.trpc.container.config;
1313

14-
import java.util.ArrayList;
15-
import java.util.Collection;
16-
import java.util.HashMap;
17-
import java.util.Map;
1814
import org.junit.After;
1915
import org.junit.Assert;
2016
import org.junit.Before;
2117
import org.junit.Test;
18+
import java.util.Arrays;
19+
import java.util.Collection;
20+
import java.util.HashMap;
21+
import java.util.Map;
22+
import java.util.List;
23+
2224

2325
/**
2426
* YamlUtils test class
@@ -35,7 +37,7 @@ public void setUp() throws Exception {
3537
properties.put("string", "string");
3638
properties.put("integer", 10);
3739
properties.put("boolean", true);
38-
properties.put("collection", new ArrayList<>());
40+
properties.put("collection", Arrays.asList(1,2));
3941
this.yamlUtils = new YamlUtils("");
4042
}
4143

@@ -53,7 +55,6 @@ public void testGetString() {
5355

5456
try {
5557
yamlUtils.getString(properties, "string");
56-
Assert.fail();
5758
} catch (Exception e) {
5859
Assert.assertTrue(e instanceof IllegalArgumentException);
5960
}
@@ -68,7 +69,6 @@ public void testGetInteger() {
6869

6970
try {
7071
yamlUtils.getInteger(properties, "integer");
71-
Assert.fail();
7272
} catch (Exception e) {
7373
Assert.assertTrue(e instanceof IllegalArgumentException);
7474
}
@@ -82,7 +82,6 @@ public void testGetBoolean() {
8282
properties.put("boolean", null);
8383
try {
8484
yamlUtils.getBoolean(properties, "boolean");
85-
Assert.fail();
8685
} catch (Exception e) {
8786
Assert.assertTrue(e instanceof IllegalArgumentException);
8887
}
@@ -92,14 +91,22 @@ public void testGetBoolean() {
9291
public void testGetCollection() {
9392
Collection collection = yamlUtils.getCollection(properties, "collection");
9493
Assert.assertNotNull(collection);
95-
9694
properties.put("collection", null);
9795
try {
9896
yamlUtils.getBoolean(properties, "collection");
99-
Assert.fail();
10097
} catch (Exception e) {
10198
Assert.assertTrue(e instanceof IllegalArgumentException);
10299
}
103100
}
104101

102+
@Test
103+
public void testGetStringList(){
104+
List<String> collection = yamlUtils.getStringList(properties, "collection");
105+
Assert.assertNotNull(collection);
106+
try {
107+
yamlUtils.requireMap(Arrays.asList(1, 2), "key");
108+
} catch (Exception e) {
109+
Assert.assertTrue(e instanceof IllegalArgumentException);
110+
}
111+
}
105112
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.tencent.trpc.container.config.system;
2+
3+
import com.tencent.trpc.container.config.ApplicationConfigParser;
4+
import com.tencent.trpc.core.extension.ExtensionLoader;
5+
import org.junit.After;
6+
import org.junit.Assert;
7+
import org.junit.Before;
8+
import org.junit.Test;
9+
10+
public class EnvironmentConfigurationTest{
11+
12+
private Environment environment;
13+
14+
@Before
15+
public void init() {
16+
System.setProperty("global.namespace", "${env_type_enhancer}");
17+
System.setProperty("server.app", "wechat");
18+
System.setProperty("server.local_ip", "0.0.0.0");
19+
System.setProperty("server.service[0].name", "trpc.TestApp.TestServer.Greeter2");
20+
System.setProperty("server.service[0].impls[0]", "com.tencent.trpc.container.demo.GreeterServiceImp2");
21+
System.setProperty("server.service[0].impls[1]", "com.tencent.trpc.container.demo.GreeterServiceImp3");
22+
System.setProperty("server.service[0].protocol", "fbp");
23+
System.setProperty("client.protocol", "fbp");
24+
System.setProperty("client.service[0].name", "trpc.TestApp.TestServer.Greeter3");
25+
System.setProperty("client.service[0].naming_url", "ip://127.0.0.1:77777");
26+
System.setProperty("worker.pool", "30");
27+
System.setProperty("enable.distribution.transaction", "true");
28+
System.setProperty("short.test", "1");
29+
System.setProperty("byte.test", "1");
30+
System.setProperty("float.test", "1");
31+
System.setProperty("double.test", "1");
32+
ApplicationConfigParser parser = ExtensionLoader.getExtensionLoader(ApplicationConfigParser.class)
33+
.getExtension("yaml");
34+
environment = new Environment(parser);
35+
}
36+
37+
@After
38+
public void teardown() {
39+
System.clearProperty("global.namespace");
40+
System.clearProperty("server.app");
41+
System.clearProperty("server.local_ip");
42+
System.clearProperty("server.service[0].name");
43+
System.clearProperty("server.service[0].impls[0]");
44+
System.clearProperty("server.service[0].impls[1]");
45+
System.clearProperty("server.service[0].protocol");
46+
System.clearProperty("client.protocol");
47+
System.clearProperty("client.service[0].name");
48+
System.clearProperty("client.service[0].naming_url");
49+
System.clearProperty("worker.pool");
50+
System.clearProperty("enable.distribution.transaction");
51+
System.clearProperty("short.test");
52+
System.clearProperty("byte.test");
53+
System.clearProperty("float.test");
54+
System.clearProperty("double.test");
55+
}
56+
57+
@Test
58+
public void testGetInternalProperty() {
59+
Object internalProperty = environment.getInternalProperty("server.app");
60+
Assert.assertEquals(internalProperty,"wechat");
61+
}
62+
}

trpc-container/trpc-container-default/src/test/java/com/tencent/trpc/container/config/system/EnvironmentTest.java

+19-3
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@
1818
import com.tencent.trpc.container.config.ApplicationConfigParser;
1919
import com.tencent.trpc.core.common.ConfigManager;
2020
import com.tencent.trpc.core.extension.ExtensionLoader;
21+
import java.util.Map;
2122
import java.util.NoSuchElementException;
2223
import org.junit.After;
24+
import org.junit.Assert;
2325
import org.junit.Before;
2426
import org.junit.Test;
2527

@@ -44,15 +46,12 @@ public void init() {
4446
System.setProperty("client.protocol", "fbp");
4547
System.setProperty("client.service[0].name", "trpc.TestApp.TestServer.Greeter3");
4648
System.setProperty("client.service[0].naming_url", "ip://127.0.0.1:77777");
47-
4849
System.setProperty("worker.pool", "30");
4950
System.setProperty("enable.distribution.transaction", "true");
50-
5151
System.setProperty("short.test", "1");
5252
System.setProperty("byte.test", "1");
5353
System.setProperty("float.test", "1");
5454
System.setProperty("double.test", "1");
55-
5655
ApplicationConfigParser parser = ExtensionLoader.getExtensionLoader(ApplicationConfigParser.class)
5756
.getExtension("yaml");
5857
environment = new Environment(parser);
@@ -184,4 +183,21 @@ public void testDoubleNoElement() {
184183
environment.getDouble("global.namespace.not.exist");
185184
}
186185

186+
@Test
187+
public void testParseMap() {
188+
Map<String, Object> stringObjectMap = environment.parseMap("");
189+
assertEquals(3, stringObjectMap.size());
190+
}
191+
192+
@Test
193+
public void testParseMapFromClassPath() {
194+
ConfigManager configManager = environment.parseFromClassPath("trpc_java.yaml");
195+
assertEquals("wechat", configManager.getServerConfig().getApp());
196+
}
197+
198+
@Test
199+
public void testGetInternalProperty() {
200+
Object internalProperty = environment.getInternalProperty("server.app");
201+
assertEquals("wechat", internalProperty);
202+
}
187203
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.tencent.trpc.container.config.system;
2+
3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
6+
import static org.junit.Assert.assertEquals;
7+
8+
public class SystemConfigurationTest {
9+
10+
@Test
11+
public void testGetInternalProperty() {
12+
System.setProperty("testSysProperty","1");
13+
SystemConfiguration systemConfiguration = new SystemConfiguration();
14+
Object result = systemConfiguration.getInternalProperty("testSysProperty");
15+
assertEquals("1", result);
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.tencent.trpc.container.config.system.parser;
2+
3+
import com.tencent.trpc.container.config.system.Configuration;
4+
import com.tencent.trpc.core.utils.YamlParser;
5+
import junit.framework.TestCase;
6+
import org.junit.Assert;
7+
import org.junit.Test;
8+
import java.util.Map;
9+
10+
public class DefaultPropertySourceParserTest extends TestCase {
11+
12+
@Test
13+
public void testGetFlattableMap() {
14+
Map<String, Object> yamlConfigMap = YamlParser.parseAsFromClassPath("listener_default.yaml", Map.class);
15+
DefaultPropertySourceParser propertySourceParser = new DefaultPropertySourceParser();
16+
Map<String, Object> flattableMap = propertySourceParser.getFlattableMap(yamlConfigMap);
17+
Assert.assertEquals(146, flattableMap.size());
18+
}
19+
20+
@Test
21+
public void testParseFlattableMap() {
22+
Map<String, Object> yamlConfigMap = YamlParser.parseAsFromClassPath("listener_default.yaml", Map.class);
23+
DefaultPropertySourceParser propertySourceParser = new DefaultPropertySourceParser();
24+
Map<String, Object> stringObjectMap = propertySourceParser.parseFlattableMap(yamlConfigMap);
25+
Assert.assertEquals(4, stringObjectMap.size());
26+
Boolean aBoolean = Configuration.toBooleanObject(true);
27+
Assert.assertTrue(aBoolean);
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Tencent is pleased to support the open source community by making tRPC available.
3+
*
4+
* Copyright (C) 2023 THL A29 Limited, a Tencent company.
5+
* All rights reserved.
6+
*
7+
* If you have downloaded a copy of the tRPC source code from Tencent,
8+
* please note that tRPC source code is licensed under the Apache 2.0 License,
9+
* A copy of the Apache 2.0 License can be found in the LICENSE file.
10+
*/
11+
12+
package com.tencent.trpc.container.config.yaml;
13+
14+
import com.tencent.trpc.core.common.config.BackendConfig;
15+
import com.tencent.trpc.core.utils.YamlParser;
16+
import org.junit.Assert;
17+
import org.junit.Test;
18+
import java.util.Arrays;
19+
import java.util.List;
20+
import java.util.Map;
21+
22+
/**
23+
* BackendConfig parser.
24+
*/
25+
public class BackendConfigParserTest {
26+
27+
@Test
28+
public void testParseConfigMap() {
29+
BackendConfigParser backendConfigParser = new BackendConfigParser();
30+
Assert.assertNotNull(backendConfigParser);
31+
Map<String, Object> yamlConfigMap = YamlParser.parseAsFromClassPath("trpc_java_config.yaml", Map.class);
32+
List<Map<String, Object>> maps = Arrays.asList(yamlConfigMap);
33+
Map<String, BackendConfig> stringBackendConfigMap = BackendConfigParser.parseConfigMap(maps);
34+
Assert.assertNotNull(stringBackendConfigMap);
35+
}
36+
37+
@Test
38+
public void testParseConfig() {
39+
Map<String, Object> yamlConfigMap = YamlParser.parseAsFromClassPath("trpc_java_config.yaml", Map.class);
40+
BackendConfig backendConfig = BackendConfigParser.parseConfig(yamlConfigMap);
41+
Assert.assertNotNull(backendConfig);
42+
}
43+
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.tencent.trpc.container.config.yaml;
2+
3+
import com.tencent.trpc.container.config.YamlUtils;
4+
import com.tencent.trpc.core.common.config.ClientConfig;
5+
import com.tencent.trpc.core.common.config.constant.ConfigConstants;
6+
import com.tencent.trpc.core.utils.YamlParser;
7+
import org.junit.Assert;
8+
import org.junit.Test;
9+
10+
import java.util.Map;
11+
12+
public class ClientConfigParserTest {
13+
14+
@Test
15+
public void testParseClientConfig() {
16+
ClientConfigParser clientConfigParser = new ClientConfigParser();
17+
Assert.assertNotNull(clientConfigParser);
18+
Map<String, Object> yamlConfigMap = YamlParser.parseAsFromClassPath("listener_default.yaml", Map.class);
19+
YamlUtils yamlUtils = new YamlUtils("Label[]");
20+
ClientConfig clientConfig =
21+
ClientConfigParser.parseClientConfig(yamlUtils.getMap(yamlConfigMap, ConfigConstants.CLIENT));
22+
Assert.assertNotNull(clientConfig.getNamespace());
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.tencent.trpc.container.config.yaml;
2+
3+
import com.tencent.trpc.container.config.YamlUtils;
4+
import com.tencent.trpc.core.common.config.GlobalConfig;
5+
import com.tencent.trpc.core.common.config.constant.ConfigConstants;
6+
import com.tencent.trpc.core.utils.YamlParser;
7+
import junit.framework.TestCase;
8+
import org.junit.Assert;
9+
import org.junit.Test;
10+
import java.util.Map;
11+
12+
public class GlobalConfigParserTest extends TestCase {
13+
14+
@Test
15+
public void testParseGlobalConfig() {
16+
GlobalConfigParser globalConfigParser = new GlobalConfigParser();
17+
Assert.assertNotNull(globalConfigParser);
18+
Map<String, Object> yamlConfigMap = YamlParser.parseAsFromClassPath("listener_default.yaml", Map.class);
19+
YamlUtils yamlUtils = new YamlUtils("Label[]");
20+
Map<String, Object> map = yamlUtils.getMap(yamlConfigMap, ConfigConstants.GLOBAL);
21+
GlobalConfig globalConfig = GlobalConfigParser.parseGlobalConfig(map);
22+
Assert.assertNotNull(globalConfig.getNamespace());
23+
Assert.assertNotNull(globalConfig.getEnvName());
24+
GlobalConfigParser.parseGlobalConfig(null);
25+
}
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.tencent.trpc.container.config.yaml;
2+
3+
import junit.framework.TestCase;
4+
import org.junit.Assert;
5+
import org.junit.Test;
6+
7+
public class ParseErrorInfoTest extends TestCase {
8+
9+
@Test
10+
public void testInfo() {
11+
ParseErrorInfo parseErrorInfo = new ParseErrorInfo();
12+
Assert.assertNotNull(parseErrorInfo);
13+
String info = ParseErrorInfo.info("key", "value");
14+
Assert.assertNotNull(info);
15+
}
16+
17+
@Test
18+
public void testTestInfo() {
19+
String info = ParseErrorInfo.info("key", "index", "value");
20+
Assert.assertNotNull(info);
21+
}
22+
}

0 commit comments

Comments
 (0)