[Update] Core 完善 JsonFrameworkDescriptorSerializerTest 单元测试;

[Add] badAuthor-InvalidField-framework.json, badAuthor-MissingField-framework.json, badAuthor-NonObject-framework.json 添加 author 属性错误解析数据;
[Add] badBotCode-NonObject-framework.json 添加 botCode 属性错误解析数据;
[Add] badPlatform-MissingField-Identify-framework.json, badPlatform-MissingField-Name-framework.json, badPlatform-NonObject-framework.json 添加 platform 属性错误解析数据;
[Add] badPluginDependency-NonPrimitive-framework.json, badPluginDependency-NonString-framework.json 添加 pluginDependency 属性错误解析数据;
[Add] JsonFrameworkDescriptorSerializerTest 添加多个异常情况的测试项;
[Add] test-framework.json 添加软错误(不影响解析过程)数据以检查软错误情况;
This commit is contained in:
2020-11-07 16:23:31 +08:00
parent cc1ed81adc
commit ce31b92b25
11 changed files with 373 additions and 11 deletions

View File

@ -19,6 +19,8 @@ package net.lamgc.cgj.bot.framework;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.GsonBuilder; import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import net.lamgc.cgj.bot.framework.message.BotCodeDescriptor; import net.lamgc.cgj.bot.framework.message.BotCodeDescriptor;
import net.lamgc.cgj.bot.framework.util.AuthorJsonSerializer; import net.lamgc.cgj.bot.framework.util.AuthorJsonSerializer;
import net.lamgc.cgj.bot.framework.util.BotCodeDescriptorJsonSerializer; import net.lamgc.cgj.bot.framework.util.BotCodeDescriptorJsonSerializer;
@ -36,17 +38,31 @@ import java.util.regex.Pattern;
public class JsonFrameworkDescriptorSerializerTest { public class JsonFrameworkDescriptorSerializerTest {
private final static Gson gson = getGson();
@Test @Test
public void deserializerTest() throws IOException { public void deserializerTest() throws IOException {
InputStream resource = getClass().getClassLoader().getResourceAsStream("test-framework.json");
if (resource == null) {
Assert.fail("未找到测试用资源: test-framework.json");
}
FrameworkDescriptor descriptor; FrameworkDescriptor descriptor;
try (Reader resourceReader = new BufferedReader(new InputStreamReader(resource, StandardCharsets.UTF_8))) { JsonObject rawObject;
descriptor = getGson().fromJson(resourceReader, DefaultFrameworkDescriptor.class); try (Reader resourceReader = getResourceAsReader("test-framework.json")) {
rawObject = gson.fromJson(resourceReader, JsonObject.class);
descriptor = gson.fromJson(rawObject, DefaultFrameworkDescriptor.class);
} }
assertDescriptor(descriptor);
assertDescriptor(gson.fromJson(gson.toJson(descriptor), DefaultFrameworkDescriptor.class));
}
private static Reader getResourceAsReader(String resourceName) {
InputStream resource = JsonFrameworkDescriptorSerializerTest.class
.getClassLoader().getResourceAsStream(resourceName);
if (resource == null) {
Assert.fail("未找到测试用资源: " + resourceName);
}
return new BufferedReader(new InputStreamReader(resource, StandardCharsets.UTF_8));
}
private void assertDescriptor(FrameworkDescriptor descriptor) {
Assert.assertEquals("cgj-mirai", descriptor.getPluginId()); Assert.assertEquals("cgj-mirai", descriptor.getPluginId());
Assert.assertEquals("test", descriptor.getPluginDescription()); Assert.assertEquals("test", descriptor.getPluginDescription());
Assert.assertEquals("3.0.0-alpha", descriptor.getVersion()); Assert.assertEquals("3.0.0-alpha", descriptor.getVersion());
@ -56,8 +72,8 @@ public class JsonFrameworkDescriptorSerializerTest {
Assert.assertEquals("com.example.FrameworkMain", descriptor.getPluginClass()); Assert.assertEquals("com.example.FrameworkMain", descriptor.getPluginClass());
List<PluginDependency> expectedDependency = new ArrayList<>(); List<PluginDependency> expectedDependency = new ArrayList<>();
expectedDependency.add(new PluginDependency("xxx@1.0.0")); expectedDependency.add(new PluginDependency("RequireDepend@1.0.0"));
expectedDependency.add(new PluginDependency("xxx optional add->?@1.0.0")); expectedDependency.add(new PluginDependency("OptionalDepend?@1.0.0"));
Assert.assertEquals(expectedDependency, descriptor.getDependencies()); Assert.assertEquals(expectedDependency, descriptor.getDependencies());
@ -79,7 +95,69 @@ public class JsonFrameworkDescriptorSerializerTest {
Assert.fail("存在不符的表达式: " + pattern.pattern()); Assert.fail("存在不符的表达式: " + pattern.pattern());
} }
} }
}
@Test(expected = JsonParseException.class)
public void author_missingFieldTest() throws IOException {
try (Reader resourceReader = getResourceAsReader("badAuthor-MissingField-framework.json")) {
gson.fromJson(resourceReader, DefaultFrameworkDescriptor.class);
}
}
@Test(expected = JsonParseException.class)
public void author_invalidFieldTest() throws IOException {
try (Reader resourceReader = getResourceAsReader("badAuthor-InvalidField-framework.json")) {
gson.fromJson(resourceReader, DefaultFrameworkDescriptor.class);
}
}
@Test(expected = JsonParseException.class)
public void author_NonAObjectTest() throws IOException {
try (Reader resourceReader = getResourceAsReader("badAuthor-NonObject-framework.json")) {
gson.fromJson(resourceReader, DefaultFrameworkDescriptor.class);
}
}
@Test(expected = JsonParseException.class)
public void botCode_NonAObjectTest() throws IOException {
try (Reader resourceReader = getResourceAsReader("badBotCode-NonObject-framework.json")) {
gson.fromJson(resourceReader, DefaultFrameworkDescriptor.class);
}
}
@Test(expected = JsonParseException.class)
public void platform_NonAObjectTest() throws IOException {
try (Reader resourceReader = getResourceAsReader("badPlatform-NonObject-framework.json")) {
gson.fromJson(resourceReader, DefaultFrameworkDescriptor.class);
}
}
@Test(expected = JsonParseException.class)
public void platform_MissingNameFieldTest() throws IOException {
try (Reader resourceReader = getResourceAsReader("badPlatform-MissingField-Name-framework.json")) {
gson.fromJson(resourceReader, DefaultFrameworkDescriptor.class);
}
}
@Test(expected = JsonParseException.class)
public void platform_MissingIdentifyFieldTest() throws IOException {
try (Reader resourceReader = getResourceAsReader("badPlatform-MissingField-Identify-framework.json")) {
gson.fromJson(resourceReader, DefaultFrameworkDescriptor.class);
}
}
@Test(expected = JsonParseException.class)
public void pluginDependency_NonPrimitiveTest() throws IOException {
try (Reader resourceReader = getResourceAsReader("badPluginDependency-NonPrimitive-framework.json")) {
gson.fromJson(resourceReader, DefaultFrameworkDescriptor.class);
}
}
@Test(expected = JsonParseException.class)
public void pluginDependency_NonStringTest() throws IOException {
try (Reader resourceReader = getResourceAsReader("badPluginDependency-NonString-framework.json")) {
gson.fromJson(resourceReader, DefaultFrameworkDescriptor.class);
}
} }
private static Gson getGson() { private static Gson getGson() {

View File

@ -0,0 +1,33 @@
// 该文件为 framework.json 格式完整示例, 非测试用文件.
{
"id": "cgj-mirai",
"description": "test",
"version": "3.0.0-alpha",
"requiresVersion": "=>3.0.0",
"provider": "Github@LamGC, Github@mamoe",
"license": "AGPL-3.0",
"frameworkClass": "com.example.FrameworkMain",
"dependencies": [
"RequireDepend@1.0.0",
"OptionalDepend?@1.0.0"
],
"platform": {
"name": "Tencent QQ",
"identify": "qq"
},
"authors": [
{
"name": [
"invalid array"
]
}
],
"botCode": {
"patterns": [
"(?:\\[mirai:([^:]+)\\])",
"(?:\\[mirai:([^\\]]*)?:(.*?)?\\])",
"(?:\\[mirai:([^\\]]*)?(:(.*?))*?\\])"
]
}
}

View File

@ -0,0 +1,31 @@
// 该文件为 framework.json 格式完整示例, 非测试用文件.
{
"id": "cgj-mirai",
"description": "test",
"version": "3.0.0-alpha",
"requiresVersion": "=>3.0.0",
"provider": "Github@LamGC, Github@mamoe",
"license": "AGPL-3.0",
"frameworkClass": "com.example.FrameworkMain",
"dependencies": [
"RequireDepend@1.0.0",
"OptionalDepend?@1.0.0"
],
"platform": {
"name": "Tencent QQ",
"identify": "qq"
},
"authors": [
{
"desc": "Missing name field"
}
],
"botCode": {
"patterns": [
"(?:\\[mirai:([^:]+)\\])",
"(?:\\[mirai:([^\\]]*)?:(.*?)?\\])",
"(?:\\[mirai:([^\\]]*)?(:(.*?))*?\\])"
]
}
}

View File

@ -0,0 +1,29 @@
// 该文件为 framework.json 格式完整示例, 非测试用文件.
{
"id": "cgj-mirai",
"description": "test",
"version": "3.0.0-alpha",
"requiresVersion": "=>3.0.0",
"provider": "Github@LamGC, Github@mamoe",
"license": "AGPL-3.0",
"frameworkClass": "com.example.FrameworkMain",
"dependencies": [
"RequireDepend@1.0.0",
"OptionalDepend?@1.0.0"
],
"platform": {
"name": "Tencent QQ",
"identify": "qq"
},
"authors": [
"invalid data"
],
"botCode": {
"patterns": [
"(?:\\[mirai:([^:]+)\\])",
"(?:\\[mirai:([^\\]]*)?:(.*?)?\\])",
"(?:\\[mirai:([^\\]]*)?(:(.*?))*?\\])"
]
}
}

View File

@ -0,0 +1,27 @@
// 该文件为 framework.json 格式完整示例, 非测试用文件.
{
"id": "cgj-mirai",
"description": "test",
"version": "3.0.0-alpha",
"requiresVersion": "=>3.0.0",
"provider": "Github@LamGC, Github@mamoe",
"license": "AGPL-3.0",
"frameworkClass": "com.example.FrameworkMain",
"dependencies": [
"RequireDepend@1.0.0",
"OptionalDepend?@1.0.0"
],
"platform": {
"name": "Tencent QQ",
"identify": "qq"
},
"authors": [
{
"name": "LamGC",
"url": "https://github.com/LamGC",
"email": "lam827@lamgc.net"
}
],
"botCode": "invalid value"
}

View File

@ -0,0 +1,32 @@
// 该文件为 framework.json 格式完整示例, 非测试用文件.
{
"id": "cgj-mirai",
"description": "test",
"version": "3.0.0-alpha",
"requiresVersion": "=>3.0.0",
"provider": "Github@LamGC, Github@mamoe",
"license": "AGPL-3.0",
"frameworkClass": "com.example.FrameworkMain",
"dependencies": [
"RequireDepend@1.0.0",
"OptionalDepend?@1.0.0"
],
"platform": {
"name": "Tencent QQ"
},
"authors": [
{
"name": "LamGC",
"url": "https://github.com/LamGC",
"email": "lam827@lamgc.net"
}
],
"botCode": {
"patterns": [
"(?:\\[mirai:([^:]+)\\])",
"(?:\\[mirai:([^\\]]*)?:(.*?)?\\])",
"(?:\\[mirai:([^\\]]*)?(:(.*?))*?\\])"
]
}
}

View File

@ -0,0 +1,32 @@
// 该文件为 framework.json 格式完整示例, 非测试用文件.
{
"id": "cgj-mirai",
"description": "test",
"version": "3.0.0-alpha",
"requiresVersion": "=>3.0.0",
"provider": "Github@LamGC, Github@mamoe",
"license": "AGPL-3.0",
"frameworkClass": "com.example.FrameworkMain",
"dependencies": [
"RequireDepend@1.0.0",
"OptionalDepend?@1.0.0"
],
"platform": {
"identify": "qq"
},
"authors": [
{
"name": "LamGC",
"url": "https://github.com/LamGC",
"email": "lam827@lamgc.net"
}
],
"botCode": {
"patterns": [
"(?:\\[mirai:([^:]+)\\])",
"(?:\\[mirai:([^\\]]*)?:(.*?)?\\])",
"(?:\\[mirai:([^\\]]*)?(:(.*?))*?\\])"
]
}
}

View File

@ -0,0 +1,30 @@
// 该文件为 framework.json 格式完整示例, 非测试用文件.
{
"id": "cgj-mirai",
"description": "test",
"version": "3.0.0-alpha",
"requiresVersion": "=>3.0.0",
"provider": "Github@LamGC, Github@mamoe",
"license": "AGPL-3.0",
"frameworkClass": "com.example.FrameworkMain",
"dependencies": [
"RequireDepend@1.0.0",
"OptionalDepend?@1.0.0"
],
"platform": "invalid value",
"authors": [
{
"name": "LamGC",
"url": "https://github.com/LamGC",
"email": "lam827@lamgc.net"
}
],
"botCode": {
"patterns": [
"(?:\\[mirai:([^:]+)\\])",
"(?:\\[mirai:([^\\]]*)?:(.*?)?\\])",
"(?:\\[mirai:([^\\]]*)?(:(.*?))*?\\])"
]
}
}

View File

@ -0,0 +1,34 @@
// 该文件为 framework.json 格式完整示例, 非测试用文件.
{
"id": "cgj-mirai",
"description": "test",
"version": "3.0.0-alpha",
"requiresVersion": "=>3.0.0",
"provider": "Github@LamGC, Github@mamoe",
"license": "AGPL-3.0",
"frameworkClass": "com.example.FrameworkMain",
"dependencies": [
{
"desc": "Error type"
}
],
"platform": {
"name": "Tencent QQ",
"identify": "qq"
},
"authors": [
{
"name": "LamGC",
"url": "https://github.com/LamGC",
"email": "lam827@lamgc.net"
}
],
"botCode": {
"patterns": [
"(?:\\[mirai:([^:]+)\\])",
"(?:\\[mirai:([^\\]]*)?:(.*?)?\\])",
"(?:\\[mirai:([^\\]]*)?(:(.*?))*?\\])"
]
}
}

View File

@ -0,0 +1,32 @@
// 该文件为 framework.json 格式完整示例, 非测试用文件.
{
"id": "cgj-mirai",
"description": "test",
"version": "3.0.0-alpha",
"requiresVersion": "=>3.0.0",
"provider": "Github@LamGC, Github@mamoe",
"license": "AGPL-3.0",
"frameworkClass": "com.example.FrameworkMain",
"dependencies": [
12345
],
"platform": {
"name": "Tencent QQ",
"identify": "qq"
},
"authors": [
{
"name": "LamGC",
"url": "https://github.com/LamGC",
"email": "lam827@lamgc.net"
}
],
"botCode": {
"patterns": [
"(?:\\[mirai:([^:]+)\\])",
"(?:\\[mirai:([^\\]]*)?:(.*?)?\\])",
"(?:\\[mirai:([^\\]]*)?(:(.*?))*?\\])"
]
}
}

View File

@ -7,8 +7,8 @@
"license": "AGPL-3.0", "license": "AGPL-3.0",
"frameworkClass": "com.example.FrameworkMain", "frameworkClass": "com.example.FrameworkMain",
"dependencies": [ "dependencies": [
"xxx@1.0.0", "RequireDepend@1.0.0",
"xxx optional add->?@1.0.0" "OptionalDepend?@1.0.0"
], ],
"platform": { "platform": {
@ -26,7 +26,11 @@
"patterns": [ "patterns": [
"(?:\\[mirai:([^:]+)\\])", "(?:\\[mirai:([^:]+)\\])",
"(?:\\[mirai:([^\\]]*)?:(.*?)?\\])", "(?:\\[mirai:([^\\]]*)?:(.*?)?\\])",
"(?:\\[mirai:([^\\]]*)?(:(.*?))*?\\])" "(?:\\[mirai:([^\\]]*)?(:(.*?))*?\\])",
{
"tips": "invalid data"
},
123456
] ]
} }
} }