1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package net.sf.json;
18
19 import java.util.HashMap;
20 import java.util.Map;
21
22 import junit.framework.TestCase;
23
24
25
26
27 public class TestJSONObjectAsMap extends TestCase {
28 public static void main( String[] args ) {
29 junit.textui.TestRunner.run( TestJSONObjectAsMap.class );
30 }
31
32 private JSONObject jsonObject;
33
34 public TestJSONObjectAsMap( String name ) {
35 super( name );
36 }
37
38 public void testClear() {
39 assertEquals( 6, jsonObject.size() );
40 jsonObject.clear();
41 assertEquals( 0, jsonObject.size() );
42 }
43
44 public void testContainsKey() {
45 assertTrue( jsonObject.containsKey( "func" ) );
46 assertFalse( jsonObject.containsKey( "bogus" ) );
47 }
48
49 public void testContainsValue() {
50 assertTrue( jsonObject.containsValue( "string" ) );
51 }
52
53 public void testIsEmpty() {
54 assertFalse( jsonObject.isEmpty() );
55 }
56
57 public void testPut() {
58 Object key = "key";
59 Object value = "value";
60 jsonObject.put( key, value );
61 assertEquals( value, jsonObject.get( key ) );
62 }
63
64 public void testPutAll() {
65 JSONObject json = new JSONObject();
66 Map map = new HashMap();
67 map.put( "key", "value" );
68 json.putAll( map );
69 assertEquals( 1, json.size() );
70 assertEquals( "value", json.get( "key" ) );
71 map.put( "key", "value2" );
72 json.putAll( map );
73 assertEquals( 1, json.size() );
74 assertEquals( "value2", json.get( "key" ) );
75 }
76
77 public void testRemove() {
78 assertTrue( jsonObject.has( "func" ) );
79 jsonObject.remove( "func" );
80 assertFalse( jsonObject.has( "func" ) );
81 }
82
83 protected void setUp() throws Exception {
84 jsonObject = new JSONObject().element( "int", "1" )
85 .element( "long", "1" )
86 .element( "boolean", "true" )
87 .element( "string", "string" )
88 .element( "func", "function(){ return this; }" )
89 .element( "array", "[1,2,3]" );
90 }
91 }