"Automated configuration bump, release for firefox 2.0.0.9rc1"
[mozilla-central.git] / netwerk / test / TestCacheService.cpp
blob619bb2b3c5edc366ab5fbcd9d127750ce75e8161
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
3 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
16 * The Original Code is TestCacheService.cpp, released
17 * February 7, 2001.
19 * The Initial Developer of the Original Code is
20 * Netscape Communications Corporation.
21 * Portions created by the Initial Developer are Copyright (C) 2001
22 * the Initial Developer. All Rights Reserved.
24 * Contributor(s):
25 * Gordon Sheridan, 7-February-2001
27 * Alternatively, the contents of this file may be used under the terms of
28 * either the GNU General Public License Version 2 or later (the "GPL"), or
29 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
30 * in which case the provisions of the GPL or the LGPL are applicable instead
31 * of those above. If you wish to allow use of your version of this file only
32 * under the terms of either the GPL or the LGPL, and not to allow others to
33 * use your version of this file under the terms of the MPL, indicate your
34 * decision by deleting the provisions above and replace them with the notice
35 * and other provisions required by the GPL or the LGPL. If you do not delete
36 * the provisions above, a recipient may use your version of this file under
37 * the terms of any one of the MPL, the GPL or the LGPL.
39 * ***** END LICENSE BLOCK ***** */
42 #include "nspr.h"
43 #include "nscore.h"
44 #include "nsError.h"
45 #include "nsIServiceManager.h"
46 #include "nsNetCID.h"
47 #include "nsICache.h"
48 #include "nsICacheService.h"
49 #include "nsICacheSession.h"
50 #include "nsICacheEntryDescriptor.h"
51 #include "nsICacheListener.h"
52 #include "nsIDNSService.h"
53 #include "nsXPCOM.h"
54 #include "nsISupportsPrimitives.h"
55 #include "nsSupportsPrimitives.h"
56 #include "nsIEventQueueService.h"
59 static NS_DEFINE_CID(kEventQueueServiceCID, NS_EVENTQUEUESERVICE_CID);
60 static NS_DEFINE_CID(kCacheServiceCID, NS_CACHESERVICE_CID);
62 nsCOMPtr<nsIEventQueue> gEventQ;
63 nsCOMPtr<nsICacheService> gCacheService;
65 class AsyncCacheRequest
67 public:
68 AsyncCacheRequest(const char * key);
69 ~AsyncCacheRequest();
71 const char * mKey;
75 nsresult
76 MakeCacheSession(const char * clientID, nsICacheSession **session)
78 nsresult rv;
80 if (!gCacheService) {
81 // nsCOMPtr<nsICacheService> cacheService =
82 // do_GetService(kCacheServiceCID, &rv);
83 gCacheService = do_GetService(kCacheServiceCID, &rv);
84 if (NS_FAILED(rv) || !gCacheService) {
85 printf("do_GetService(kCacheServiceCID) failed : %x\n", rv);
86 goto error_exit;
90 rv = gCacheService->CreateSession(clientID,
91 nsICache::STORE_IN_MEMORY,
92 nsICache::NOT_STREAM_BASED,
93 session);
94 if (NS_FAILED(rv))
95 printf("nsCacheService::CreateSession() failed : %x\n", rv);
97 error_exit:
98 return rv;
102 void
103 TestMemoryObjectCache()
105 printf("\nTesting Memory Object Cache:\n");
106 nsCOMPtr<nsICacheSession> session;
107 nsresult rv = MakeCacheSession("testClientID", getter_AddRefs(session));
108 if (NS_FAILED(rv)) return;
110 nsCOMPtr<nsICacheEntryDescriptor> descriptor;
112 // Test ACCESS_READ for non-existent entry
113 printf("\nTest ACCESS_READ:\n");
114 rv = session->OpenCacheEntry(NS_LITERAL_CSTRING("non-existent entry"),
115 nsICache::ACCESS_READ,
116 nsICache::BLOCKING,
117 getter_AddRefs(descriptor));
118 if (rv != NS_ERROR_CACHE_KEY_NOT_FOUND)
119 printf("OpenCacheEntry(ACCESS_READ) returned: %x for non-existent entry\n", rv);
121 NS_NAMED_LITERAL_CSTRING(cacheKey, "http://www.mozilla.org/somekey");
123 // Test creating new entry
124 printf("\nTest ACCESS_READ_WRITE:\n");
125 rv = session->OpenCacheEntry(cacheKey,
126 nsICache::ACCESS_READ_WRITE,
127 nsICache::BLOCKING,
128 getter_AddRefs(descriptor));
129 if (NS_FAILED(rv)) {
130 printf("OpenCacheEntry(ACCESS_READ_WRITE) failed: %x\n", rv);
131 goto error_exit;
134 nsCOMPtr<nsISupportsCString> foo =
135 do_CreateInstance(NS_SUPPORTS_CSTRING_CONTRACTID, &rv);
137 foo->SetData(NS_LITERAL_CSTRING("hello world"));
139 rv = descriptor->SetCacheElement(foo);
140 rv = descriptor->SetDataSize(11);
141 rv = descriptor->SetMetaDataElement("itemOne", "metaData works");
142 descriptor = nsnull;
144 // Test refetching entry
146 rv = session->OpenCacheEntry(cacheKey,
147 nsICache::ACCESS_READ_WRITE,
148 nsICache::BLOCKING,
149 getter_AddRefs(descriptor));
150 if (NS_FAILED(rv)) {
151 printf("OpenCacheEntry(ACCESS_READ_WRITE #2) failed: %x", rv);
152 goto error_exit;
155 nsCOMPtr<nsISupportsCString> bar;
156 descriptor->GetCacheElement(getter_AddRefs(bar));
157 if (foo.get() != bar.get()) {
158 printf("cache elements not the same\n");
159 } else {
160 printf("data matches...\n");
163 char * metaData;
164 rv = descriptor->GetMetaDataElement("itemOne", &metaData);
165 if (NS_SUCCEEDED(rv)) printf("metaData = %s\n", metaData);
166 else printf("GetMetaDataElement failed : rv = %x\n", rv);
167 descriptor = nsnull;
169 // Test ACCESS_WRITE entry
170 printf("\nTest ACCESS_WRITE:\n");
171 rv = session->OpenCacheEntry(cacheKey,
172 nsICache::ACCESS_WRITE,
173 nsICache::BLOCKING,
174 getter_AddRefs(descriptor));
175 if (NS_FAILED(rv)) {
176 printf("OpenCacheEntry(ACCESS_WRITE) failed: %x", rv);
177 goto error_exit;
179 rv = descriptor->GetCacheElement(getter_AddRefs(bar));
180 if (bar)
181 printf("FAILED: we didn't get new entry.\n");
182 if (NS_FAILED(rv))
183 printf("GetCacheElement failed : %x\n", rv);
185 rv = descriptor->GetMetaDataElement("itemOne", &metaData);
186 if (NS_SUCCEEDED(rv))
187 if (metaData) printf("metaData = %s\n", metaData);
188 else printf("metaData = nsnull\n");
189 else printf("GetMetaDataElement failed : rv = %x\n", rv);
191 printf("\n");
192 error_exit:
194 return;
199 main(int argc, char* argv[])
201 nsresult rv = NS_OK;
203 // Start up XPCOM
204 rv = NS_InitXPCOM2(nsnull, nsnull, nsnull);
205 if (NS_FAILED(rv)) return rv;
208 * Create event queue for this thread
210 nsCOMPtr<nsIEventQueueService> eventQService =
211 do_GetService(kEventQueueServiceCID, &rv);
212 if (NS_FAILED(rv)) goto error_exit;
214 eventQService->GetThreadEventQueue(NS_CURRENT_THREAD, getter_AddRefs(gEventQ));
217 * Test Cache
219 TestMemoryObjectCache();
222 error_exit:
223 gEventQ = nsnull;
224 eventQService = nsnull;
226 NS_ShutdownXPCOM(nsnull);
228 printf("XPCOM shut down.\n\n");
229 return rv;