Back to index

lightning-sunbird  0.9+nobinonly
TestCacheService.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
00002  *
00003  * ***** BEGIN LICENSE BLOCK *****
00004  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00005  *
00006  * The contents of this file are subject to the Mozilla Public License Version
00007  * 1.1 (the "License"); you may not use this file except in compliance with
00008  * the License. You may obtain a copy of the License at
00009  * http://www.mozilla.org/MPL/
00010  *
00011  * Software distributed under the License is distributed on an "AS IS" basis,
00012  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00013  * for the specific language governing rights and limitations under the
00014  * License.
00015  *
00016  * The Original Code is TestCacheService.cpp, released
00017  * February 7, 2001.
00018  *
00019  * The Initial Developer of the Original Code is
00020  * Netscape Communications Corporation.
00021  * Portions created by the Initial Developer are Copyright (C) 2001
00022  * the Initial Developer. All Rights Reserved.
00023  *
00024  * Contributor(s):
00025  *   Gordon Sheridan, 7-February-2001
00026  *
00027  * Alternatively, the contents of this file may be used under the terms of
00028  * either the GNU General Public License Version 2 or later (the "GPL"), or
00029  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00030  * in which case the provisions of the GPL or the LGPL are applicable instead
00031  * of those above. If you wish to allow use of your version of this file only
00032  * under the terms of either the GPL or the LGPL, and not to allow others to
00033  * use your version of this file under the terms of the MPL, indicate your
00034  * decision by deleting the provisions above and replace them with the notice
00035  * and other provisions required by the GPL or the LGPL. If you do not delete
00036  * the provisions above, a recipient may use your version of this file under
00037  * the terms of any one of the MPL, the GPL or the LGPL.
00038  *
00039  * ***** END LICENSE BLOCK ***** */
00040 
00041 
00042 #include "nspr.h"
00043 #include "nscore.h"
00044 #include "nsError.h"
00045 #include "nsIServiceManager.h"
00046 #include "nsNetCID.h"
00047 #include "nsICache.h"
00048 #include "nsICacheService.h"
00049 #include "nsICacheSession.h"
00050 #include "nsICacheEntryDescriptor.h"
00051 #include "nsICacheListener.h"
00052 #include "nsIDNSService.h"
00053 #include "nsXPCOM.h"
00054 #include "nsISupportsPrimitives.h"
00055 #include "nsSupportsPrimitives.h"
00056 #include "nsIEventQueueService.h"
00057 
00058 
00059 static NS_DEFINE_CID(kEventQueueServiceCID,      NS_EVENTQUEUESERVICE_CID);
00060 static NS_DEFINE_CID(kCacheServiceCID,           NS_CACHESERVICE_CID);
00061 
00062 nsCOMPtr<nsIEventQueue>   gEventQ;
00063 nsCOMPtr<nsICacheService> gCacheService;
00064 nsCOMPtr<nsICacheSession> gSession;
00065 
00066 class AsyncCacheRequest
00067 {
00068 public:
00069     AsyncCacheRequest(const char * key);
00070     ~AsyncCacheRequest();
00071 
00072     const char * mKey;
00073 };
00074 
00075 
00076 nsresult
00077 MakeCacheSession(const char * clientID, nsICacheSession **session)
00078 {
00079     nsresult rv;
00080 
00081     if (!gCacheService) {
00082         //    nsCOMPtr<nsICacheService> cacheService = 
00083         //             do_GetService(kCacheServiceCID, &rv);
00084         gCacheService = do_GetService(kCacheServiceCID, &rv);
00085         if (NS_FAILED(rv) || !gCacheService) {
00086             printf("do_GetService(kCacheServiceCID) failed : %x\n", rv);
00087             goto error_exit;
00088         }
00089     }
00090 
00091     rv = gCacheService->CreateSession(clientID,
00092                                      nsICache::STORE_IN_MEMORY,
00093                                      nsICache::NOT_STREAM_BASED,
00094                                      session);
00095     if (NS_FAILED(rv))
00096         printf("nsCacheService::CreateSession() failed : %x\n", rv);
00097 
00098  error_exit:
00099     return rv;
00100 }
00101 
00102 
00103 void
00104 TestMemoryObjectCache()
00105 {
00106     printf("\nTesting Memory Object Cache:\n");
00107     nsCOMPtr<nsICacheSession> session;
00108     nsresult rv = MakeCacheSession("testClientID", getter_AddRefs(session));
00109     if (NS_FAILED(rv))  return;
00110 
00111     nsCOMPtr<nsICacheEntryDescriptor> descriptor;
00112 
00113     // Test ACCESS_READ for non-existent entry
00114     printf("\nTest ACCESS_READ:\n");
00115     rv = session->OpenCacheEntry(NS_LITERAL_CSTRING("non-existent entry"),
00116                                  nsICache::ACCESS_READ,
00117                                  nsICache::BLOCKING,
00118                                  getter_AddRefs(descriptor));
00119     if (rv != NS_ERROR_CACHE_KEY_NOT_FOUND)
00120         printf("OpenCacheEntry(ACCESS_READ) returned: %x for non-existent entry\n", rv);
00121 
00122     NS_NAMED_LITERAL_CSTRING(cacheKey, "http://www.mozilla.org/somekey");
00123 
00124     // Test creating new entry
00125     printf("\nTest ACCESS_READ_WRITE:\n");
00126     rv = session->OpenCacheEntry(cacheKey,
00127                                  nsICache::ACCESS_READ_WRITE,
00128                                  nsICache::BLOCKING,
00129                                  getter_AddRefs(descriptor));
00130     if (NS_FAILED(rv)) {
00131         printf("OpenCacheEntry(ACCESS_READ_WRITE) failed: %x\n", rv);
00132         goto error_exit;
00133     }
00134 
00135     nsCOMPtr<nsISupportsCString> foo =
00136         do_CreateInstance(NS_SUPPORTS_CSTRING_CONTRACTID, &rv);
00137 
00138     foo->SetData(NS_LITERAL_CSTRING("hello world"));
00139 
00140     rv = descriptor->SetCacheElement(foo);
00141     rv = descriptor->SetDataSize(11);
00142     rv = descriptor->SetMetaDataElement("itemOne", "metaData works");
00143     descriptor = nsnull;
00144 
00145     // Test refetching entry
00146 
00147     rv = session->OpenCacheEntry(cacheKey,
00148                                  nsICache::ACCESS_READ_WRITE,
00149                                  nsICache::BLOCKING,
00150                                  getter_AddRefs(descriptor));
00151     if (NS_FAILED(rv)) {
00152         printf("OpenCacheEntry(ACCESS_READ_WRITE #2) failed: %x", rv);
00153         goto error_exit;
00154     }
00155 
00156     nsCOMPtr<nsISupportsCString> bar;
00157     descriptor->GetCacheElement(getter_AddRefs(bar));
00158     if (foo.get() != bar.get()) {
00159         printf("cache elements not the same\n");
00160     } else {
00161         printf("data matches...\n");
00162     }
00163 
00164     char * metaData;
00165     rv = descriptor->GetMetaDataElement("itemOne", &metaData);
00166     if (NS_SUCCEEDED(rv))   printf("metaData = %s\n", metaData);
00167     else printf("GetMetaDataElement failed : rv = %x\n", rv);
00168     descriptor = nsnull;
00169 
00170     // Test ACCESS_WRITE entry
00171     printf("\nTest ACCESS_WRITE:\n");
00172     rv = session->OpenCacheEntry(cacheKey,
00173                                  nsICache::ACCESS_WRITE,
00174                                  nsICache::BLOCKING,
00175                                  getter_AddRefs(descriptor));
00176     if (NS_FAILED(rv)) {
00177         printf("OpenCacheEntry(ACCESS_WRITE) failed: %x", rv);
00178         goto error_exit;
00179     }
00180     rv = descriptor->GetCacheElement(getter_AddRefs(bar));
00181     if (bar)
00182         printf("FAILED: we didn't get new entry.\n");
00183     if (NS_FAILED(rv))
00184         printf("GetCacheElement failed : %x\n", rv);
00185 
00186     rv = descriptor->GetMetaDataElement("itemOne", &metaData);
00187     if (NS_SUCCEEDED(rv))
00188         if (metaData)  printf("metaData = %s\n", metaData);
00189         else           printf("metaData = nsnull\n");
00190     else printf("GetMetaDataElement failed : rv = %x\n", rv);
00191 
00192     printf("\n");
00193  error_exit:
00194 
00195     return;
00196 }
00197 
00198 
00199 int
00200 main(int argc, char* argv[])
00201 {
00202     nsresult rv = NS_OK;
00203 
00204     // Start up XPCOM
00205     rv = NS_InitXPCOM2(nsnull, nsnull, nsnull);
00206     if (NS_FAILED(rv)) return rv;
00207 
00211     nsCOMPtr<nsIEventQueueService> eventQService = 
00212              do_GetService(kEventQueueServiceCID, &rv);
00213     if (NS_FAILED(rv)) goto error_exit;
00214 
00215     eventQService->GetThreadEventQueue(NS_CURRENT_THREAD, getter_AddRefs(gEventQ));
00216 
00220     TestMemoryObjectCache();
00221 
00222 
00223  error_exit:
00224     gEventQ = nsnull;
00225     eventQService = nsnull;
00226 
00227     NS_ShutdownXPCOM(nsnull);
00228 
00229     printf("XPCOM shut down.\n\n");
00230     return rv;
00231 }
00232 
00233 
00234