ICU-9795 Fixed to throw different exception, and added test.

X-SVN-Rev: 36496
This commit is contained in:
Mark Davis 2014-09-13 08:25:46 +00:00
parent 77b162e87b
commit c37851c95d
2 changed files with 56 additions and 47 deletions

View file

@ -188,9 +188,9 @@ public final class LocaleData {
// currency symbol exemplar is no longer available
return noSubstitute ? null : UnicodeSet.EMPTY;
}
final String aKey = exemplarSetTypes[extype]; // will throw an out-of-bounds exception
try{
final String aKey = exemplarSetTypes[extype]; // will throw an out-of-bounds exception
ICUResourceBundle stringBundle = (ICUResourceBundle) bundle.get(aKey);
if ( noSubstitute && (stringBundle.getLoadingStatus() == ICUResourceBundle.FROM_ROOT) ) {
@ -198,8 +198,9 @@ public final class LocaleData {
}
String unicodeSetPattern = stringBundle.getString();
return new UnicodeSet(unicodeSetPattern, UnicodeSet.IGNORE_SPACE | options);
}catch (Exception ex){
} catch (ArrayIndexOutOfBoundsException aiooe) {
throw new IllegalArgumentException(aiooe);
} catch (Exception ex){
return noSubstitute ? null : UnicodeSet.EMPTY;
}
}

View file

