Commits

Anonymous committed c52ec80

Solved issues XW-670 - additional tests based on given Locale and DateFormats

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

Comments (0)

Files changed (2)

src/java/com/opensymphony/xwork2/conversion/impl/XWorkBasicConverter.java

                 }
             } else if (java.util.Date.class == toType) {
                 Date check = null;
-                SimpleDateFormat d1 = (SimpleDateFormat) DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.LONG, locale);
-                SimpleDateFormat d2 = (SimpleDateFormat) DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM, locale);
-                SimpleDateFormat d3 = (SimpleDateFormat) DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, locale);
-                SimpleDateFormat rfc3399 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
-                SimpleDateFormat[] dfs = {d1, d2, d3, rfc3399}; //added RFC 3339 date format (XW-473)
-                for (SimpleDateFormat df1 : dfs) {
+                DateFormat[] dfs = getDateFormats(locale);
+                for (DateFormat df1 : dfs) {
                     try {
                         check = df1.parse(sa);
                         df = df1;
         return result;
     }
 
+    private DateFormat[] getDateFormats(Locale locale) {
+        DateFormat dt1 = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.LONG, locale);
+        DateFormat dt2 = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM, locale);
+        DateFormat dt3 = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, locale);
+
+        DateFormat d1 = DateFormat.getDateInstance(DateFormat.SHORT, locale);
+        DateFormat d2 = DateFormat.getDateInstance(DateFormat.MEDIUM, locale);
+        DateFormat d3 = DateFormat.getDateInstance(DateFormat.LONG, locale);
+
+        DateFormat rfc3399 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
+
+        DateFormat[] dfs = {dt1, dt2, dt3, rfc3399, d1, d2, d3}; //added RFC 3339 date format (XW-473)
+        return dfs;
+    }
+
     private Object doConvertToNumber(Map<String, Object> context, Object value, Class toType) {
         if (value instanceof String) {
             if (toType == BigDecimal.class) {

src/test/com/opensymphony/xwork2/conversion/impl/XWorkBasicConverterTest.java

  */
 package com.opensymphony.xwork2.conversion.impl;
 
+import com.opensymphony.xwork2.ActionContext;
 import com.opensymphony.xwork2.XWorkException;
 import junit.framework.TestCase;
 
-import java.util.Date;
-import java.util.HashMap;
+import java.text.DateFormat;
+import java.util.*;
 
 /**
  * Test case for XWorkBasicConverter
         }
     }
 
+    public void testDateWithLocalePoland() throws Exception {
+        XWorkBasicConverter basicConverter = new XWorkBasicConverter();
+
+        Map<String, Object> map = new HashMap<String, Object>();
+        Locale locale = new Locale("pl", "PL");
+        map.put(ActionContext.LOCALE, locale);
+
+        String reference = "2009-01-09";
+        Object convertedObject = basicConverter.convertValue(map, null, null, null, reference, Date.class);
+
+        assertNotNull(convertedObject);
+
+        compareDates(locale, convertedObject);
+    }
+
+    public void testDateWithLocaleFrance() throws Exception {
+        XWorkBasicConverter basicConverter = new XWorkBasicConverter();
+
+        Map<String, Object> map = new HashMap<String, Object>();
+        Locale locale = new Locale("fr", "FR");
+        map.put(ActionContext.LOCALE, locale);
+
+        String reference = "09/01/2009";
+        Object convertedObject = basicConverter.convertValue(map, null, null, null, reference, Date.class);
+
+        assertNotNull(convertedObject);
+
+        compareDates(locale, convertedObject);
+    }
+
+    public void testDateWithLocaleUK() throws Exception {
+        XWorkBasicConverter basicConverter = new XWorkBasicConverter();
+
+        Map<String, Object> map = new HashMap<String, Object>();
+        Locale locale = new Locale("en", "US");
+        map.put(ActionContext.LOCALE, locale);
+
+        String reference = "01/09/2009";
+        Object convertedObject = basicConverter.convertValue(map, null, null, null, reference, Date.class);
+
+        assertNotNull(convertedObject);
+
+        compareDates(locale, convertedObject);
+    }
+
+    private void compareDates(Locale locale, Object convertedObject) {
+        Calendar cal = Calendar.getInstance(locale);
+        cal.set(Calendar.YEAR, 2009);
+        cal.set(Calendar.MONTH, Calendar.JANUARY);
+        cal.set(Calendar.DATE, 9);
+
+        Calendar cal1 = Calendar.getInstance(locale);
+        cal1.setTime((Date) convertedObject);
+
+        assertEquals(cal.get(Calendar.YEAR), cal1.get(Calendar.YEAR));
+        assertEquals(cal.get(Calendar.MONTH), cal1.get(Calendar.MONTH));
+        assertEquals(cal.get(Calendar.DATE), cal1.get(Calendar.DATE));
+
+        DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, locale);
+        assertEquals(df.format(cal.getTime()), df.format(convertedObject));
+    }
+
     public void testEmptyArrayConversion() throws Exception {
         XWorkBasicConverter basicConverter = new XWorkBasicConverter();
         Object convertedObject = basicConverter.convertValue(new HashMap<String, Object>(), null, null, null, new Object[]{}, Object[].class);
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.