ICU-1949 ICUService

Deal with changing default locale, integrate tests

X-SVN-Rev: 8912
This commit is contained in:
Doug Felt 2002-06-20 23:20:37 +00:00
parent cf90102603
commit 699e4d0238
5 changed files with 75 additions and 18 deletions

View File

@ -5,8 +5,8 @@
*******************************************************************************
*
* $Source: /xsrl/Nsvn/icu/icu4j/src/com/ibm/icu/dev/test/TestAll.java,v $
* $Date: 2002/06/14 19:08:57 $
* $Revision: 1.26 $
* $Date: 2002/06/20 23:20:36 $
* $Revision: 1.27 $
*
*****************************************************************************************
*/
@ -161,4 +161,11 @@ public class TestAll extends TestFmwk {
new com.ibm.icu.dev.test.format.DateFormatRegressionTestJ()
});
}
public void TestService() throws Exception {
run(new TestFmwk[] {
new com.ibm.icu.dev.test.util.ICUServiceTest(),
new com.ibm.icu.dev.test.util.ICUServiceThreadTest()
});
}
}

View File

@ -568,4 +568,38 @@ public class ICUServiceTest extends TestFmwk
return prefix + suffix;
}
}
public void TestLocale() {
ICULocaleService service = new ICULocaleService();
service.registerObject("root", "");
service.registerObject("german", "de");
service.registerObject("german_Germany", "de_DE");
service.registerObject("japanese", "ja");
service.registerObject("japanese_Japan", "ja_JP");
Object target = service.get("de_US");
confirmEqual("test de_US", "german", target);
target = service.get("za_PPP");
confirmEqual("test zappp", "root", target);
Locale loc = Locale.getDefault();
Locale.setDefault(Locale.JAPANESE);
target = service.get("za_PPP");
confirmEqual("test with ja locale", "japanese", target);
Set ids = service.getVisibleIDs();
for (Iterator iter = ids.iterator(); iter.hasNext();) {
logln("id: " + iter.next());
}
Locale.setDefault(loc);
ids = service.getVisibleIDs();
for (Iterator iter = ids.iterator(); iter.hasNext();) {
logln("id: " + iter.next());
}
target = service.get("za_PPP");
confirmEqual("test with en locale", "root", target);
}
}

View File

@ -342,7 +342,7 @@ public class ICUServiceThreadTest extends TestFmwk
new GetThread("[" + Integer.toString(i) + "]", stableService(), 0, this).start();
}
runThreads();
logln(stableService.stats());
System.out.println(stableService.stats());
}
// run multiple getVisibleID on a stable service
@ -351,7 +351,7 @@ public class ICUServiceThreadTest extends TestFmwk
new GetVisibleThread("[" + Integer.toString(i) + "]", stableService(), 0, this).start();
}
runThreads();
logln(stableService.stats());
System.out.println(stableService.stats());
}
// run multiple getDisplayName on a stable service
@ -368,7 +368,7 @@ public class ICUServiceThreadTest extends TestFmwk
this).start();
}
runThreads();
logln(stableService.stats());
System.out.println(stableService.stats());
}
// run register/unregister on a service
@ -381,7 +381,7 @@ public class ICUServiceThreadTest extends TestFmwk
new UnregisterFactoryThread("[" + i + "]", service, 0, this).start();
}
runThreads();
logln(service.stats());
System.out.println(service.stats());
}
public void Test04_WitheringService() {
@ -402,7 +402,7 @@ public class ICUServiceThreadTest extends TestFmwk
new UnregisterFactoryListThread("", service, 3, factories, this).start();
runThreads(2000);
logln(service.stats());
System.out.println(service.stats());
}
// "all hell breaks loose"
@ -438,6 +438,6 @@ public class ICUServiceThreadTest extends TestFmwk
// yoweee!!!
runThreads(10000);
logln(service.stats());
System.out.println(service.stats());
}
}

View File

@ -12,21 +12,16 @@ import java.util.Set;
import java.util.Enumeration;
public class ICULocaleService extends ICUService {
private String fallbackLocale;
Locale fallbackLocale;
String fallbackLocaleName;
/**
* Construct an ICULocaleService with a fallback locale string based on the current
* default locale at the time of construction.
*/
public ICULocaleService() {
fallbackLocale = LocaleUtility.canonicalLocaleString(Locale.getDefault().toString());
}
/**
* Construct an ICULocaleService with the provided fallback locale string.
*/
public ICULocaleService(String fallbackLocale) {
this.fallbackLocale = fallbackLocale;
fallbackLocale = Locale.getDefault();
fallbackLocaleName = LocaleUtility.canonicalLocaleString(fallbackLocale.toString());
}
/**
@ -359,6 +354,17 @@ public class ICULocaleService extends ICUService {
}
protected Key createKey(String id) {
return LocaleKey.createWithCanonicalFallback(id, fallbackLocale);
Locale loc = Locale.getDefault();
if (loc != fallbackLocale) {
synchronized (this) {
if (loc != fallbackLocale) {
fallbackLocale = loc;
fallbackLocaleName = LocaleUtility.canonicalLocaleString(loc.toString());
clearServiceCache();
}
}
}
return LocaleKey.createWithCanonicalFallback(id, fallbackLocaleName);
}
}

View File

@ -608,6 +608,16 @@ public class ICUService extends ICUNotifier {
dnref = null;
}
/**
* Clears only the service cache.
* This can be called by subclasses when a change affects the service
* cache but not the id caches, e.g., when the default locale changes
* the resolution of ids changes, but not the visible ids themselves.
*/
protected void clearServiceCache() {
cacheref = null;
}
/**
* ServiceListener is the listener that ICUService provides by default.
* ICUService will notifiy this listener when factories are added to