Initial import
[jpf-core.git] / src / tests / gov / nasa / jpf / util / json / JSONParserTest.java
1 /*
2  * Copyright (C) 2014, United States Government, as represented by the
3  * Administrator of the National Aeronautics and Space Administration.
4  * All rights reserved.
5  *
6  * The Java Pathfinder core (jpf-core) platform is licensed under the
7  * Apache License, Version 2.0 (the "License"); you may not use this file except
8  * in compliance with the License. You may obtain a copy of the License at
9  * 
10  *        http://www.apache.org/licenses/LICENSE-2.0. 
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and 
16  * limitations under the License.
17  */
18
19 package gov.nasa.jpf.util.json;
20
21 import gov.nasa.jpf.util.test.TestJPF;
22
23 import org.junit.Test;
24
25 /**
26  *
27  * @author Ivan Mushketik
28  */
29 public class JSONParserTest extends TestJPF {
30
31   @Test
32   public void testOneLevelJSON() {
33     String json = "{"
34             + "\"key1\" : \"str\","
35             + "\"key2\" : 123"
36             + "}";
37     JSONObject o = parseJSON(json);
38
39     Value key1 = o.getValue("key1");
40     assert key1.getString().equals("str");
41     Value key2 = o.getValue("key2");
42     assert key2.getDouble() == 123;
43   }
44
45   @Test
46   public void testEmptyObject() {
47     String json = "{}";
48     JSONObject o = parseJSON(json);
49
50     assert o.getValue("noValue") == null;
51   }
52
53   @Test
54   public void testArrayParse() {
55     String json = "{"
56             + "\"key\" : ["
57             + "{ \"key1\" : 123 },"
58             + "{ \"key2\" : \"str\" } ]"
59             + "}";
60     JSONObject o = parseJSON(json);
61
62     Value objects[] = o.getValue("key").getArray();
63     assert objects[0].getObject().getValue("key1").getDouble() == 123;
64     assert objects[1].getObject().getValue("key2").getString().equals("str") == true;
65   }
66
67   @Test
68   public void testEmptyArray() {
69     String json = "{ \"emptyArr\" : [] }";
70     JSONObject o = parseJSON(json);
71
72     assert o.getValue("noArray") == null;
73   }
74
75   @Test
76   public void testIdentifacatorsParsing() {
77     String json = "{ "
78             + "\"Null\" : null,"
79             + "\"True\" : true,"
80             + "\"False\": false"
81             + "}";
82     JSONObject o = parseJSON(json);
83
84     assert o.getValue("Null").getString() == null;
85     assert o.getValue("True").getBoolean() == true;
86     assert o.getValue("False").getBoolean() == false;
87   }
88
89   @Test
90   public void testCGCallParsing() {
91     String json = "{"
92             + "\"id\" : CGName(1, \"str\", true, false, null)"
93             + "}";
94
95     JSONObject o = parseJSON(json);
96     CGCall cgCall = o.getCGCall("id");
97
98     assert cgCall.getName().equals("CGName") == true;
99     Value params[] = cgCall.getValues();
100
101     assertEquals(1d, params[0].getDouble(), 0.0001);
102     assert params[1].getString().equals("str") == true;
103     assert params[2].getBoolean() == true;
104     assert params[3].getBoolean() == false;
105     assert params[4].getDouble() == null;
106   }
107
108   private JSONObject parseJSON(String json) {
109     JSONLexer lexer = new JSONLexer(json);
110     JSONParser parser = new JSONParser(lexer);
111     JSONObject o = parser.parse();
112     return o;
113   }
114 }