1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.velocity.tools

File JSONToolTest.java

 

Code metrics

0
92
25
2
269
207
25
0.27
3.68
12.5
1

Classes

Class Line # Actions
JSONToolTest 44 85 0% 18 0
1.0100%
JSONToolTest.MockBean 46 7 0% 7 2
0.8571428785.7%
 

Contributing tests

This file is covered by 18 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
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    * Unit tests for {@link JSONTool}.
40    *
41    * @version $Id: e6bb35b63e24d58e76b0e5d89d23fc2f93dcf5b5 $
42    * @since 4.0M2
43    */
 
44    public class JSONToolTest
45    {
 
46    public static class MockBean
47    {
 
48  1 toggle public boolean isEnabled()
49    {
50  1 return true;
51    }
52   
 
53  1 toggle public int getAge()
54    {
55  1 return 28;
56    }
57   
 
58  1 toggle public double getGrade()
59    {
60  1 return 9.48;
61    }
62   
 
63  1 toggle public String getName()
64    {
65  1 return "XWiki";
66    }
67   
 
68  1 toggle public List<String> getItems()
69    {
70  1 return Arrays.asList("one");
71    }
72   
 
73  1 toggle public Map<String, String> getParameters()
74    {
75  1 return Collections.singletonMap("foo", "bar");
76    }
77   
 
78  0 toggle @Transient
79    public String getTransientProperty()
80    {
81  0 return "transient";
82    }
83    }
84   
85    /**
86    * The object being tested.
87    */
88    private JSONTool tool = new JSONTool();
89   
 
90  1 toggle @Test
91    public void testSerializeNull()
92    {
93  1 Assert.assertEquals("null", this.tool.serialize(null));
94    }
95   
 
96  1 toggle @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    // We can't predict the order in the map.
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   
 
121  1 toggle @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   
 
128  1 toggle @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   
 
135  1 toggle @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   
 
142  1 toggle @Test
143    public void testSerializeString()
144    {
145  1 Assert.assertEquals("\"\\\"te'st\\\"\"", this.tool.serialize("\"te'st\""));
146    }
147   
 
148  1 toggle @Test
149    public void testSerializeBean()
150    {
151  1 String json = this.tool.serialize(new MockBean());
152    // We can't predict the order in the map.
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   
 
162  1 toggle @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   
 
170  1 toggle @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   
 
179  1 toggle @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   
 
189  1 toggle @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   
 
198  1 toggle @Test
199    public void testParseNull()
200    {
201  1 Assert.assertTrue(this.tool.parse(null) instanceof JSONNull);
202    }
203   
 
204  1 toggle @Test
205    public void testParseEmptyString()
206    {
207  1 Assert.assertNull(this.tool.parse(""));
208    }
209   
 
210  1 toggle @Test
211    public void testParseInvalidJSON()
212    {
213  1 Assert.assertNull(this.tool.parse("This is not the JSON you are looking for..."));
214    }
215   
 
216  1 toggle @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    // Assert.assertEquals(variants.get(0), this.tool.serialize(object));
226  1 Assert.assertTrue(variants.contains(this.tool.serialize(object)));
227    }
228   
 
229  1 toggle @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    // Assert.assertEquals(variants.get(0), this.tool.serialize(map));
243  1 Assert.assertTrue(variants.contains(this.tool.serialize(map)));
244    }
245   
 
246  1 toggle @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   
 
256  1 toggle @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    }