[not part of build] Added test cases for ACCESS_READ of non-existent entry and ACCESS_WRITE of existing entry.

This commit is contained in:
gordon%netscape.com 2001-03-07 09:31:45 +00:00
Родитель acd18fda01
Коммит dc5d568ca6
1 изменённых файлов: 158 добавлений и 49 удалений

Просмотреть файл

@ -31,14 +31,145 @@
#include "nsICacheService.h" #include "nsICacheService.h"
#include "nsICacheSession.h" #include "nsICacheSession.h"
#include "nsICacheEntryDescriptor.h" #include "nsICacheEntryDescriptor.h"
#include "nsICacheListener.h"
#include "nsIDNSService.h" #include "nsIDNSService.h"
#include "nsISupportsPrimitives.h" #include "nsISupportsPrimitives.h"
#include "nsSupportsPrimitives.h" #include "nsSupportsPrimitives.h"
#include "nsIEventQueueService.h"
static NS_DEFINE_CID(kEventQueueServiceCID, NS_EVENTQUEUESERVICE_CID);
static NS_DEFINE_CID(kDNSServiceCID, NS_DNSSERVICE_CID);
static NS_DEFINE_CID(kCacheServiceCID, NS_CACHESERVICE_CID);
static NS_DEFINE_CID(kDNSServiceCID, NS_DNSSERVICE_CID); nsCOMPtr<nsIEventQueue> gEventQ;
static NS_DEFINE_CID(kCacheServiceCID, NS_CACHESERVICE_CID); nsCOMPtr<nsICacheService> gCacheService;
nsCOMPtr<nsICacheSession> gSession;
class AsyncCacheRequest
{
public:
AsyncCacheRequest(const char * key);
~AsyncCacheRequest();
const char * mKey;
};
nsresult
MakeCacheSession(const char * clientID, nsICacheSession **session)
{
nsresult rv;
if (!gCacheService) {
// NS_WITH_SERVICE(nsICacheService, cacheService, kCacheServiceCID, &rv);
gCacheService = do_GetService(kCacheServiceCID, &rv);
if (NS_FAILED(rv) || !gCacheService) {
printf("do_GetService(kCacheServiceCID) failed : %x\n", rv);
goto error_exit;
}
}
rv = gCacheService->CreateSession(clientID,
nsICache::STORE_IN_MEMORY,
nsICache::NOT_STREAM_BASED,
session);
if (NS_FAILED(rv))
printf("nsCacheService::CreateSession() failed : %x\n", rv);
error_exit:
return rv;
}
void
TestMemoryObjectCache()
{
printf("\nTesting Memory Object Cache:\n");
nsCOMPtr<nsICacheSession> session;
nsresult rv = MakeCacheSession("testClientID", getter_AddRefs(session));
if (NS_FAILED(rv)) return;
nsCOMPtr<nsICacheEntryDescriptor> descriptor;
// Test ACCESS_READ for non-existent entry
printf("\nTest ACCESS_READ:\n");
rv = session->OpenCacheEntry("non-existent entry",
nsICache::ACCESS_READ,
getter_AddRefs(descriptor));
if (rv != NS_ERROR_CACHE_KEY_NOT_FOUND)
printf("OpenCacheEntry(ACCESS_READ) returned: %x for non-existent entry\n", rv);
// Test creating new entry
printf("\nTest ACCESS_READ_WRITE:\n");
rv = session->OpenCacheEntry("http://www.mozilla.org/somekey",
nsICache::ACCESS_READ_WRITE,
getter_AddRefs(descriptor));
if (NS_FAILED(rv)) {
printf("OpenCacheEntry(ACCESS_READ_WRITE) failed: %x\n", rv);
goto error_exit;
}
nsCOMPtr<nsISupportsString> foo =
do_CreateInstance(NS_SUPPORTS_STRING_CONTRACTID, &rv);
foo->SetData("hello world");
rv = descriptor->SetCacheElement(foo);
rv = descriptor->SetDataSize(11);
rv = descriptor->SetMetaDataElement("itemOne", "metaData works");
descriptor = nsnull;
// Test refetching entry
rv = session->OpenCacheEntry("http://www.mozilla.org/somekey",
nsICache::ACCESS_READ_WRITE,
getter_AddRefs(descriptor));
if (NS_FAILED(rv)) {
printf("OpenCacheEntry(ACCESS_READ_WRITE #2) failed: %x", rv);
goto error_exit;
}
nsCOMPtr<nsISupportsString> bar;
descriptor->GetCacheElement(getter_AddRefs(bar));
if (foo.get() != bar.get()) {
printf("cache elements not the same\n");
} else {
printf("data matches...\n");
}
char * metaData;
rv = descriptor->GetMetaDataElement("itemOne", &metaData);
if (NS_SUCCEEDED(rv)) printf("metaData = %s\n", metaData);
else printf("GetMetaDataElement failed : rv = %x\n", rv);
descriptor = nsnull;
// Test ACCESS_WRITE entry
printf("\nTest ACCESS_WRITE:\n");
rv = session->OpenCacheEntry("http://www.mozilla.org/somekey",
nsICache::ACCESS_WRITE,
getter_AddRefs(descriptor));
if (NS_FAILED(rv)) {
printf("OpenCacheEntry(ACCESS_WRITE) failed: %x", rv);
goto error_exit;
}
rv = descriptor->GetCacheElement(getter_AddRefs(bar));
if (bar)
printf("FAILED: we didn't get new entry.\n");
if (NS_FAILED(rv))
printf("GetCacheElement failed : %x\n", rv);
rv = descriptor->GetMetaDataElement("itemOne", &metaData);
if (NS_SUCCEEDED(rv))
if (metaData) printf("metaData = %s\n", metaData);
else printf("metaData = nsnull\n");
else printf("GetMetaDataElement failed : rv = %x\n", rv);
printf("\n");
error_exit:
return;
}
int int
@ -49,60 +180,38 @@ main(int argc, char* argv[])
// Start up XPCOM // Start up XPCOM
rv = NS_InitXPCOM(nsnull, nsnull); rv = NS_InitXPCOM(nsnull, nsnull);
if (NS_FAILED(rv)) return rv; if (NS_FAILED(rv)) return rv;
/**
* Create event queue for this thread
*/
NS_WITH_SERVICE(nsIEventQueueService, eventQService, kEventQueueServiceCID, &rv);
if (NS_FAILED(rv)) goto error_exit;
rv = eventQService->CreateThreadEventQueue();
if (NS_FAILED(rv)) goto error_exit;
eventQService->GetThreadEventQueue(NS_CURRENT_THREAD, getter_AddRefs(gEventQ));
printf("Test nsCacheService:\n"); /**
* Test Cache
*/
TestMemoryObjectCache();
// NS_WITH_SERVICE(nsICacheService, cacheService, kCacheServiceCID, &rv);
nsCOMPtr<nsICacheService> cacheService = do_GetService(kCacheServiceCID, &rv);
if (NS_FAILED(rv) || !cacheService) goto error_exit;
nsCOMPtr<nsICacheSession> session;
rv = cacheService->CreateSession("testClientID",
nsICache::STORE_IN_MEMORY,
nsICache::NOT_STREAM_BASED,
getter_AddRefs(session));
printf("nsCacheService::CreateSession : rv = %x\n", rv);
printf(" session = %x\n\n",
(unsigned int)session.get());
nsCOMPtr<nsICacheEntryDescriptor> descriptor;
rv = session->OpenCacheEntry("http://www.mozilla.org/pretty.jpg",
nsICache::ACCESS_READ_WRITE,
getter_AddRefs(descriptor));
if (NS_FAILED(rv)) {
printf("OpenCacheEntry failed: %x\n", rv);
} else {
printf("descriptor = %x\n", (unsigned int)descriptor.get());
}
nsCOMPtr<nsISupportsString> foo = do_CreateInstance(NS_SUPPORTS_STRING_CONTRACTID, &rv);
foo->SetData("hello world");
descriptor->SetCacheElement(foo);
descriptor = nsnull;
rv = session->OpenCacheEntry("http://www.mozilla.org/pretty.jpg",
nsICache::ACCESS_READ_WRITE,
getter_AddRefs(descriptor));
nsCOMPtr<nsISupportsString> bar;
descriptor->GetCacheElement(getter_AddRefs(bar));
if (foo.get() != bar.get()) {
printf("cache elements not the same\n");
} else {
printf("It works!");
}
rv = cacheService->Shutdown();
printf("nsCacheService::Shutdown() : rv = %x\n", rv);
error_exit: error_exit:
if (gCacheService) {
rv = gCacheService->Shutdown();
if (NS_FAILED(rv))
printf("nsCacheService::Shutdown() : rv = %x\n", rv);
gCacheService = nsnull;
}
gEventQ = nsnull;
eventQService = nsnull;
NS_ShutdownXPCOM(nsnull); NS_ShutdownXPCOM(nsnull);
printf("XPCOM shut down...\n"); printf("XPCOM shut down.\n\n");
return rv; return rv;
} }