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
This commit is contained in:
Sebastian Bazley 2012-09-25 21:11:34 +00:00
parent 34beef1d08
commit 005f41f136
1 changed files with 84 additions and 0 deletions

View File

@ -20,19 +20,24 @@ import static org.junit.Assert.assertEquals;
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 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);