1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package net.sf.json;
18
19 import java.util.Calendar;
20 import java.util.Date;
21 import java.util.HashMap;
22 import java.util.Map;
23
24 import junit.framework.TestCase;
25 import net.sf.json.processors.JsDateJsonBeanProcessor;
26 import net.sf.json.processors.JsDateJsonValueProcessor;
27 import net.sf.json.sample.DateBean;
28 import net.sf.json.sample.IdentityJsonValueProcessor;
29 import net.sf.json.test.JSONAssert;
30
31
32
33
34 public class TestJSONObjectWithProcessors extends TestCase {
35 public static void main( String[] args ) {
36 junit.textui.TestRunner.run( TestJSONObjectWithProcessors.class );
37 }
38
39 private Date date;
40 private JsonConfig jsonConfig;
41
42 public TestJSONObjectWithProcessors( String name ) {
43 super( name );
44 }
45
46 public void testBeanWithDateProperty_fromObject_withJsonBeanProcessor() {
47 DateBean bean = new DateBean();
48 bean.setDate( date );
49 bean.setValue( 42 );
50 jsonConfig.registerJsonBeanProcessor( Date.class, new JsDateJsonBeanProcessor() );
51 JSONObject jsonObject = JSONObject.fromObject( bean, jsonConfig );
52 assertNotNull( jsonObject );
53 assertEquals( 42, jsonObject.getInt( "value" ) );
54 JSONObject jsDate = jsonObject.getJSONObject( "date" );
55 assertJsDate( jsDate );
56 }
57
58 public void testBeanWithDateProperty_fromObject_withJsonValueProcessor_1() {
59 DateBean bean = new DateBean();
60 bean.setDate( date );
61 bean.setValue( 42 );
62 jsonConfig.registerJsonValueProcessor( DateBean.class, Date.class,
63 new JsDateJsonValueProcessor() );
64 JSONObject jsonObject = JSONObject.fromObject( bean, jsonConfig );
65 assertNotNull( jsonObject );
66 assertEquals( 42, jsonObject.getInt( "value" ) );
67 JSONObject jsDate = jsonObject.getJSONObject( "date" );
68 assertJsDate( jsDate );
69 }
70
71 public void testBeanWithDateProperty_fromObject_withJsonValueProcessor_2() {
72 DateBean bean = new DateBean();
73 bean.setDate( date );
74 bean.setValue( 42 );
75 jsonConfig.registerJsonValueProcessor( DateBean.class, "date", new JsDateJsonValueProcessor() );
76 JSONObject jsonObject = JSONObject.fromObject( bean, jsonConfig );
77 assertNotNull( jsonObject );
78 assertEquals( 42, jsonObject.getInt( "value" ) );
79 JSONObject jsDate = jsonObject.getJSONObject( "date" );
80 assertJsDate( jsDate );
81 }
82
83 public void testBeanWithDateProperty_fromObject_withJsonValueProcessor_3() {
84 DateBean bean = new DateBean();
85 bean.setDate( date );
86 bean.setValue( 42 );
87 jsonConfig.registerJsonValueProcessor( Date.class, new JsDateJsonValueProcessor() );
88 JSONObject jsonObject = JSONObject.fromObject( bean, jsonConfig );
89 assertNotNull( jsonObject );
90 assertEquals( 42, jsonObject.getInt( "value" ) );
91 JSONObject jsDate = jsonObject.getJSONObject( "date" );
92 assertJsDate( jsDate );
93 }
94
95 public void testBeanWithDateProperty_fromObject_withJsonValueProcessor_4() {
96 DateBean bean = new DateBean();
97 bean.setDate( date );
98 bean.setValue( 42 );
99 jsonConfig.registerJsonValueProcessor( "date", new JsDateJsonValueProcessor() );
100 JSONObject jsonObject = JSONObject.fromObject( bean, jsonConfig );
101 assertNotNull( jsonObject );
102 assertEquals( 42, jsonObject.getInt( "value" ) );
103 JSONObject jsDate = jsonObject.getJSONObject( "date" );
104 assertJsDate( jsDate );
105 }
106
107 public void testBeanWithDateProperty_put() {
108 DateBean bean = new DateBean();
109 bean.setValue( 42 );
110 jsonConfig.registerJsonBeanProcessor( Date.class, new JsDateJsonBeanProcessor() );
111 JSONObject jsonObject = JSONObject.fromObject( bean, jsonConfig );
112 assertNotNull( jsonObject );
113 assertEquals( 42, jsonObject.getInt( "value" ) );
114 JSONObject jsDate = jsonObject.getJSONObject( "date" );
115 JSONAssert.assertNull( jsDate );
116 jsonObject.element( "date", date, jsonConfig );
117 jsDate = jsonObject.getJSONObject( "date" );
118 assertJsDate( jsDate );
119 }
120
121 public void testDateAsBean_fromObject() {
122 jsonConfig.registerJsonBeanProcessor( Date.class, new JsDateJsonBeanProcessor() );
123 JSONObject jsDate = JSONObject.fromObject( date, jsonConfig );
124 assertJsDate( jsDate );
125 }
126
127 public void testNumericValueWithProcessor_Byte(){
128 Map bean = new HashMap();
129 bean.put( "value", new Byte(Byte.MAX_VALUE) );
130 jsonConfig.registerJsonValueProcessor( "value", new IdentityJsonValueProcessor() );
131 JSONObject jsonObject = JSONObject.fromObject( bean, jsonConfig );
132 assertNotNull( jsonObject );
133 assertEquals( new Integer(Byte.MAX_VALUE), jsonObject.get( "value" ));
134 }
135
136 public void testNumericValueWithProcessor_Short(){
137 Map bean = new HashMap();
138 bean.put( "value", new Short(Short.MAX_VALUE) );
139 jsonConfig.registerJsonValueProcessor( "value", new IdentityJsonValueProcessor() );
140 JSONObject jsonObject = JSONObject.fromObject( bean, jsonConfig );
141 assertNotNull( jsonObject );
142 assertEquals( new Integer(Short.MAX_VALUE), jsonObject.get( "value" ));
143 }
144
145 protected void setUp() throws Exception {
146 Calendar c = Calendar.getInstance();
147 c.set( Calendar.YEAR, 2007 );
148 c.set( Calendar.MONTH, 5 );
149 c.set( Calendar.DAY_OF_MONTH, 17 );
150 c.set( Calendar.HOUR_OF_DAY, 12 );
151 c.set( Calendar.MINUTE, 13 );
152 c.set( Calendar.SECOND, 14 );
153 c.set( Calendar.MILLISECOND, 150 );
154 date = c.getTime();
155 jsonConfig = new JsonConfig();
156 }
157
158 private void assertJsDate( JSONObject jsDate ) {
159 JSONAssert.assertNotNull( jsDate );
160 int year = jsDate.getInt( "year" );
161
162 assertTrue( year == 2007 || year == 107 );
163 assertEquals( 5, jsDate.getInt( "month" ) );
164 assertEquals( 17, jsDate.getInt( "day" ) );
165 assertEquals( 12, jsDate.getInt( "hours" ) );
166 assertEquals( 13, jsDate.getInt( "minutes" ) );
167 assertEquals( 14, jsDate.getInt( "seconds" ) );
168 assertEquals( 150, jsDate.getInt( "milliseconds" ) );
169 }
170 }