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 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 }