@ -1,9 +1,9 @@
/*
*******************************************************************************
* Copyright (C) 2003-2013, International Business Machines Corporation and *
* Copyright (C) 2003-2014, International Business Machines Corporation and *
* others. All Rights Reserved. *
*******************************************************************************
*/
*/
package com.ibm.icu.dev.test.util;
import java.util.Arrays;
@ -14,6 +14,7 @@ import com.ibm.icu.impl.ICUResourceBundle;
import com.ibm.icu.lang.UScript;
import com.ibm.icu.text.UnicodeSet;
import com.ibm.icu.text.UnicodeSetIterator;
import com.ibm.icu.util.ICUException;
import com.ibm.icu.util.LocaleData;
import com.ibm.icu.util.ULocale;
@ -28,9 +29,9 @@ public class LocaleDataTest extends TestFmwk{
public static void main(String[] args) throws Exception{
new LocaleDataTest().run(args);
}
private ULocale[] availableLocales = null;
public LocaleDataTest(){
}
protected void init(){
@ -47,26 +48,26 @@ public class LocaleDataTest extends TestFmwk{
}
ULocale fullLoc = ULocale.addLikelySubtags(locale);
if(fullLoc.toString().indexOf("_BZ") >= 0 || fullLoc.toString().indexOf("_CA") >= 0 ||
fullLoc.toString().indexOf("_CL") >= 0 || fullLoc.toString().indexOf("_CO") >= 0 ||
fullLoc.toString().indexOf("_CR") >= 0 || fullLoc.toString().indexOf("_GT") >= 0 ||
fullLoc.toString().indexOf("_MX") >= 0 || fullLoc.toString().indexOf("_NI") >= 0 ||
fullLoc.toString().indexOf("_PA") >= 0 || fullLoc.toString().indexOf("_PH") >= 0 ||
fullLoc.toString().indexOf("_PR") >= 0 || fullLoc.toString().indexOf("_SV") >= 0 ||
fullLoc.toString().indexOf("_US") >= 0 || fullLoc.toString().indexOf("_VE") >= 0 ){
fullLoc.toString().indexOf("_CL") >= 0 || fullLoc.toString().indexOf("_CO") >= 0 ||
fullLoc.toString().indexOf("_CR") >= 0 || fullLoc.toString().indexOf("_GT") >= 0 ||
fullLoc.toString().indexOf("_MX") >= 0 || fullLoc.toString().indexOf("_NI") >= 0 ||
fullLoc.toString().indexOf("_PA") >= 0 || fullLoc.toString().indexOf("_PH") >= 0 ||
fullLoc.toString().indexOf("_PR") >= 0 || fullLoc.toString().indexOf("_SV") >= 0 ||
fullLoc.toString().indexOf("_US") >= 0 || fullLoc.toString().indexOf("_VE") >= 0 ){
if(paperSize.getHeight()!= 279 || paperSize.getWidth() != 216 ){
errln("PaperSize did not return the expected value for locale "+ locale+
" Expected height: 279 width: 216."+
" Got height: "+paperSize.getHeight()+" width: "+paperSize.getWidth()
);
" Expected height: 279 width: 216."+
" Got height: "+paperSize.getHeight()+" width: "+paperSize.getWidth()
);
}else{
logln("PaperSize returned the expected values for locale " + locale);
}
}else{
if(paperSize.getHeight()!= 297 || paperSize.getWidth() != 210 ){
errln("PaperSize did not return the expected value for locale "+ locale +
" Expected height: 297 width: 210."+
" Got height: "+paperSize.getHeight() +" width: "+paperSize.getWidth()
);
" Expected height: 297 width: 210."+
" Got height: "+paperSize.getHeight() +" width: "+paperSize.getWidth()
);
}else{
logln("PaperSize returned the expected values for locale " + locale);
}
@ -102,11 +103,11 @@ public class LocaleDataTest extends TestFmwk{
// Simple test case for checking exemplar character type coverage
public void TestEnglishExemplarCharacters() {
final char[] testChars = {
0x61, // standard
0xE1, // auxiliary
0x41, // index
0, // filler for deprecated currency exemplar
0x2D, // punctuation
0x61, // standard
0xE1, // auxiliary
0x41, // index
0, // filler for deprecated currency exemplar
0x2D, // punctuation
};
LocaleData ld = LocaleData.getInstance(ULocale.ENGLISH);
for (int type = 0; type < LocaleData.ES_COUNT; type++) {
@ -117,6 +118,13 @@ public class LocaleDataTest extends TestFmwk{
}
}
}
try {
ld.getExemplarSet(0, LocaleData.ES_COUNT); // out of bounds value
throw new ICUException("Test failure; should throw exception");
} catch (IllegalArgumentException e) {
assertEquals("", "java.lang.ArrayIndexOutOfBoundsException", e.getCause().getClass().getName());
}
}
// Bundle together a UnicodeSet (of expemplars) and ScriptCode combination.
@ -126,7 +134,7 @@ public class LocaleDataTest extends TestFmwk{
static class ExemplarGroup {
private int[] scs;
private UnicodeSet set;
ExemplarGroup(UnicodeSet s, int[] scriptCodes) {
set = s;
scs = scriptCodes;
@ -141,11 +149,11 @@ public class LocaleDataTest extends TestFmwk{
public boolean equals(Object other) {
ExemplarGroup o = (ExemplarGroup)other;
boolean r = Arrays.equals(scs, o.scs) &&
set.equals(o.set);
set.equals(o.set);
return r;
}
}
public void TestExemplarSet(){
HashSet testedExemplars = new HashSet();
int equalCount = 0;
@ -214,7 +222,7 @@ public class LocaleDataTest extends TestFmwk{
// Note: The case-folded set should sometimes be a strict superset
// and sometimes be equal.
assertTrue("case-folded is sometimes a strict superset, and sometimes equal",
equalCount > 0 && equalCount < availableLocales.length);
equalCount > 0 && equalCount < availableLocales.length);
}
public void TestExemplarSet2(){
int equalCount = 0;
@ -302,18 +310,18 @@ public class LocaleDataTest extends TestFmwk{
// See #9785, #9794 and #9795
public void TestExemplarSetTypes() {
final String[] testLocales = {
"am", // No auxiliary / index exemplars as of ICU 50
"en",
"th", // #9785
"foo", // Bogus locale
"am", // No auxiliary / index exemplars as of ICU 50
"en",
"th", // #9785
"foo", // Bogus locale
};
final int[] testTypes = {
LocaleData.ES_STANDARD,
LocaleData.ES_AUXILIARY,
LocaleData.ES_INDEX,
LocaleData.ES_CURRENCY,
LocaleData.ES_PUNCTUATION,
LocaleData.ES_STANDARD,
LocaleData.ES_AUXILIARY,
LocaleData.ES_INDEX,
LocaleData.ES_CURRENCY,
LocaleData.ES_PUNCTUATION,
};
final String[] testTypeNames = {
@ -322,7 +330,7 @@ public class LocaleDataTest extends TestFmwk{
"ES_INDEX",
"ES_CURRENCY",
"ES_PUNCTUATION",
};
};
for (String locstr : testLocales) {
ULocale loc = new ULocale(locstr);
@ -350,7 +358,7 @@ public class LocaleDataTest extends TestFmwk{
assertEquals("LocaleData get/set NoSubstitute",
t,
ld.getNoSubstitute());
logln(ld.getDelimiter(LocaleData.QUOTATION_START));
logln(ld.getDelimiter(LocaleData.QUOTATION_END));
logln(ld.getDelimiter(LocaleData.ALT_QUOTATION_START));
@ -375,11 +383,11 @@ public class LocaleDataTest extends TestFmwk{
String separator = ld.getLocaleSeparator();
logln("LocaleDisplayPattern for locale " + locale + ": " + pattern);
if (!pattern.equals("{0} ({1})")) {
errln("Unexpected LocaleDisplayPattern for locale: "+ locale);
errln("Unexpected LocaleDisplayPattern for locale: "+ locale);
}
logln("LocaleSeparator for locale " + locale + ": " + separator);
if (!separator.equals(", ")) {
errln("Unexpected LocaleSeparator for locale: "+ locale);
errln("Unexpected LocaleSeparator for locale: "+ locale);
}
locale = ULocale.CHINESE;
@ -388,18 +396,18 @@ public class LocaleDataTest extends TestFmwk{
separator = ld.getLocaleSeparator();
logln("LocaleDisplayPattern for locale " + locale + ": " + pattern);
if (!pattern.equals("{0}\uFF08{1}\uFF09")) {
errln("Unexpected LocaleDisplayPattern for locale: "+ locale);
errln("Unexpected LocaleDisplayPattern for locale: "+ locale);
}
logln("LocaleSeparator for locale " + locale + ": " + separator);
if (!separator.equals("\u3001")) {
errln("Unexpected LocaleSeparator for locale: "+ locale);
errln("Unexpected LocaleSeparator for locale: "+ locale);
}
for(int i = 0; i < availableLocales.length; i++){
locale = availableLocales[i];
ld = LocaleData.getInstance(locale);
logln(locale.toString() + " LocaleDisplayPattern:" + ld.getLocaleDisplayPattern());
logln(locale.toString() + " LocaleSeparator:" + ld.getLocaleSeparator());
locale = availableLocales[i];
ld = LocaleData.getInstance(locale);
logln(locale.toString() + " LocaleDisplayPattern:" + ld.getLocaleDisplayPattern());
logln(locale.toString() + " LocaleSeparator:" + ld.getLocaleSeparator());
}
}
}