Commits

Anonymous committed 1eceb2a

XW-546 Support Type conversion for Calendar fields
* The same logic that is applied to convert a parameter value to a Date will be used to convert to a Calendar

git-svn-id: http://svn.opensymphony.com/svn/xwork/trunk@1583e221344d-f017-0410-9bd5-d282ab1896d7

  • Participants
  • Parent commits 3b0e99e

Comments (0)

Files changed (3)

src/java/com/opensymphony/xwork2/util/XWorkBasicConverter.java

 import java.text.ParsePosition;
 import java.text.SimpleDateFormat;
 import java.util.ArrayList;
+import java.util.Calendar;
 import java.util.Collection;
 import java.util.Date;
 import java.util.HashSet;
             result = doConvertToArray(context, o, member, s, value, toType);
         } else if (Date.class.isAssignableFrom(toType)) {
             result = doConvertToDate(context, value, toType);
+        } else if (Calendar.class.isAssignableFrom(toType)) {
+            Date dateResult = (Date) doConvertToDate(context, value, Date.class);
+            if (dateResult != null) {
+                Calendar calendar = Calendar.getInstance();
+                calendar.setTime(dateResult);
+                result = calendar;
+            } 
         } else if (Collection.class.isAssignableFrom(toType)) {
             result = doConvertToCollection(context, o, member, s, value, toType);
         } else if (toType == Character.class) {

src/test/com/opensymphony/xwork2/util/Foo.java

  */
 package com.opensymphony.xwork2.util;
 
+import java.util.Calendar;
 import java.util.Collection;
 import java.util.Date;
 import java.util.List;
     boolean useful;
     int number;
     long aLong;
-
+    Calendar calendar;
 
     public void setALong(long aLong) {
         this.aLong = aLong;
     
     public void setMeeting(Date meeting) {
     	this.meeting = meeting;
+    }
+
+    public Calendar getCalendar() {
+        return calendar;
+    }
+
+    public void setCalendar(Calendar calendar) {
+        this.calendar = calendar;
     }     
 }

src/test/com/opensymphony/xwork2/util/OgnlUtilTest.java

         cal.set(Calendar.SECOND, 57);
         
         assertEquals(cal.getTime(), foo.getEvent());
+        
+        //test setting a calendar property
+        props.put("calendar", "1996-12-19T16:39:57Z");
+        OgnlUtil.setProperties(props, foo, context);
+        assertEquals(cal, foo.getCalendar());
     }
 
     public void testSetPropertiesInt() {
         // just do some of the 15 tests
         Map beans = OgnlUtil.getBeanMap(foo);
         assertNotNull(beans);
-        assertEquals(17, beans.size());
+        assertEquals(18, beans.size());
         assertEquals("Hello Santa", beans.get("title"));
         assertEquals(new Long("123"), beans.get("ALong"));
         assertEquals(new Integer("44"), beans.get("number"));