@@ -157,7 +157,7 @@ public void tokenListIsCopiedInConstructor() {
157157 }
158158
159159 /**
160- * Coverage for JSONObject queryFrom( )
160+ * Coverage for JSONObject query(String )
161161 */
162162 @ Test
163163 public void queryFromJSONObject () {
@@ -187,7 +187,61 @@ public void queryFromJSONObject() {
187187 }
188188
189189 /**
190- * Coverage for JSONArray queryFrom()
190+ * Coverage for JSONObject query(JSONPointer)
191+ */
192+ @ Test
193+ public void queryFromJSONObjectUsingPointer () {
194+ String str = "{" +
195+ "\" stringKey\" :\" hello world!\" ," +
196+ "\" arrayKey\" :[0,1,2]," +
197+ "\" objectKey\" : {" +
198+ "\" a\" :\" aVal\" ," +
199+ "\" b\" :\" bVal\" " +
200+ "}" +
201+ "}" ;
202+ JSONObject jsonObject = new JSONObject (str );
203+ Object obj = jsonObject .query (new JSONPointer ("/stringKey" ));
204+ assertTrue ("Expected 'hello world!'" , "hello world!" .equals (obj ));
205+ obj = jsonObject .query (new JSONPointer ("/arrayKey/1" ));
206+ assertTrue ("Expected 1" , Integer .valueOf (1 ).equals (obj ));
207+ obj = jsonObject .query (new JSONPointer ("/objectKey/b" ));
208+ assertTrue ("Expected bVal" , "bVal" .equals (obj ));
209+ try {
210+ obj = jsonObject .query (new JSONPointer ("/a/b/c" ));
211+ assertTrue ("Expected JSONPointerException" , false );
212+ } catch (JSONPointerException e ) {
213+ assertTrue ("Expected bad key/value exception" ,
214+ "value [null] is not an array or object therefore its key b cannot be resolved" .
215+ equals (e .getMessage ()));
216+ }
217+ }
218+
219+ /**
220+ * Coverage for JSONObject optQuery(JSONPointer)
221+ */
222+ @ Test
223+ public void optQueryFromJSONObjectUsingPointer () {
224+ String str = "{" +
225+ "\" stringKey\" :\" hello world!\" ," +
226+ "\" arrayKey\" :[0,1,2]," +
227+ "\" objectKey\" : {" +
228+ "\" a\" :\" aVal\" ," +
229+ "\" b\" :\" bVal\" " +
230+ "}" +
231+ "}" ;
232+ JSONObject jsonObject = new JSONObject (str );
233+ Object obj = jsonObject .optQuery (new JSONPointer ("/stringKey" ));
234+ assertTrue ("Expected 'hello world!'" , "hello world!" .equals (obj ));
235+ obj = jsonObject .optQuery (new JSONPointer ("/arrayKey/1" ));
236+ assertTrue ("Expected 1" , Integer .valueOf (1 ).equals (obj ));
237+ obj = jsonObject .optQuery (new JSONPointer ("/objectKey/b" ));
238+ assertTrue ("Expected bVal" , "bVal" .equals (obj ));
239+ obj = jsonObject .optQuery (new JSONPointer ("/a/b/c" ));
240+ assertTrue ("Expected null" , obj == null );
241+ }
242+
243+ /**
244+ * Coverage for JSONArray query(String)
191245 */
192246 @ Test
193247 public void queryFromJSONArray () {
@@ -214,4 +268,57 @@ public void queryFromJSONArray() {
214268 "a is not an array index" .equals (e .getMessage ()));
215269 }
216270 }
271+
272+ /**
273+ * Coverage for JSONArray query(JSONPointer)
274+ */
275+ @ Test
276+ public void queryFromJSONArrayUsingPointer () {
277+ String str = "[" +
278+ "\" hello world!\" ," +
279+ "[0,1,2]," +
280+ "{" +
281+ "\" a\" :\" aVal\" ," +
282+ "\" b\" :\" bVal\" " +
283+ "}" +
284+ "]" ;
285+ JSONArray jsonArray = new JSONArray (str );
286+ Object obj = jsonArray .query (new JSONPointer ("/0" ));
287+ assertTrue ("Expected 'hello world!'" , "hello world!" .equals (obj ));
288+ obj = jsonArray .query (new JSONPointer ("/1/1" ));
289+ assertTrue ("Expected 1" , Integer .valueOf (1 ).equals (obj ));
290+ obj = jsonArray .query (new JSONPointer ("/2/b" ));
291+ assertTrue ("Expected bVal" , "bVal" .equals (obj ));
292+ try {
293+ obj = jsonArray .query (new JSONPointer ("/a/b/c" ));
294+ assertTrue ("Expected JSONPointerException" , false );
295+ } catch (JSONPointerException e ) {
296+ assertTrue ("Expected bad index exception" ,
297+ "a is not an array index" .equals (e .getMessage ()));
298+ }
299+ }
300+
301+ /**
302+ * Coverage for JSONArray optQuery(JSONPointer)
303+ */
304+ @ Test
305+ public void optQueryFromJSONArrayUsingPointer () {
306+ String str = "[" +
307+ "\" hello world!\" ," +
308+ "[0,1,2]," +
309+ "{" +
310+ "\" a\" :\" aVal\" ," +
311+ "\" b\" :\" bVal\" " +
312+ "}" +
313+ "]" ;
314+ JSONArray jsonArray = new JSONArray (str );
315+ Object obj = jsonArray .optQuery (new JSONPointer ("/0" ));
316+ assertTrue ("Expected 'hello world!'" , "hello world!" .equals (obj ));
317+ obj = jsonArray .optQuery (new JSONPointer ("/1/1" ));
318+ assertTrue ("Expected 1" , Integer .valueOf (1 ).equals (obj ));
319+ obj = jsonArray .optQuery (new JSONPointer ("/2/b" ));
320+ assertTrue ("Expected bVal" , "bVal" .equals (obj ));
321+ obj = jsonArray .optQuery (new JSONPointer ("/a/b/c" ));
322+ assertTrue ("Expected null" , obj == null );
323+ }
217324}
0 commit comments