mirror of
https://github.com/tiennm99/zfoo.git
synced 2026-09-09 00:21:14 +00:00
perf[protocol]: 规范test测试用例
This commit is contained in:
+2
-3
@@ -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;
|
||||
@@ -0,0 +1,29 @@
|
||||
/*
|
||||
* 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 org.junit.Test;
|
||||
|
||||
/**
|
||||
* @author jaysunxiao
|
||||
* @version 3.0
|
||||
*/
|
||||
public class AssertionUtilsTest {
|
||||
|
||||
|
||||
@Test
|
||||
public void classLocation() {
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
/*
|
||||
* 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 org.junit.Assert;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* @author jaysunxiao
|
||||
* @version 3.0
|
||||
*/
|
||||
@Ignore
|
||||
public class ClassUtilTest {
|
||||
|
||||
// ClassUtilTest
|
||||
@Test
|
||||
public void classLocation() {
|
||||
String str = ClassUtils.classLocation(Integer.class);
|
||||
Assert.assertEquals("jrt:/java.base/java/lang/Integer.class", str);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getAllClasses() throws Exception {
|
||||
System.out.println(StringUtils.MULTIPLE_HYPHENS);
|
||||
System.out.println("某个包下的所有类查找测试:");
|
||||
Set<Class<?>> set = ClassUtils.getAllClasses("com.zfoo");
|
||||
for (Class<?> clazz : set) {
|
||||
System.out.println(clazz.getName());
|
||||
}
|
||||
System.out.println(StringUtils.MULTIPLE_HYPHENS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getClassPath() {
|
||||
System.out.println(ClassUtils.getClassAbsPath(ClassUtilTest.class));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void getClassFromClassPath() throws IOException {
|
||||
System.out.println(new String(IOUtils.toByteArray(ClassUtils.getFileFromClassPath("com.zfoo.util.ClassUtilsTest"))));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
/*
|
||||
* 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 org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author jaysunxiao
|
||||
* @version 3.0
|
||||
*/
|
||||
@Ignore
|
||||
public class FileUtilTest {
|
||||
|
||||
@Test
|
||||
public void absPathTest() {
|
||||
var absPath = FileUtils.getProAbsPath();
|
||||
System.out.println(absPath);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createFile() throws IOException {
|
||||
FileUtils.createFile(FileUtils.getProAbsPath() + File.separator + "hello", "hhh");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void deleteFile() {
|
||||
FileUtils.deleteFile(new File(FileUtils.getProAbsPath() + File.separator + "hello"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void writeFile() {
|
||||
FileUtils.writeStringToFile(new File(FileUtils.getProAbsPath() + File.separator + "test.txt"), "hello world!");
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void readFile() {
|
||||
String str = FileUtils.readFileToString(new File(FileUtils.getProAbsPath() + File.separator + "test.txt"));
|
||||
System.out.println(str);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void getProjectPath() {
|
||||
System.out.println(FileUtils.getProAbsPath());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void searchFile() {
|
||||
FileUtils.searchFileInProject(new File(FileUtils.getProAbsPath()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getAllFiles() {
|
||||
List<File> list = FileUtils.getAllReadableFiles(new File(FileUtils.getProAbsPath()));
|
||||
for (File file : list) {
|
||||
System.out.println(file.getName());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void searchFileInProject() {
|
||||
System.out.println(FileUtils.searchFileInProject("User"));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,118 @@
|
||||
/*
|
||||
* 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.model.Triple;
|
||||
import com.zfoo.protocol.util.model.User;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* @author jaysunxiao
|
||||
* @version 3.0
|
||||
*/
|
||||
|
||||
|
||||
public class JsonUtilTest {
|
||||
|
||||
public static String id = "\"id\":\"1000\"";
|
||||
public static String name = "\"name\":\"jaysunxiao\"";
|
||||
public static String sex = "\"sex\":\"man\"";
|
||||
public static String age = "\"age\":22";
|
||||
public static String list = "\"list\":[1,2,3]";
|
||||
public static String map = "\"map\":{\"1\":1,\"2\":2,\"3\":3}";
|
||||
|
||||
public static String userJson = "{" + id + "," + name + "," + sex + ","
|
||||
+ age + "," + list + "," + map + "}";
|
||||
|
||||
@Test
|
||||
public void string2Object() {
|
||||
User user = JsonUtils.string2Object(userJson, User.class);
|
||||
Assert.assertEquals(user.getId(), "1000");
|
||||
Assert.assertEquals(user.getName(), "jaysunxiao");
|
||||
Assert.assertEquals(user.getSex(), "man");
|
||||
Assert.assertEquals(user.getList().size(), 3);
|
||||
Assert.assertEquals(user.getMap().size(), 3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void object2String() {
|
||||
User user = new User();
|
||||
user.setId("1000");
|
||||
user.setName("jaysunxiao");
|
||||
user.setSex("man");
|
||||
user.setAge(22);
|
||||
//数组,链表,list
|
||||
List<Integer> list = new ArrayList<>();
|
||||
list.add(1);
|
||||
list.add(2);
|
||||
list.add(3);
|
||||
user.setList(list);
|
||||
//map
|
||||
Map<Integer, Integer> map = new HashMap<>();
|
||||
map.put(1, 1);
|
||||
map.put(2, 2);
|
||||
map.put(3, 3);
|
||||
user.setMap(map);
|
||||
|
||||
Assert.assertEquals(JsonUtils.object2String(user), userJson);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void string2List() {
|
||||
String str = "[1,2,3]";
|
||||
List<Integer> list = JsonUtils.string2List(str, Integer.class);
|
||||
|
||||
Assert.assertEquals(list.size(), 3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void string2Set() {
|
||||
String str = "[1,2,3]";
|
||||
Set<Integer> set = JsonUtils.string2Set(str, Integer.class);
|
||||
|
||||
Assert.assertEquals(set.size(), 3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void string2Map() {
|
||||
String str = "{\"1\":1,\"2\":2,\"3\":3}";
|
||||
Map<Integer, Integer> map = JsonUtils.string2Map(str, Integer.class, Integer.class);
|
||||
|
||||
Assert.assertEquals(map.size(), 3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void string2Array() {
|
||||
String str = "[1,2,3]";
|
||||
Integer[] list = JsonUtils.string2Array(str, Integer.class);
|
||||
|
||||
Assert.assertEquals(list.length, 3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getNodeTest() {
|
||||
Assert.assertEquals(JsonUtils.getNode(userJson, "id").asText(), "1000");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void tripleTest() {
|
||||
var triple = new Triple<String, String, String>("a", "b", "c");
|
||||
var tripleStr = JsonUtils.object2String(triple);
|
||||
var temp = JsonUtils.string2Object(tripleStr, Triple.class);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
* 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.fasterxml.jackson.annotation.JsonIgnore;
|
||||
import com.zfoo.protocol.util.model.User;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
/**
|
||||
* @author jaysunxiao
|
||||
* @version 3.0
|
||||
*/
|
||||
|
||||
|
||||
@Ignore
|
||||
public class ReflectUtilTest {
|
||||
|
||||
@Test
|
||||
public void testGetFieldsByAnnotation() {
|
||||
Field[] fields = ReflectionUtils.getFieldsByAnnoInPOJOClass(User.class, JsonIgnore.class);
|
||||
System.out.println(fields.length);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFilterFieldsInClass() {
|
||||
ReflectionUtils.filterFieldsInClass(User.class, new Predicate<Field>() {
|
||||
@Override
|
||||
public boolean test(Field field) {
|
||||
return field != null;
|
||||
}
|
||||
}, new Consumer<Field>() {
|
||||
@Override
|
||||
public void accept(Field field) {
|
||||
System.out.println(field.getName());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
* 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 org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* @author jaysunxiao
|
||||
* @version 3.0
|
||||
*/
|
||||
public class StringUtilTest {
|
||||
|
||||
@Test
|
||||
public void formatTest() {
|
||||
String str = StringUtils.format("this is {} for {}", "a", "b");
|
||||
Assert.assertEquals("this is a for b", str);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isEmpty() {
|
||||
Assert.assertFalse(StringUtils.isEmpty(" "));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void capitalize() {
|
||||
String str = "hello world!";
|
||||
Assert.assertEquals(StringUtils.capitalize(str), "Hello world!");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void unCapitalize() {
|
||||
String str = "Hello world!";
|
||||
Assert.assertEquals(StringUtils.uncapitalize(str), "hello world!");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user