From 005f41f13651577bb7e647604abad7ed061c160d Mon Sep 17 00:00:00 2001 From: Sebastian Bazley Date: Tue, 25 Sep 2012 21:11:34 +0000 Subject: [PATCH] One reason why the ja_JP_JP and th_TH_* locales don't work git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@1390143 13f79535-47bb-0310-9956-ffa450edef68 --- .../lang3/time/FastDateParserTest.java | 84 +++++++++++++++++++ 1 file changed, 84 insertions(+) diff --git a/src/test/java/org/apache/commons/lang3/time/FastDateParserTest.java b/src/test/java/org/apache/commons/lang3/time/FastDateParserTest.java index 80cbf6df2..aaa1dd51e 100644 --- a/src/test/java/org/apache/commons/lang3/time/FastDateParserTest.java +++ b/src/test/java/org/apache/commons/lang3/time/FastDateParserTest.java @@ -20,19 +20,24 @@ 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; /** @@ -228,6 +233,85 @@ public void testParses() throws Exception { } } + @Test + // Check that all Locales generate Strings containing the expected eras + public void testEras() throws Exception { + Set> locale2Absent = new HashSet>(); + Map locale2Eras = new HashMap(); + 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 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);