-
Notifications
You must be signed in to change notification settings - Fork 18
/
XMLTest.java
138 lines (122 loc) · 4.46 KB
/
XMLTest.java
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package org.osgl.util;
/*-
* #%L
* Java Tool
* %%
* Copyright (C) 2014 - 2017 OSGL (Open Source General Library)
* %%
* 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.
* #L%
*/
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.junit.Test;
import org.osgl.$;
import org.osgl.TestBase;
import org.osgl.util.converter.XmlToJson;
import org.w3c.dom.Document;
public class XMLTest extends TestBase {
@Test
public void testConvertValueOnly() {
String s = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?><xml>x</xml>";
Document doc = XML.read(s);
JSONObject json = $.convert(doc).to(JSONObject.class);
yes(json.isEmpty());
}
@Test
public void testConvertXmlToJsonSimple() {
String s = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?><xml><name>x</name><id>5</id></xml>";
Document doc = XML.read(s);
JSONObject json = $.convert(doc).to(JSONObject.class);
eq("x", json.getString("name"));
eq(5, json.getInteger("id"));
}
@Test
public void testConvertXmlToJsonWithAttributes() {
String s = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?><xml><id len=\"3\">5</id></xml>";
Document doc = XML.read(s);
JSONObject json = $.convert(doc).to(JSONObject.class);
JSONObject id = json.getJSONObject("id");
eq(3, id.getInteger("len"));
eq(5, id.getInteger(XmlToJson.INNER_VALUE));
}
@Test
public void testConvertXmlToJsonEhCacheConfigMergeMap() {
String s = loadFileAsString("ehcache.xml");
Document doc = XML.read(s);
JSONObject json = $.convert(doc).hint(XmlToJson.HINT_MERGE_MAP).to(JSONObject.class);
notNull(json);
JSONObject cache = json.getJSONObject("cache");
eq(8, cache.size());
JSONArray timeToLiveSeconds = cache.getJSONArray("timeToLiveSeconds");
eq(45, timeToLiveSeconds.size());
eq(20, timeToLiveSeconds.getInteger(0));
eq("charityCache", cache.getJSONArray("name").getString(0));
JSONObject persistence = cache.getJSONObject("persistence");
JSONArray strategy = persistence.getJSONArray("strategy");
eq(45, strategy.size());
eq("none", strategy.getString(0));
}
@Test
public void testConvertXmlToJsonEhCacheConfigNoMerge() {
String s = loadFileAsString("ehcache.xml");
Document doc = XML.read(s);
JSONObject json = $.convert(doc).to(JSONObject.class);
notNull(json);
JSONArray caches = json.getJSONArray("cache");
eq(45, caches.size());
JSONObject obj = caches.getJSONObject(0);
eq(8, obj.size());
eq(20, obj.getInteger("timeToLiveSeconds"));
no(obj.getBoolean("eternal"));
eq("off", obj.getString("transactionalMode"));
JSONObject persistence = obj.getJSONObject("persistence");
notNull(persistence);
eq("none", persistence.getString("strategy"));
}
@Test
public void testCase1_1() {
test("1_1");
}
@Test
public void testCase1_2() {
test("1_2");
}
@Test
public void testCase1_3() {
test("1_3");
}
@Test
public void testCase2_1() {
test("2_1");
}
@Test
public void testCase3_1() {
test("3_1");
}
private void test(String caseName) {
test(caseName, 0);
test(caseName, XmlToJson.HINT_MERGE_MAP);
}
private void test(String caseName, int hint) {
String xmlFile = "xml_json/" + caseName + ".xml";
String jsonFile = "xml_json/" + caseName + suffix(hint) + ".json";
Document doc = XML.read(loadFileAsString(xmlFile));
JSONObject json = JSON.parseObject(loadFileAsString(jsonFile));
eq(json, $.convert(doc).hint(hint).to(JSONObject.class));
}
private static String suffix(int hint) {
return XmlToJson.doMergeMap(hint) ? "_merge_map" : "";
}
}