1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package net.sf.json;
18
19 import junit.framework.TestCase;
20
21
22
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 }