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 junit.framework.TestCase;
20  
21  /**
22   * @author Andres Almiray <aalmiray@users.sourceforge.net>
23   */
24  public abstract class AbstractJSONArrayStaticBuildersTestCase extends TestCase {
25     public AbstractJSONArrayStaticBuildersTestCase( String testName ) {
26        super( testName );
27     }
28  
29     public void testFromObject() {
30        JSONArray jsonArray = JSONArray.fromObject( getSource() );
31        assertNotNull( jsonArray );
32        assertEquals( 1, jsonArray.size() );
33        JSONObject jsonObject = jsonArray.getJSONObject( 0 );
34        assertJSONObject( jsonObject, getProperties() );
35        assertTrue( !jsonObject.has( "class" ) );
36     }
37  
38     public void testFromObject_excludes() {
39        JsonConfig jsonConfig = new JsonConfig();
40        jsonConfig.setExcludes( getExclusions() );
41        JSONArray jsonArray = JSONArray.fromObject( getSource(), jsonConfig );
42        assertNotNull( jsonArray );
43        assertEquals( 1, jsonArray.size() );
44        JSONObject jsonObject = jsonArray.getJSONObject( 0 );
45        assertJSONObject( jsonObject, getProperties() );
46        String[] excluded = getExclusions();
47        for( int i = 0; i < excluded.length; i++ ){
48           assertTrue( !jsonObject.has( excluded[i] ) );
49        }
50        assertTrue( !jsonObject.has( "class" ) );
51        assertTrue( !jsonObject.has( "pexcluded" ) );
52     }
53  
54     public void testFromObject_excludes_ignoreDefaults() {
55        JsonConfig jsonConfig = new JsonConfig();
56        jsonConfig.setExcludes( getExclusions() );
57        jsonConfig.setIgnoreDefaultExcludes( true );
58        JSONArray jsonArray = JSONArray.fromObject( getSource(), jsonConfig );
59        assertNotNull( jsonArray );
60        assertEquals( 1, jsonArray.size() );
61        JSONObject jsonObject = jsonArray.getJSONObject( 0 );
62        assertJSONObject( jsonObject, getProperties() );
63        assertTrue( jsonObject.has( "class" ) );
64        assertTrue( !jsonObject.has( "pexcluded" ) );
65     }
66  
67     protected String[] getExclusions() {
68        return new String[] { "pexcluded" };
69     }
70  
71     protected String[] getProperties() {
72        return PropertyConstants.getProperties();
73     }
74  
75     protected abstract Object getSource();
76  
77     private void assertJSONObject( JSONObject json, String[] properties ) {
78        assertNotNull( json );
79        for( int i = 0; i < properties.length; i++ ){
80           assertTrue( json.has( properties[i] ) );
81        }
82     }
83  }