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.List;
20  
21  import junit.framework.TestCase;
22  import net.sf.json.test.JSONAssert;
23  
24  /**
25   * @author Andres Almiray <aalmiray@users.sourceforge.net>
26   */
27  public class TestJSONArrayAsList extends TestCase {
28     public static void main( String[] args ) {
29        junit.textui.TestRunner.run( TestJSONArrayAsList.class );
30     }
31  
32     private JSONArray jsonArray;
33  
34     public TestJSONArrayAsList( String name ) {
35        super( name );
36     }
37  
38     public void testAdd() {
39        assertEquals( 5, jsonArray.size() );
40        jsonArray.add( "value" );
41        assertEquals( 6, jsonArray.size() );
42     }
43  
44     public void testAdd_index_value() {
45        assertEquals( 5, jsonArray.size() );
46        Object first = jsonArray.get( 0 );
47        jsonArray.add( 0, "value" );
48        assertEquals( 6, jsonArray.size() );
49        assertEquals( "value", jsonArray.get( 0 ) );
50        assertEquals( first, jsonArray.get( 1 ) );
51     }
52  
53     public void testAddAll() {
54        JSONArray array = new JSONArray();
55        array.addAll( jsonArray );
56        JSONAssert.assertEquals( jsonArray, array );
57     }
58  
59     public void testAddAll_index_value() {
60        JSONArray array = new JSONArray().element( "value" );
61        array.addAll( 0, jsonArray );
62        assertEquals( 6, array.size() );
63        assertEquals( "value", array.get( 5 ) );
64     }
65  
66     public void testClear() {
67        assertEquals( 5, jsonArray.size() );
68        jsonArray.clear();
69        assertEquals( 0, jsonArray.size() );
70     }
71  
72     public void testContains() {
73        assertTrue( jsonArray.contains( "1" ) );
74        assertFalse( jsonArray.contains( "2" ) );
75     }
76  
77     public void testContainsAll() {
78        assertTrue( jsonArray.containsAll( jsonArray ) );
79     }
80  
81     public void testIndexOf() {
82        jsonArray.element( "1" );
83        assertEquals( 0, jsonArray.indexOf( "1" ) );
84     }
85  
86     public void testIsEmpty() {
87        assertFalse( jsonArray.isEmpty() );
88     }
89  
90     public void testLastIndexOf() {
91        jsonArray.element( "1" );
92        assertEquals( 5, jsonArray.lastIndexOf( "1" ) );
93     }
94  
95     public void testRemove() {
96        assertEquals( 5, jsonArray.size() );
97        jsonArray.remove( "string" );
98        assertEquals( 4, jsonArray.size() );
99        assertTrue( !jsonArray.contains( "string" ) );
100    }
101 
102    public void testRemove_index() {
103       assertEquals( 5, jsonArray.size() );
104       jsonArray.remove( 2 );
105       assertEquals( 4, jsonArray.size() );
106       assertTrue( !jsonArray.contains( "string" ) );
107    }
108 
109    public void testRemoveAll() {
110       assertEquals( 5, jsonArray.size() );
111       jsonArray.removeAll( jsonArray );
112       assertEquals( 0, jsonArray.size() );
113    }
114 
115    public void testRetainAll() {
116       assertEquals( 5, jsonArray.size() );
117       jsonArray.retainAll( jsonArray );
118       assertEquals( 5, jsonArray.size() );
119    }
120 
121    public void testSubList() {
122       List actual = jsonArray.subList( 0, 3 );
123       JSONArray expected = new JSONArray().element( "1" )
124             .element( "true" )
125             .element( "string" );
126       JSONAssert.assertEquals( expected, JSONArray.fromObject( actual ) );
127    }
128 
129    /*
130     * public void testToArray() { } public void testToArray_array() { }
131     */
132 
133    protected void setUp() throws Exception {
134       jsonArray = new JSONArray().element( "1" )
135             .element( "true" )
136             .element( "string" )
137             .element( "function(){ return this; }" )
138             .element( "[1,2,3]" );
139    }
140 }