-
-
Notifications
You must be signed in to change notification settings - Fork 9.1k
Expand file tree
/
Copy pathXmlUtilsTest.java
More file actions
75 lines (64 loc) · 2.29 KB
/
Copy pathXmlUtilsTest.java
File metadata and controls
75 lines (64 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package me.chanjar.weixin.channel.util;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertNull;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import me.chanjar.weixin.channel.bean.base.AttrInfo;
import org.testng.annotations.Test;
/**
* {@link XmlUtils} 单元测试
*/
public class XmlUtilsTest {
private static final String XML = "<AttrInfo><attr_key>这是Key</attr_key><attr_value>这是Value</attr_value></AttrInfo>";
@Test
public void testEncode() {
String xml = XmlUtils.encode(new AttrInfo("这是Key", "这是Value"));
assertNotNull(xml);
assertEquals(xml, XML);
}
@Test
public void testEncodeWithObjectMapper() {
String xml = XmlUtils.encode(new XmlMapper(), new AttrInfo("这是Key", "这是Value"));
assertNotNull(xml);
assertEquals(XmlUtils.decode(xml, AttrInfo.class).getKey(), "这是Key");
}
@Test
public void testDecode() {
AttrInfo info = XmlUtils.decode(XML, AttrInfo.class);
assertNotNull(info);
assertEquals(info.getKey(), "这是Key");
assertEquals(info.getValue(), "这是Value");
}
@Test
public void testDecodeUnknownProperty() {
String xml = "<AttrInfo><attr_key>k</attr_key><unknown>v</unknown></AttrInfo>";
AttrInfo info = XmlUtils.decode(xml, AttrInfo.class);
assertNotNull(info);
assertEquals(info.getKey(), "k");
assertNull(info.getValue());
}
@Test
public void testDecodeEmptyOrInvalidXml() {
assertNull(XmlUtils.decode((String) null, AttrInfo.class));
assertNull(XmlUtils.decode("", AttrInfo.class));
assertNull(XmlUtils.decode("not a xml", AttrInfo.class));
}
@Test
public void testDecodeWithTypeReference() {
AttrInfo info = XmlUtils.decode(XML, new TypeReference<AttrInfo>() {
});
assertNotNull(info);
assertEquals(info.getValue(), "这是Value");
}
@Test
public void testDecodeInputStream() {
InputStream is = new ByteArrayInputStream(XML.getBytes(StandardCharsets.UTF_8));
AttrInfo info = XmlUtils.decode(is, AttrInfo.class);
assertNotNull(info);
assertEquals(info.getKey(), "这是Key");
}
}