From 65fd02443d025fd43c5b4846551ab52cf957f937 Mon Sep 17 00:00:00 2001 From: Doug Felt Date: Tue, 11 Jan 2005 20:58:27 +0000 Subject: [PATCH] ICU-4092 service code coverage X-SVN-Rev: 17110 --- icu4c/source/common/serv.h | 5 +- icu4c/source/common/servlkf.cpp | 10 +- icu4c/source/common/servloc.h | 4 +- icu4c/source/common/servrbf.cpp | 2 + icu4c/source/common/servslkf.cpp | 10 +- icu4c/source/i18n/calendar.cpp | 16 ++-- icu4c/source/test/intltest/icusvtst.cpp | 120 ++++++++++++++++++++---- 7 files changed, 125 insertions(+), 42 deletions(-) diff --git a/icu4c/source/common/serv.h b/icu4c/source/common/serv.h index 5ebd79309e..d905b7c5e9 100644 --- a/icu4c/source/common/serv.h +++ b/icu4c/source/common/serv.h @@ -251,9 +251,10 @@ class U_COMMON_API ICUServiceFactory : public UObject { /** *

Return, in result, the display name of the id in the provided locale. * This is an id, not a descriptor. If the id is - * not visible or not defined by the factory, sets result to bogus. If the + * not visible, sets result to bogus. If the * incoming result is bogus, it remains bogus. Result is returned as a - * convenience.

+ * convenience. Results are not defined if id is not one supported by this + * factory.

* * @param id a visible id supported by this factory. * @param locale the locale for which to generate the corresponding localized display name. diff --git a/icu4c/source/common/servlkf.cpp b/icu4c/source/common/servlkf.cpp index cf9d1cd255..5b1fa93112 100644 --- a/icu4c/source/common/servlkf.cpp +++ b/icu4c/source/common/servlkf.cpp @@ -111,11 +111,11 @@ LocaleKeyFactory::handleCreate(const Locale& /* loc */, return NULL; } -UBool -LocaleKeyFactory::isSupportedID(const UnicodeString& id, UErrorCode& status) const { - const Hashtable* ids = getSupportedIDs(status); - return ids && ids->get(id); -} +//UBool +//LocaleKeyFactory::isSupportedID(const UnicodeString& id, UErrorCode& status) const { +// const Hashtable* ids = getSupportedIDs(status); +// return ids && ids->get(id); +//} const Hashtable* LocaleKeyFactory::getSupportedIDs(UErrorCode& /* status */) const { diff --git a/icu4c/source/common/servloc.h b/icu4c/source/common/servloc.h index 1919114d81..d5013e8292 100644 --- a/icu4c/source/common/servloc.h +++ b/icu4c/source/common/servloc.h @@ -263,7 +263,7 @@ protected: * Return true if this id is one the factory supports (visible or * otherwise). */ - virtual UBool isSupportedID(const UnicodeString& id, UErrorCode& status) const; + // virtual UBool isSupportedID(const UnicodeString& id, UErrorCode& status) const; /** * Return the set of ids that this factory supports (visible or @@ -333,7 +333,7 @@ class U_COMMON_API SimpleLocaleKeyFactory : public LocaleKeyFactory { /** * Return true if this id is equal to the locale name. */ - virtual UBool isSupportedID(const UnicodeString& id, UErrorCode& status) const; + //virtual UBool isSupportedID(const UnicodeString& id, UErrorCode& status) const; public: diff --git a/icu4c/source/common/servrbf.cpp b/icu4c/source/common/servrbf.cpp index d4b6266e4b..ef1f8935ea 100644 --- a/icu4c/source/common/servrbf.cpp +++ b/icu4c/source/common/servrbf.cpp @@ -55,6 +55,8 @@ ICUResourceBundleFactory::handleCreate(const Locale& loc, int32_t /* kind */, co if (U_SUCCESS(status)) { // _bundleName is a package name // and should only contain invariant characters + // ??? is it always true that the max length of the bundle name is 19? + // who made this change? -- dlf char pkg[20]; int32_t length; length=_bundleName.extract(0, INT32_MAX, pkg, (int32_t)sizeof(pkg), US_INV); diff --git a/icu4c/source/common/servslkf.cpp b/icu4c/source/common/servslkf.cpp index 406b07ebc8..4610351847 100644 --- a/icu4c/source/common/servslkf.cpp +++ b/icu4c/source/common/servslkf.cpp @@ -75,11 +75,11 @@ SimpleLocaleKeyFactory::create(const ICUServiceKey& key, const ICUService* servi return NULL; } -UBool -SimpleLocaleKeyFactory::isSupportedID(const UnicodeString& id, UErrorCode& /* status */) const -{ - return id == _id; -} +//UBool +//SimpleLocaleKeyFactory::isSupportedID(const UnicodeString& id, UErrorCode& /* status */) const +//{ +// return id == _id; +//} void SimpleLocaleKeyFactory::updateVisibleIDs(Hashtable& result, UErrorCode& status) const diff --git a/icu4c/source/i18n/calendar.cpp b/icu4c/source/i18n/calendar.cpp index 8d409b2f79..f2ae7445cb 100644 --- a/icu4c/source/i18n/calendar.cpp +++ b/icu4c/source/i18n/calendar.cpp @@ -197,14 +197,14 @@ public: virtual ~BasicCalendarFactory() {} protected: - virtual UBool isSupportedID( const UnicodeString& id, UErrorCode& status) const { - if(U_FAILURE(status)) { - return FALSE; - } - char keyword[ULOC_FULLNAME_CAPACITY]; - getCalendarKeyword(id, keyword, (int32_t)sizeof(keyword)); - return isStandardSupportedKeyword(keyword, status); - } + //virtual UBool isSupportedID( const UnicodeString& id, UErrorCode& status) const { + // if(U_FAILURE(status)) { + // return FALSE; + // } + // char keyword[ULOC_FULLNAME_CAPACITY]; + // getCalendarKeyword(id, keyword, (int32_t)sizeof(keyword)); + // return isStandardSupportedKeyword(keyword, status); + //} virtual void updateVisibleIDs(Hashtable& result, UErrorCode& status) const { diff --git a/icu4c/source/test/intltest/icusvtst.cpp b/icu4c/source/test/intltest/icusvtst.cpp index 72ff276ad7..4242745e00 100644 --- a/icu4c/source/test/intltest/icusvtst.cpp +++ b/icu4c/source/test/intltest/icusvtst.cpp @@ -33,6 +33,15 @@ class ICUNSubclass : public ICUNotifier { } }; +// This factory does nothing +class LKFSubclass0 : public LocaleKeyFactory { +public: + LKFSubclass0() + : LocaleKeyFactory(VISIBLE, "LKFSubclass0") + { + } +}; + class LKFSubclass : public LocaleKeyFactory { Hashtable table; @@ -460,6 +469,25 @@ ICUServiceTest::testAPI_One() /* ****************************************************************** */ +class TestStringSimpleKeyService : public ICUService { +public: + + virtual ICUServiceFactory* createSimpleFactory(UObject* obj, const UnicodeString& id, UBool visible, UErrorCode& status) + { + // We could put this type check into ICUService itself, but we'd still + // have to implement cloneInstance. Otherwise we could just tell the service + // what the object type is when we create it, and the default implementation + // could handle everything for us. Phooey. + if (obj && obj->getDynamicClassID() == UnicodeString::getStaticClassID()) { + return ICUService::createSimpleFactory(obj, id, visible, status); + } + return NULL; + } + + virtual UObject* cloneInstance(UObject* instance) const { + return instance ? new UnicodeString(*(UnicodeString*)instance) : NULL; + } +}; class TestStringService : public ICUService { public: @@ -668,7 +696,7 @@ ICUServiceTest::testAPI_Two() { UErrorCode status = U_ZERO_ERROR; UVector names(userv_deleteStringPair, NULL, status); - service.getDisplayNames(names, Locale::getGerman(), status); + service.getDisplayNames(names, status); for (int i = 0; i < names.size(); ++i) { const StringPair* pair = (const StringPair*)names[i]; logln(" " + pair->displayName + " --> " + pair->id); @@ -1014,7 +1042,7 @@ void ICUServiceTest::testLocale() { service.registerInstance(root, "", status); service.registerInstance(german, "de", status); service.registerInstance(germany, Locale::getGermany(), status); - service.registerInstance(japanese, "ja", status); + service.registerInstance(japanese, (UnicodeString)"ja", TRUE, status); service.registerInstance(japan, Locale::getJapan(), status); { @@ -1303,18 +1331,45 @@ void ICUServiceTest::testCoverage() } // ICUService - TestStringService service; - service.registerFactory(sf, status); + { + TestStringService service; + service.registerFactory(sf, status); - { - UnicodeString* result = (UnicodeString*)service.get("object", status); - if (result) { - logln("object is: " + *result); - delete result; - } else { - errln("could not get object"); - } - } + { + UnicodeString* result = (UnicodeString*)service.get("object", status); + if (result) { + logln("object is: " + *result); + delete result; + } else { + errln("could not get object"); + } + } + } + } + + // ICUServiceKey + { + UErrorCode status = U_ZERO_ERROR; + UnicodeString* howdy = new UnicodeString("Howdy"); + + TestStringSimpleKeyService service; + service.registerInstance(howdy, "Greetings", status); + { + UnicodeString* result = (UnicodeString*)service.get("Greetings", status); + if (result) { + logln("object is: " + *result); + delete result; + } else { + errln("could not get object"); + } + } + + UVector ids(uhash_deleteUnicodeString, uhash_compareUnicodeString, status); + // yuck, this is awkward to use. All because we pass null in an overload. + // TODO: change this. + UnicodeString str("Greet"); + service.getVisibleIDs(ids, &str, status); + confirmIdentical("no fallback of greet", ids.size(), 0); } // ICULocaleService @@ -1374,15 +1429,40 @@ void ICUServiceTest::testCoverage() } delete obj; delete key; + + key = LocaleKey::createWithCanonicalFallback(&primary, &fallback, 123, status); + if (U_SUCCESS(status)) { + UnicodeString str; + key->currentDescriptor(str); + key->parsePrefix(str); + if (str != "123") { + errln("did not get expected prefix"); + } + delete key; + } + + // coverage, getSupportedIDs is either overridden or the calling method is + LKFSubclass0 lkFactory; + Hashtable table0; + lkFactory.updateVisibleIDs(table0, status); + if (table0.count() != 0) { + errln("LKF returned non-empty hashtable"); + } + + + // ResourceBundleFactory + key = LocaleKey::createWithCanonicalFallback(&primary, &fallback, status); + ICUResourceBundleFactory rbf; + UObject* icurb = rbf.create(*key, NULL, status); + if (icurb != NULL) { + logln("got resource bundle for key"); + delete icurb; + } + delete key; } - -#if 0 - // ResourceBundleFactory - ICUResourceBundleFactory rbf = new ICUResourceBundleFactory(); - logln("RB: " + rbf.create(lkey, null)); - - // ICUNotifier + #if 0 + // ICUNotifier ICUNotifier nf = new ICUNSubclass(); try { nf.addListener(null);