Skip to content

Commit c47b8ec

Browse files
committed
修复toMap方法的bug,并加入单元测试代码
1 parent 70af494 commit c47b8ec

2 files changed

Lines changed: 64 additions & 3 deletions

File tree

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/pay/result/WxPayBaseResult.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44
import com.thoughtworks.xstream.XStream;
55
import com.thoughtworks.xstream.annotations.XStreamAlias;
66
import io.restassured.internal.path.xml.NodeChildrenImpl;
7+
import io.restassured.internal.path.xml.NodeImpl;
78
import io.restassured.path.xml.XmlPath;
89
import io.restassured.path.xml.element.Node;
9-
import io.restassured.path.xml.element.NodeChildren;
10+
import io.restassured.path.xml.exception.XmlPathException;
1011
import me.chanjar.weixin.common.util.ToStringUtils;
1112
import me.chanjar.weixin.common.util.xml.XStreamInitializer;
1213
import org.slf4j.Logger;
@@ -186,12 +187,23 @@ public void setSign(String sign) {
186187
public Map<String, String> toMap() {
187188
Map<String, String> result = Maps.newHashMap();
188189
XmlPath xmlPath = new XmlPath(this.xmlString);
189-
NodeChildren nodeChildren = xmlPath.getNodeChildren("xml");
190-
Iterator<Node> iterator = nodeChildren.nodeIterator();
190+
NodeImpl rootNode = null;
191+
try {
192+
rootNode = xmlPath.get("xml");
193+
} catch (XmlPathException e) {
194+
throw new RuntimeException("xml数据有问题,请核实!");
195+
}
196+
197+
if (rootNode == null) {
198+
throw new RuntimeException("xml数据有问题,请核实!");
199+
}
200+
201+
Iterator<Node> iterator = rootNode.children().nodeIterator();
191202
while (iterator.hasNext()) {
192203
Node node = iterator.next();
193204
result.put(node.name(), node.value());
194205
}
206+
195207
return result;
196208
}
197209

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package me.chanjar.weixin.mp.bean.pay.result;
2+
3+
import org.testng.Assert;
4+
import org.testng.annotations.Test;
5+
6+
import java.util.Map;
7+
8+
/**
9+
* <pre>
10+
* Created by Binary Wang on 2017-01-04.
11+
* @author <a href="https://github.com/binarywang">binarywang(Binary Wang)</a>
12+
* </pre>
13+
*/
14+
public class WxPayBaseResultTest {
15+
@Test
16+
public void testToMap() throws Exception {
17+
WxPayOrderQueryResult result = new WxPayOrderQueryResult();
18+
result.setXmlString("<xml>\n" +
19+
" <return_code><![CDATA[SUCCESS]]></return_code>\n" +
20+
" <return_msg><![CDATA[OK]]></return_msg>\n" +
21+
" <appid><![CDATA[wx2421b1c4370ec43b]]></appid>\n" +
22+
" <mch_id><![CDATA[10000100]]></mch_id>\n" +
23+
" <device_info><![CDATA[1000]]></device_info>\n" +
24+
" <nonce_str><![CDATA[TN55wO9Pba5yENl8]]></nonce_str>\n" +
25+
" <sign><![CDATA[BDF0099C15FF7BC6B1585FBB110AB635]]></sign>\n" +
26+
" <result_code><![CDATA[SUCCESS]]></result_code>\n" +
27+
" <openid><![CDATA[oUpF8uN95-Ptaags6E_roPHg7AG0]]></openid>\n" +
28+
" <is_subscribe><![CDATA[Y]]></is_subscribe>\n" +
29+
" <trade_type><![CDATA[MICROPAY]]></trade_type>\n" +
30+
" <bank_type><![CDATA[CCB_DEBIT]]></bank_type>\n" +
31+
" <total_fee>1</total_fee>\n" +
32+
" <fee_type><![CDATA[CNY]]></fee_type>\n" +
33+
" <transaction_id><![CDATA[1008450740201411110005820873]]></transaction_id>\n" +
34+
" <out_trade_no><![CDATA[1415757673]]></out_trade_no>\n" +
35+
" <attach><![CDATA[订单额外描述]]></attach>\n" +
36+
" <time_end><![CDATA[20141111170043]]></time_end>\n" +
37+
" <trade_state><![CDATA[SUCCESS]]></trade_state>\n" +
38+
"</xml>");
39+
Map<String, String> map = result.toMap();
40+
System.out.println(map);
41+
42+
Assert.assertEquals("SUCCESS", map.get("return_code"));
43+
Assert.assertEquals("订单额外描述", map.get("attach"));
44+
45+
result.setXmlString("");
46+
System.out.println(result.toMap());
47+
}
48+
49+
}

0 commit comments

Comments
 (0)