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.processors;
18  
19  import java.util.Calendar;
20  import java.util.Date;
21  
22  import junit.framework.TestCase;
23  import net.sf.json.JSONObject;
24  import net.sf.json.JsonConfig;
25  
26  /**
27   * @author Andres Almiray <aalmiray@users.sourceforge.net>
28   */
29  public class TestJsDateJsonBeanProcessor extends TestCase {
30     public static void main( String[] args ) {
31        junit.textui.TestRunner.run( TestJsDateJsonBeanProcessor.class );
32     }
33  
34     private JsDateJsonBeanProcessor processor;
35  
36     public TestJsDateJsonBeanProcessor( String testName ) {
37        super( testName );
38     }
39  
40     public void testProcessBean() {
41        Calendar c = Calendar.getInstance();
42        c.set( Calendar.YEAR, 2007 );
43        c.set( Calendar.MONTH, 5 );
44        c.set( Calendar.DAY_OF_MONTH, 17 );
45        c.set( Calendar.HOUR_OF_DAY, 12 );
46        c.set( Calendar.MINUTE, 13 );
47        c.set( Calendar.SECOND, 14 );
48        c.set( Calendar.MILLISECOND, 150 );
49        Date date = c.getTime();
50        JSONObject jsonObject = processor.processBean( date, new JsonConfig() );
51        assertNotNull( jsonObject );
52        assertEquals( 2007, jsonObject.getInt( "year" ) );
53        assertEquals( 5, jsonObject.getInt( "month" ) );
54        assertEquals( 17, jsonObject.getInt( "day" ) );
55        assertEquals( 12, jsonObject.getInt( "hours" ) );
56        assertEquals( 13, jsonObject.getInt( "minutes" ) );
57        assertEquals( 14, jsonObject.getInt( "seconds" ) );
58        assertEquals( 150, jsonObject.getInt( "milliseconds" ) );
59     }
60  
61     public void testProcessBean_sqlDate() {
62        Calendar c = Calendar.getInstance();
63        c.set( Calendar.YEAR, 2007 );
64        c.set( Calendar.MONTH, 5 );
65        c.set( Calendar.DAY_OF_MONTH, 17 );
66        c.set( Calendar.HOUR_OF_DAY, 12 );
67        c.set( Calendar.MINUTE, 13 );
68        c.set( Calendar.SECOND, 14 );
69        c.set( Calendar.MILLISECOND, 150 );
70        Date date = c.getTime();
71        JSONObject jsonObject = processor.processBean( new java.sql.Date( date.getTime() ),
72              new JsonConfig() );
73        assertNotNull( jsonObject );
74        assertEquals( 2007, jsonObject.getInt( "year" ) );
75        assertEquals( 5, jsonObject.getInt( "month" ) );
76        assertEquals( 17, jsonObject.getInt( "day" ) );
77        assertEquals( 12, jsonObject.getInt( "hours" ) );
78        assertEquals( 13, jsonObject.getInt( "minutes" ) );
79        assertEquals( 14, jsonObject.getInt( "seconds" ) );
80        assertEquals( 150, jsonObject.getInt( "milliseconds" ) );
81     }
82  
83     protected void setUp() throws Exception {
84        processor = new JsDateJsonBeanProcessor();
85     }
86  }