1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
3
* The contents of this file are subject to the Mozilla Public
4
* License Version 1.1 (the "License"); you may not use this file
5
* except in compliance with the License. You may obtain a copy of
6
* the License at http://www.mozilla.org/MPL/
8
* Software distributed under the License is distributed on an "AS
9
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
10
* implied. See the License for the specific language governing
11
* rights and limitations under the License.
13
* The Original Code is TestCacheService.cpp, released February 7, 2001.
15
* The Initial Developer of the Original Code is Netscape Communications
16
* Corporation. Portions created by Netscape are
17
* Copyright (C) 2001 Netscape Communications Corporation. All
21
* Gordon Sheridan, 7-February-2001
28
#include "nsIServiceManager.h"
31
#include "nsICacheService.h"
32
#include "nsICacheSession.h"
33
#include "nsICacheEntryDescriptor.h"
34
#include "nsICacheListener.h"
35
#include "nsIDNSService.h"
37
#include "nsISupportsPrimitives.h"
38
#include "nsSupportsPrimitives.h"
39
#include "nsIEventQueueService.h"
42
static NS_DEFINE_CID(kEventQueueServiceCID, NS_EVENTQUEUESERVICE_CID);
43
static NS_DEFINE_CID(kCacheServiceCID, NS_CACHESERVICE_CID);
45
nsCOMPtr<nsIEventQueue> gEventQ;
46
nsCOMPtr<nsICacheService> gCacheService;
47
nsCOMPtr<nsICacheSession> gSession;
49
class AsyncCacheRequest
52
AsyncCacheRequest(const char * key);
60
MakeCacheSession(const char * clientID, nsICacheSession **session)
65
// nsCOMPtr<nsICacheService> cacheService =
66
// do_GetService(kCacheServiceCID, &rv);
67
gCacheService = do_GetService(kCacheServiceCID, &rv);
68
if (NS_FAILED(rv) || !gCacheService) {
69
printf("do_GetService(kCacheServiceCID) failed : %x\n", rv);
74
rv = gCacheService->CreateSession(clientID,
75
nsICache::STORE_IN_MEMORY,
76
nsICache::NOT_STREAM_BASED,
79
printf("nsCacheService::CreateSession() failed : %x\n", rv);
87
TestMemoryObjectCache()
89
printf("\nTesting Memory Object Cache:\n");
90
nsCOMPtr<nsICacheSession> session;
91
nsresult rv = MakeCacheSession("testClientID", getter_AddRefs(session));
92
if (NS_FAILED(rv)) return;
94
nsCOMPtr<nsICacheEntryDescriptor> descriptor;
96
// Test ACCESS_READ for non-existent entry
97
printf("\nTest ACCESS_READ:\n");
98
rv = session->OpenCacheEntry("non-existent entry",
99
nsICache::ACCESS_READ,
101
getter_AddRefs(descriptor));
102
if (rv != NS_ERROR_CACHE_KEY_NOT_FOUND)
103
printf("OpenCacheEntry(ACCESS_READ) returned: %x for non-existent entry\n", rv);
105
// Test creating new entry
106
printf("\nTest ACCESS_READ_WRITE:\n");
107
rv = session->OpenCacheEntry("http://www.mozilla.org/somekey",
108
nsICache::ACCESS_READ_WRITE,
110
getter_AddRefs(descriptor));
112
printf("OpenCacheEntry(ACCESS_READ_WRITE) failed: %x\n", rv);
116
nsCOMPtr<nsISupportsCString> foo =
117
do_CreateInstance(NS_SUPPORTS_CSTRING_CONTRACTID, &rv);
119
foo->SetData(NS_LITERAL_CSTRING("hello world"));
121
rv = descriptor->SetCacheElement(foo);
122
rv = descriptor->SetDataSize(11);
123
rv = descriptor->SetMetaDataElement("itemOne", "metaData works");
126
// Test refetching entry
128
rv = session->OpenCacheEntry("http://www.mozilla.org/somekey",
129
nsICache::ACCESS_READ_WRITE,
131
getter_AddRefs(descriptor));
133
printf("OpenCacheEntry(ACCESS_READ_WRITE #2) failed: %x", rv);
137
nsCOMPtr<nsISupportsCString> bar;
138
descriptor->GetCacheElement(getter_AddRefs(bar));
139
if (foo.get() != bar.get()) {
140
printf("cache elements not the same\n");
142
printf("data matches...\n");
146
rv = descriptor->GetMetaDataElement("itemOne", &metaData);
147
if (NS_SUCCEEDED(rv)) printf("metaData = %s\n", metaData);
148
else printf("GetMetaDataElement failed : rv = %x\n", rv);
151
// Test ACCESS_WRITE entry
152
printf("\nTest ACCESS_WRITE:\n");
153
rv = session->OpenCacheEntry("http://www.mozilla.org/somekey",
154
nsICache::ACCESS_WRITE,
156
getter_AddRefs(descriptor));
158
printf("OpenCacheEntry(ACCESS_WRITE) failed: %x", rv);
161
rv = descriptor->GetCacheElement(getter_AddRefs(bar));
163
printf("FAILED: we didn't get new entry.\n");
165
printf("GetCacheElement failed : %x\n", rv);
167
rv = descriptor->GetMetaDataElement("itemOne", &metaData);
168
if (NS_SUCCEEDED(rv))
169
if (metaData) printf("metaData = %s\n", metaData);
170
else printf("metaData = nsnull\n");
171
else printf("GetMetaDataElement failed : rv = %x\n", rv);
181
main(int argc, char* argv[])
186
rv = NS_InitXPCOM2(nsnull, nsnull, nsnull);
187
if (NS_FAILED(rv)) return rv;
190
* Create event queue for this thread
192
nsCOMPtr<nsIEventQueueService> eventQService =
193
do_GetService(kEventQueueServiceCID, &rv);
194
if (NS_FAILED(rv)) goto error_exit;
196
eventQService->GetThreadEventQueue(NS_CURRENT_THREAD, getter_AddRefs(gEventQ));
201
TestMemoryObjectCache();
206
rv = gCacheService->Shutdown();
208
printf("nsCacheService::Shutdown() : rv = %x\n", rv);
209
gCacheService = nsnull;
213
eventQService = nsnull;
215
NS_ShutdownXPCOM(nsnull);
217
printf("XPCOM shut down.\n\n");