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
16 * The Original Code is TestCacheService.cpp, released
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.
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 ***** */
45 #include "nsIServiceManager.h"
48 #include "nsICacheService.h"
49 #include "nsICacheSession.h"
50 #include "nsICacheEntryDescriptor.h"
51 #include "nsICacheListener.h"
52 #include "nsIDNSService.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
68 AsyncCacheRequest(const char * key
);
76 MakeCacheSession(const char * clientID
, nsICacheSession
**session
)
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
);
90 rv
= gCacheService
->CreateSession(clientID
,
91 nsICache::STORE_IN_MEMORY
,
92 nsICache::NOT_STREAM_BASED
,
95 printf("nsCacheService::CreateSession() failed : %x\n", rv
);
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
,
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
,
128 getter_AddRefs(descriptor
));
130 printf("OpenCacheEntry(ACCESS_READ_WRITE) failed: %x\n", rv
);
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");
144 // Test refetching entry
146 rv
= session
->OpenCacheEntry(cacheKey
,
147 nsICache::ACCESS_READ_WRITE
,
149 getter_AddRefs(descriptor
));
151 printf("OpenCacheEntry(ACCESS_READ_WRITE #2) failed: %x", rv
);
155 nsCOMPtr
<nsISupportsCString
> bar
;
156 descriptor
->GetCacheElement(getter_AddRefs(bar
));
157 if (foo
.get() != bar
.get()) {
158 printf("cache elements not the same\n");
160 printf("data matches...\n");
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
);
169 // Test ACCESS_WRITE entry
170 printf("\nTest ACCESS_WRITE:\n");
171 rv
= session
->OpenCacheEntry(cacheKey
,
172 nsICache::ACCESS_WRITE
,
174 getter_AddRefs(descriptor
));
176 printf("OpenCacheEntry(ACCESS_WRITE) failed: %x", rv
);
179 rv
= descriptor
->GetCacheElement(getter_AddRefs(bar
));
181 printf("FAILED: we didn't get new entry.\n");
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
);
199 main(int argc
, char* argv
[])
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
));
219 TestMemoryObjectCache();
224 eventQService
= nsnull
;
226 NS_ShutdownXPCOM(nsnull
);
228 printf("XPCOM shut down.\n\n");