diff --git a/net/src/main/java/com/zfoo/net/packet/service/PacketService.java b/net/src/main/java/com/zfoo/net/packet/service/PacketService.java index dd63032a..267e12e3 100644 --- a/net/src/main/java/com/zfoo/net/packet/service/PacketService.java +++ b/net/src/main/java/com/zfoo/net/packet/service/PacketService.java @@ -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; diff --git a/net/src/main/java/com/zfoo/net/schema/NetDefinitionParser.java b/net/src/main/java/com/zfoo/net/schema/NetDefinitionParser.java index 8a544983..2dfe5f3c 100644 --- a/net/src/main/java/com/zfoo/net/schema/NetDefinitionParser.java +++ b/net/src/main/java/com/zfoo/net/schema/NetDefinitionParser.java @@ -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; diff --git a/orm/src/main/java/com/zfoo/orm/schema/OrmDefinitionParser.java b/orm/src/main/java/com/zfoo/orm/schema/OrmDefinitionParser.java index a4467a11..57122838 100644 --- a/orm/src/main/java/com/zfoo/orm/schema/OrmDefinitionParser.java +++ b/orm/src/main/java/com/zfoo/orm/schema/OrmDefinitionParser.java @@ -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; diff --git a/util/src/main/java/com/zfoo/util/DomUtils.java b/protocol/src/main/java/com/zfoo/protocol/util/DomUtils.java similarity index 78% rename from util/src/main/java/com/zfoo/util/DomUtils.java rename to protocol/src/main/java/com/zfoo/protocol/util/DomUtils.java index 943cffb5..2d58bdcc 100644 --- a/util/src/main/java/com/zfoo/util/DomUtils.java +++ b/protocol/src/main/java/com/zfoo/protocol/util/DomUtils.java @@ -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 file2Object(File xmlFile, Class 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 getChildElements(Element element) { AssertionUtils.notNull(element, "Element must not be null"); - NodeList nodeList = element.getChildNodes(); - List childEles = new ArrayList(); - for (int i = 0; i < nodeList.getLength(); i++) { - Node node = nodeList.item(i); + var nodeList = element.getChildNodes(); + var childEles = new ArrayList(); + 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 getChildElementsByTagName(Element element, String... childElementNames) { AssertionUtils.notNull(element, "Element must not be null"); AssertionUtils.notNull(childElementNames, "Element names collection must not be null"); - List childEleNameList = Arrays.asList(childElementNames); - NodeList childNodes = element.getChildNodes(); - List 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(); + 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; } diff --git a/protocol/src/test/java/com/zfoo/protocol/util/ByteBufUtilsTest.java b/protocol/src/test/java/com/zfoo/protocol/buffer/ByteBufUtilsTest.java similarity index 98% rename from protocol/src/test/java/com/zfoo/protocol/util/ByteBufUtilsTest.java rename to protocol/src/test/java/com/zfoo/protocol/buffer/ByteBufUtilsTest.java index 3b2218fa..f91d1d8f 100644 --- a/protocol/src/test/java/com/zfoo/protocol/util/ByteBufUtilsTest.java +++ b/protocol/src/test/java/com/zfoo/protocol/buffer/ByteBufUtilsTest.java @@ -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; diff --git a/util/src/test/java/com/zfoo/util/AssertionUtilsTest.java b/protocol/src/test/java/com/zfoo/protocol/util/AssertionUtilsTest.java similarity index 95% rename from util/src/test/java/com/zfoo/util/AssertionUtilsTest.java rename to protocol/src/test/java/com/zfoo/protocol/util/AssertionUtilsTest.java index 60340988..f0fb7222 100644 --- a/util/src/test/java/com/zfoo/util/AssertionUtilsTest.java +++ b/protocol/src/test/java/com/zfoo/protocol/util/AssertionUtilsTest.java @@ -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; diff --git a/util/src/test/java/com/zfoo/util/ClassUtilTest.java b/protocol/src/test/java/com/zfoo/protocol/util/ClassUtilTest.java similarity index 88% rename from util/src/test/java/com/zfoo/util/ClassUtilTest.java rename to protocol/src/test/java/com/zfoo/protocol/util/ClassUtilTest.java index ee67afba..a6e77f67 100644 --- a/util/src/test/java/com/zfoo/util/ClassUtilTest.java +++ b/protocol/src/test/java/com/zfoo/protocol/util/ClassUtilTest.java @@ -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)); } diff --git a/protocol/src/test/java/com/zfoo/protocol/util/DomUtilsTest.java b/protocol/src/test/java/com/zfoo/protocol/util/DomUtilsTest.java new file mode 100644 index 00000000..615f3caa --- /dev/null +++ b/protocol/src/test/java/com/zfoo/protocol/util/DomUtilsTest.java @@ -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 = "\n" + + "\n" + + "\n" + + " \n" + + " \n" + + " \n" + + " \n" + + "\n" + + " \n" + + " \n" + + " \n" + + ""; + + private static final String XML_OF_STANDARD_TEXT = "\n" + + " \n" + + " \n" + + " \n" + + " \n" + + "\n" + + " \n" + + " \n" + + " \n" + + ""; + + @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()); + } + +} diff --git a/util/src/test/java/com/zfoo/util/FileUtilTest.java b/protocol/src/test/java/com/zfoo/protocol/util/FileUtilTest.java similarity index 97% rename from util/src/test/java/com/zfoo/util/FileUtilTest.java rename to protocol/src/test/java/com/zfoo/protocol/util/FileUtilTest.java index 3611f56b..75a0664b 100644 --- a/util/src/test/java/com/zfoo/util/FileUtilTest.java +++ b/protocol/src/test/java/com/zfoo/protocol/util/FileUtilTest.java @@ -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; diff --git a/util/src/test/java/com/zfoo/util/JsonUtilTest.java b/protocol/src/test/java/com/zfoo/protocol/util/JsonUtilTest.java similarity index 66% rename from util/src/test/java/com/zfoo/util/JsonUtilTest.java rename to protocol/src/test/java/com/zfoo/protocol/util/JsonUtilTest.java index f3d3c606..3a42e20b 100644 --- a/util/src/test/java/com/zfoo/util/JsonUtilTest.java +++ b/protocol/src/test/java/com/zfoo/protocol/util/JsonUtilTest.java @@ -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 list; - private Map 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 getList() { - return list; - } - - public void setList(List list) { - this.list = list; - } - - public Map getMap() { - return map; - } - - public void setMap(Map map) { - this.map = map; - } -} - diff --git a/util/src/test/java/com/zfoo/util/ReflectUtilTest.java b/protocol/src/test/java/com/zfoo/protocol/util/ReflectUtilTest.java similarity index 90% rename from util/src/test/java/com/zfoo/util/ReflectUtilTest.java rename to protocol/src/test/java/com/zfoo/protocol/util/ReflectUtilTest.java index e19a4626..66f5416f 100644 --- a/util/src/test/java/com/zfoo/util/ReflectUtilTest.java +++ b/protocol/src/test/java/com/zfoo/protocol/util/ReflectUtilTest.java @@ -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); } diff --git a/util/src/test/java/com/zfoo/util/StringUtilTest.java b/protocol/src/test/java/com/zfoo/protocol/util/StringUtilTest.java similarity index 95% rename from util/src/test/java/com/zfoo/util/StringUtilTest.java rename to protocol/src/test/java/com/zfoo/protocol/util/StringUtilTest.java index 2496be91..3f828d48 100644 --- a/util/src/test/java/com/zfoo/util/StringUtilTest.java +++ b/protocol/src/test/java/com/zfoo/protocol/util/StringUtilTest.java @@ -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; diff --git a/protocol/src/test/java/com/zfoo/protocol/util/model/User.java b/protocol/src/test/java/com/zfoo/protocol/util/model/User.java new file mode 100644 index 00000000..5baa8aa4 --- /dev/null +++ b/protocol/src/test/java/com/zfoo/protocol/util/model/User.java @@ -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 list; + private Map 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 getList() { + return list; + } + + public void setList(List list) { + this.list = list; + } + + public Map getMap() { + return map; + } + + public void setMap(Map map) { + this.map = map; + } +} diff --git a/storage/src/main/java/com/zfoo/storage/schema/StorageDefinitionParser.java b/storage/src/main/java/com/zfoo/storage/schema/StorageDefinitionParser.java index 7796ceaf..54362937 100644 --- a/storage/src/main/java/com/zfoo/storage/schema/StorageDefinitionParser.java +++ b/storage/src/main/java/com/zfoo/storage/schema/StorageDefinitionParser.java @@ -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; diff --git a/util/src/test/java/com/zfoo/util/DomUtilsTest.java b/util/src/test/java/com/zfoo/util/DomUtilsTest.java deleted file mode 100644 index bb5a12a3..00000000 --- a/util/src/test/java/com/zfoo/util/DomUtilsTest.java +++ /dev/null @@ -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 = "" + - "\n" + - "\n" + - "\n" + - "\n" + - " \n" + - " \n" + - " \n" + - "\n" + - ""; - - private static final String XML_OF_STANDARD_TEXT = - "\n" + - "\n" + - " \n" + - " \n" + - " \n" + - "\n" + - ""; - - @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 protos; - - public String getVersion() { - return version; - } - - public String getAuthor() { - return author; - } - - public List 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; - } -} - -