2 * License : The MIT License
3 * Copyright(c) 2009 olyutorskii
6 package jp.sourceforge.jovsonz;
8 import java.util.Iterator;
9 import java.util.LinkedList;
10 import java.util.List;
12 import org.junit.After;
13 import org.junit.AfterClass;
14 import org.junit.Before;
15 import org.junit.BeforeClass;
16 import org.junit.Test;
18 import static org.junit.Assert.*;
23 public class JsObjectTest {
25 public JsObjectTest() {
29 public static void setUpClass() throws Exception{
33 public static void tearDownClass() throws Exception{
41 public void tearDown() {
45 * Test of parseObject method, of class JsObject.
48 public void testParseObject() throws Exception{
49 System.out.println("parseObject");
54 source = new JsonSource("{}");
55 object = JsObject.parseObject(source);
56 assertEquals(0, object.size());
58 source = new JsonSource("{\"A\":true}");
59 object = JsObject.parseObject(source);
60 assertEquals(1, object.size());
61 assertEquals(JsBoolean.TRUE, object.getPair("A").getValue());
63 source = new JsonSource("{\"A\":true,\"B\":false}");
64 object = JsObject.parseObject(source);
65 assertEquals(2, object.size());
66 assertEquals(JsBoolean.TRUE, object.getPair("A").getValue());
67 assertEquals(JsBoolean.FALSE, object.getPair("B").getValue());
69 source = new JsonSource("\n{\r\"A\"\t: true,\"B\":false\n}\r");
70 object = JsObject.parseObject(source);
73 source = new JsonSource("{\r\"A\"\t: true,\"B\":false\n}\r");
74 object = JsObject.parseObject(source);
75 assertEquals(2, object.size());
76 assertEquals(JsBoolean.TRUE, object.getPair("A").getValue());
77 assertEquals(JsBoolean.FALSE, object.getPair("B").getValue());
80 source = new JsonSource("{,}");
81 object = JsObject.parseObject(source);
83 }catch(JsParseException e){
88 source = new JsonSource("{true,}");
89 object = JsObject.parseObject(source);
91 }catch(JsParseException e){
96 source = new JsonSource("{true");
97 object = JsObject.parseObject(source);
99 }catch(JsParseException e){
104 source = new JsonSource("{\"A\",");
105 object = JsObject.parseObject(source);
107 }catch(JsParseException e){
112 source = new JsonSource("{\"A\":");
113 object = JsObject.parseObject(source);
115 }catch(JsParseException e){
120 source = new JsonSource("{\"A\":#");
121 object = JsObject.parseObject(source);
123 }catch(JsParseException e){
128 source = new JsonSource("{\"A\":true#");
129 object = JsObject.parseObject(source);
131 }catch(JsParseException e){
136 source = new JsonSource("{\"A\":true,");
137 object = JsObject.parseObject(source);
139 }catch(JsParseException e){
144 source = new JsonSource("{\"A\":true,#");
145 object = JsObject.parseObject(source);
147 }catch(JsParseException e){
151 source = new JsonSource("true}");
152 object = JsObject.parseObject(source);
159 * Test of putValue method, of class JsObject.
162 public void testPutGetValue(){
163 System.out.println("putValue");
165 JsObject object = new JsObject();
166 assertEquals(0, object.size());
168 object.putValue("x", JsNull.NULL);
169 assertEquals(1, object.size());
170 assertEquals(JsNull.NULL, object.getValue("x"));
171 assertEquals(null, object.getValue("y"));
173 object.putValue("y", JsBoolean.TRUE);
174 assertEquals(2, object.size());
175 assertEquals(JsBoolean.TRUE, object.getValue("y"));
177 object.putValue("x", JsBoolean.FALSE);
178 assertEquals(2, object.size());
179 assertEquals(JsBoolean.FALSE, object.getValue("x"));
182 object.putValue("x", null);
184 }catch(NullPointerException e){
189 object.putValue(null, JsNull.NULL);
191 }catch(NullPointerException e){
199 * Test of putPair method, of class JsObject.
202 public void testPutGetPair(){
203 System.out.println("putPair");
205 JsObject object = new JsObject();
206 assertEquals(0, object.size());
208 JsPair pair = new JsPair("x", JsNull.NULL);
209 object.putPair(pair);
210 assertEquals(1, object.size());
211 assertEquals(JsNull.NULL, object.getValue("x"));
213 JsPair pair2 = object.getPair("x");
214 assertNotNull(pair2);
215 assertEquals("x", pair2.getName());
216 assertEquals(JsNull.NULL, pair2.getValue());
218 assertNull(object.getPair("y"));
224 * Test of clear method, of class JsObject.
227 public void testClear(){
228 System.out.println("clear");
230 JsObject object = new JsObject();
231 assertEquals(0, object.size());
233 object.putValue("x", JsNull.NULL);
234 assertEquals(1, object.size());
237 assertEquals(0, object.size());
239 object = new JsObject();
240 object.putValue("x", JsNull.NULL);
241 assertEquals(1, object.size());
242 assertTrue(object.hasChanged());
243 object.setUnchanged();
244 assertFalse(object.hasChanged());
246 assertEquals(0, object.size());
247 assertTrue(object.hasChanged());
248 object.setUnchanged();
250 assertEquals(0, object.size());
251 assertFalse(object.hasChanged());
257 * Test of remove method, of class JsObject.
260 public void testRemove(){
261 System.out.println("remove");
263 JsObject object = new JsObject();
265 object.putValue("x", JsNull.NULL);
266 assertEquals(1, object.size());
268 assertNotNull(object.getValue("x"));
270 assertEquals(JsNull.NULL, object.remove("x").getValue());
271 assertEquals(0, object.size());
272 assertNull(object.getValue("x"));
274 assertNull(object.remove("y"));
280 * Test of nameSet method, of class JsObject.
283 public void testNameSet(){
284 System.out.println("nameSet");
286 JsObject object = new JsObject();
289 set = object.nameSet();
290 assertEquals(0, set.size());
292 object.putValue("y", JsNull.NULL);
293 object.putValue("z", JsNull.NULL);
294 object.putValue("x", JsNull.NULL);
296 set = object.nameSet();
297 assertEquals(3, set.size());
298 Object[] names = set.toArray();
300 assertEquals("x", names[0]);
301 assertEquals("y", names[1]);
302 assertEquals("z", names[2]);
308 * Test of getPairList method, of class JsObject.
311 public void testGetPairList(){
312 System.out.println("getPairList");
314 JsObject object = new JsObject();
317 list = object.getPairList();
318 assertEquals(0, list.size());
320 object.putValue("y", JsNull.NULL);
321 object.putValue("z", JsBoolean.TRUE);
322 object.putValue("x", JsBoolean.FALSE);
324 list = object.getPairList();
325 assertEquals(3, list.size());
327 assertEquals("x", list.get(0).getName());
328 assertEquals("y", list.get(1).getName());
329 assertEquals("z", list.get(2).getName());
330 assertEquals(JsBoolean.FALSE, list.get(0).getValue());
331 assertEquals(JsNull.NULL, list.get(1).getValue());
332 assertEquals(JsBoolean.TRUE, list.get(2).getValue());
338 * Test of iterator method, of class JsObject.
341 public void testIterator(){
342 System.out.println("iterator");
344 JsObject object = new JsObject();
345 object.putValue("y", JsBoolean.FALSE);
346 object.putValue("x", JsBoolean.TRUE);
348 Iterator<JsPair> it = object.iterator();
350 assertTrue(it.hasNext());
351 assertEquals(JsBoolean.TRUE, it.next().getValue());
352 assertTrue(it.hasNext());
353 assertEquals(JsBoolean.FALSE, it.next().getValue());
354 assertFalse(it.hasNext());
360 * Test of size method, of class JsObject.
363 public void testSize(){
364 System.out.println("size");
366 JsObject object = new JsObject();
367 assertEquals(0, object.size());
368 assertTrue(object.isEmpty());
370 object.putValue("x", JsBoolean.TRUE);
371 assertEquals(1, object.size());
372 assertFalse(object.isEmpty());
374 object.putValue("y", JsBoolean.FALSE);
375 assertEquals(2, object.size());
376 assertFalse(object.isEmpty());
382 * Test of hashCode method, of class JsObject.
385 public void testHashCode(){
386 System.out.println("hashCode");
388 JsObject obj1 = new JsObject();
389 JsObject obj2 = new JsObject();
390 assertEquals(obj1.hashCode(), obj2.hashCode());
392 obj1.putValue("x", new JsNumber("99"));
393 obj2.putValue("x", new JsNumber("99"));
394 assertEquals(obj1.hashCode(), obj2.hashCode());
400 * Test of equals method, of class JsObject.
403 public void testEquals(){
404 System.out.println("equals");
406 JsObject obj1 = new JsObject();
407 JsObject obj2 = new JsObject();
408 assertTrue(obj1.equals(obj2));
410 obj1.putValue("x", new JsNumber("99"));
411 obj2.putValue("x", new JsNumber("99"));
412 assertTrue(obj1.equals(obj2));
414 obj1.putValue("x", new JsNumber("99"));
415 obj2.putValue("x", new JsNumber("999"));
416 assertFalse(obj1.equals(obj2));
418 JsObject nullVal = null;
419 assertFalse(obj1.equals(nullVal));
420 assertFalse(obj1.equals(""));
426 * Test of toString method, of class JsObject.
429 public void testToString(){
430 System.out.println("toString");
432 JsObject object = new JsObject();
434 assertEquals("{}", object.toString());
436 object.putValue("x", JsBoolean.TRUE);
437 assertEquals("{\"x\":true}", object.toString());
439 object.putValue("y", JsBoolean.FALSE);
440 assertEquals("{\"x\":true,\"y\":false}", object.toString());
442 object.putValue("z", new JsObject());
443 assertEquals("{\"x\":true,\"y\":false,\"z\":{}}", object.toString());
449 * Test of traverse method, of class JsObject.
452 public void testTraverse() throws Exception{
453 System.out.println("traverse");
455 JsObject obj = new JsObject();
456 JsValue val1 = new JsNumber("12");
457 JsValue val2 = new JsString("AB");
458 obj.putValue("x", val1);
459 obj.putValue("y", val2);
461 final List<Object> visited = new LinkedList<Object>();
464 obj.traverse(new ValueVisitor(){
465 public void visitValue(JsValue value)
466 throws JsVisitException{
471 public void visitPairName(String name)
472 throws JsVisitException{
477 public void visitCompositionClose(JsComposition<?> composite)
478 throws JsVisitException{
479 visited.add(composite);
483 }catch(JsVisitException e){
487 assertEquals(6, visited.size());
488 assertEquals(obj, visited.get(0));
489 assertEquals("x", visited.get(1));
490 assertEquals(val1, visited.get(2));
491 assertEquals("y", visited.get(3));
492 assertEquals(val2, visited.get(4));
493 assertEquals(obj, visited.get(5));
499 * Test of hasChanged method, of class JsObject.
502 public void testHasChanged(){
503 System.out.println("hasChanged");
505 JsObject obj = new JsObject();
506 assertFalse(obj.hasChanged());
508 obj.putValue("x", JsNull.NULL);
509 assertTrue(obj.hasChanged());
512 assertFalse(obj.hasChanged());
514 JsObject child = new JsObject();
515 obj.putValue("y", child);
517 assertFalse(obj.hasChanged());
519 child.putValue("z", JsBoolean.TRUE);
520 assertTrue(obj.hasChanged());
522 assertFalse(obj.hasChanged());
528 * Test of setUnchanged method, of class JsObject.
531 public void testSetUnchanged(){
532 System.out.println("setUnchanged");
534 JsObject obj = new JsObject();
535 JsObject child = new JsObject();
536 obj.putValue("x", child);
538 child.putValue("y", JsNull.NULL);
539 assertTrue(child.hasChanged());
542 assertFalse(child.hasChanged());
548 * Test of getJsTypes method, of class JsObject.
551 public void testGetJsTypes() {
552 System.out.println("getJsTypes");
554 JsObject instance = new JsObject();
556 assertEquals(JsTypes.OBJECT, instance.getJsTypes());