1   /*
2    * Copyright 2002-2009 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * @author Andres Almiray <aalmiray@users.sourceforge.net>
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  }