test[generate]: generate test

This commit is contained in:
godotg committed 2023-09-03 12:02:47 +08:00
1 parent 0f099de567
commit 3935f1e303
6 files changed
+86 -35

No files matched your search

@@ -20,7 +20,8 @@ import java.util.EnumSet;
import java.util.Set;
/**
* 创建协议文件的操作类
* EN: Action class for creating protocol files
* CN: 创建协议文件的操作类
*
* @author godotg
* @version 3.0
@@ -28,27 +29,33 @@ import java.util.Set;
public class GenerateOperation {
/**
* 不创建任何协议文件
* EN: No protocol file is created
* CN: 不创建任何协议文件
*/
public static final GenerateOperation NO_OPERATION = new GenerateOperation();
/**
* 折叠协议,生成协议文件会和Java源文件保持相同的目录结构,默认不折叠
* EN: Fold the protocol. The generated protocol file will maintain the same directory structure as the Java source file.
* It will not be folded by default.
* <p>
* CN: 折叠协议,生成协议文件会和Java源文件保持相同的目录结构,默认不折叠
*/
private boolean foldProtocol;
/**
* 生成协议的路径,默认为当前运行项目的路径
* EN: The path to generate the protocol, the default is the path of the currently running project
* CN: 生成协议的路径,默认为当前运行项目的路径
*/
private String protocolPath = StringUtils.EMPTY;
/**
* 保留参数
* Reserved parameters,保留参数
*/
private String protocolParam;
/**
* 需要生成的协议文件(c#、js、ts...等需要生成协议的语言都列出来)
* EN: The protocol files that need to be generated (c#, js, ts... and other languages that need to generate the protocol are listed)
* CN: 需要生成的协议文件(c#、js、ts...等需要生成协议的语言都列出来)
*/
private final Set<CodeLanguage> generateLanguages = EnumSet.noneOf(CodeLanguage.class);
@@ -47,7 +47,8 @@ import static com.zfoo.protocol.util.StringUtils.TAB_ASCII;
public abstract class GenerateProtocolFile {
/**
* 生成协议的过滤器,默认不过滤
* EN: Filter for generating protocol files. If no filter is set, all protocols will be generated by default.
* CN: 生成协议文件的过滤器,如果没有设置过滤器默认生成全部的协议
*/
public static Predicate<IProtocolRegistration> generateProtocolFilter = registration -> true;
@@ -15,7 +15,6 @@ package com.zfoo.protocol.generate;
import com.zfoo.protocol.collection.CollectionUtils;
import com.zfoo.protocol.collection.tree.GeneralTree;
import com.zfoo.protocol.collection.tree.TreeNode;
import com.zfoo.protocol.exception.RunException;
import com.zfoo.protocol.registration.IProtocolRegistration;
import com.zfoo.protocol.serializer.CodeLanguage;
import com.zfoo.protocol.serializer.enhance.EnhanceObjectProtocolSerializer;
@@ -26,14 +25,18 @@ import java.util.*;
import java.util.stream.Collectors;
/**
* 生成协议的时候,协议的最终生成路径会使用这个类
* EN: When generating a protocol, the final generation path of the protocol will use this class.
* CN: 生成协议的时候,协议的最终生成路径会使用这个类
*
* @author godotg
* @version 3.0
*/
public abstract class GenerateProtocolPath {
// 临时变量,启动完成就会销毁,协议生成的路径
/**
* EN: The paths and temporary variables generated by the protocol will be destroyed after startup is completed.
* CN: 协议生成的路径,临时变量,启动完成就会销毁
*/
private static Map<Short, String> protocolPathMap = new HashMap<>();
@@ -73,13 +76,11 @@ public abstract class GenerateProtocolPath {
path = StringUtils.PERIOD;
} else {
path = StringUtils.substringBeforeLast(path, StringUtils.SLASH);
path = path.replaceAll(StringUtils.SLASH , StringUtils.PERIOD);
path = path.replaceAll(StringUtils.SLASH, StringUtils.PERIOD);
}
break;
case Enhance:
break;
default:
throw new RunException("无法识别的枚举类型[{}]", language);
}
return path;
}
@@ -88,7 +89,9 @@ public abstract class GenerateProtocolPath {
* 获取协议生成的路径
*/
public static String getProtocolPath(short protocolId) {
AssertionUtils.notNull(protocolPathMap, "[{}]已经初始完成,初始化完成过后不能调用getProtocolPath", GenerateProtocolPath.class.getSimpleName());
AssertionUtils.notNull(protocolPathMap
, "[{}]The initialization has been completed. Get Protocol Path cannot be called after the initialization is completed."
, GenerateProtocolPath.class.getSimpleName());
var protocolPath = protocolPathMap.get(protocolId);
if (StringUtils.isBlank(protocolPath)) {
@@ -137,7 +140,9 @@ public abstract class GenerateProtocolPath {
* @param protocolRegistrations 需要解析的路径
*/
public static void initProtocolPath(List<IProtocolRegistration> protocolRegistrations) {
AssertionUtils.notNull(protocolPathMap, "[{}]已经初始完成,初始化完成过后不能调用initProtocolPath", GenerateProtocolPath.class.getSimpleName());
AssertionUtils.notNull(protocolPathMap
, "[{}]The initialization has been completed. Get Protocol Path cannot be called after the initialization is completed."
, GenerateProtocolPath.class.getSimpleName());
// 将需要生成的协议的路径添加到多叉树中
var protocolPathTree = new GeneralTree<IProtocolRegistration>();
@@ -181,7 +186,7 @@ public abstract class GenerateProtocolPath {
for (var child : allChildren) {
var protocolSimpleName = child.getData().protocolConstructor().getDeclaringClass().getSimpleName();
var splits = Arrays.stream(StringUtils.substringBeforeLast(StringUtils.substringAfterFirst(child.fullName(), pathBefore), protocolSimpleName)
.split(StringUtils.PERIOD_REGEX))
.split(StringUtils.PERIOD_REGEX))
.filter(it -> StringUtils.isNotBlank(it))
.toArray();
protocolPathMap.put(child.getData().protocolId(), StringUtils.joinWith(StringUtils.PERIOD, splits));
@@ -23,7 +23,6 @@ import com.google.protobuf.CodedOutputStream;
import com.zfoo.protocol.collection.ArrayUtils;
import com.zfoo.protocol.generate.GenerateOperation;
import com.zfoo.protocol.packet.*;
import com.zfoo.protocol.serializer.CodeLanguage;
import com.zfoo.protocol.util.StringUtils;
import io.fury.Fury;
import io.fury.Language;
@@ -358,23 +357,8 @@ public class SpeedTest {
};
static {
var op = GenerateOperation.NO_OPERATION;
// 这行加上,会在protocol目录下,生成jsProtocol文件夹及其对应的js协议文件
// op.getGenerateLanguages().add(CodeLanguage.Cpp);
// op.getGenerateLanguages().add(CodeLanguage.JavaScript);
// op.getGenerateLanguages().add(CodeLanguage.TypeScript);
// op.getGenerateLanguages().add(CodeLanguage.Lua);
// op.getGenerateLanguages().add(CodeLanguage.CSharp);
// op.getGenerateLanguages().add(CodeLanguage.GdScript);
op.getGenerateLanguages().add(CodeLanguage.Python);
// 需要protocol协议的字段里面都加上JProtobuf注解才能用
// op.setProtocolParam("protobuf=protobuf.xml");
// op.getGenerateLanguages().add(CodeLanguage.Protobuf);
// zfoo协议注册(其实就是:将Set里面的协议号和对应的类注册好,这样子就可以根据协议号知道是反序列化为哪个类)
ProtocolManager.initProtocolAuto(Set.of(ComplexObject.class, NormalObject.class, SimpleObject.class, EmptyObject.class, VeryBigObject.class), op);
ProtocolManager.initProtocolAuto(Set.of(ComplexObject.class, NormalObject.class, SimpleObject.class, EmptyObject.class, VeryBigObject.class), GenerateOperation.NO_OPERATION);
for (int i = 0; i < executors.length; i++) {
executors[i] = Executors.newSingleThreadExecutor();
@@ -35,9 +35,9 @@ public class CompatiblityTest {
/**
* EN: The order of the bytecode-enhanced Map traversal order will be different, so the order of the serialized content will change.
* You can see that the different ones are not consecutive.
* You can see that the different bytes are not consecutive.
* <p>
* CN: 字节码增强的Map遍历顺序会出现不一样,所以序列化的内容顺序会改变,可以看到不相同的直接并不是连续的
* CN: 字节码增强的Map遍历顺序会出现不一样,所以序列化的内容顺序会改变,可以看到不相同的字节并不是连续的
*/
@Ignore
@Test
@@ -0,0 +1,54 @@
/*
* Copyright (C) 2020 The zfoo Authors
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and limitations under the License.
*/
package com.zfoo.protocol.generate;
import com.zfoo.protocol.ProtocolManager;
import com.zfoo.protocol.packet.*;
import com.zfoo.protocol.serializer.CodeLanguage;
import org.junit.Ignore;
import org.junit.Test;
import java.util.Set;
/**
* @author godotg
*/
@Ignore
public class GenerateTest {
@Test
public void generateAllProtocols() {
var op = GenerateOperation.NO_OPERATION;
op.setFoldProtocol(true);
var generateLanguages = op.getGenerateLanguages();
// generate the jsProtocol folder and its corresponding js protocol file in the current protocol directory
generateLanguages.add(CodeLanguage.Cpp);
generateLanguages.add(CodeLanguage.Go);
generateLanguages.add(CodeLanguage.JavaScript);
generateLanguages.add(CodeLanguage.TypeScript);
generateLanguages.add(CodeLanguage.Lua);
generateLanguages.add(CodeLanguage.CSharp);
generateLanguages.add(CodeLanguage.GdScript);
generateLanguages.add(CodeLanguage.Python);
// Protobuf needs to specify protobuf.xml to generate a protocol
op.setProtocolParam("protobuf=protobuf.xml");
generateLanguages.add(CodeLanguage.Protobuf);
// Initialize and then generate the protocol
ProtocolManager.initProtocolAuto(Set.of(ComplexObject.class, NormalObject.class, SimpleObject.class, EmptyObject.class, VeryBigObject.class), op);
}
}