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 AbstractJSONObjectStaticBuildersTestCase extends TestCase {
25     public AbstractJSONObjectStaticBuildersTestCase( String testName ) {
26        super( testName );
27     }
28  
29     public void testFromObject() {
30        JSONObject json = JSONObject.fromObject( getSource() );
31        assertJSONObject( json, getProperties() );
32        assertTrue( !json.has( "class" ) );
33     }
34  
35     public void testFromObject_excludes() {
36        JsonConfig jsonConfig = new JsonConfig();
37        jsonConfig.setExcludes( getExclusions() );
38        JSONObject json = JSONObject.fromObject( getSource(), jsonConfig );
39        assertJSONObject( json, getProperties() );
40        String[] excluded = getExclusions();
41        for( int i = 0; i < excluded.length; i++ ){
42           assertTrue( !json.has( excluded[i] ) );
43        }
44        assertTrue( !json.has( "class" ) );
45        assertTrue( !json.has( "pexcluded" ) );
46     }
47  
48     public void testFromObject_excludes_ignoreDefaults() {
49        JsonConfig jsonConfig = new JsonConfig();
50        jsonConfig.setExcludes( getExclusions() );
51        jsonConfig.setIgnoreDefaultExcludes( true );
52        JSONObject json = JSONObject.fromObject( getSource(), jsonConfig );
53        assertJSONObject( json, getProperties() );
54        assertTrue( json.has( "class" ) );
55        assertTrue( !json.has( "pexcluded" ) );
56     }
57  
58     protected String[] getExclusions() {
59        return new String[] { "pexcluded" };
60     }
61  
62     protected String[] getProperties() {
63        return PropertyConstants.getProperties();
64     }
65  
66     protected abstract Object getSource();
67  
68     private void assertJSONObject( JSONObject json, String[] properties ) {
69        assertNotNull( json );
70        for( int i = 0; i < properties.length; i++ ){
71           assertTrue( json.has( properties[i] ) );
72        }
73     }
74  }