}
}
public void TestAPI() {
// create a service using locale keys,
ICUService service = new TestService();
logln("service name:" + service.getName());
// register an object with one locale,
// search for an object with a more specific locale
// should return the original object
Integer singleton0 = new Integer(0);
service.registerObject(singleton0, "en_US");
Object result = service.get("en_US_FOO");
confirmIdentical("1) en_US_FOO -> en_US", result, singleton0);
// register a new object with the more specific locale
// search for an object with that locale
// should return the new object
Integer singleton1 = new Integer(1);
service.registerObject(singleton1, "en_US_FOO");
result = service.get("en_US_FOO");
confirmIdentical("2) en_US_FOO -> en_US_FOO", result, singleton1);
// search for an object that falls back to the first registered locale
result = service.get("en_US_BAR");
confirmIdentical("3) en_US_BAR -> en_US", result, singleton0);
// get a list of the factories, should be two
List factories = service.factories();
confirmIdentical("4) factory size", factories.size(), 2);
// register a new object with yet another locale
// original factory list is unchanged
Integer singleton2 = new Integer(2);
service.registerObject(singleton2, "en");
confirmIdentical("5) factory size", factories.size(), 2);
// search for an object with the new locale
// stack of factories is now en, en_US_FOO, en_US
// search for en_US should still find en_US object
result = service.get("en_US_BAR");
confirmIdentical("6) en_US_BAR -> en_US", result, singleton0);
// register a new object with an old id, should hide earlier factory using this id, but leave it there
Integer singleton3 = new Integer(3);
service.registerObject(singleton3, "en_US");
factories = service.factories();
confirmIdentical("9) factory size", factories.size(), 4);
// should get data from that new factory
result = service.get("en_US_BAR");
confirmIdentical("10) en_US_BAR -> (3)", result, singleton3);
// remove new factory
// should have fewer factories again
service.unregisterFactory((Factory)factories.get(0));
factories = service.factories();
confirmIdentical("11) factory size", factories.size(), 3);
// should get original data again after remove factory
result = service.get("en_US_BAR");
confirmIdentical("12) en_US_BAR -> 0", result, singleton0);
// shouldn't find unregistered ids
result = service.get("foo");
confirmIdentical("13) foo -> null", result, null);
// should find non-canonical strings
String[] resultID = new String[1];
result = service.get("EN_us_fOo", resultID);
confirmEqual("14) find non-canonical", resultID[0], "en_US_FOO");
// should be able to register non-canonical strings and get them canonicalized
service.registerObject(singleton3, "eN_ca_dUde");
result = service.get("En_Ca_DuDe", resultID);
confirmEqual("15) register non-canonical", resultID[0], "en_CA_DUDE");
// should be able to register invisible factories, these will not
// be visible by default, but if you know the secret password you
// can still access these services...
Integer singleton4 = new Integer(4);
service.registerObject(singleton4, "en_US_BAR", false);
result = service.get("en_US_BAR");
confirmIdentical("17) get invisible", result, singleton4);
// should not be able to locate invisible services
Set ids = service.getVisibleIDs();
confirmBoolean("18) find invisible", !ids.contains("en_US_BAR"));
service.reset();
// an anonymous factory than handles all ids
{
Factory factory = new Factory() {
public Object create(Key key, ICUService unusedService) {
return new ULocale(key.currentID());
}
public void updateVisibleIDs(Map unusedResult) {
}
public String getDisplayName(String id, ULocale l) {
return null;
}
};
service.registerFactory(factory);
// anonymous factory will still handle the id
result = service.get(ULocale.US.toString());
confirmEqual("21) locale", result, ULocale.US);
// still normalizes id
result = service.get("EN_US_BAR");
confirmEqual("22) locale", result, new ULocale("en_US_BAR"));
// we can override for particular ids
service.registerObject(singleton3, "en_US_BAR");
result = service.get("en_US_BAR");
confirmIdentical("23) override super", result, singleton3);
}
// empty service should not recognize anything
service.reset();
result = service.get("en_US");
confirmIdentical("24) empty", result, null);
// create a custom multiple key factory
{
String[] xids = { "en_US_VALLEY_GIRL",
"en_US_VALLEY_BOY",
"en_US_SURFER_GAL",
"en_US_SURFER_DUDE"
};
service.registerFactory(new TestLocaleKeyFactory(xids, "Later"));
}
// iterate over the visual ids returned by the multiple factory
{
Set vids = service.getVisibleIDs();
Iterator iter = vids.iterator();
int count = 0;
while (iter.hasNext()) {
++count;
String id = (String)iter.next();
logln(" " + id + " --> " + service.get(id));
}
// four visible ids
confirmIdentical("25) visible ids", count, 4);
}
// iterate over the display names
{
Map dids = getDisplayNames(service, ULocale.GERMANY);
Iterator iter = dids.entrySet().iterator();
int count = 0;
while (iter.hasNext()) {
++count;
Entry e = (Entry)iter.next();
logln(" " + e.getKey() + " -- > " + e.getValue());
}
// four display names, in german
confirmIdentical("26) display names", count, 4);
}
// no valid display name
confirmIdentical("27) get display name", service.getDisplayName("en_US_VALLEY_GEEK"), null);
{
String name = service.getDisplayName("en_US_SURFER_DUDE", ULocale.US);
confirmEqual("28) get display name", name, "English (United States, SURFER_DUDE)");
}
// register another multiple factory
{
String[] xids = {
"en_US_SURFER", "en_US_SURFER_GAL", "en_US_SILICON", "en_US_SILICON_GEEK"
};
service.registerFactory(new TestLocaleKeyFactory(xids, "Rad dude"));
}
// this time, we have seven display names
// Rad dude's surfer gal 'replaces' later's surfer gal
{
Map dids = getDisplayNames(service);
Iterator iter = dids.entrySet().iterator();
int count = 0;
while (iter.hasNext()) {
++count;
Entry e = (Entry)iter.next();
logln(" " + e.getKey() + " --> " + e.getValue());
}
// seven display names, in spanish
confirmIdentical("29) display names", count, 7);
}
// we should get the display name corresponding to the actual id
// returned by the id we used.
{
String[] actualID = new String[1];
String id = "en_us_surfer_gal";
String gal = (String)service.get(id, actualID);
if (gal != null) {
logln("actual id: " + actualID[0]);
String displayName = service.getDisplayName(actualID[0], ULocale.US);
logln("found actual: " + gal + " with display name: " + displayName);
confirmBoolean("30) found display name for actual", displayName != null);
displayName = service.getDisplayName(id, ULocale.US);
logln("found query: " + gal + " with display name: " + displayName);
// this is no longer a bug, we want to return display names for anything
// that a factory handles. since we handle it, we should return a display
// name. see jb3549
// confirmBoolean("31) found display name for query", displayName == null);
} else {
errln("30) service could not find entry for " + id);
}
// this should be handled by the 'dude' factory, since it overrides en_US_SURFER.
id = "en_US_SURFER_BOZO";
String bozo = (String)service.get(id, actualID);
if (bozo != null) {
String displayName = service.getDisplayName(actualID[0], ULocale.US);
logln("found actual: " + bozo + " with display name: " + displayName);
confirmBoolean("32) found display name for actual", displayName != null);
displayName = service.getDisplayName(id, ULocale.US);
logln("found actual: " + bozo + " with display name: " + displayName);
// see above and jb3549
// confirmBoolean("33) found display name for query", displayName == null);
} else {
errln("32) service could not find entry for " + id);
}
confirmBoolean("34) is default ", !service.isDefault());
}
/*
// disallow hiding for now
// hiding factory should obscure 'sublocales'
{
String[] xids = {
"en_US_VALLEY", "en_US_SILICON"
};
service.registerFactory(new TestHidingFactory(xids, "hiding"));
}
{
Map dids = service.getDisplayNames();
Iterator iter = dids.entrySet().iterator();
int count = 0;
while (iter.hasNext()) {
++count;
Entry e = (Entry)iter.next();
logln(" " + e.getKey() + " -- > " + e.getValue());
}
confirmIdentical("35) hiding factory", count, 5);
}
*/
{
Set xids = service.getVisibleIDs();
Iterator iter = xids.iterator();
while (iter.hasNext()) {
String xid = (String)iter.next();
logln(xid + "? " + service.get(xid));
}
logln("valleygirl? " + service.get("en_US_VALLEY_GIRL"));
logln("valleyboy? " + service.get("en_US_VALLEY_BOY"));
logln("valleydude? " + service.get("en_US_VALLEY_DUDE"));
logln("surfergirl? " + service.get("en_US_SURFER_GIRL"));
}
// resource bundle factory.
service.reset();
service.registerFactory(new ICUResourceBundleFactory());
// list all of the resources
{
logln("all visible ids: " + service.getVisibleIDs());
/*
Set xids = service.getVisibleIDs();
StringBuffer buf = new StringBuffer("{");
boolean notfirst = false;
Iterator iter = xids.iterator();
while (iter.hasNext()) {
String xid = (String)iter.next();
if (notfirst) {
buf.append(", ");
} else {
notfirst = true;
}
buf.append(xid);
}
buf.append("}");
logln(buf.toString());
*/
}
// list only the resources for es, default locale
// since we're using the default Key, only "es" is matched
{
logln("visible ids for es locale: " + service.getVisibleIDs("es"));
}
// list only the spanish display names for es, spanish collation order
// since we're using the default Key, only "es" is matched
{
logln("display names: " + getDisplayNames(service, new ULocale("es"), "es"));
}
// list the display names in reverse order
{
logln("display names in reverse order: " +
service.getDisplayNames(ULocale.US, new Comparator() {
public int compare(Object lhs, Object rhs) {
return -String.CASE_INSENSITIVE_ORDER.compare((String)lhs, (String)rhs);
}
}));
}
// get all the display names of these resources
// this should be fast since the display names were cached.
{
logln("service display names for de_DE");
Map names = getDisplayNames(service, new ULocale("de_DE"));
StringBuffer buf = new StringBuffer("{");
Iterator iter = names.entrySet().iterator();
while (iter.hasNext()) {
Entry e = (Entry)iter.next();
String name = (String)e.getKey();
String id = (String)e.getValue();
buf.append("\n " + name + " --> " + id);
}
buf.append("\n}");
logln(buf.toString());
}
CalifornioLanguageFactory califactory = new CalifornioLanguageFactory();
service.registerFactory(califactory);
// get all the display names of these resources
{
logln("californio language factory");
StringBuffer buf = new StringBuffer("{");
String[] idNames = {