1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.velocity.tools; |
21 |
|
|
22 |
|
import java.beans.Transient; |
23 |
|
import java.util.ArrayList; |
24 |
|
import java.util.Arrays; |
25 |
|
import java.util.Collections; |
26 |
|
import java.util.HashMap; |
27 |
|
import java.util.LinkedHashMap; |
28 |
|
import java.util.List; |
29 |
|
import java.util.Map; |
30 |
|
|
31 |
|
import org.junit.Assert; |
32 |
|
import org.junit.Test; |
33 |
|
|
34 |
|
import net.sf.json.JSON; |
35 |
|
import net.sf.json.JSONNull; |
36 |
|
import net.sf.json.JSONObject; |
37 |
|
|
38 |
|
|
39 |
|
@link |
40 |
|
|
41 |
|
@version |
42 |
|
@since |
43 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (103) |
Complexity: 18 |
Complexity Density: 0.21 |
|
44 |
|
public class JSONToolTest |
45 |
|
{ |
|
|
| 85.7% |
Uncovered Elements: 2 (14) |
Complexity: 7 |
Complexity Density: 1 |
|
46 |
|
public static class MockBean |
47 |
|
{ |
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
48 |
1 |
public boolean isEnabled()... |
49 |
|
{ |
50 |
1 |
return true; |
51 |
|
} |
52 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
53 |
1 |
public int getAge()... |
54 |
|
{ |
55 |
1 |
return 28; |
56 |
|
} |
57 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
58 |
1 |
public double getGrade()... |
59 |
|
{ |
60 |
1 |
return 9.48; |
61 |
|
} |
62 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
63 |
1 |
public String getName()... |
64 |
|
{ |
65 |
1 |
return "XWiki"; |
66 |
|
} |
67 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
68 |
1 |
public List<String> getItems()... |
69 |
|
{ |
70 |
1 |
return Arrays.asList("one"); |
71 |
|
} |
72 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
73 |
1 |
public Map<String, String> getParameters()... |
74 |
|
{ |
75 |
1 |
return Collections.singletonMap("foo", "bar"); |
76 |
|
} |
77 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
78 |
0 |
@Transient... |
79 |
|
public String getTransientProperty() |
80 |
|
{ |
81 |
0 |
return "transient"; |
82 |
|
} |
83 |
|
} |
84 |
|
|
85 |
|
|
86 |
|
|
87 |
|
|
88 |
|
private JSONTool tool = new JSONTool(); |
89 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1PASS
|
|
90 |
1 |
@Test... |
91 |
|
public void testSerializeNull() |
92 |
|
{ |
93 |
1 |
Assert.assertEquals("null", this.tool.serialize(null)); |
94 |
|
} |
95 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (18) |
Complexity: 1 |
Complexity Density: 0.06 |
1PASS
|
|
96 |
1 |
@Test... |
97 |
|
public void testSerializeMap() |
98 |
|
{ |
99 |
1 |
Map<String, Object> map = new HashMap<String, Object>(); |
100 |
1 |
map.put("bool", false); |
101 |
1 |
map.put("int", 13); |
102 |
1 |
map.put("double", 0.78); |
103 |
1 |
map.put("string", "foo"); |
104 |
1 |
map.put("array", new int[] { 9, 8 }); |
105 |
1 |
map.put("list", Arrays.asList("one", "two")); |
106 |
1 |
map.put("map", Collections.singletonMap("level2", true)); |
107 |
1 |
map.put("null", "null key value"); |
108 |
|
|
109 |
1 |
String json = this.tool.serialize(map); |
110 |
|
|
111 |
1 |
Assert.assertTrue(json.contains("\"bool\":false")); |
112 |
1 |
Assert.assertTrue(json.contains("\"int\":13")); |
113 |
1 |
Assert.assertTrue(json.contains("\"double\":0.78")); |
114 |
1 |
Assert.assertTrue(json.contains("\"string\":\"foo\"")); |
115 |
1 |
Assert.assertTrue(json.contains("\"array\":[9,8]")); |
116 |
1 |
Assert.assertTrue(json.contains("\"list\":[\"one\",\"two\"]")); |
117 |
1 |
Assert.assertTrue(json.contains("\"map\":{\"level2\":true}")); |
118 |
1 |
Assert.assertTrue(json.contains("\"null\":\"null key value\"")); |
119 |
|
} |
120 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
121 |
1 |
@Test... |
122 |
|
public void testSerializeList() |
123 |
|
{ |
124 |
1 |
Assert.assertEquals("[1,2]", this.tool.serialize(Arrays.asList(1, 2))); |
125 |
1 |
Assert.assertEquals("[1.3,2.4]", this.tool.serialize(new double[] { 1.3, 2.4 })); |
126 |
|
} |
127 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
128 |
1 |
@Test... |
129 |
|
public void testSerializeNumber() |
130 |
|
{ |
131 |
1 |
Assert.assertEquals("27", this.tool.serialize(27)); |
132 |
1 |
Assert.assertEquals("2.7", this.tool.serialize(2.7)); |
133 |
|
} |
134 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
135 |
1 |
@Test... |
136 |
|
public void testSerializeBoolean() |
137 |
|
{ |
138 |
1 |
Assert.assertEquals("false", this.tool.serialize(false)); |
139 |
1 |
Assert.assertEquals("true", this.tool.serialize(true)); |
140 |
|
} |
141 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1PASS
|
|
142 |
1 |
@Test... |
143 |
|
public void testSerializeString() |
144 |
|
{ |
145 |
1 |
Assert.assertEquals("\"\\\"te'st\\\"\"", this.tool.serialize("\"te'st\"")); |
146 |
|
} |
147 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 1 |
Complexity Density: 0.12 |
1PASS
|
|
148 |
1 |
@Test... |
149 |
|
public void testSerializeBean() |
150 |
|
{ |
151 |
1 |
String json = this.tool.serialize(new MockBean()); |
152 |
|
|
153 |
1 |
Assert.assertTrue(json.contains("\"age\":28")); |
154 |
1 |
Assert.assertTrue(json.contains("\"enabled\":true")); |
155 |
1 |
Assert.assertTrue(json.contains("\"grade\":9.48")); |
156 |
1 |
Assert.assertTrue(json.contains("\"items\":[\"one\"]")); |
157 |
1 |
Assert.assertTrue(json.contains("\"name\":\"XWiki\"")); |
158 |
1 |
Assert.assertTrue(json.contains("\"parameters\":{\"foo\":\"bar\"}")); |
159 |
1 |
Assert.assertFalse(json.contains("\"transientProperty\":\"transient\"")); |
160 |
|
} |
161 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
162 |
1 |
@Test... |
163 |
|
public void testParseArray() |
164 |
|
{ |
165 |
1 |
JSON json = this.tool.parse("[1,2,3]"); |
166 |
1 |
Assert.assertTrue(json.isArray()); |
167 |
1 |
Assert.assertEquals(3, json.size()); |
168 |
|
} |
169 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
170 |
1 |
@Test... |
171 |
|
public void testParseEmptyArray() |
172 |
|
{ |
173 |
1 |
JSON json = this.tool.parse("[]"); |
174 |
1 |
Assert.assertTrue(json.isArray()); |
175 |
1 |
Assert.assertTrue(json.isEmpty()); |
176 |
1 |
Assert.assertEquals(0, json.size()); |
177 |
|
} |
178 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
1PASS
|
|
179 |
1 |
@Test... |
180 |
|
public void testParseMap() |
181 |
|
{ |
182 |
1 |
JSONObject json = (JSONObject) this.tool.parse("{\"a\" : 1, \"b\": [1], \"c\": true}"); |
183 |
1 |
Assert.assertFalse(json.isArray()); |
184 |
1 |
Assert.assertFalse(json.isEmpty()); |
185 |
1 |
Assert.assertEquals(3, json.size()); |
186 |
1 |
Assert.assertTrue(json.getBoolean("c")); |
187 |
|
} |
188 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
189 |
1 |
@Test... |
190 |
|
public void testParseEmptyMap() |
191 |
|
{ |
192 |
1 |
JSONObject json = (JSONObject) this.tool.parse("{}"); |
193 |
1 |
Assert.assertFalse(json.isArray()); |
194 |
1 |
Assert.assertTrue(json.isEmpty()); |
195 |
1 |
Assert.assertEquals(0, json.size()); |
196 |
|
} |
197 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1PASS
|
|
198 |
1 |
@Test... |
199 |
|
public void testParseNull() |
200 |
|
{ |
201 |
1 |
Assert.assertTrue(this.tool.parse(null) instanceof JSONNull); |
202 |
|
} |
203 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1PASS
|
|
204 |
1 |
@Test... |
205 |
|
public void testParseEmptyString() |
206 |
|
{ |
207 |
1 |
Assert.assertNull(this.tool.parse("")); |
208 |
|
} |
209 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1PASS
|
|
210 |
1 |
@Test... |
211 |
|
public void testParseInvalidJSON() |
212 |
|
{ |
213 |
1 |
Assert.assertNull(this.tool.parse("This is not the JSON you are looking for...")); |
214 |
|
} |
215 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
1PASS
|
|
216 |
1 |
@Test... |
217 |
|
public void serializeOrgJsonObjectWorks() |
218 |
|
{ |
219 |
1 |
List<String> variants = new ArrayList<>(2); |
220 |
1 |
variants.add("{\"a\":\"b\",\"c\":true}"); |
221 |
1 |
variants.add("{\"c\":true,\"a\":\"b\"}"); |
222 |
1 |
org.json.JSONObject object = new org.json.JSONObject(); |
223 |
1 |
object.put("a", "b"); |
224 |
1 |
object.put("c", true); |
225 |
|
|
226 |
1 |
Assert.assertTrue(variants.contains(this.tool.serialize(object))); |
227 |
|
} |
228 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 1 |
Complexity Density: 0.09 |
1PASS
|
|
229 |
1 |
@Test... |
230 |
|
public void serializeNestedOrgJsonObjectWorks() |
231 |
|
{ |
232 |
1 |
List<String> variants = new ArrayList<>(2); |
233 |
1 |
variants.add("{\"before\":[\"nothing\"],\"json\":{\"a\":\"b\",\"c\":true},\"after\":42}"); |
234 |
1 |
variants.add("{\"before\":[\"nothing\"],\"json\":{\"c\":true,\"a\":\"b\"},\"after\":42}"); |
235 |
1 |
org.json.JSONObject object = new org.json.JSONObject(); |
236 |
1 |
object.put("a", "b"); |
237 |
1 |
object.put("c", true); |
238 |
1 |
Map<String, Object> map = new LinkedHashMap<>(); |
239 |
1 |
map.put("before", Collections.singletonList("nothing")); |
240 |
1 |
map.put("json", object); |
241 |
1 |
map.put("after", 42); |
242 |
|
|
243 |
1 |
Assert.assertTrue(variants.contains(this.tool.serialize(map))); |
244 |
|
} |
245 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
1PASS
|
|
246 |
1 |
@Test... |
247 |
|
public void serializeOrgJsonArrayWorks() |
248 |
|
{ |
249 |
1 |
org.json.JSONArray array = new org.json.JSONArray(); |
250 |
1 |
array.put("a"); |
251 |
1 |
array.put(42); |
252 |
1 |
array.put(true); |
253 |
1 |
Assert.assertEquals("[\"a\",42,true]", this.tool.serialize(array)); |
254 |
|
} |
255 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 1 |
Complexity Density: 0.11 |
1PASS
|
|
256 |
1 |
@Test... |
257 |
|
public void serializeNestedOrgJsonArrayWorks() |
258 |
|
{ |
259 |
1 |
org.json.JSONArray array = new org.json.JSONArray(); |
260 |
1 |
array.put("a"); |
261 |
1 |
array.put(42); |
262 |
1 |
array.put(true); |
263 |
1 |
Map<String, Object> map = new LinkedHashMap<>(); |
264 |
1 |
map.put("before", Collections.singletonList("nothing")); |
265 |
1 |
map.put("json", array); |
266 |
1 |
map.put("after", 42); |
267 |
1 |
Assert.assertEquals("{\"before\":[\"nothing\"],\"json\":[\"a\",42,true],\"after\":42}", this.tool.serialize(map)); |
268 |
|
} |
269 |
|
} |