You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by se...@apache.org on 2012/09/25 23:11:34 UTC

svn commit: r1390143 - /commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/time/FastDateParserTest.java

Author: sebb
Date: Tue Sep 25 21:11:34 2012
New Revision: 1390143

URL: http://svn.apache.org/viewvc?rev=1390143&view=rev
Log:
One reason why the ja_JP_JP and th_TH_* locales don't work

Modified:
    commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/time/FastDateParserTest.java

Modified: commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/time/FastDateParserTest.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/time/FastDateParserTest.java?rev=1390143&r1=1390142&r2=1390143&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/time/FastDateParserTest.java (original)
+++ commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/time/FastDateParserTest.java Tue Sep 25 21:11:34 2012
@@ -20,19 +20,24 @@ import static org.junit.Assert.assertEqu
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertTrue;
 import java.io.Serializable;
+import java.text.DateFormatSymbols;
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
+import java.util.Arrays;
 import java.util.Calendar;
 import java.util.Date;
 import java.util.GregorianCalendar;
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.Locale;
 import java.util.Map;
+import java.util.Set;
 import java.util.TimeZone;
 
 import junit.framework.Assert;
 
 import org.apache.commons.lang3.SerializationUtils;
+import org.apache.commons.lang3.tuple.ImmutablePair;
 import org.junit.Test;
 
 /**
@@ -229,6 +234,85 @@ public class FastDateParserTest {
     }
 
     @Test
+    // Check that all Locales generate Strings containing the expected eras
+    public void testEras() throws Exception {
+        Set<ImmutablePair<Locale, String>> locale2Absent = new HashSet<ImmutablePair<Locale, String>>();
+        Map<Locale, String[]> locale2Eras = new HashMap<Locale, String[]>();
+        for(Locale locale : Locale.getAvailableLocales()) {
+            for(TimeZone tz : new TimeZone[]{GMT}) {
+                Calendar cal = Calendar.getInstance(tz);
+                String[] eras = DateFormatSymbols.getInstance(locale).getEras();
+                String[] erasPrint = new String[eras.length];
+                for(int i = 0; i < eras.length ; i++) {
+                    String s = eras[i];
+                    if (s.length() > 4) {
+                        erasPrint[i] = s;
+                    } else {
+                        erasPrint[i] = display(s);
+                    }
+                }
+                for(int year : new int[]{2003, 1927, 1913, 1868, 1867, -2003}) {
+                    cal.clear();
+                    if (year < 0) {
+                        cal.set(-year, 1, 10);
+                        cal.set(Calendar.ERA, GregorianCalendar.BC);
+                    } else {
+                        cal.set(year, 1, 10);
+                    }
+                    Date in = cal.getTime();
+                    for(String format : new String[]{"GGGG","G"}) {
+                        SimpleDateFormat sdf = new SimpleDateFormat(format, locale);
+                        String fmt = sdf.format(in);
+                        boolean found = false;
+                        for(String era : eras) {
+                            if (fmt.startsWith(era)) {
+                                found=true;
+                            }
+                        }
+                        if (!found) {
+                            locale2Absent.add(ImmutablePair.of(locale, fmt));
+                            locale2Eras.put(locale, erasPrint);
+                        }
+                    }
+                }
+            }
+        }
+        
+        if (locale2Absent.size() > 0) {
+            System.out.println("One or more missing era designators detected");
+            for(ImmutablePair<Locale, String> me : locale2Absent) {
+                Locale loc = me.getKey();
+                String [] erasPrint = locale2Eras.get(loc);
+                System.out.println("Locale: "+loc.toString()+" era: '"+display(me.getValue())+"' not found in eras: " + Arrays.toString(erasPrint));                
+            }
+        }
+//        assertFalse("One or more failures detected",fail);
+    }
+
+    private String display(String fmt) {
+        if (fmt.matches("\\p{ASCII}*")) {
+            return fmt;
+        }
+        StringBuilder sb = new StringBuilder();
+        sb.append(fmt);
+        sb.append(" = ");
+        for(int i =0; i < fmt.length(); i++) {
+            if (i > 0) {
+                sb.append(' ');
+            }
+            String s = fmt.substring(i,i+1);
+            if (s.matches("\\p{ASCII}")) {
+                sb.append(s);
+            } else {
+                char charAt = fmt.charAt(i);
+                sb.append("\\u");
+                sb.append(Integer.toHexString(charAt));                
+            }
+        }
+        return sb.toString();
+    }
+
+    @Test
     public void testLocales_Long_AD() throws Exception {
         testLocales(LONG_FORMAT, false);
     }