mirror of
https://github.com/tiennm99/zfoo.git
synced 2026-08-24 08:30:36 +00:00
perf[protocol]: 规范test测试用例
This commit is contained in:
@@ -0,0 +1,169 @@
|
||||
/*
|
||||
* 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.databind.DeserializationFeature;
|
||||
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
|
||||
import com.zfoo.protocol.exception.RunException;
|
||||
import org.w3c.dom.Element;
|
||||
import org.w3c.dom.Node;
|
||||
|
||||
import javax.xml.stream.XMLInputFactory;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* DOM(Document Object Model)文档对象模型
|
||||
* <p>
|
||||
* Convenience methods for working with the DOM API,in particular for working with DOM Nodes and DOM Elements.
|
||||
* </p>
|
||||
*
|
||||
* @author jaysunxiao
|
||||
* @version 3.0
|
||||
*/
|
||||
public abstract class DomUtils {
|
||||
|
||||
private static final XmlMapper MAPPER = XmlMapper.builder()
|
||||
.defaultUseWrapper(false)
|
||||
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, true) // 当反序列化有未知属性则抛异常,true打开这个设置
|
||||
.build();
|
||||
|
||||
public static <T> T string2Object(String xml, Class<T> clazz) {
|
||||
try {
|
||||
return MAPPER.readValue(xml, clazz);
|
||||
} catch (IOException e) {
|
||||
throw new RunException(e, "将xml字符串[xml:{}]转换为对象[{}]异常", xml, clazz);
|
||||
}
|
||||
}
|
||||
|
||||
public static <T> T file2Object(File xmlFile, Class<T> clazz) {
|
||||
try {
|
||||
var f = XMLInputFactory.newFactory();
|
||||
var sr = f.createXMLStreamReader(new FileInputStream(xmlFile));
|
||||
return MAPPER.readValue(sr, clazz);
|
||||
} catch (Exception e) {
|
||||
throw new RunException(e, "将xml文件[xml:{}]转换为对象[{}]异常", xmlFile, clazz);
|
||||
}
|
||||
}
|
||||
|
||||
public static <T> T inputStream2Object(InputStream xmlInputStream, Class<T> clazz) {
|
||||
try {
|
||||
return MAPPER.readValue(xmlInputStream, clazz);
|
||||
} catch (Exception e) {
|
||||
throw new RunException(e, "将xmlInputStream转换为对象[{}]异常", clazz);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 只返回第一层的孩子节点,不返回第一层孩子节点的孩子节点
|
||||
* <p>
|
||||
* Retrieves all child elements of the given DOM element
|
||||
* </p>
|
||||
*
|
||||
* @param element the DOM element to analyze
|
||||
* @return a List of child {@code org.w3c.dom.Element} instances
|
||||
*/
|
||||
public static List<Element> getChildElements(Element element) {
|
||||
AssertionUtils.notNull(element, "Element must not be null");
|
||||
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);
|
||||
}
|
||||
}
|
||||
return childEles;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves all child elements of the given DOM element that match any of the given element names.
|
||||
* Only looks at the direct child level of the given element; do not go into further depth
|
||||
* (in contrast to the DOM API's {@code getElementsByTagName} method).
|
||||
*
|
||||
* @param element the DOM element to analyze
|
||||
* @param childElementNames the child element names to look for
|
||||
* @return a List of child {@code org.w3c.dom.Element} instances
|
||||
* @see org.w3c.dom.Element
|
||||
* @see org.w3c.dom.Element#getElementsByTagName
|
||||
*/
|
||||
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");
|
||||
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);
|
||||
}
|
||||
}
|
||||
return elements;
|
||||
}
|
||||
|
||||
public static List<Element> getChildElementsByTagName(Element ele, String childElementName) {
|
||||
return getChildElementsByTagName(ele, new String[]{childElementName});
|
||||
}
|
||||
|
||||
/**
|
||||
* Utility method that returns the first child element identified by its name.
|
||||
*
|
||||
* @param ele the DOM element to analyze
|
||||
* @param childEleName the child element name to look for
|
||||
* @return the {@code org.w3c.dom.Element} instance, or {@code null} if none found
|
||||
*/
|
||||
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");
|
||||
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;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/*
|
||||
Namespace-aware equals comparison.
|
||||
*/
|
||||
public static boolean nodeNameEquals(Node node, String desiredName) {
|
||||
AssertionUtils.notNull(node, "Node must not be null");
|
||||
AssertionUtils.notNull(desiredName, "Desired name must not be null");
|
||||
return nodeNameMatch(node, desiredName);
|
||||
}
|
||||
|
||||
/*
|
||||
Matches the given node's name and local name against the given desired names.
|
||||
*/
|
||||
private static boolean nodeNameMatch(Node node, Collection<?> desiredNames) {
|
||||
return (desiredNames.contains(node.getNodeName()) || desiredNames.contains(node.getLocalName()));
|
||||
}
|
||||
|
||||
/*
|
||||
Matches the given node's name and local name against the given desired name.
|
||||
*/
|
||||
private static boolean nodeNameMatch(Node node, String desiredName) {
|
||||
return (desiredName.equals(node.getNodeName()) || desiredName.equals(node.getLocalName()));
|
||||
}
|
||||
|
||||
}
|
||||
+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