perf[protocol]: 规范test测试用例

This commit is contained in:
jaysunxiao
2021-07-16 23:41:16 +08:00
parent 70ed832c7a
commit 7bdc35dfce
15 changed files with 193 additions and 231 deletions
@@ -23,8 +23,8 @@ import com.zfoo.protocol.exception.ExceptionUtils;
import com.zfoo.protocol.generate.GenerateOperation;
import com.zfoo.protocol.generate.GenerateProtocolFile;
import com.zfoo.protocol.registration.IProtocolRegistration;
import com.zfoo.protocol.util.DomUtils;
import com.zfoo.protocol.xml.XmlProtocols;
import com.zfoo.util.DomUtils;
import io.netty.buffer.ByteBuf;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -22,8 +22,8 @@ import com.zfoo.net.packet.service.PacketService;
import com.zfoo.net.session.manager.SessionManager;
import com.zfoo.protocol.ProtocolManager;
import com.zfoo.protocol.registration.ProtocolModule;
import com.zfoo.protocol.util.DomUtils;
import com.zfoo.protocol.util.StringUtils;
import com.zfoo.util.DomUtils;
import org.springframework.beans.factory.config.BeanDefinitionHolder;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
@@ -18,8 +18,8 @@ import com.zfoo.orm.manager.OrmManager;
import com.zfoo.orm.model.accessor.MongodbAccessor;
import com.zfoo.orm.model.config.*;
import com.zfoo.orm.model.query.MongodbQuery;
import com.zfoo.protocol.util.DomUtils;
import com.zfoo.protocol.util.StringUtils;
import com.zfoo.util.DomUtils;
import org.springframework.beans.factory.config.BeanDefinitionHolder;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
@@ -1,6 +1,5 @@
/*
* 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
*
@@ -9,21 +8,18 @@
* 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.util;
package com.zfoo.protocol.util;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.zfoo.protocol.exception.ExceptionUtils;
import com.zfoo.protocol.util.AssertionUtils;
import com.zfoo.protocol.util.StringUtils;
import com.zfoo.protocol.exception.RunException;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
@@ -53,17 +49,17 @@ public abstract class DomUtils {
try {
return MAPPER.readValue(xml, clazz);
} catch (IOException e) {
throw new RuntimeException(StringUtils.format("将xml字符串[xml:{}]转换为对象[class:{}]异常[error:{}]", xml, clazz, ExceptionUtils.getStackTrace(e)));
throw new RunException(e, "将xml字符串[xml:{}]转换为对象[{}]异常", xml, clazz);
}
}
public static <T> T file2Object(File xmlFile, Class<T> clazz) {
try {
XMLInputFactory f = XMLInputFactory.newFactory();
XMLStreamReader sr = f.createXMLStreamReader(new FileInputStream(xmlFile));
var f = XMLInputFactory.newFactory();
var sr = f.createXMLStreamReader(new FileInputStream(xmlFile));
return MAPPER.readValue(sr, clazz);
} catch (Exception e) {
throw new RuntimeException(StringUtils.format("将xml文件[xml:{}]转换为对象[class:{}]异常[error:{}]", xmlFile, clazz, ExceptionUtils.getStackTrace(e)));
throw new RunException(e, "将xml文件[xml:{}]转换为对象[{}]异常", xmlFile, clazz);
}
}
@@ -71,8 +67,7 @@ public abstract class DomUtils {
try {
return MAPPER.readValue(xmlInputStream, clazz);
} catch (Exception e) {
throw new RuntimeException(StringUtils
.format("将xmlInputStream转换为对象[class:{}]异常[error:{}]", clazz, ExceptionUtils.getStackTrace(e)));
throw new RunException(e, "将xmlInputStream转换为对象[{}]异常", clazz);
}
}
@@ -87,10 +82,10 @@ public abstract class DomUtils {
*/
public static List<Element> getChildElements(Element element) {
AssertionUtils.notNull(element, "Element must not be null");
NodeList nodeList = element.getChildNodes();
List<Element> childEles = new ArrayList<Element>();
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
var nodeList = element.getChildNodes();
var childEles = new ArrayList<Element>();
for (var i = 0; i < nodeList.getLength(); i++) {
var node = nodeList.item(i);
if (node instanceof Element) {
childEles.add((Element) node);
}
@@ -112,11 +107,11 @@ public abstract class DomUtils {
public static List<Element> getChildElementsByTagName(Element element, String... childElementNames) {
AssertionUtils.notNull(element, "Element must not be null");
AssertionUtils.notNull(childElementNames, "Element names collection must not be null");
List<String> childEleNameList = Arrays.asList(childElementNames);
NodeList childNodes = element.getChildNodes();
List<Element> elements = new ArrayList<>();
for (int i = 0; i < childNodes.getLength(); i++) {
Node node = childNodes.item(i);
var childEleNameList = Arrays.asList(childElementNames);
var childNodes = element.getChildNodes();
var elements = new ArrayList<Element>();
for (var i = 0; i < childNodes.getLength(); i++) {
var node = childNodes.item(i);
if (node instanceof Element && nodeNameMatch(node, childEleNameList)) {
elements.add((Element) node);
}
@@ -138,9 +133,9 @@ public abstract class DomUtils {
public static Element getFirstChildElementByTagName(Element ele, String childEleName) {
AssertionUtils.notNull(ele, "Element must not be null");
AssertionUtils.notNull(childEleName, "Element name must not be null");
NodeList nodeList = ele.getChildNodes();
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
var nodeList = ele.getChildNodes();
for (var i = 0; i < nodeList.getLength(); i++) {
var node = nodeList.item(i);
if (node instanceof Element && nodeNameMatch(node, childEleName)) {
return (Element) node;
}
@@ -1,6 +1,5 @@
/*
* 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
*
@@ -9,11 +8,11 @@
* 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.util;
package com.zfoo.protocol.buffer;
import com.zfoo.protocol.buffer.ByteBufUtils;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.buffer.Unpooled;
@@ -1,6 +1,5 @@
/*
* 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
*
@@ -9,9 +8,10 @@
* 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.util;
package com.zfoo.protocol.util;
import org.junit.Test;
@@ -1,6 +1,5 @@
/*
* 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
*
@@ -9,13 +8,11 @@
* 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.util;
package com.zfoo.protocol.util;
import com.zfoo.protocol.util.ClassUtils;
import com.zfoo.protocol.util.IOUtils;
import com.zfoo.protocol.util.StringUtils;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
@@ -50,7 +47,7 @@ public class ClassUtilTest {
@Test
public void getClassPath() {
System.out.println(ClassUtils.getClassAbsPath(User.class));
System.out.println(ClassUtils.getClassAbsPath(ClassUtilTest.class));
}
@@ -0,0 +1,58 @@
/*
* 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.util;
import com.zfoo.protocol.xml.XmlProtocols;
import org.junit.Assert;
import org.junit.Test;
public class DomUtilsTest {
private static final String XML_WITH_HEAD = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\" ?>\n" +
"\n" +
"<protocols author=\"jaysunxiao\">\n" +
" <module id=\"1\" name=\"common\" minId=\"1000\" maxId=\"2000\" version=\"1.0.0\">\n" +
" <protocol id=\"1000\" location=\"com.zfoo.test.CM_Int\"/>\n" +
" <protocol id=\"2000\" location=\"com.zfoo.test.SM_Int\"/>\n" +
" </module>\n" +
"\n" +
" <module id=\"2\" name=\"common\" minId=\"2000\" maxId=\"3000\" version=\"1.0.0\">\n" +
" <protocol id=\"3000\" location=\"com.zfoo.test.CM_Float\"/>\n" +
" </module>\n" +
"</protocols>";
private static final String XML_OF_STANDARD_TEXT = "<protocols author=\"jaysunxiao\">\n" +
" <module id=\"1\" name=\"common\" minId=\"1000\" maxId=\"2000\" version=\"1.0.0\">\n" +
" <protocol id=\"1000\" location=\"com.zfoo.test.CM_Int\"/>\n" +
" <protocol id=\"2000\" location=\"com.zfoo.test.SM_Int\"/>\n" +
" </module>\n" +
"\n" +
" <module id=\"2\" name=\"common\" minId=\"2000\" maxId=\"3000\" version=\"1.0.0\">\n" +
" <protocol id=\"3000\" location=\"com.zfoo.test.CM_Float\"/>\n" +
" </module>\n" +
"</protocols>";
@Test
public void testXmlWithHead() {
var protos = DomUtils.string2Object(XML_WITH_HEAD, XmlProtocols.class);
Assert.assertEquals("jaysunxiao", protos.getAuthor());
}
@Test
public void testXmlOfStandardText() {
var protos = DomUtils.string2Object(XML_OF_STANDARD_TEXT, XmlProtocols.class);
Assert.assertEquals("jaysunxiao", protos.getAuthor());
}
}
@@ -1,6 +1,5 @@
/*
* 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
*
@@ -9,11 +8,11 @@
* 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.util;
package com.zfoo.protocol.util;
import com.zfoo.protocol.util.FileUtils;
import org.junit.Ignore;
import org.junit.Test;
@@ -1,6 +1,5 @@
/*
* 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
*
@@ -9,12 +8,13 @@
* 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.util;
package com.zfoo.protocol.util;
import com.zfoo.protocol.model.Triple;
import com.zfoo.protocol.util.JsonUtils;
import com.zfoo.protocol.util.model.User;
import org.junit.Assert;
import org.junit.Test;
@@ -116,75 +116,3 @@ public class JsonUtilTest {
}
}
//@JsonIgnoreProperties({"name", "age"})//可以将它看做是@JsonIgnore的批量操作
class User {
private String id;
//@JsonIgnore//作用在字段或方法上用来完全忽略被注解的字段和方法对应的属性
//@JsonProperty//注意这里必须得有该注解因为没有提供对应的getId和setId函数而是其他的getter和setter防止遗漏该属性
private String name;
private String sex;
private int age;
private List<Integer> list;
private Map<Integer, Integer> map;
@Override
public String toString() {
return "User{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", sex='" + sex + '\'' +
", age=" + age +
", list=" + list +
", map=" + map +
'}';
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public List<Integer> getList() {
return list;
}
public void setList(List<Integer> list) {
this.list = list;
}
public Map<Integer, Integer> getMap() {
return map;
}
public void setMap(Map<Integer, Integer> map) {
this.map = map;
}
}
@@ -1,6 +1,5 @@
/*
* 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
*
@@ -9,11 +8,13 @@
* 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.util;
package com.zfoo.protocol.util;
import com.zfoo.protocol.util.ReflectionUtils;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.zfoo.protocol.util.model.User;
import org.junit.Ignore;
import org.junit.Test;
@@ -26,16 +27,13 @@ import java.util.function.Predicate;
* @version 3.0
*/
@interface Id {
}
@Ignore
public class ReflectUtilTest {
@Test
public void testGetFieldsByAnnotation() {
Field[] fields = ReflectionUtils.getFieldsByAnnoInPOJOClass(User.class, Id.class);
Field[] fields = ReflectionUtils.getFieldsByAnnoInPOJOClass(User.class, JsonIgnore.class);
System.out.println(fields.length);
}
@@ -1,6 +1,5 @@
/*
* 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
*
@@ -9,11 +8,11 @@
* 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.util;
package com.zfoo.protocol.util;
import com.zfoo.protocol.util.StringUtils;
import org.junit.Assert;
import org.junit.Test;
@@ -0,0 +1,92 @@
/*
* 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.util.model;
import java.util.List;
import java.util.Map;
/**
* @author jaysunxiao
* @version 3.0
*/ //@JsonIgnoreProperties({"name", "age"})//可以将它看做是@JsonIgnore的批量操作
public class User {
private String id;
//@JsonIgnore//作用在字段或方法上,用来完全忽略被注解的字段和方法对应的属性
//@JsonProperty//注意这里必须得有该注解,因为没有提供对应的getId和setId函数,而是其他的getter和setter,防止遗漏该属性
private String name;
private String sex;
private int age;
private List<Integer> list;
private Map<Integer, Integer> map;
@Override
public String toString() {
return "User{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", sex='" + sex + '\'' +
", age=" + age +
", list=" + list +
", map=" + map +
'}';
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public List<Integer> getList() {
return list;
}
public void setList(List<Integer> list) {
this.list = list;
}
public Map<Integer, Integer> getMap() {
return map;
}
public void setMap(Map<Integer, Integer> map) {
this.map = map;
}
}
@@ -13,13 +13,13 @@
package com.zfoo.storage.schema;
import com.zfoo.protocol.util.DomUtils;
import com.zfoo.protocol.util.StringUtils;
import com.zfoo.storage.StorageContext;
import com.zfoo.storage.interpreter.ExcelResourceReader;
import com.zfoo.storage.manager.StorageManager;
import com.zfoo.storage.model.config.StorageConfig;
import com.zfoo.storage.strategy.*;
import com.zfoo.util.DomUtils;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.BeanDefinitionParser;
@@ -1,103 +0,0 @@
/*
* 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.util;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
import org.junit.Assert;
import org.junit.Test;
import java.util.List;
public class DomUtilsTest {
private static final String XML_WITH_HEAD = "" +
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\" ?>\n" +
"\n" +
"<protocols version=\"1.0\" author=\"jaysunxiao\">\n" +
"\n" +
" <protocol id=\"0\" location=\"com.zfoo.test.CM_Int\"/>\n" +
" <protocol id=\"1\" location=\"com.zfoo.test.SM_Int\"/>\n" +
" <protocol id=\"2\" location=\"com.zfoo.test.CM_Float\"/>\n" +
"\n" +
"</protocols>";
private static final String XML_OF_STANDARD_TEXT =
"<protocols version=\"1.0\" author=\"jaysunxiao\">\n" +
"\n" +
" <protocol id=\"0\" location=\"com.zfoo.test.CM_Int\"/>\n" +
" <protocol id=\"1\" location=\"com.zfoo.test.SM_Int\"/>\n" +
" <protocol id=\"2\" location=\"com.zfoo.test.CM_Float\"/>\n" +
"\n" +
"</protocols>";
@Test
public void testXmlWithHead() {
Protos protos = DomUtils.string2Object(XML_WITH_HEAD, Protos.class);
Assert.assertEquals("jaysunxiao", protos.getAuthor());
Assert.assertEquals("1.0", protos.getVersion());
}
@Test
public void testXmlOfStandardText() {
Protos protos = DomUtils.string2Object(XML_OF_STANDARD_TEXT, Protos.class);
Assert.assertEquals("jaysunxiao", protos.getAuthor());
Assert.assertEquals("1.0", protos.getVersion());
}
}
@JsonPropertyOrder({"version", "author", "list"})
@JacksonXmlRootElement(localName = "protocols")
class Protos {
@JacksonXmlProperty(isAttribute = true, localName = "version")
private String version;
@JacksonXmlProperty(isAttribute = true, localName = "author")
private String author;
@JacksonXmlProperty(localName = "protocol")
@JacksonXmlElementWrapper(useWrapping = false)
private List<Proto> protos;
public String getVersion() {
return version;
}
public String getAuthor() {
return author;
}
public List<Proto> getProtos() {
return protos;
}
}
@JsonPropertyOrder({"id", "location"})
class Proto {
@JacksonXmlProperty(isAttribute = true, localName = "id")
private int id;
@JacksonXmlProperty(isAttribute = true, localName = "location")
private String location;
public int getId() {
return id;
}
public String getLocation() {
return location;
}
}